Skip to content

Commit 42f67c0

Browse files
committed
Add new assertions
1 parent c37d23e commit 42f67c0

32 files changed

Lines changed: 1483 additions & 143 deletions

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
First of all: Contributions are very welcome!
44

5+
## Assertion Naming Conventions
6+
7+
When adding new assertions, prefer these conventions:
8+
9+
- Use `see*` / `dontSee*` for high-level, actor-style checks (Codeception style), where the method expresses an observed application state.
10+
- Use `assert*` for direct value/constraint comparisons that map closely to PHPUnit/Symfony assertion semantics.
11+
12+
### Doctrine checks in this module
13+
14+
Doctrine-related assertions in this repository belong to the Symfony module because they rely on Symfony kernel/container integration and the service graph available during functional tests.
15+
516
**Does your change require a test?**
617

718
## No, my change does not require a test

src/Codeception/Module/Symfony.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Codeception\Module\Symfony\DataCollectorName;
1717
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
1818
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
19+
use Codeception\Module\Symfony\EnvironmentAssertionsTrait;
1920
use Codeception\Module\Symfony\EventsAssertionsTrait;
2021
use Codeception\Module\Symfony\FormAssertionsTrait;
2122
use Codeception\Module\Symfony\HttpClientAssertionsTrait;
@@ -143,6 +144,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
143144
use ConsoleAssertionsTrait;
144145
use DoctrineAssertionsTrait;
145146
use DomCrawlerAssertionsTrait;
147+
use EnvironmentAssertionsTrait;
146148
use EventsAssertionsTrait;
147149
use FormAssertionsTrait;
148150
use HttpClientAssertionsTrait;
@@ -389,10 +391,10 @@ protected function debugResponse(mixed $url): void
389391
return;
390392
}
391393

392-
$this->debugCollector($profile, DataCollectorName::SECURITY->value);
393-
$this->debugCollector($profile, DataCollectorName::MAILER->value);
394-
$this->debugCollector($profile, DataCollectorName::NOTIFIER->value);
395-
$this->debugCollector($profile, DataCollectorName::TIME->value);
394+
$this->debugCollector($profile, DataCollectorName::SECURITY);
395+
$this->debugCollector($profile, DataCollectorName::MAILER);
396+
$this->debugCollector($profile, DataCollectorName::NOTIFIER);
397+
$this->debugCollector($profile, DataCollectorName::TIME);
396398
}
397399

398400
protected function doRebootClientKernel(): void
@@ -463,13 +465,13 @@ private function debugTimeData(TimeDataCollector $timeCollector): void
463465
$this->debugSection('Time', sprintf('%.2f ms', $timeCollector->getDuration()));
464466
}
465467

466-
private function debugCollector(Profile $profile, string $name): void
468+
private function debugCollector(Profile $profile, DataCollectorName $name): void
467469
{
468-
if (!$profile->hasCollector($name)) {
470+
if (!$profile->hasCollector($name->value)) {
469471
return;
470472
}
471473

472-
$collector = $profile->getCollector($name);
474+
$collector = $profile->getCollector($name->value);
473475
match (true) {
474476
$collector instanceof SecurityDataCollector => $this->debugSecurityData($collector),
475477
$collector instanceof MessageDataCollector => $this->debugMailerData($collector),

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PHPUnit\Framework\Constraint\LogicalNot;
1010
use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame;
1111
use Symfony\Component\BrowserKit\Test\Constraint\BrowserHasCookie;
12+
use Symfony\Component\BrowserKit\Test\Constraint\BrowserHistoryIsOnFirstPage;
13+
use Symfony\Component\BrowserKit\Test\Constraint\BrowserHistoryIsOnLastPage;
1214
use Symfony\Component\HttpFoundation\Test\Constraint\RequestAttributeValueSame;
1315
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame;
1416
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
@@ -21,6 +23,8 @@
2123
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsUnprocessable;
2224
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
2325

26+
use function class_exists;
27+
use function count;
2428
use function sprintf;
2529

2630
trait BrowserAssertionsTrait
@@ -69,6 +73,78 @@ public function assertBrowserNotHasCookie(string $name, string $path = '/', ?str
6973
$this->assertThatForClient(new LogicalNot(new BrowserHasCookie($name, $path, $domain)), $message);
7074
}
7175

76+
/**
77+
* Asserts that the browser history is currently on the first page.
78+
*
79+
* ```php
80+
* <?php
81+
* $I->assertBrowserHistoryIsOnFirstPage();
82+
* ```
83+
*/
84+
public function assertBrowserHistoryIsOnFirstPage(string $message = ''): void
85+
{
86+
$this->assertTrue(
87+
class_exists(BrowserHistoryIsOnFirstPage::class),
88+
'The assertBrowserHistoryIsOnFirstPage method requires symfony/browser-kit >= 7.4.'
89+
);
90+
91+
$this->assertThatForClient(new BrowserHistoryIsOnFirstPage(), $message);
92+
}
93+
94+
/**
95+
* Asserts that the browser history is not currently on the first page.
96+
*
97+
* ```php
98+
* <?php
99+
* $I->assertBrowserHistoryIsNotOnFirstPage();
100+
* ```
101+
*/
102+
public function assertBrowserHistoryIsNotOnFirstPage(string $message = ''): void
103+
{
104+
$this->assertTrue(
105+
class_exists(BrowserHistoryIsOnFirstPage::class),
106+
'The assertBrowserHistoryIsNotOnFirstPage method requires symfony/browser-kit >= 7.4.'
107+
);
108+
109+
$this->assertThatForClient(new LogicalNot(new BrowserHistoryIsOnFirstPage()), $message);
110+
}
111+
112+
/**
113+
* Asserts that the browser history is currently on the last page.
114+
*
115+
* ```php
116+
* <?php
117+
* $I->assertBrowserHistoryIsOnLastPage();
118+
* ```
119+
*/
120+
public function assertBrowserHistoryIsOnLastPage(string $message = ''): void
121+
{
122+
$this->assertTrue(
123+
class_exists(BrowserHistoryIsOnLastPage::class),
124+
'The assertBrowserHistoryIsOnLastPage method requires symfony/browser-kit >= 7.4.'
125+
);
126+
127+
$this->assertThatForClient(new BrowserHistoryIsOnLastPage(), $message);
128+
}
129+
130+
/**
131+
* Asserts that the browser history is not currently on the last page.
132+
*
133+
* ```php
134+
* <?php
135+
* $I->assertBrowserHistoryIsNotOnLastPage();
136+
* ```
137+
*/
138+
public function assertBrowserHistoryIsNotOnLastPage(string $message = ''): void
139+
{
140+
$this->assertTrue(
141+
class_exists(BrowserHistoryIsOnLastPage::class),
142+
'The assertBrowserHistoryIsNotOnLastPage method requires symfony/browser-kit >= 7.4.'
143+
);
144+
145+
$this->assertThatForClient(new LogicalNot(new BrowserHistoryIsOnLastPage()), $message);
146+
}
147+
72148
/**
73149
* Asserts that the specified request attribute matches the expected value.
74150
*

src/Codeception/Module/Symfony/CacheTrait.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ public function _getContainer(): ContainerInterface
3232
protected function getInternalDomains(): array
3333
{
3434
if (isset($this->state['internalDomains'])) {
35-
/** @var list<non-empty-string> $domains */
36-
$domains = $this->state['internalDomains'];
37-
38-
return $domains;
35+
/** @var list<non-empty-string> */
36+
return $this->state['internalDomains'];
3937
}
4038

4139
$domains = [];
@@ -48,12 +46,14 @@ protected function getInternalDomains(): array
4846
}
4947
}
5048

51-
return $this->state['internalDomains'] = array_values(array_unique($domains));
49+
/** @var list<non-empty-string> $domains */
50+
$domains = array_values(array_unique($domains));
51+
return $this->state['internalDomains'] = $domains;
5252
}
5353

54-
protected function clearInternalDomainsCache(): void
54+
protected function clearRouterCache(): void
5555
{
56-
unset($this->state['internalDomains']);
56+
unset($this->state['internalDomains'], $this->state['cachedRoutes']);
5757
}
5858

5959
/**
@@ -64,7 +64,6 @@ protected function clearInternalDomainsCache(): void
6464
*/
6565
protected function grabCachedService(string $expectedClass, array $serviceIds): ?object
6666
{
67-
/** @var ?string $serviceId */
6867
$serviceId = $this->state[$expectedClass] ??= (function () use ($serviceIds, $expectedClass): ?string {
6968
foreach ($serviceIds as $id) {
7069
if ($this->getService($id) instanceof $expectedClass) {
@@ -75,7 +74,7 @@ protected function grabCachedService(string $expectedClass, array $serviceIds):
7574
return null;
7675
})();
7776

78-
if ($serviceId === null) {
77+
if (!is_string($serviceId)) {
7978
return null;
8079
}
8180

src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Console\Tester\CommandTester;
1010
use Symfony\Component\HttpKernel\KernelInterface;
1111

12+
use function is_int;
1213
use function sprintf;
1314

1415
trait ConsoleAssertionsTrait

0 commit comments

Comments
 (0)