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