Skip to content

Commit

Permalink
Merge pull request #637 from alexislefebvre/doc-add-alternative-ways-…
Browse files Browse the repository at this point in the history
…to-perform-tests

doc: add alternative ways to perform tests
  • Loading branch information
alexislefebvre committed Jan 8, 2024
2 parents a1713e3 + 1891034 commit f8e0bd2
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
106 changes: 105 additions & 1 deletion doc/basic.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Basic usage
===========

> [!TIP]
> Some methods provided by this bundle have been implemented in Symfony. Alternative ways will be shown below.
Use `$this->makeClient` 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)
Expand Down Expand Up @@ -39,13 +42,31 @@ class MyControllerTest extends WebTestCase
}
```

> [!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');

// …
}
}
```

### Methods

#### Check HTTP status codes

##### isSuccessful()

Check that the request succedded:
Check that the request succeeded:

```php
$client = $this->makeClient();
Expand All @@ -55,6 +76,24 @@ $client->request('GET', '/contact');
$this->isSuccessful($client->getResponse());
```

> [!TIP]
> Call `assertResponseIsSuccessful()` 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::assertResponseIsSuccessful();
}
}
```

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

```php
Expand All @@ -79,6 +118,24 @@ $client->request('GET', '/contact');
$this->assertStatusCode(302, $client);
```

> [!TIP]
> Call `assertResponseStatusCodeSame()` 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);
}
}
```

#### Get Crawler or content

##### fetchCrawler()
Expand All @@ -95,6 +152,28 @@ $this->assertSame(
);
```

> [!TIP]
> Use the crawler returned by `request()` from Symfony's `WebTestCase`:
```php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

// There is one <body> tag
$this->assertSame(
1,
$crawler->filter('html > body')->count()
);
}
}
```

##### fetchContent()

Get the content of an URL:
Expand All @@ -109,6 +188,27 @@ $this->assertStringContainsString(
);
```

> [!TIP]
> Call `getResponse()->getContent()` 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');

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

#### Routing

##### getURL()
Expand All @@ -129,3 +229,7 @@ $client->request('GET', $path);

$this->isSuccessful($client->getResponse());
```

> [!TIP]
> Consider hard-coding the URLs in the test: it will ensure that if a route is changed,
> the test will fail, so you'll know that there is a Breaking Change.
6 changes: 6 additions & 0 deletions doc/logged.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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 conveniency method to create an already logged in client using the first parameter of
`WebTestCase::makeClient()`.

Expand All @@ -10,6 +13,9 @@ You have three alternatives to create an already logged in client:
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

You can set the credentials for your test user in your `config_test.yml` file:
Expand Down

0 comments on commit f8e0bd2

Please sign in to comment.