-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graphql): Test assertions should work event when `LIGHTHOUSE_CACH…
…E_ENABLE=true`. Closes: #43
- Loading branch information
1 parent
7ff3ba0
commit e5a2129
Showing
6 changed files
with
199 additions
and
32 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,19 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\GraphQL\Testing\Package; | ||
|
||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class Provider extends ServiceProvider { | ||
public function register(): void { | ||
parent::register(); | ||
|
||
$this->callAfterResolving( | ||
Repository::class, | ||
static function (Repository $config): void { | ||
$config->set('lighthouse.schema.register', __DIR__.'/schema.graphql'); | ||
}, | ||
); | ||
} | ||
} |
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,3 @@ | ||
type Query { | ||
test: String! | ||
} |
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,84 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\GraphQL\Testing; | ||
|
||
use GraphQL\Type\Schema; | ||
use Illuminate\Config\Repository; | ||
use Illuminate\Contracts\Config\Repository as ConfigContract; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher; | ||
use Illuminate\Support\Arr; | ||
use Nuwave\Lighthouse\Schema\AST\ASTBuilder; | ||
use Nuwave\Lighthouse\Schema\AST\ASTCache; | ||
use Nuwave\Lighthouse\Schema\DirectiveLocator; | ||
use Nuwave\Lighthouse\Schema\SchemaBuilder; | ||
use Nuwave\Lighthouse\Schema\Source\SchemaSourceProvider; | ||
use Nuwave\Lighthouse\Schema\TypeRegistry; | ||
|
||
use function spl_object_hash; | ||
|
||
class SchemaBuilderWrapper extends SchemaBuilder { | ||
protected ?SchemaBuilder $current = null; | ||
|
||
/** | ||
* @var array<class-string, object> | ||
*/ | ||
protected array $singletons = []; | ||
|
||
/** | ||
* @noinspection PhpMissingParentConstructorInspection | ||
* @phpstan-ignore-next-line no need to call parent `__construct` | ||
*/ | ||
public function __construct( | ||
protected Container $container, | ||
protected SchemaBuilder $builder, | ||
) { | ||
// no need to call parent | ||
} | ||
|
||
protected function getSchemaBuilder(): SchemaBuilder { | ||
return $this->current ?? $this->builder; | ||
} | ||
|
||
public function schema(): Schema { | ||
return $this->getSchemaBuilder()->schema(); | ||
} | ||
|
||
public function setSchema(?SchemaSourceProvider $provider): void { | ||
// Origins | ||
if (!$this->singletons) { | ||
$this->singletons = [ | ||
ASTCache::class => $this->container->make(ASTCache::class), | ||
ASTBuilder::class => $this->container->make(ASTBuilder::class), | ||
]; | ||
} | ||
|
||
// Build | ||
$builder = null; | ||
|
||
if ($provider) { | ||
$config = $this->container->make(ConfigContract::class)->all(); | ||
|
||
Arr::set($config, 'lighthouse.cache.key', spl_object_hash($provider)); | ||
Arr::set($config, 'lighthouse.cache.enable', true); | ||
Arr::set($config, 'lighthouse.cache.version', 1); // cache | ||
|
||
$types = $this->container->make(TypeRegistry::class); | ||
$dispatcher = $this->container->make(EventsDispatcher::class); | ||
$directives = $this->container->make(DirectiveLocator::class); | ||
$astCache = new ASTCache(new Repository($config)); | ||
$astBuilder = new ASTBuilder($directives, $provider, $dispatcher, $astCache); | ||
$builder = new SchemaBuilder($types, $astBuilder); | ||
|
||
$this->container->instance(ASTCache::class, $astCache); | ||
$this->container->instance(ASTBuilder::class, $astBuilder); | ||
} else { | ||
foreach ($this->singletons as $abstract => $instance) { | ||
$this->container->instance($abstract, $instance); | ||
} | ||
} | ||
|
||
// Set | ||
$this->current = $builder; | ||
} | ||
} |
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,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\GraphQL\Testing; | ||
|
||
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; | ||
use Nuwave\Lighthouse\Schema\SchemaBuilder; | ||
use ReflectionClass; | ||
use ReflectionMethod; | ||
|
||
/** | ||
* @internal | ||
* @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\Testing\SchemaBuilderWrapper | ||
*/ | ||
class SchemaBuilderWrapperTest extends TestCase { | ||
/** | ||
* @covers ::__construct | ||
*/ | ||
public function testWrappedSuccessfully(): void { | ||
$missed = []; | ||
$origin = new ReflectionClass(SchemaBuilder::class); | ||
$methods = $origin->getMethods(ReflectionMethod::IS_PUBLIC); | ||
$wrapper = new ReflectionClass(SchemaBuilderWrapper::class); | ||
|
||
foreach ($methods as $method) { | ||
if ($method->isConstructor()) { | ||
continue; | ||
} | ||
|
||
$wrapped = $wrapper->getMethod($method->name); | ||
|
||
if ($wrapped->getDeclaringClass()->getName() !== $wrapper->getName()) { | ||
$missed[] = $method->name; | ||
} | ||
} | ||
|
||
self::assertEquals([], $missed, 'Some methods are not wrapped.'); | ||
} | ||
} |