1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Functional; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Tests\Functional\App\TestKernel; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* TestCase. |
21
|
|
|
*/ |
22
|
|
|
abstract class TestCase extends WebTestCase |
23
|
|
|
{ |
24
|
|
|
const USER_RYAN = 'ryan'; |
25
|
|
|
const USER_ADMIN = 'admin'; |
26
|
|
|
const ANONYMOUS_USER = null; |
27
|
|
|
const DEFAULT_PASSWORD = '123'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TestKernel[] |
31
|
|
|
*/ |
32
|
|
|
private static $kernels = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected static function getKernelClass() |
38
|
|
|
{ |
39
|
|
|
return TestKernel::class; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected static function createKernel(array $options = []) |
46
|
|
|
{ |
47
|
|
|
if (null === static::$class) { |
48
|
|
|
static::$class = static::getKernelClass(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$options['test_case'] = isset($options['test_case']) ? $options['test_case'] : null; |
52
|
|
|
|
53
|
|
|
$env = isset($options['environment']) ? $options['environment'] : 'overbloggraphbundletest'.strtolower($options['test_case']); |
54
|
|
|
$debug = isset($options['debug']) ? $options['debug'] : true; |
55
|
|
|
|
56
|
|
|
$kernelKey = $options['test_case'] ?: '__default__'; |
57
|
|
|
$kernelKey .= '//'.$env.'//'.var_export($debug, true); |
58
|
|
|
|
59
|
|
|
if (!isset(self::$kernels[$kernelKey])) { |
60
|
|
|
self::$kernels[$kernelKey] = new static::$class($env, $debug, $options['test_case']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return self::$kernels[$kernelKey]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public static function setUpBeforeClass() |
70
|
|
|
{ |
71
|
|
|
$fs = new Filesystem(); |
72
|
|
|
$fs->remove(sys_get_temp_dir().'/OverblogGraphQLBundle/'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
protected function tearDown() |
79
|
|
|
{ |
80
|
|
|
static::$kernel = null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected static function executeGraphQLRequest($query, $rootValue = [], $throwException = false) |
84
|
|
|
{ |
85
|
|
|
$request = new Request(); |
86
|
|
|
$request->query->set('query', $query); |
87
|
|
|
|
88
|
|
|
$req = static::getContainer()->get('overblog_graphql.request_parser')->parse($request); |
89
|
|
|
$executor = static::getContainer()->get('overblog_graphql.request_executor'); |
90
|
|
|
$executor->setThrowException($throwException); |
91
|
|
|
$res = $executor->execute($req, $rootValue); |
92
|
|
|
|
93
|
|
|
return $res->toArray(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected static function assertGraphQL($query, array $expectedData = null, array $expectedErrors = null, $rootValue = []) |
97
|
|
|
{ |
98
|
|
|
$result = static::executeGraphQLRequest($query, $rootValue, true); |
99
|
|
|
|
100
|
|
|
$expected = []; |
101
|
|
|
|
102
|
|
|
if (null !== $expectedData) { |
103
|
|
|
$expected['data'] = $expectedData; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (null !== $expectedErrors) { |
107
|
|
|
$expected['errors'] = $expectedErrors; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
static::assertEquals($expected, $result, json_encode($result)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected static function getContainer() |
114
|
|
|
{ |
115
|
|
|
return static::$kernel->getContainer(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected static function query($query, $username, $testCase, $password = self::DEFAULT_PASSWORD) |
119
|
|
|
{ |
120
|
|
|
$client = static::createClientAuthenticated($username, $testCase, $password); |
121
|
|
|
$client->request('GET', '/', ['query' => $query]); |
122
|
|
|
|
123
|
|
|
return $client; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected static function createClientAuthenticated($username, $testCase, $password = self::DEFAULT_PASSWORD) |
127
|
|
|
{ |
128
|
|
|
$client = static::createClient(['test_case' => $testCase]); |
129
|
|
|
|
130
|
|
|
if ($username) { |
131
|
|
|
$client->setServerParameters([ |
132
|
|
|
'PHP_AUTH_USER' => $username, |
133
|
|
|
'PHP_AUTH_PW' => $password, |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $client; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected static function assertResponse($query, array $expected, $username, $testCase, $password = self::DEFAULT_PASSWORD) |
141
|
|
|
{ |
142
|
|
|
$client = self::createClientAuthenticated($username, $testCase, $password); |
143
|
|
|
$client->request('GET', '/', ['query' => $query]); |
144
|
|
|
|
145
|
|
|
$result = $client->getResponse()->getContent(); |
146
|
|
|
|
147
|
|
|
static::assertEquals($expected, json_decode($result, true), $result); |
148
|
|
|
|
149
|
|
|
return $client; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|