Skip to content

Commit 9563e4b

Browse files
committed
get the assertions inherent to Symfony
1 parent 9b77251 commit 9563e4b

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,38 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use PHPUnit\Framework\Constraint\Constraint;
8+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
79
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
10+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
811
use function sprintf;
912

1013
trait BrowserAssertionsTrait
1114
{
15+
/**
16+
* Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value.
17+
*/
18+
public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
19+
{
20+
$this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message);
21+
}
22+
23+
/**
24+
* Asserts that the response was successful (HTTP status is 2xx).
25+
*/
26+
public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void
27+
{
28+
$this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message);
29+
}
30+
31+
/**
32+
* Asserts a specific HTTP status code.
33+
*/
34+
public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void
35+
{
36+
$this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode, $verbose), $message);
37+
}
38+
1239
/**
1340
* Reboot client's kernel.
1441
* Can be used to manually reboot kernel when 'rebootable_client' => false
@@ -50,7 +77,7 @@ public function seePageIsAvailable(?string $url = null): void
5077
$this->seeInCurrentUrl($url);
5178
}
5279

53-
$this->assertThat($this->getClient()->getResponse(), new ResponseIsSuccessful());
80+
$this->assertResponseIsSuccessful();
5481
}
5582

5683
/**
@@ -104,4 +131,9 @@ public function submitSymfonyForm(string $name, array $fields): void
104131

105132
$this->submitForm($selector, $params, $button);
106133
}
134+
135+
protected function assertThatForResponse(Constraint $constraint, string $message = ''): void
136+
{
137+
$this->assertThat($this->getClient()->getResponse(), $constraint, $message);
138+
}
107139
}
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)