Skip to content

Commit 213fe0f

Browse files
committed
Split test case
1 parent 66b0678 commit 213fe0f

File tree

2 files changed

+172
-167
lines changed

2 files changed

+172
-167
lines changed

tests/Functional/BaseTestCase.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Functional;
6+
7+
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8+
use Overblog\GraphQLBundle\Tests\Functional\App\TestKernel;
9+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
10+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
11+
use Symfony\Component\Filesystem\Filesystem;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpKernel\KernelInterface;
14+
15+
use function call_user_func;
16+
use function func_get_args;
17+
use function implode;
18+
use function is_callable;
19+
use function json_decode;
20+
use function json_encode;
21+
use function sprintf;
22+
use function strtolower;
23+
use function sys_get_temp_dir;
24+
25+
/**
26+
* TestCase.
27+
*/
28+
abstract class BaseTestCase extends WebTestCase
29+
{
30+
public const USER_RYAN = 'ryan';
31+
public const USER_ADMIN = 'admin';
32+
public const ANONYMOUS_USER = null;
33+
public const DEFAULT_PASSWORD = '123';
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected static function getKernelClass(): string
39+
{
40+
return TestKernel::class;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected static function createKernel(array $options = []): KernelInterface
47+
{
48+
if (null === static::$class) {
49+
static::$class = static::getKernelClass();
50+
}
51+
52+
$options['test_case'] ??= '';
53+
54+
$env = $options['environment'] ?? 'test' . strtolower($options['test_case']);
55+
$debug = $options['debug'] ?? true;
56+
57+
return new static::$class($env, $debug, $options['test_case']);
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public static function setUpBeforeClass(): void
64+
{
65+
$fs = new Filesystem();
66+
$fs->remove(sys_get_temp_dir() . '/OverblogGraphQLBundle/');
67+
}
68+
69+
protected function tearDown(): void
70+
{
71+
static::ensureKernelShutdown();
72+
}
73+
74+
protected static function executeGraphQLRequest(string $query, array $rootValue = [], string $schemaName = null, array $variables = []): array
75+
{
76+
$request = new Request();
77+
$request->query->set('query', $query);
78+
if (!empty($variables)) {
79+
$request->query->set('variables', $variables);
80+
}
81+
82+
// @phpstan-ignore-next-line
83+
$req = static::getContainer()->get('overblog_graphql.request_parser')->parse($request);
84+
// @phpstan-ignore-next-line
85+
$res = static::getContainer()->get('overblog_graphql.request_executor')->execute($schemaName, $req, $rootValue);
86+
87+
return $res->toArray();
88+
}
89+
90+
protected static function assertGraphQL(string $query, array $expectedData = null, array $expectedErrors = null, array $rootValue = [], string $schemaName = null): void
91+
{
92+
$result = static::executeGraphQLRequest($query, $rootValue, $schemaName);
93+
94+
$expected = [];
95+
96+
if (null !== $expectedErrors) {
97+
$expected['errors'] = $expectedErrors;
98+
}
99+
100+
if (null !== $expectedData) {
101+
$expected['data'] = $expectedData;
102+
}
103+
104+
static::assertSame($expected, $result, json_encode($result));
105+
}
106+
107+
protected static function query(string $query, string $username, string $testCase, string $password = self::DEFAULT_PASSWORD): KernelBrowser
108+
{
109+
$client = static::createClientAuthenticated($username, $testCase, $password);
110+
$client->request('GET', '/', ['query' => $query]);
111+
112+
return $client;
113+
}
114+
115+
protected static function createClientAuthenticated(?string $username, string $testCase, ?string $password = self::DEFAULT_PASSWORD): KernelBrowser
116+
{
117+
static::ensureKernelShutdown();
118+
$client = static::createClient(['test_case' => $testCase]);
119+
120+
if (null !== $username) {
121+
$client->setServerParameters([
122+
'PHP_AUTH_USER' => $username,
123+
'PHP_AUTH_PW' => $password,
124+
]);
125+
}
126+
127+
return $client;
128+
}
129+
130+
protected static function assertResponse(string $query, array $expected, ?string $username, string $testCase, ?string $password = self::DEFAULT_PASSWORD, array $variables = null): KernelBrowser
131+
{
132+
$client = self::createClientAuthenticated($username, $testCase, $password);
133+
$result = self::sendRequest($client, $query, false, $variables);
134+
135+
static::assertSame($expected, json_decode($result, true), $result);
136+
137+
return $client;
138+
}
139+
140+
/**
141+
* @return mixed
142+
*/
143+
protected static function sendRequest(KernelBrowser $client, string $query, bool $isDecoded = false, array $variables = null)
144+
{
145+
$client->request('GET', '/', ['query' => $query, 'variables' => json_encode($variables)]);
146+
$result = $client->getResponse()->getContent();
147+
148+
return $isDecoded ? json_decode($result, true) : $result;
149+
}
150+
151+
/**
152+
* @return mixed|ExpressionFunction
153+
*/
154+
public static function expressionFunctionFromPhp(string $phpFunctionName)
155+
{
156+
if (is_callable([ExpressionFunction::class, 'fromPhp'])) {
157+
return call_user_func([ExpressionFunction::class, 'fromPhp'], $phpFunctionName);
158+
}
159+
160+
return new ExpressionFunction($phpFunctionName, fn() => sprintf('\%s(%s)', $phpFunctionName, implode(', ', func_get_args())), function (): void {});
161+
}
162+
163+
/**
164+
* @param KernelBrowser $client
165+
*/
166+
protected function disableCatchExceptions($client): void
167+
{
168+
if (is_callable([$client, 'catchExceptions'])) {
169+
$client->catchExceptions(false);
170+
}
171+
}
172+
}

tests/Functional/TestCase.php

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,9 @@
44

55
namespace Overblog\GraphQLBundle\Tests\Functional;
66

7-
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Overblog\GraphQLBundle\Tests\Functional\App\TestKernel;
9-
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
10-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
117
use Symfony\Component\DependencyInjection\Container;
128
use Symfony\Component\DependencyInjection\ContainerInterface;
13-
use Symfony\Component\Filesystem\Filesystem;
14-
use Symfony\Component\HttpFoundation\Request;
159
use Symfony\Component\HttpKernel\Kernel;
16-
use Symfony\Component\HttpKernel\KernelInterface;
17-
18-
use function call_user_func;
19-
use function func_get_args;
20-
use function implode;
21-
use function is_callable;
22-
use function json_decode;
23-
use function json_encode;
24-
use function sprintf;
25-
use function strtolower;
26-
use function sys_get_temp_dir;
2710

2811
if (Kernel::VERSION_ID < 70000) {
2912
abstract class TestCase extends BaseTestCase
@@ -44,153 +27,3 @@ protected static function getContainer(): Container
4427
}
4528
}
4629
}
47-
48-
49-
/**
50-
* TestCase.
51-
*/
52-
abstract class BaseTestCase extends WebTestCase
53-
{
54-
public const USER_RYAN = 'ryan';
55-
public const USER_ADMIN = 'admin';
56-
public const ANONYMOUS_USER = null;
57-
public const DEFAULT_PASSWORD = '123';
58-
59-
/**
60-
* {@inheritdoc}
61-
*/
62-
protected static function getKernelClass(): string
63-
{
64-
return TestKernel::class;
65-
}
66-
67-
/**
68-
* {@inheritdoc}
69-
*/
70-
protected static function createKernel(array $options = []): KernelInterface
71-
{
72-
if (null === static::$class) {
73-
static::$class = static::getKernelClass();
74-
}
75-
76-
$options['test_case'] ??= '';
77-
78-
$env = $options['environment'] ?? 'test' . strtolower($options['test_case']);
79-
$debug = $options['debug'] ?? true;
80-
81-
return new static::$class($env, $debug, $options['test_case']);
82-
}
83-
84-
/**
85-
* {@inheritdoc}
86-
*/
87-
public static function setUpBeforeClass(): void
88-
{
89-
$fs = new Filesystem();
90-
$fs->remove(sys_get_temp_dir() . '/OverblogGraphQLBundle/');
91-
}
92-
93-
protected function tearDown(): void
94-
{
95-
static::ensureKernelShutdown();
96-
}
97-
98-
protected static function executeGraphQLRequest(string $query, array $rootValue = [], string $schemaName = null, array $variables = []): array
99-
{
100-
$request = new Request();
101-
$request->query->set('query', $query);
102-
if (!empty($variables)) {
103-
$request->query->set('variables', $variables);
104-
}
105-
106-
// @phpstan-ignore-next-line
107-
$req = static::getContainer()->get('overblog_graphql.request_parser')->parse($request);
108-
// @phpstan-ignore-next-line
109-
$res = static::getContainer()->get('overblog_graphql.request_executor')->execute($schemaName, $req, $rootValue);
110-
111-
return $res->toArray();
112-
}
113-
114-
protected static function assertGraphQL(string $query, array $expectedData = null, array $expectedErrors = null, array $rootValue = [], string $schemaName = null): void
115-
{
116-
$result = static::executeGraphQLRequest($query, $rootValue, $schemaName);
117-
118-
$expected = [];
119-
120-
if (null !== $expectedErrors) {
121-
$expected['errors'] = $expectedErrors;
122-
}
123-
124-
if (null !== $expectedData) {
125-
$expected['data'] = $expectedData;
126-
}
127-
128-
static::assertSame($expected, $result, json_encode($result));
129-
}
130-
131-
protected static function query(string $query, string $username, string $testCase, string $password = self::DEFAULT_PASSWORD): KernelBrowser
132-
{
133-
$client = static::createClientAuthenticated($username, $testCase, $password);
134-
$client->request('GET', '/', ['query' => $query]);
135-
136-
return $client;
137-
}
138-
139-
protected static function createClientAuthenticated(?string $username, string $testCase, ?string $password = self::DEFAULT_PASSWORD): KernelBrowser
140-
{
141-
static::ensureKernelShutdown();
142-
$client = static::createClient(['test_case' => $testCase]);
143-
144-
if (null !== $username) {
145-
$client->setServerParameters([
146-
'PHP_AUTH_USER' => $username,
147-
'PHP_AUTH_PW' => $password,
148-
]);
149-
}
150-
151-
return $client;
152-
}
153-
154-
protected static function assertResponse(string $query, array $expected, ?string $username, string $testCase, ?string $password = self::DEFAULT_PASSWORD, array $variables = null): KernelBrowser
155-
{
156-
$client = self::createClientAuthenticated($username, $testCase, $password);
157-
$result = self::sendRequest($client, $query, false, $variables);
158-
159-
static::assertSame($expected, json_decode($result, true), $result);
160-
161-
return $client;
162-
}
163-
164-
/**
165-
* @return mixed
166-
*/
167-
protected static function sendRequest(KernelBrowser $client, string $query, bool $isDecoded = false, array $variables = null)
168-
{
169-
$client->request('GET', '/', ['query' => $query, 'variables' => json_encode($variables)]);
170-
$result = $client->getResponse()->getContent();
171-
172-
return $isDecoded ? json_decode($result, true) : $result;
173-
}
174-
175-
/**
176-
* @return mixed|ExpressionFunction
177-
*/
178-
public static function expressionFunctionFromPhp(string $phpFunctionName)
179-
{
180-
if (is_callable([ExpressionFunction::class, 'fromPhp'])) {
181-
return call_user_func([ExpressionFunction::class, 'fromPhp'], $phpFunctionName);
182-
}
183-
184-
return new ExpressionFunction($phpFunctionName, fn() => sprintf('\%s(%s)', $phpFunctionName, implode(', ', func_get_args())), function (): void {});
185-
}
186-
187-
/**
188-
* @param KernelBrowser $client
189-
*/
190-
protected function disableCatchExceptions($client): void
191-
{
192-
if (is_callable([$client, 'catchExceptions'])) {
193-
$client->catchExceptions(false);
194-
}
195-
}
196-
}

0 commit comments

Comments
 (0)