Skip to content

Commit 65d6f71

Browse files
committed
get the inherent Symfony assertions
1 parent 9b77251 commit 65d6f71

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-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: 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->getClient()->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)