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\AppKernel; |
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 AppKernel[] |
31
|
|
|
*/ |
32
|
|
|
private static $kernels = []; |
33
|
|
|
|
34
|
|
|
protected static function getKernelClass() |
35
|
|
|
{ |
36
|
|
|
require_once __DIR__.'/app/AppKernel.php'; |
37
|
|
|
|
38
|
|
|
return 'Overblog\GraphQLBundle\Tests\Functional\app\AppKernel'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected static function createKernel(array $options = []) |
45
|
|
|
{ |
46
|
|
|
if (null === static::$class) { |
47
|
|
|
static::$class = static::getKernelClass(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$options['test_case'] = isset($options['test_case']) ? $options['test_case'] : null; |
51
|
|
|
|
52
|
|
|
$env = isset($options['environment']) ? $options['environment'] : 'overbloggraphbundletest'.strtolower($options['test_case']); |
53
|
|
|
$debug = isset($options['debug']) ? $options['debug'] : true; |
54
|
|
|
|
55
|
|
|
$kernelKey = $options['test_case'] ?: '__default__'; |
56
|
|
|
$kernelKey .= '//'.$env.'//'.var_export($debug, true); |
57
|
|
|
|
58
|
|
|
if (!isset(self::$kernels[$kernelKey])) { |
59
|
|
|
self::$kernels[$kernelKey] = new static::$class($env, $debug, $options['test_case']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return self::$kernels[$kernelKey]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public static function setUpBeforeClass() |
69
|
|
|
{ |
70
|
|
|
$fs = new Filesystem(); |
71
|
|
|
$fs->remove(sys_get_temp_dir().'/OverblogGraphQLBundle/'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected function tearDown() |
78
|
|
|
{ |
79
|
|
|
static::$kernel = null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected static function createAndBootKernel(array $options = []) |
83
|
|
|
{ |
84
|
|
|
static::bootKernel($options); |
85
|
|
|
|
86
|
|
|
static::getContainer()->get('overblog_graphql.cache_compiler')->loadClasses(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected static function executeGraphQLRequest($query, $rootValue = [], $throwException = false) |
90
|
|
|
{ |
91
|
|
|
$request = new Request(); |
92
|
|
|
$request->query->set('query', $query); |
93
|
|
|
|
94
|
|
|
$req = static::getContainer()->get('overblog_graphql.request_parser')->parse($request); |
95
|
|
|
$executor = static::getContainer()->get('overblog_graphql.request_executor'); |
96
|
|
|
$executor->setThrowException($throwException); |
97
|
|
|
$res = $executor->execute($req, $rootValue); |
98
|
|
|
|
99
|
|
|
return $res->toArray(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected static function assertGraphQL($query, array $expectedData = null, array $expectedErrors = null, $rootValue = []) |
103
|
|
|
{ |
104
|
|
|
$result = static::executeGraphQLRequest($query, $rootValue, true); |
105
|
|
|
|
106
|
|
|
$expected = []; |
107
|
|
|
|
108
|
|
|
if (null !== $expectedData) { |
109
|
|
|
$expected['data'] = $expectedData; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (null !== $expectedErrors) { |
113
|
|
|
$expected['errors'] = $expectedErrors; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
static::assertEquals($expected, $result, json_encode($result)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected static function getContainer() |
120
|
|
|
{ |
121
|
|
|
return static::$kernel->getContainer(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected static function query($query, $username, $testCase, $password = self::DEFAULT_PASSWORD) |
125
|
|
|
{ |
126
|
|
|
$client = static::createClientAuthenticated($username, $testCase, $password); |
127
|
|
|
$client->request('GET', '/', ['query' => $query]); |
128
|
|
|
|
129
|
|
|
return $client; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected static function createClientAuthenticated($username, $testCase, $password = self::DEFAULT_PASSWORD) |
133
|
|
|
{ |
134
|
|
|
$client = static::createClient(['test_case' => $testCase]); |
135
|
|
|
|
136
|
|
|
if ($username) { |
137
|
|
|
$client->setServerParameters([ |
138
|
|
|
'PHP_AUTH_USER' => $username, |
139
|
|
|
'PHP_AUTH_PW' => $password, |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $client; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected static function assertResponse($query, array $expected, $username, $testCase, $password = self::DEFAULT_PASSWORD) |
147
|
|
|
{ |
148
|
|
|
$client = self::createClientAuthenticated($username, $testCase, $password); |
149
|
|
|
$client->request('GET', '/', ['query' => $query]); |
150
|
|
|
|
151
|
|
|
$result = $client->getResponse()->getContent(); |
152
|
|
|
|
153
|
|
|
static::assertEquals($expected, json_decode($result, true), $result); |
154
|
|
|
|
155
|
|
|
return $client; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|