Skip to content

Commit 5e043c9

Browse files
wip: 5.x (2)
1 parent c92bf73 commit 5e043c9

File tree

11 files changed

+34
-554
lines changed

11 files changed

+34
-554
lines changed

UPGRADE-5.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Upgrade guide from 3.x to 4.x
2+
3+
## Needed actions
4+
This is the list of actions that you need to take when upgrading this bundle from the 3.x to the 4.x version:
5+
6+
*

doc/basic.md

Lines changed: 5 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ Basic usage
44
> [!TIP]
55
> Some methods provided by this bundle have been [implemented in Symfony](https://symfony.com/doc/current/testing.html#application-tests). Alternative ways will be shown below.
66
7-
Use `$this->makeClient` to create a Client object. Client is a Symfony class
7+
Use `$this->createClientWithParams()` to create a Client object. Client is a Symfony class
88
that can simulate HTTP requests to your controllers and then inspect the
99
results. It is covered by the [functional tests](http://symfony.com/doc/current/book/testing.html#functional-tests)
1010
section of the Symfony documentation.
1111

12-
After making a request, use `assertStatusCode` to verify the HTTP status code.
13-
If it fails it will display the last exception message or validation errors
14-
encountered by the Client object.
15-
1612
If you are expecting validation errors, test them with `assertValidationErrors`.
1713

1814
```php
@@ -22,120 +18,28 @@ class MyControllerTest extends WebTestCase
2218
{
2319
public function testContact()
2420
{
25-
$client = $this->makeClient();
21+
$client = $this->createClient();
2622
$crawler = $client->request('GET', '/contact');
27-
$this->assertStatusCode(200, $client);
23+
self::assertResponseStatusCodeSame(200);
2824

2925
$form = $crawler->selectButton('Submit')->form();
3026
$crawler = $client->submit($form);
3127

3228
// We should get a validation error for the empty fields.
33-
$this->assertStatusCode(200, $client);
29+
self::assertResponseStatusCodeSame(200);
3430
$this->assertValidationErrors(['data.email', 'data.message'], $client->getContainer());
3531

3632
// Try again with with the fields filled out.
3733
$form = $crawler->selectButton('Submit')->form();
3834
$form->setValues(['contact[email]' => '[email protected]', 'contact[message]' => 'Hello']);
3935
$client->submit($form);
40-
$this->assertStatusCode(302, $client);
41-
}
42-
}
43-
```
44-
45-
> [!TIP]
46-
> Instead of calling `$this->makeClient`, consider calling `createClient()` from Symfony's `WebTestCase`:
47-
48-
```php
49-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
50-
51-
class MyControllerTest extends WebTestCase
52-
{
53-
public function testContact()
54-
{
55-
$client = static::createClient();
56-
$client->request('GET', '/contact');
57-
58-
// …
36+
self::assertResponseStatusCodeSame(302);
5937
}
6038
}
6139
```
6240

6341
### Methods
6442

65-
#### Check HTTP status codes
66-
67-
##### isSuccessful()
68-
69-
Check that the request succeeded:
70-
71-
```php
72-
$client = $this->makeClient();
73-
$client->request('GET', '/contact');
74-
75-
// Successful HTTP request
76-
$this->isSuccessful($client->getResponse());
77-
```
78-
79-
> [!TIP]
80-
> Call `assertResponseIsSuccessful()` from Symfony's `WebTestCase` ([documentation](https://symfony.com/doc/current/testing.html#response-assertions)):
81-
82-
```php
83-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
84-
85-
class MyControllerTest extends WebTestCase
86-
{
87-
public function testContact()
88-
{
89-
$client = static::createClient();
90-
$client->request('GET', '/contact');
91-
92-
self::assertResponseIsSuccessful();
93-
}
94-
}
95-
```
96-
97-
Add `false` as the second argument in order to check that the request failed:
98-
99-
```php
100-
$client = $this->makeClient();
101-
$client->request('GET', '/error');
102-
103-
// Request returned an error
104-
$this->isSuccessful($client->getResponse(), false);
105-
```
106-
107-
In order to test more specific status codes, use `assertStatusCode()`:
108-
109-
##### assertStatusCode()
110-
111-
Check the HTTP status code from the request:
112-
113-
```php
114-
$client = $this->makeClient();
115-
$client->request('GET', '/contact');
116-
117-
// Standard response for successful HTTP request
118-
$this->assertStatusCode(302, $client);
119-
```
120-
121-
> [!TIP]
122-
> Call `assertResponseStatusCodeSame()` from Symfony's `WebTestCase` ([documentation](https://symfony.com/doc/current/testing.html#response-assertions)):
123-
124-
```php
125-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
126-
127-
class MyControllerTest extends WebTestCase
128-
{
129-
public function testContact()
130-
{
131-
$client = static::createClient();
132-
$client->request('GET', '/contact');
133-
134-
self::assertResponseStatusCodeSame(302);
135-
}
136-
}
137-
```
138-
13943
#### Get Crawler or content
14044

14145
##### fetchCrawler()
@@ -181,44 +85,6 @@ class MyControllerTest extends WebTestCase
18185
}
18286
```
18387

184-
##### fetchContent()
185-
186-
Get the content of a URL:
187-
188-
```php
189-
$content = $this->fetchContent('/contact');
190-
191-
// `filter()` can't be used since the output is HTML code, check the content directly
192-
$this->assertStringContainsString(
193-
'<h1>LiipFunctionalTestBundle</h1>',
194-
$content
195-
);
196-
```
197-
198-
> [!TIP]
199-
> Call `getResponse()->getContent()` or use `assertSelectorText*()` from Symfony's `WebTestCase` ([documentation](https://symfony.com/doc/current/testing.html#crawler-assertions)):
200-
201-
```php
202-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
203-
204-
class MyControllerTest extends WebTestCase
205-
{
206-
public function testContact()
207-
{
208-
$client = static::createClient();
209-
$client->request('GET', '/contact');
210-
211-
$this->assertStringContainsString(
212-
'<h1>LiipFunctionalTestBundle</h1>',
213-
$client->getResponse()->getContent()
214-
);
215-
216-
//or
217-
self::assertSelectorTextContains('h1', 'LiipFunctionalTestBundle');
218-
}
219-
}
220-
```
221-
22288
#### Routing
22389

22490
##### getURL()

doc/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class ExampleFunctionalTest extends WebTestCase
9393

9494
public function testValidationErrors(): void
9595
{
96-
$client = $this->makeClient(true);
96+
$client = $this->createClientWithParams();
9797
$crawler = $client->request('GET', '/users/1/edit');
9898

9999
$client->submit($crawler->selectButton('Save')->form());

doc/installation.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Upgrade from previous version
22
========================
33

4-
See [Upgrade guide from 1.x to 2.0](../UPGRADE-2.0.md) and [Upgrade guide from 2.x to 3.x](../UPGRADE-3.0.md).
4+
See [Upgrade guide from 1.x to 2.0](../UPGRADE-2.0.md),
5+
[Upgrade guide from 2.x to 3.x](../UPGRADE-3.0.md) and
6+
[Upgrade guide from 4.x to 5.x](../UPGRADE-5.0.md).
57

68
Installation
79
============
@@ -12,7 +14,7 @@ Installation
1214
following command to download the latest stable version of this bundle:
1315

1416
```bash
15-
$ composer require --dev liip/functional-test-bundle:^4.0.0
17+
$ composer require --dev liip/functional-test-bundle:^5.0.0
1618
```
1719

1820
This command requires you to have Composer installed globally, as explained

doc/logged.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
Create an already logged client
22
===============================
33

4-
> [!TIP]
5-
> Some methods provided by this bundle have been implemented in Symfony. Alternative ways will be shown below.
6-
7-
The `WebTestCase` provides a convenience method to create an already logged in client using the first parameter of
8-
`WebTestCase::makeClient()`.
9-
10-
You have three alternatives to create an already logged in client:
4+
You have two alternatives to create an already logged in client:
115

126
1. Use the `liip_functional_test.authentication` key in the `config_test.yml` file;
137
2. Pass an array with login parameters directly when you call the method;
14-
3. Use the method `WebTestCase::loginClient()`;
15-
16-
> [!TIP]
17-
> Since Symfony 5.1, [`loginUser()`](https://symfony.com/doc/5.x/testing.html#logging-in-users-authentication) can be used.
188

199
### Logging in a user from the `config_test.yml` file
2010

src/Test/WebTestCase.php

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
use Symfony\Component\Console\Tester\CommandTester;
2424
use Symfony\Component\DependencyInjection\ContainerInterface;
2525
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
26-
use Symfony\Component\DomCrawler\Crawler;
2726
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
2827
use Symfony\Component\HttpFoundation\Request;
2928
use Symfony\Component\HttpFoundation\RequestStack;
30-
use Symfony\Component\HttpFoundation\Response;
3129
use Symfony\Component\HttpFoundation\Session\Session;
3230
use Symfony\Component\HttpFoundation\Session\SessionInterface;
3331
use Symfony\Component\HttpKernel\KernelInterface;
@@ -261,22 +259,6 @@ protected static function createKernel(array $options = []): KernelInterface
261259
* $params can be used to pass headers to the client, note that they have
262260
* to follow the naming format used in $_SERVER.
263261
* Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With'
264-
*
265-
* @deprecated
266-
*/
267-
protected function makeClient(array $params = []): Client
268-
{
269-
return $this->createClientWithParams($params);
270-
}
271-
272-
/**
273-
* Creates an instance of a lightweight Http client.
274-
*
275-
* $params can be used to pass headers to the client, note that they have
276-
* to follow the naming format used in $_SERVER.
277-
* Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With'
278-
*
279-
* @deprecated
280262
*/
281263
protected function makeAuthenticatedClient(array $params = []): Client
282264
{
@@ -288,21 +270,6 @@ protected function makeAuthenticatedClient(array $params = []): Client
288270
return $this->createClientWithParams($params, $username, $password);
289271
}
290272

291-
/**
292-
* Creates an instance of a lightweight Http client and log in user with
293-
* username and password params.
294-
*
295-
* $params can be used to pass headers to the client, note that they have
296-
* to follow the naming format used in $_SERVER.
297-
* Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With'
298-
*
299-
* @deprecated
300-
*/
301-
protected function makeClientWithCredentials(string $username, string $password, array $params = []): Client
302-
{
303-
return $this->createClientWithParams($params, $username, $password);
304-
}
305-
306273
/**
307274
* Create User Token.
308275
*
@@ -348,71 +315,6 @@ protected function getUrl(string $route, array $params = [], int $absolute = Url
348315
return $this->getContainer()->get('router')->generate($route, $params, $absolute);
349316
}
350317

351-
/**
352-
* Checks the success state of a response.
353-
*
354-
* @param Response $response Response object
355-
* @param bool $success to define whether the response is expected to be successful
356-
* @param string $type
357-
*/
358-
public function isSuccessful(Response $response, $success = true, $type = 'text/html'): void
359-
{
360-
HttpAssertions::isSuccessful($response, $success, $type);
361-
}
362-
363-
/**
364-
* Executes a request on the given url and returns the response contents.
365-
*
366-
* This method also asserts the request was successful.
367-
*
368-
* @param string $path path of the requested page
369-
* @param string $method The HTTP method to use, defaults to GET
370-
* @param bool $authentication Whether to use authentication, defaults to false
371-
* @param bool $success to define whether the response is expected to be successful
372-
*/
373-
public function fetchContent(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): string
374-
{
375-
$client = ($authentication) ? $this->makeAuthenticatedClient() : $this->makeClient();
376-
377-
$client->request($method, $path);
378-
379-
$content = $client->getResponse()->getContent();
380-
$this->isSuccessful($client->getResponse(), $success);
381-
382-
return $content;
383-
}
384-
385-
/**
386-
* Executes a request on the given url and returns a Crawler object.
387-
*
388-
* This method also asserts the request was successful.
389-
*
390-
* @param string $path path of the requested page
391-
* @param string $method The HTTP method to use, defaults to GET
392-
* @param bool $authentication Whether to use authentication, defaults to false
393-
* @param bool $success Whether the response is expected to be successful
394-
*/
395-
public function fetchCrawler(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): Crawler
396-
{
397-
$client = ($authentication) ? $this->makeAuthenticatedClient() : $this->makeClient();
398-
399-
$crawler = $client->request($method, $path);
400-
401-
$this->isSuccessful($client->getResponse(), $success);
402-
403-
return $crawler;
404-
}
405-
406-
/**
407-
* Asserts that the HTTP response code of the last request performed by
408-
* $client matches the expected code. If not, raises an error with more
409-
* information.
410-
*/
411-
public static function assertStatusCode(int $expectedStatusCode, Client $client): void
412-
{
413-
HttpAssertions::assertStatusCode($expectedStatusCode, $client);
414-
}
415-
416318
/**
417319
* Assert that the last validation errors within $container match the
418320
* expected keys.
@@ -439,10 +341,7 @@ protected function tearDown(): void
439341
parent::tearDown();
440342
}
441343

442-
/**
443-
* @deprecated
444-
*/
445-
protected function createClientWithParams(array $params, ?string $username = null, ?string $password = null): Client
344+
protected function createClientWithParams(array $params = [], ?string $username = null, ?string $password = null): Client
446345
{
447346
if ($username && $password) {
448347
$params = array_merge($params, [

0 commit comments

Comments
 (0)