Skip to content

Commit 79ac0ff

Browse files
committed
test: cover session login and add time, translation, twig, validator asserts
# Conflicts: # composer.json
1 parent 70e5e06 commit 79ac0ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1745
-15
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
/vendor/
33
/composer.lock
44
/framework-tests
5-
/.php-cs-fixer.cache
5+
/.php-cs-fixer.cache
6+
tests/_output/
7+
tests/_support/_generated/
8+
tests/_support/FunctionalTester.php
9+
var/
10+

codeception.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace: Tests
2+
actor_suffix: Tester
3+
paths:
4+
tests: tests
5+
output: tests/_output
6+
data: tests/_data
7+
support: tests/_support

composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
"require": {
2525
"php": "^8.2",
2626
"ext-json": "*",
27-
"codeception/codeception": "^5.3",
2827
"codeception/lib-innerbrowser": "^3.1 | ^4.0"
2928
},
3029
"require-dev": {
30+
"codeception/codeception": "^5.3",
3131
"codeception/module-asserts": "^3.0",
3232
"codeception/module-doctrine": "^3.1",
3333
"doctrine/orm": "^3.5",
3434
"friendsofphp/php-cs-fixer": "^3.85",
3535
"phpstan/phpstan": "^2.1",
36-
"phpunit/phpunit": "^10.0",
36+
"phpunit/phpunit": "^10.5",
3737
"symfony/browser-kit": "^5.4 | ^6.4 | ^7.3",
3838
"symfony/cache": "^5.4 | ^6.4 | ^7.3",
3939
"symfony/config": "^5.4 | ^6.4 | ^7.3",
@@ -73,6 +73,17 @@
7373
"Codeception\\": "src/Codeception/"
7474
}
7575
},
76+
"autoload-dev": {
77+
"psr-4": {
78+
"Tests\\": "tests/"
79+
},
80+
"files": [
81+
"tests/_app/TestKernel.php",
82+
"tests/_app/HelloCommand.php",
83+
"tests/_app/TestUser.php",
84+
"tests/_app/ValidEntity.php"
85+
]
86+
},
7687
"config": {
7788
"sort-packages": true
7889
},

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Codeception Module Symfony
22

33
A Codeception module for Symfony framework.
4+
It can be used with Codeception or as a standalone Symfony BrowserKit client.
45

56
[![Actions Status](https://github.com/Codeception/module-symfony/workflows/CI/badge.svg)](https://github.com/Codeception/module-symfony/actions)
67
[![Latest Stable Version](https://poser.pugx.org/codeception/module-symfony/v/stable)](https://github.com/Codeception/module-symfony/releases)
@@ -18,6 +19,9 @@ A Codeception module for Symfony framework.
1819
composer require "codeception/module-symfony" --dev
1920
```
2021

22+
To use the connector without Codeception, require the package and instantiate
23+
`Codeception\\Lib\\Connector\\Symfony` with your kernel.
24+
2125
## Documentation
2226

2327
See [the module documentation](https://codeception.com/docs/modules/Symfony).

src/Codeception/Lib/Connector/Symfony.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\HttpKernel\KernelInterface;
1818
use Symfony\Component\HttpKernel\Profiler\Profiler;
1919

20-
use function codecept_debug;
20+
use function function_exists;
2121

2222
/**
2323
* @property KernelInterface $kernel
@@ -73,7 +73,9 @@ public function rebootKernel(): void
7373
try {
7474
$this->container->set($name, $service);
7575
} catch (InvalidArgumentException $e) {
76-
codecept_debug("[Symfony] Can't set persistent service {$name}: {$e->getMessage()}");
76+
if (function_exists('codecept_debug')) {
77+
codecept_debug("[Symfony] Can't set persistent service {$name}: {$e->getMessage()}");
78+
}
7779
}
7880
}
7981

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ public function rebootClientKernel(): void
309309
public function seePageIsAvailable(?string $url = null): void
310310
{
311311
if ($url !== null) {
312-
$this->amOnPage($url);
313-
$this->seeInCurrentUrl($url);
312+
$this->getClient()->request('GET', $url);
313+
$this->assertStringContainsString($url, $this->getClient()->getRequest()->getRequestUri());
314314
}
315315

316316
$this->assertResponseIsSuccessful();
@@ -328,12 +328,12 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
328328
{
329329
$client = $this->getClient();
330330
$client->followRedirects(false);
331-
$this->amOnPage($page);
331+
$client->request('GET', $page);
332332

333333
$this->assertThatForResponse(new ResponseIsRedirected(), 'The response is not a redirection.');
334334

335335
$client->followRedirect();
336-
$this->seeInCurrentUrl($redirectsTo);
336+
$this->assertStringContainsString($redirectsTo, $client->getRequest()->getRequestUri());
337337
}
338338

339339
/**
@@ -362,9 +362,16 @@ public function submitSymfonyForm(string $name, array $fields): void
362362
$params[$name . $key] = $value;
363363
}
364364

365-
$button = sprintf('%s_submit', $name);
365+
if (method_exists($this, 'submitForm')) { // @phpstan-ignore-line
366+
$button = sprintf('%s_submit', $name);
367+
$this->submitForm($selector, $params, $button);
368+
return;
369+
}
366370

367-
$this->submitForm($selector, $params, $button);
371+
$node = $this->getClient()->getCrawler()->filter($selector);
372+
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $selector));
373+
$form = $node->form();
374+
$this->getClient()->submit($form, $params);
368375
}
369376

370377
protected function assertThatForClient(Constraint $constraint, string $message = ''): void

src/Codeception/Module/Symfony/RouterAssertionsTrait.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ private function getCurrentRouteMatch(string $routeName): array
112112
{
113113
$this->assertRouteExists($routeName);
114114

115-
$url = $this->grabFromCurrentUrl();
116-
Assert::assertIsString($url, 'Unable to obtain current URL.');
115+
$url = $this->getClient()->getRequest()->getRequestUri();
117116
$path = (string) parse_url($url, PHP_URL_PATH);
118117

119118
/** @var array<string, mixed> $match */
@@ -143,7 +142,7 @@ private function assertRouteExists(string $routeName): void
143142
/** @param array<string, mixed> $params */
144143
private function openRoute(string $routeName, array $params = []): void
145144
{
146-
$this->amOnPage($this->grabRouterService()->generate($routeName, $params));
145+
$this->getClient()->request('GET', $this->grabRouterService()->generate($routeName, $params));
147146
}
148147

149148
protected function grabRouterService(): RouterInterface

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function dontSeeInSession(string $attribute, mixed $value = null): void
8282
*/
8383
public function goToLogoutPath(): void
8484
{
85-
$this->amOnPage($this->getLogoutUrlGenerator()->getLogoutPath());
85+
$this->getClient()->request('GET', $this->getLogoutUrlGenerator()->getLogoutPath());
8686
}
8787

8888
/**

tests/BrowserAssertionsTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Codeception\Module\Symfony\BrowserAssertionsTrait;
6+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8+
use Symfony\Component\BrowserKit\Cookie;
9+
10+
class BrowserAssertionsTest extends KernelTestCase
11+
{
12+
use BrowserAssertionsTrait;
13+
14+
private KernelBrowser $client;
15+
16+
protected function setUp(): void
17+
{
18+
self::bootKernel();
19+
$this->client = new KernelBrowser(self::$kernel);
20+
$this->client->getCookieJar()->set(new Cookie('browser_cookie', 'value'));
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
parent::tearDown();
26+
restore_exception_handler();
27+
}
28+
29+
protected function getClient(): KernelBrowser
30+
{
31+
return $this->client;
32+
}
33+
34+
protected static function getKernelClass(): string
35+
{
36+
return \TestKernel::class;
37+
}
38+
39+
public function testBrowserAssertions(): void
40+
{
41+
$this->client->request('GET', '/sample');
42+
43+
$this->assertBrowserHasCookie('browser_cookie');
44+
$this->assertBrowserCookieValueSame('browser_cookie', 'value');
45+
$this->assertBrowserNotHasCookie('missing_cookie');
46+
47+
$this->assertRequestAttributeValueSame('foo', 'bar');
48+
49+
$this->assertResponseHasCookie('response_cookie');
50+
$this->assertResponseCookieValueSame('response_cookie', 'yum');
51+
$this->assertResponseNotHasCookie('other_cookie');
52+
53+
$this->assertResponseHasHeader('X-Test');
54+
$this->assertResponseHeaderSame('X-Test', '1');
55+
$this->assertResponseHeaderNotSame('X-Test', '2');
56+
$this->assertResponseNotHasHeader('X-None');
57+
58+
$this->assertResponseFormatSame('html');
59+
$this->assertResponseIsSuccessful();
60+
$this->assertResponseStatusCodeSame(200);
61+
$this->assertRouteSame('sample');
62+
63+
$this->seePageIsAvailable('/sample');
64+
$this->seePageRedirectsTo('/redirect', '/sample');
65+
66+
$this->client->request('GET', '/unprocessable');
67+
$this->assertResponseIsUnprocessable();
68+
}
69+
}

tests/BrowserKitConnectorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Codeception\Lib\Connector\Symfony as SymfonyConnector;
6+
use PHPUnit\Framework\TestCase;
7+
class BrowserKitConnectorTest extends TestCase
8+
{
9+
public function testRequestReturnsSuccessfulResponse(): void
10+
{
11+
$kernel = new \TestKernel('test', true);
12+
$kernel->boot();
13+
$browser = new SymfonyConnector($kernel);
14+
15+
$browser->request('GET', '/');
16+
17+
$this->assertSame(200, $browser->getResponse()->getStatusCode());
18+
$this->assertSame('OK', $browser->getResponse()->getContent());
19+
}
20+
21+
protected function tearDown(): void
22+
{
23+
restore_exception_handler();
24+
parent::tearDown();
25+
}
26+
}

0 commit comments

Comments
 (0)