Skip to content

Commit 8d6c9b9

Browse files
committed
get the assertions inherent to Symfony
1 parent 9b77251 commit 8d6c9b9

File tree

5 files changed

+193
-1
lines changed

5 files changed

+193
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "library",
66
"keywords": [
77
"codeception",
8+
"functional testing",
89
"symfony"
910
],
1011
"authors": [

src/Codeception/Module/Symfony.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Codeception\Module\Symfony\BrowserAssertionsTrait;
1414
use Codeception\Module\Symfony\ConsoleAssertionsTrait;
1515
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
16+
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
1617
use Codeception\Module\Symfony\EventsAssertionsTrait;
1718
use Codeception\Module\Symfony\FormAssertionsTrait;
1819
use Codeception\Module\Symfony\MailerAssertionsTrait;
@@ -135,6 +136,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
135136
use BrowserAssertionsTrait;
136137
use ConsoleAssertionsTrait;
137138
use DoctrineAssertionsTrait;
139+
use DomCrawlerAssertionsTrait;
138140
use EventsAssertionsTrait;
139141
use FormAssertionsTrait;
140142
use MailerAssertionsTrait;

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,118 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use PHPUnit\Framework\Constraint\Constraint;
8+
use PHPUnit\Framework\Constraint\LogicalAnd;
9+
use PHPUnit\Framework\Constraint\LogicalNot;
10+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
11+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie;
12+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasHeader;
13+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderLocationSame;
14+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderSame;
15+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsRedirected;
716
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
17+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
818
use function sprintf;
919

1020
trait BrowserAssertionsTrait
1121
{
22+
/**
23+
* Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value.
24+
*/
25+
public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
26+
{
27+
$this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message);
28+
}
29+
30+
/**
31+
* Asserts that the response was successful (HTTP status is 2xx).
32+
*/
33+
public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void
34+
{
35+
$this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message);
36+
}
37+
38+
/**
39+
* Asserts a specific HTTP status code.
40+
*/
41+
public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void
42+
{
43+
$this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode, $verbose), $message);
44+
}
45+
46+
/**
47+
* Asserts the response is a redirect response (optionally, you can check the target location and status code).
48+
* The excepted location can be either an absolute or a relative path.
49+
*/
50+
public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void
51+
{
52+
$constraint = new ResponseIsRedirected($verbose);
53+
if ($expectedLocation) {
54+
if (class_exists(ResponseHeaderLocationSame::class)) {
55+
$locationConstraint = new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation);
56+
} else {
57+
$locationConstraint = new ResponseHeaderSame('Location', $expectedLocation);
58+
}
59+
60+
$constraint = LogicalAnd::fromConstraints($constraint, $locationConstraint);
61+
}
62+
if ($expectedCode) {
63+
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode));
64+
}
65+
66+
$this->assertThatForResponse($constraint, $message);
67+
}
68+
69+
/**
70+
* Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');.
71+
*/
72+
public function assertResponseHasHeader(string $headerName, string $message = ''): void
73+
{
74+
$this->assertThatForResponse(new ResponseHasHeader($headerName), $message);
75+
}
76+
77+
/**
78+
* Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');.
79+
*/
80+
public function assertResponseNotHasHeader(string $headerName, string $message = ''): void
81+
{
82+
$this->assertThatForResponse(new LogicalNot(new ResponseHasHeader($headerName)), $message);
83+
}
84+
85+
/**
86+
* Asserts the given header does contain the expected value on the response,
87+
* e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');.
88+
*/
89+
public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
90+
{
91+
$this->assertThatForResponse(new ResponseHeaderSame($headerName, $expectedValue), $message);
92+
}
93+
94+
/**
95+
* Asserts the given header does not contain the expected value on the response,
96+
* e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');.
97+
*/
98+
public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
99+
{
100+
$this->assertThatForResponse(new LogicalNot(new ResponseHeaderSame($headerName, $expectedValue)), $message);
101+
}
102+
103+
/**
104+
* Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain).
105+
*/
106+
public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
107+
{
108+
$this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message);
109+
}
110+
111+
/**
112+
* Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain).
113+
*/
114+
public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
115+
{
116+
$this->assertThatForResponse(new LogicalNot(new ResponseHasCookie($name, $path, $domain)), $message);
117+
}
118+
12119
/**
13120
* Reboot client's kernel.
14121
* Can be used to manually reboot kernel when 'rebootable_client' => false
@@ -50,7 +157,7 @@ public function seePageIsAvailable(?string $url = null): void
50157
$this->seeInCurrentUrl($url);
51158
}
52159

53-
$this->assertThat($this->getClient()->getResponse(), new ResponseIsSuccessful());
160+
$this->assertResponseIsSuccessful();
54161
}
55162

56163
/**
@@ -104,4 +211,9 @@ public function submitSymfonyForm(string $name, array $fields): void
104211

105212
$this->submitForm($selector, $params, $button);
106213
}
214+
215+
protected function assertThatForResponse(Constraint $constraint, string $message = ''): void
216+
{
217+
$this->assertThat($this->getClient()->getResponse(), $constraint, $message);
218+
}
107219
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Symfony;
6+
7+
use PHPUnit\Framework\Constraint\LogicalNot;
8+
use Symfony\Component\DomCrawler\Crawler;
9+
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
10+
11+
trait DomCrawlerAssertionsTrait
12+
{
13+
/**
14+
* Asserts that the checkbox with the given name is checked.
15+
*/
16+
public function assertCheckboxChecked(string $fieldName, string $message = ''): void
17+
{
18+
$this->assertThat(
19+
$this->getCrawler(),
20+
new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"),
21+
$message
22+
);
23+
}
24+
25+
/**
26+
* Asserts that the checkbox with the given name is not checked.
27+
*/
28+
public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
29+
{
30+
$this->assertThat(
31+
$this->getCrawler(),
32+
new LogicalNot(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")),
33+
$message
34+
);
35+
}
36+
37+
protected function getCrawler(): Crawler
38+
{
39+
return $this->client->getCrawler();
40+
}
41+
}

src/Codeception/Module/Symfony/FormAssertionsTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use DateInterval;
8+
use DateTime;
79
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
810
use function array_key_exists;
911
use function in_array;
@@ -12,6 +14,40 @@
1214

1315
trait FormAssertionsTrait
1416
{
17+
public function assertDateTimeEquals(DateTime $expected, DateTime $actual): void
18+
{
19+
$this->assertSame($expected->format('c'), $actual->format('c'));
20+
}
21+
22+
public function assertDateIntervalEquals(DateInterval $expected, DateInterval $actual): void
23+
{
24+
$format = '%RP%yY%mM%dDT%hH%iM%sS';
25+
$this->assertSame($expected->format($format), $actual->format($format));
26+
}
27+
28+
/**
29+
* Asserts that value of the field of the first form matching the given selector does equal the expected value.
30+
*/
31+
public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void
32+
{
33+
$node = $this->getCrawler()->filter($formSelector);
34+
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
35+
$values = $node->form()->getValues();
36+
$this->assertArrayHasKey($fieldName, $values, $message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector));
37+
$this->assertSame($value, $values[$fieldName]);
38+
}
39+
40+
/**
41+
* Asserts that value of the field of the first form matching the given selector does equal the expected value.
42+
*/
43+
public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void
44+
{
45+
$node = $this->getCrawler()->filter($formSelector);
46+
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
47+
$values = $node->form()->getValues();
48+
$this->assertArrayNotHasKey($fieldName, $values, $message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector));
49+
}
50+
1551
/**
1652
* Verifies that there are no errors bound to the submitted form.
1753
*

0 commit comments

Comments
 (0)