-
-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathWebTestCaseTest.php
227 lines (194 loc) · 10.1 KB
/
WebTestCaseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\Symfony\Bundle\Test;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\HttpFoundation\Cookie as HttpFoundationCookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Copied from Symfony, to remove when https://github.com/symfony/symfony/pull/32207 will be merged.
*/
class WebTestCaseTest extends TestCase
{
public function testAssertResponseIsSuccessful()
{
$this->getResponseTester(new Response())->assertResponseIsSuccessful();
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found");
$this->getResponseTester(new Response('', 404))->assertResponseIsSuccessful();
}
public function testAssertResponseStatusCodeSame()
{
$this->getResponseTester(new Response())->assertResponseStatusCodeSame(200);
$this->getResponseTester(new Response('', 404))->assertResponseStatusCodeSame(404);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found");
$this->getResponseTester(new Response('', 404))->assertResponseStatusCodeSame(200);
}
public function testAssertResponseRedirects()
{
$this->getResponseTester(new Response('', 301))->assertResponseRedirects();
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK");
$this->getResponseTester(new Response())->assertResponseRedirects();
}
public function testAssertResponseRedirectsWithLocation()
{
$this->getResponseTester(new Response('', 301, ['Location' => 'https://example.com/']))->assertResponseRedirects('https://example.com/');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('is redirected and has header "Location" with value "https://example.com/".');
$this->getResponseTester(new Response('', 301))->assertResponseRedirects('https://example.com/');
}
public function testAssertResponseRedirectsWithStatusCode()
{
$this->getResponseTester(new Response('', 302))->assertResponseRedirects(null, 302);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('is redirected and status code is 301.');
$this->getResponseTester(new Response('', 302))->assertResponseRedirects(null, 301);
}
public function testAssertResponseRedirectsWithLocationAndStatusCode()
{
$this->getResponseTester(new Response('', 302, ['Location' => 'https://example.com/']))->assertResponseRedirects('https://example.com/', 302);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessageMatches('#https://example\.com/#');
$this->getResponseTester(new Response('', 302))->assertResponseRedirects('https://example.com/', 301);
}
public function testAssertResponseHasHeader()
{
$this->getResponseTester(new Response())->assertResponseHasHeader('Date');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response has header "X-Date".');
$this->getResponseTester(new Response())->assertResponseHasHeader('X-Date');
}
public function testAssertResponseNotHasHeader()
{
$this->getResponseTester(new Response())->assertResponseNotHasHeader('X-Date');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response does not have header "Date".');
$this->getResponseTester(new Response())->assertResponseNotHasHeader('Date');
}
public function testAssertResponseHeaderSame()
{
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'no-cache, private');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public".');
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'public');
}
public function testAssertResponseHeaderNotSame()
{
$this->getResponseTester(new Response())->assertResponseHeaderNotSame('Cache-Control', 'public');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response does not have header "Cache-Control" with value "no-cache, private".');
$this->getResponseTester(new Response())->assertResponseHeaderNotSame('Cache-Control', 'no-cache, private');
}
public function testAssertResponseHasCookie()
{
$response = new Response();
$response->headers->setCookie(HttpFoundationCookie::create('foo', 'bar'));
$this->getResponseTester($response)->assertResponseHasCookie('foo');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response has cookie "bar".');
$this->getResponseTester($response)->assertResponseHasCookie('bar');
}
public function testAssertResponseNotHasCookie()
{
$response = new Response();
$response->headers->setCookie(HttpFoundationCookie::create('foo', 'bar'));
$this->getResponseTester($response)->assertResponseNotHasCookie('bar');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response does not have cookie "foo".');
$this->getResponseTester($response)->assertResponseNotHasCookie('foo');
}
public function testAssertResponseCookieValueSame()
{
$response = new Response();
$response->headers->setCookie(HttpFoundationCookie::create('foo', 'bar'));
$this->getResponseTester($response)->assertResponseCookieValueSame('foo', 'bar');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('has cookie "bar" and has cookie "bar" with value "bar".');
$this->getResponseTester($response)->assertResponseCookieValueSame('bar', 'bar');
}
public function testAssertBrowserHasCookie()
{
$this->getClientTester()->assertBrowserHasCookie('foo', '/path');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Browser has cookie "bar".');
$this->getClientTester()->assertBrowserHasCookie('bar');
}
public function testAssertBrowserNotHasCookie()
{
$this->getClientTester()->assertBrowserNotHasCookie('bar');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Browser does not have cookie "foo" with path "/path".');
$this->getClientTester()->assertBrowserNotHasCookie('foo', '/path');
}
public function testAssertBrowserCookieValueSame()
{
$this->getClientTester()->assertBrowserCookieValueSame('foo', 'bar', false, '/path');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('has cookie "foo" with path "/path" and has cookie "foo" with path "/path" with value "babar".');
$this->getClientTester()->assertBrowserCookieValueSame('foo', 'babar', false, '/path');
}
public function testAssertRequestAttributeValueSame()
{
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'bar');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Request has attribute "foo" with value "baz".');
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'baz');
}
public function testAssertRouteSame()
{
$this->getRequestTester()->assertRouteSame('homepage', ['foo' => 'bar']);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Request has attribute "_route" with value "articles".');
$this->getRequestTester()->assertRouteSame('articles');
}
private function getResponseTester(Response $response): WebTestCase
{
$client = $this->createMock(KernelBrowser::class);
$client->expects($this->any())->method('getResponse')->willReturn($response);
return $this->getTester($client);
}
private function getClientTester(): WebTestCase
{
$client = $this->createMock(KernelBrowser::class);
$jar = new CookieJar();
$jar->set(new Cookie('foo', 'bar', null, '/path', 'example.com'));
$client->expects($this->any())->method('getCookieJar')->willReturn($jar);
return $this->getTester($client);
}
private function getRequestTester(): WebTestCase
{
$client = $this->createMock(KernelBrowser::class);
$request = new Request();
$request->attributes->set('foo', 'bar');
$request->attributes->set('_route', 'homepage');
$client->expects($this->any())->method('getRequest')->willReturn($request);
return $this->getTester($client);
}
private function getTester(KernelBrowser $client): WebTestCase
{
return new class($client) extends WebTestCase {
use WebTestAssertionsTrait;
public function __construct(KernelBrowser $client)
{
parent::__construct();
self::getClient($client);
}
};
}
}