Skip to content

Commit

Permalink
Merge pull request #380 from creative-commoners/pulls/master/testsession
Browse files Browse the repository at this point in the history
NEW Add TestSessionEnvironmentExtension to support behat tests
  • Loading branch information
bergice authored May 14, 2021
2 parents 0dc90fe + 2a121e2 commit 2914b68
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ Only:
SilverStripe\Admin\LeftAndMain:
extensions:
- SilverStripe\GraphQL\Extensions\ClientConfigProvider
---
Only:
moduleexists: 'silverstripe/testsession'
---
SilverStripe\TestSession\TestSessionEnvironment:
extensions:
- SilverStripe\GraphQL\Extensions\TestSessionEnvironmentExtension
45 changes: 45 additions & 0 deletions src/Extensions/TestSessionEnvironmentExtension.php
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.')
);
}
}
}

0 comments on commit 2914b68

Please sign in to comment.