protected function MockGraphQLPluginTrait::injectTypeSystemPluginManagers in GraphQL 8.3
Register the mocked plugin managers during container build.
Injects the mocked schema managers into the drupal container. Has to be invoked during the KernelTest's register callback.
Parameters
\Drupal\Core\DependencyInjection\ContainerBuilder $container: The container instance.
Throws
\Exception
\ReflectionException
1 call to MockGraphQLPluginTrait::injectTypeSystemPluginManagers()
- GraphQLTestBase::setUp in tests/
src/ Kernel/ GraphQLTestBase.php
File
- tests/
src/ Traits/ MockGraphQLPluginTrait.php, line 95
Class
- MockGraphQLPluginTrait
- Trait for mocking GraphQL type system plugins.
Namespace
Drupal\Tests\graphql\TraitsCode
protected function injectTypeSystemPluginManagers(ContainerBuilder $container) {
foreach ($this->graphQLPluginClassMap as $id => $class) {
$this->graphQLPlugins[$class] = [];
/** @var \Drupal\Core\Plugin\DefaultPluginManager $manager */
$manager = $container
->get($id);
$this->graphQLPluginManagers[$id] = $manager;
// Really?
$factoryMethod = new \ReflectionMethod($manager, 'getFactory');
$factoryMethod
->setAccessible(TRUE);
$factoryProp = new \ReflectionProperty($manager, 'factory');
$factoryProp
->setAccessible(TRUE);
$discoveryMethod = new \ReflectionMethod($manager, 'getDiscovery');
$discoveryMethod
->setAccessible(TRUE);
$discoveryProp = new \ReflectionProperty($manager, 'discovery');
$discoveryProp
->setAccessible(TRUE);
/** @var FactoryInterface $factory */
$factory = $factoryMethod
->invoke($manager);
/** @var DiscoveryInterface $discovery */
$discovery = $discoveryMethod
->invoke($manager);
$decoratedProp = new \ReflectionProperty(DerivativeDiscoveryDecorator::class, 'decorated');
$decoratedProp
->setAccessible(TRUE);
$unwrappedDiscovery = $decoratedProp
->getValue($discovery);
$this->graphQLPlugins[$class] = [];
$mockFactory = $this
->getMockBuilder(FactoryInterface::class)
->setMethods([
'createInstance',
])
->getMock();
$mockDiscovery = $this
->getMockBuilder(DiscoveryInterface::class)
->setMethods([
'hasDefinition',
'getDefinitions',
'getDefinition',
])
->getMock();
$decoratedDiscovery = new ContainerDerivativeDiscoveryDecorator($mockDiscovery);
$this->graphqlPluginDecorators[$id] = $decoratedDiscovery;
$mockDiscovery
->expects(static::any())
->method('getDefinitions')
->willReturnCallback(function () use ($class, $unwrappedDiscovery) {
$mockDefinitions = array_map(function ($plugin) {
return $plugin['definition'];
}, $this->graphQLPlugins[$class]);
$realDefinitions = $unwrappedDiscovery
->getDefinitions();
return array_merge($mockDefinitions, $realDefinitions);
});
$mockDiscovery
->expects(static::any())
->method('hasDefinition')
->with(static::anything())
->willReturnCallback(function ($pluginId) use ($class, $discovery) {
$basePluginId = $this
->getBasePluginId($pluginId);
return isset($this->graphQLPlugins[$class][$basePluginId]) || $discovery
->hasDefinition($pluginId);
});
$mockDiscovery
->expects(static::any())
->method('getDefinition')
->with(static::anything(), static::anything())
->willReturnCallback(function ($pluginId, $except) use ($class, $discovery) {
$basePluginId = $this
->getBasePluginId($pluginId);
if (array_key_exists($basePluginId, $this->graphQLPlugins[$class])) {
return $this->graphQLPlugins[$class][$basePluginId]['definition'];
}
return $discovery
->getDefinition($pluginId, $except);
});
$discoveryProp
->setValue($manager, $decoratedDiscovery);
$mockFactory
->expects(static::any())
->method('createInstance')
->with(static::anything(), static::anything())
->willReturnCallback(function ($pluginId, $configuration) use ($class, $factory, $decoratedDiscovery) {
$basePluginId = $this
->getBasePluginId($pluginId);
if (array_key_exists($basePluginId, $this->graphQLPlugins[$class])) {
$definition = $decoratedDiscovery
->getDefinition($pluginId);
$args = $this->graphQLPlugins[$class][$basePluginId];
$args['definition'] = $definition;
return call_user_func_array([
$this,
$definition['mock_factory'],
], $args);
}
return $factory
->createInstance($pluginId, $configuration);
});
$factoryProp
->setValue($manager, $mockFactory);
}
}