View source
<?php
namespace Drupal\Tests\graphql\Kernel\Framework;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
class PersistedQueriesTest extends GraphQLTestBase {
protected static $modules = [
'graphql_persisted_queries_test',
];
protected function setUp() : void {
parent::setUp();
$schema = <<<GQL
schema {
query: Query
}
type Query {
field_one: String
field_two: String
field_three: Link
}
type Link {
url: String
title: String
}
GQL;
$this
->setUpSchema($schema);
$this
->mockResolver('Query', 'field_one', 'this is the field one');
$this
->mockResolver('Query', 'field_two', 'this is the field two');
$this
->mockResolver('Query', 'field_three', []);
$this
->mockResolver('Link', 'url', 'https://www.ecosia.org');
$this
->mockResolver('Link', 'title', 'Ecosia');
$manager = $this->container
->get('plugin.manager.graphql.persisted_query');
$this->plugin_one = $manager
->createInstance('persisted_query_plugin_one');
$this->plugin_two = $manager
->createInstance('persisted_query_plugin_two');
$this->plugin_three = $manager
->createInstance('persisted_query_plugin_three');
}
public function testPersistedQueries(array $instanceIds, string $queryId, array $expected) : void {
$this->server
->removeAllPersistedQueryInstances();
foreach ($instanceIds as $index => $instanceId) {
$this->{$instanceId}
->setWeight($index);
$this->server
->addPersistedQueryInstance($this->{$instanceId});
}
$this->server
->save();
$result = $this
->query($queryId, NULL, [], NULL, TRUE);
$this
->assertSame(200, $result
->getStatusCode());
$this
->assertSame($expected, json_decode($result
->getContent(), TRUE));
}
public function persistedQueriesDataProvider() : array {
return [
[
[
'plugin_one',
'plugin_two',
'plugin_three',
],
'query_1',
[
'data' => [
'field_one' => 'this is the field one',
],
],
],
[
[
'plugin_two',
'plugin_one',
'plugin_three',
],
'query_1',
[
'data' => [
'field_two' => 'this is the field two',
],
],
],
[
[
'plugin_one',
'plugin_two',
'plugin_three',
],
'query_2',
[
'data' => [
'field_three' => [
'url' => 'https://www.ecosia.org',
'title' => 'Ecosia',
],
],
],
],
];
}
}