1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
* This file is part of the Symfony package. |
16
|
|
|
* |
17
|
|
|
* (c) Fabien Potencier <[email protected]> |
18
|
|
|
* |
19
|
|
|
* For the full copyright and license information, please view the LICENSE |
20
|
|
|
* file that was distributed with this source code. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test; |
24
|
|
|
|
25
|
|
|
use PHPUnit\Framework\Constraint\LogicalAnd; |
26
|
|
|
use PHPUnit\Framework\Constraint\LogicalNot; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
28
|
|
|
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
|
|
|
31
|
|
|
use Symfony\Component\HttpFoundation\Test\Constraint as ResponseConstraint; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Copied from Symfony, to remove when https://github.com/symfony/symfony/pull/32207 will be merged. |
35
|
|
|
* |
36
|
|
|
* @internal |
37
|
|
|
*/ |
38
|
|
|
trait BrowserKitAssertionsTrait |
39
|
|
|
{ |
40
|
|
|
public static function assertResponseIsSuccessful(string $message = ''): void |
41
|
|
|
{ |
42
|
|
|
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void |
46
|
|
|
{ |
47
|
|
|
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void |
51
|
|
|
{ |
52
|
|
|
$constraint = new ResponseConstraint\ResponseIsRedirected(); |
53
|
|
|
if ($expectedLocation) { |
54
|
|
|
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseHeaderSame('Location', $expectedLocation)); |
55
|
|
|
} |
56
|
|
|
if ($expectedCode) { |
|
|
|
|
57
|
|
|
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
self::assertThat(self::getResponse(), $constraint, $message); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function assertResponseHasHeader(string $headerName, string $message = ''): void |
64
|
|
|
{ |
65
|
|
|
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function assertResponseNotHasHeader(string $headerName, string $message = ''): void |
69
|
|
|
{ |
70
|
|
|
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void |
74
|
|
|
{ |
75
|
|
|
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void |
79
|
|
|
{ |
80
|
|
|
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void |
84
|
|
|
{ |
85
|
|
|
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void |
89
|
|
|
{ |
90
|
|
|
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void |
94
|
|
|
{ |
95
|
|
|
self::assertThat(self::getResponse(), LogicalAnd::fromConstraints( |
96
|
|
|
new ResponseConstraint\ResponseHasCookie($name, $path, $domain), |
97
|
|
|
new ResponseConstraint\ResponseCookieValueSame($name, $expectedValue, $path, $domain) |
98
|
|
|
), $message); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function assertBrowserHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void |
102
|
|
|
{ |
103
|
|
|
self::assertThat(self::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function assertBrowserNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void |
107
|
|
|
{ |
108
|
|
|
self::assertThat(self::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', string $domain = null, string $message = ''): void |
112
|
|
|
{ |
113
|
|
|
self::assertThat(self::getClient(), LogicalAnd::fromConstraints( |
|
|
|
|
114
|
|
|
new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), |
115
|
|
|
new BrowserKitConstraint\BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain) |
116
|
|
|
), $message); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void |
120
|
|
|
{ |
121
|
|
|
self::assertThat(self::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public static function assertRouteSame($expectedRoute, array $parameters = [], string $message = ''): void |
125
|
|
|
{ |
126
|
|
|
$constraint = new ResponseConstraint\RequestAttributeValueSame('_route', $expectedRoute); |
127
|
|
|
$constraints = []; |
128
|
|
|
foreach ($parameters as $key => $value) { |
129
|
|
|
$constraints[] = new ResponseConstraint\RequestAttributeValueSame($key, $value); |
130
|
|
|
} |
131
|
|
|
if ($constraints) { |
132
|
|
|
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
self::assertThat(self::getRequest(), $constraint, $message); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private static function getClient(KernelBrowser $newClient = null): ?KernelBrowser |
139
|
|
|
{ |
140
|
|
|
static $client; |
141
|
|
|
|
142
|
|
|
if (0 < \func_num_args()) { |
143
|
|
|
return $client = $newClient; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (!$client instanceof KernelBrowser) { |
147
|
|
|
static::fail(sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $client; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private static function getResponse(): Response |
154
|
|
|
{ |
155
|
|
|
if (!$response = self::getClient()->getResponse()) { |
|
|
|
|
156
|
|
|
static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $response; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private static function getRequest(): Request |
163
|
|
|
{ |
164
|
|
|
if (!$request = self::getClient()->getRequest()) { |
|
|
|
|
165
|
|
|
static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $request; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: