View source
<?php
namespace Drupal\Tests\graphql\Traits;
use Drupal\graphql\GraphQL\Resolver\Callback;
use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
use Drupal\graphql\GraphQL\Resolver\Value;
use Drupal\graphql\Plugin\GraphQL\Schema\SdlSchemaPluginBase;
use Drupal\graphql\Plugin\SchemaExtensionPluginManager;
use Drupal\graphql\Plugin\SchemaPluginManager;
use Drupal\graphql\Entity\Server;
use Drupal\graphql\GraphQL\ResolverRegistry;
use Drupal\Tests\RandomGeneratorTrait;
trait MockingTrait {
use RandomGeneratorTrait;
protected $server;
protected $registry;
protected $schema;
protected $schemaPluginManager;
protected $dataProducerPluginManager;
protected function toPromise($value) {
return $this
->returnCallback(is_callable($value) ? $value : function () use ($value) {
(yield $value);
});
}
protected function toBoundPromise($value, $scope) {
return $this
->toPromise(is_callable($value) ? \Closure::bind($value, $scope, $scope) : $value);
}
protected function setUpSchema($schema, $id = 'test', array $values = []) : void {
$this
->mockSchema($id, $schema);
$this
->mockSchemaPluginManager($id);
$this
->createTestServer($id, '/graphql/' . $id, $values);
$this->schemaPluginManager
->method('createInstance')
->with($this
->equalTo($id))
->will($this
->returnValue($this->schema));
$this->container
->set('plugin.manager.graphql.schema', $this->schemaPluginManager);
}
protected function createTestServer($schema, $endpoint, array $values = []) : void {
$this->server = Server::create([
'schema' => $schema,
'name' => $this->randomGenerator
->name(),
'endpoint' => $endpoint,
] + $values);
$this->server
->save();
}
protected function mockSchema($id, $schema, array $extensions = []) : void {
$extensionManager = $this
->getMockBuilder(SchemaExtensionPluginManager::class)
->disableOriginalConstructor()
->setMethods([
'getExtensions',
])
->getMock();
$extensionManager
->expects(static::any())
->method('getExtensions')
->willReturn($extensions);
$this->schema = $this
->getMockBuilder(SdlSchemaPluginBase::class)
->setConstructorArgs([
[],
$id,
[],
$this->container
->get('cache.graphql.ast'),
$this->container
->get('module_handler'),
$extensionManager,
[
'development' => FALSE,
],
])
->setMethods([
'getSchemaDefinition',
'getResolverRegistry',
])
->getMockForAbstractClass();
$this->schema
->expects(static::any())
->method('getSchemaDefinition')
->willReturn($schema);
$this->registry = new ResolverRegistry();
$this->schema
->expects($this
->any())
->method('getResolverRegistry')
->willReturn($this->registry);
}
protected function mockSchemaPluginManager($id) : void {
$this->schemaPluginManager = $this
->getMockBuilder(SchemaPluginManager::class)
->disableOriginalConstructor()
->getMock();
$this->schemaPluginManager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue([
$id => [
'id' => $id,
'name' => 'Test schema',
'provider' => 'graphql',
'class' => '\\Drupal\\graphql\\Plugin\\GraphQL\\Schema\\SdlSchemaPluginBase',
],
]));
}
protected function mockResolver($type, $field, $resolver = NULL) : void {
if (is_callable($resolver)) {
$resolver = new Callback($resolver);
}
if (!$resolver instanceof ResolverInterface) {
$resolver = new Value($resolver);
}
$this->registry
->addFieldResolver($type, $field, $resolver);
}
protected function mockTypeResolver($type, callable $resolver) : void {
$this->registry
->addTypeResolver($type, $resolver);
}
}