Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ jobs:
symfony-version: ['^6.4']
phpunit-version: ['^9.6']
include:
- php-version: 8.0
symfony-version: "^5.4"
- php-version: 8.1
symfony-version: "^6.4"
phpunit-version: '^9.6'
composer-flags: "--prefer-lowest"
- php-version: 8.1
symfony-version: "^5.4"
phpunit-version: "^10.0"
- php-version: 8.1
symfony-version: "^6.4"
phpunit-version: "^10.0"
Expand Down Expand Up @@ -98,11 +95,6 @@ jobs:
symfony/yaml=${{ matrix.symfony-version }}
phpunit/phpunit=${{ matrix.phpunit-version }}

# This is needed to fix builds where the `annotation_reader` service may not be set up if
# the doctrine/annotations package is not in the production dependencies
- name: Move doctrine/annotations v1/v2 to require (non-dev) for PHP 8+
run: composer require --no-update "doctrine/annotations:^1.8.0|^2.0"

- name: Install Composer dependencies
if: matrix.composer-flags == ''
run: composer install
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Documentation
* [Basic usage](doc/basic.md)
* [Command test](doc/command.md)
* [Logged client](doc/logged.md)
* [Query counter](doc/query.md)
* [Examples](doc/examples.md)
* [Caveats](doc/caveats.md)
* [Contributing](doc/contributing.md)
Expand Down
6 changes: 6 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Upgrade guide from 3.x to 4.x

## Needed actions
This is the list of actions that you need to take when upgrading this bundle from the 3.x to the 4.x version:

* …
25 changes: 12 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@
"require": {
"php": "^8.0",
"phpunit/phpunit": "^9.6 || ^10.0 || ^11.0 || ^12.0",
"symfony/browser-kit": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0 || ^8.0"
"symfony/browser-kit": "^6.4 || ^7.0 || ^8.0",
"symfony/framework-bundle": "^6.4 || ^7.0 || ^8.0"
},
"require-dev": {
"ext-json": "*",
"doctrine/annotations": "^1.3 || ^2.0",
"ext-sqlite3": "*",
"doctrine/doctrine-bundle": "^2.11 || ^3.1.0",
"doctrine/orm": "^2.7 || ^3.0",
"monolog/monolog": "^1.25.1 || ^2.0 || ^3.0",
"symfony/css-selector": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/doctrine-bridge": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/form": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/http-kernel": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/css-selector": "^6.4 || ^7.0 || ^8.0",
"symfony/doctrine-bridge": "^6.4 || ^7.0 || ^8.0",
"symfony/form": "^6.4 || ^7.0 || ^8.0",
"symfony/http-kernel": "^6.4 || ^7.0 || ^8.0",
"symfony/monolog-bundle": "^3.4 || ^4.0",
"symfony/security-bundle": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/twig-bundle": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/validator": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/yaml": "^5.4 || ^6.4 || ^7.0 || ^8.0",
"symfony/security-bundle": "^6.4 || ^7.0 || ^8.0",
"symfony/twig-bundle": "^6.4 || ^7.0 || ^8.0",
"symfony/validator": "^6.4 || ^7.0 || ^8.0",
"symfony/yaml": "^6.4 || ^7.0 || ^8.0",
"twig/twig": "^2.0 || ^3.8"
},
"conflict": {
"symfony/framework-bundle": "4.3.0"
},
"suggest": {
"doctrine/annotations": "Required to use the @QueryCount(…) annotation",
"liip/test-fixtures-bundle": "Efficient loading of Doctrine fixtures in functional test-cases for Symfony applications"
},
"autoload": {
Expand All @@ -59,7 +58,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.x-dev"
"dev-master": "5.x-dev"
}
},
"scripts": {
Expand Down
144 changes: 5 additions & 139 deletions doc/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ Basic usage
> [!TIP]
> 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.

Use `$this->makeClient` to create a Client object. Client is a Symfony class
Use `$this->createClientWithParams()` to create a Client object. Client is a Symfony class
that can simulate HTTP requests to your controllers and then inspect the
results. It is covered by the [functional tests](http://symfony.com/doc/current/book/testing.html#functional-tests)
section of the Symfony documentation.

After making a request, use `assertStatusCode` to verify the HTTP status code.
If it fails it will display the last exception message or validation errors
encountered by the Client object.

If you are expecting validation errors, test them with `assertValidationErrors`.

```php
Expand All @@ -22,120 +18,28 @@ class MyControllerTest extends WebTestCase
{
public function testContact()
{
$client = $this->makeClient();
$client = $this->createClient();
$crawler = $client->request('GET', '/contact');
$this->assertStatusCode(200, $client);
self::assertResponseStatusCodeSame(200);

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

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

// Try again with with the fields filled out.
$form = $crawler->selectButton('Submit')->form();
$form->setValues(['contact[email]' => '[email protected]', 'contact[message]' => 'Hello']);
$client->submit($form);
$this->assertStatusCode(302, $client);
}
}
```

> [!TIP]
> Instead of calling `$this->makeClient`, consider calling `createClient()` from Symfony's `WebTestCase`:

```php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
public function testContact()
{
$client = static::createClient();
$client->request('GET', '/contact');

// …
self::assertResponseStatusCodeSame(302);
}
}
```

### Methods

#### Check HTTP status codes

##### isSuccessful()

Check that the request succeeded:

```php
$client = $this->makeClient();
$client->request('GET', '/contact');

// Successful HTTP request
$this->isSuccessful($client->getResponse());
```

> [!TIP]
> Call `assertResponseIsSuccessful()` from Symfony's `WebTestCase` ([documentation](https://symfony.com/doc/current/testing.html#response-assertions)):

```php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
public function testContact()
{
$client = static::createClient();
$client->request('GET', '/contact');

self::assertResponseIsSuccessful();
}
}
```

Add `false` as the second argument in order to check that the request failed:

```php
$client = $this->makeClient();
$client->request('GET', '/error');

// Request returned an error
$this->isSuccessful($client->getResponse(), false);
```

In order to test more specific status codes, use `assertStatusCode()`:

##### assertStatusCode()

Check the HTTP status code from the request:

```php
$client = $this->makeClient();
$client->request('GET', '/contact');

// Standard response for successful HTTP request
$this->assertStatusCode(302, $client);
```

> [!TIP]
> Call `assertResponseStatusCodeSame()` from Symfony's `WebTestCase` ([documentation](https://symfony.com/doc/current/testing.html#response-assertions)):

```php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
public function testContact()
{
$client = static::createClient();
$client->request('GET', '/contact');

self::assertResponseStatusCodeSame(302);
}
}
```

#### Get Crawler or content

##### fetchCrawler()
Expand Down Expand Up @@ -181,44 +85,6 @@ class MyControllerTest extends WebTestCase
}
```

##### fetchContent()

Get the content of a URL:

```php
$content = $this->fetchContent('/contact');

// `filter()` can't be used since the output is HTML code, check the content directly
$this->assertStringContainsString(
'<h1>LiipFunctionalTestBundle</h1>',
$content
);
```

> [!TIP]
> Call `getResponse()->getContent()` or use `assertSelectorText*()` from Symfony's `WebTestCase` ([documentation](https://symfony.com/doc/current/testing.html#crawler-assertions)):

```php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
public function testContact()
{
$client = static::createClient();
$client->request('GET', '/contact');

$this->assertStringContainsString(
'<h1>LiipFunctionalTestBundle</h1>',
$client->getResponse()->getContent()
);

//or
self::assertSelectorTextContains('h1', 'LiipFunctionalTestBundle');
}
}
```

#### Routing

##### getURL()
Expand Down
4 changes: 2 additions & 2 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ExampleFunctionalTest extends WebTestCase

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

$client->submit($crawler->selectButton('Save')->form());
Expand All @@ -103,4 +103,4 @@ class ExampleFunctionalTest extends WebTestCase
}
```

← [Query counter](./query.md) • [Caveats](./caveats.md) →
← [Examples](./examples.md) • [Caveats](./caveats.md) →
6 changes: 4 additions & 2 deletions doc/installation.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Upgrade from previous version
========================

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).
See [Upgrade guide from 1.x to 2.0](../UPGRADE-2.0.md),
[Upgrade guide from 2.x to 3.x](../UPGRADE-3.0.md) and
[Upgrade guide from 4.x to 5.x](../UPGRADE-5.0.md).

Installation
============
Expand All @@ -12,7 +14,7 @@ Installation
following command to download the latest stable version of this bundle:

```bash
$ composer require --dev liip/functional-test-bundle:^4.0.0
$ composer require --dev liip/functional-test-bundle:^5.0.0
```

This command requires you to have Composer installed globally, as explained
Expand Down
14 changes: 2 additions & 12 deletions doc/logged.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
Create an already logged client
===============================

> [!TIP]
> Some methods provided by this bundle have been implemented in Symfony. Alternative ways will be shown below.

The `WebTestCase` provides a convenience method to create an already logged in client using the first parameter of
`WebTestCase::makeClient()`.

You have three alternatives to create an already logged in client:
You have two alternatives to create an already logged in client:

1. Use the `liip_functional_test.authentication` key in the `config_test.yml` file;
2. Pass an array with login parameters directly when you call the method;
3. Use the method `WebTestCase::loginClient()`;

> [!TIP]
> Since Symfony 5.1, [`loginUser()`](https://symfony.com/doc/5.x/testing.html#logging-in-users-authentication) can be used.

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

Expand Down Expand Up @@ -95,4 +85,4 @@ security:

For more details, you can check the implementation of `WebTestCase` in that bundle.

← [Command test](./command.md) • [Query counter](./query.md) →
← [Command test](./command.md) • [Examples](./examples.md) →
Loading