ThunderGqlsTestBase.php in Thunder 6.2.x
File
modules/thunder_gqls/tests/src/Functional/ThunderGqlsTestBase.php
View source
<?php
namespace Drupal\Tests\thunder_gqls\Functional;
use Drupal\Tests\thunder\Functional\ThunderTestBase;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
abstract class ThunderGqlsTestBase extends ThunderTestBase {
protected static $modules = [
'thunder_gqls',
'thunder_testing_demo',
];
protected static $configSchemaCheckerExclusions = [
'graphql.graphql_servers.thunder_graphql',
];
protected $graphqlUser;
protected function setUp() : void {
parent::setUp();
$this->graphqlUser = $this
->drupalCreateUser([
'execute thunder_graphql arbitrary graphql requests',
'access content',
'access user profiles',
'view media',
'view published terms in channel',
'view published terms in tags',
]);
$this
->drupalLogin($this->graphqlUser);
}
protected function query(string $query, string $variables) : ResponseInterface {
$urlGenerator = $this->container
->get('url_generator');
$url = $urlGenerator
->generate('graphql.query.thunder_graphql');
$requestOptions = [];
$requestOptions[RequestOptions::HEADERS]['Content-Type'] = 'application/json';
$requestOptions[RequestOptions::COOKIES] = $this
->getSessionCookies();
$requestOptions[RequestOptions::JSON]['query'] = $query;
$requestOptions[RequestOptions::JSON]['variables'] = $variables;
return $this
->getHttpClient()
->request('POST', $this
->getAbsoluteUrl($url), $requestOptions);
}
protected function getQueriesDirectory() {
return drupal_get_path('module', explode('\\', get_class($this))[2]) . '/tests/examples';
}
public function getQueryFromFile(string $name) : string {
return file_get_contents($this
->getQueriesDirectory() . '/' . $name . '.query.graphql');
}
public function getVariablesFromFile(string $name) : string {
return file_get_contents($this
->getQueriesDirectory() . '/' . $name . '.variables.json');
}
public function getExpectedResponseFromFile(string $name) : string {
return file_get_contents($this
->getQueriesDirectory() . '/' . $name . '.response.json');
}
protected function runAndTestQuery(string $schema) : void {
$query = $this
->getQueryFromFile($schema);
$variables = $this
->getVariablesFromFile($schema);
$response = $this
->query($query, $variables);
$this
->assertEquals(200, $response
->getStatusCode(), 'Response not 200');
$responseData = json_decode($response
->getBody(), TRUE)['data'];
$expectedData = json_decode($this
->getExpectedResponseFromFile($schema), TRUE)['data'];
$this
->assertEqualsCanonicalizing($expectedData, $responseData);
}
}