-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #380 from creative-commoners/pulls/master/testsession
NEW Add TestSessionEnvironmentExtension to support behat tests
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace SilverStripe\GraphQL\Extensions; | ||
|
||
use SilverStripe\Core\Extension; | ||
use SilverStripe\GraphQL\Schema\Schema; | ||
use SilverStripe\GraphQL\Schema\SchemaBuilder; | ||
use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException; | ||
use SilverStripe\GraphQL\Dev\Benchmark; | ||
|
||
class TestSessionEnvironmentExtension extends Extension | ||
{ | ||
/** | ||
* Build the graphql schema after a new testsession is started | ||
* This is to ensure that the schema is available when a behat test is run, particularly on CI | ||
* This does laregely the same thing as SilverStripe\GraphQL\Dev\Build::buildSchema(), though | ||
* it also checks for the existance of persisted schemas first do that the schema is not rebuilt | ||
* after each behat scenario | ||
*/ | ||
public function onAfterStartTestSession() | ||
{ | ||
Schema::setVerbose(true); | ||
$keys = array_keys(Schema::config()->get('schemas')); | ||
$keys = array_filter($keys, function ($key) { | ||
return $key !== Schema::ALL; | ||
}); | ||
$builder = SchemaBuilder::singleton(); | ||
foreach ($keys as $key) { | ||
// skip if the schema has already been built and persisted in the filesystem | ||
if ($builder->getSchema($key)) { | ||
continue; | ||
} | ||
Benchmark::start('build-schema-' . $key); | ||
$schema = $builder->boot($key); | ||
try { | ||
$builder->build($schema); | ||
} catch (EmptySchemaException $e) { | ||
Schema::message('Schema ' . $key . ' is empty. Skipping.'); | ||
} | ||
Schema::message( | ||
Benchmark::end('build-schema-' . $key, 'Built schema in %sms.') | ||
); | ||
} | ||
} | ||
} |