Skip to content

Commit 4b0b042

Browse files
committed
get the inherent Symfony assertions
1 parent 9b77251 commit 4b0b042

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
78
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
89
use function sprintf;
910

1011
trait BrowserAssertionsTrait
1112
{
13+
/**
14+
* Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value.
15+
*/
16+
public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
17+
{
18+
$this->assertThat($this->client->getResponse(), new ResponseFormatSame($this->client->getRequest(), $expectedFormat), $message);
19+
}
20+
1221
/**
1322
* Reboot client's kernel.
1423
* Can be used to manually reboot kernel when 'rebootable_client' => false
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Test\Constraint\CrawlerSelectorExists;
9+
10+
trait DomCrawlerAssertionsTrait
11+
{
12+
/**
13+
* Asserts that the checkbox with the given name is checked.
14+
*/
15+
public function assertCheckboxChecked(string $fieldName, string $message = ''): void
16+
{
17+
$this->assertThat(
18+
$this->getClient()->getCrawler(),
19+
new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"),
20+
$message
21+
);
22+
}
23+
24+
/**
25+
* Asserts that the checkbox with the given name is not checked.
26+
*/
27+
public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
28+
{
29+
$this->assertThat(
30+
$this->getClient()->getCrawler(),
31+
new LogicalNot(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")),
32+
$message
33+
);
34+
}
35+
36+
}

src/Codeception/Module/Symfony/FormAssertionsTrait.php

Lines changed: 13 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,17 @@
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+
1528
/**
1629
* Verifies that there are no errors bound to the submitted form.
1730
*

0 commit comments

Comments
 (0)