Skip to content

Commit c37d23e

Browse files
committed
Run module tests using PHPUnit
1 parent 768cdc6 commit c37d23e

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

+2467
-1
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ jobs:
4242
if: matrix.symfony == '8.0'
4343
run: composer validate --strict && composer audit && composer cs-check && composer phpstan
4444

45+
- name: Run module tests
46+
run: vendor/bin/phpunit tests ${{ matrix.symfony == '8.0' && '--coverage-text --coverage-filter src' || '' }}
47+
4548
- name: Prepare Symfony app & run tests
4649
run: |
4750
composer -d framework-tests remove codeception/module-symfony --dev --no-update

.gitignore

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

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
"Codeception\\": "src/Codeception/"
8585
}
8686
},
87+
"autoload-dev": {
88+
"psr-4": {
89+
"Tests\\": "tests",
90+
"Tests\\App\\": "tests/_app"
91+
}
92+
},
8793
"config": {
8894
"sort-packages": true
8995
},

tests/BrowserAssertionsTest.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Codeception\Module\Symfony\BrowserAssertionsTrait;
8+
use Symfony\Component\BrowserKit\Cookie;
9+
use Tests\Support\CodeceptTestCase;
10+
11+
final class BrowserAssertionsTest extends CodeceptTestCase
12+
{
13+
use BrowserAssertionsTrait;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->client->followRedirects(false);
19+
$this->client->getCookieJar()->set(new Cookie('browser_cookie', 'value'));
20+
}
21+
22+
public function testAssertBrowserCookieValueSame(): void
23+
{
24+
$this->assertBrowserCookieValueSame('browser_cookie', 'value');
25+
}
26+
27+
public function testAssertBrowserHasCookie(): void
28+
{
29+
$this->assertBrowserHasCookie('browser_cookie');
30+
}
31+
32+
public function testAssertBrowserNotHasCookie(): void
33+
{
34+
$this->client->getCookieJar()->expire('browser_cookie');
35+
$this->assertBrowserNotHasCookie('browser_cookie');
36+
}
37+
38+
public function testAssertRequestAttributeValueSame(): void
39+
{
40+
$this->client->request('GET', '/request_attr');
41+
$this->assertRequestAttributeValueSame('page', 'register');
42+
}
43+
44+
public function testAssertResponseCookieValueSame(): void
45+
{
46+
$this->client->request('GET', '/response_cookie');
47+
$this->assertResponseCookieValueSame('TESTCOOKIE', 'codecept');
48+
}
49+
50+
public function testAssertResponseFormatSame(): void
51+
{
52+
$this->client->request('GET', '/response_json');
53+
$this->assertResponseFormatSame('json');
54+
}
55+
56+
public function testAssertResponseHasCookie(): void
57+
{
58+
$this->client->request('GET', '/response_cookie');
59+
$this->assertResponseHasCookie('TESTCOOKIE');
60+
}
61+
62+
public function testAssertResponseHasHeader(): void
63+
{
64+
$this->client->request('GET', '/response_json');
65+
$this->assertResponseHasHeader('content-type');
66+
}
67+
68+
public function testAssertResponseHeaderNotSame(): void
69+
{
70+
$this->client->request('GET', '/response_json');
71+
$this->assertResponseHeaderNotSame('content-type', 'application/octet-stream');
72+
}
73+
74+
public function testAssertResponseHeaderSame(): void
75+
{
76+
$this->client->request('GET', '/response_json');
77+
$this->assertResponseHeaderSame('content-type', 'application/json');
78+
}
79+
80+
public function testAssertResponseIsSuccessful(): void
81+
{
82+
$this->client->request('GET', '/');
83+
$this->assertResponseIsSuccessful();
84+
}
85+
86+
public function testAssertResponseIsUnprocessable(): void
87+
{
88+
$this->client->request('GET', '/unprocessable_entity');
89+
$this->assertResponseIsUnprocessable();
90+
}
91+
92+
public function testAssertResponseNotHasCookie(): void
93+
{
94+
$this->client->request('GET', '/');
95+
$this->assertResponseNotHasCookie('TESTCOOKIE');
96+
}
97+
98+
public function testAssertResponseNotHasHeader(): void
99+
{
100+
$this->client->request('GET', '/');
101+
$this->assertResponseNotHasHeader('accept-charset');
102+
}
103+
104+
public function testAssertResponseRedirects(): void
105+
{
106+
$this->client->followRedirects(false);
107+
$this->client->request('GET', '/redirect_home');
108+
$this->assertResponseRedirects();
109+
$this->assertResponseRedirects('/');
110+
}
111+
112+
public function testAssertResponseStatusCodeSame(): void
113+
{
114+
$this->client->followRedirects(false);
115+
$this->client->request('GET', '/redirect_home');
116+
$this->assertResponseStatusCodeSame(302);
117+
}
118+
119+
public function testAssertRouteSame(): void
120+
{
121+
$this->client->request('GET', '/');
122+
$this->assertRouteSame('index');
123+
$this->client->request('GET', '/login');
124+
$this->assertRouteSame('app_login');
125+
}
126+
127+
public function testRebootClientKernel(): void
128+
{
129+
$this->markTestSkipped('This method relies on Codeception\Lib\Connector\Symfony::rebootKernel(), which is not available in KernelBrowser.');
130+
}
131+
132+
public function testSeePageIsAvailable(): void
133+
{
134+
$this->seePageIsAvailable('/login');
135+
$this->client->request('GET', '/register');
136+
$this->seePageIsAvailable();
137+
}
138+
139+
public function testSeePageRedirectsTo(): void
140+
{
141+
$this->seePageRedirectsTo('/dashboard', '/login');
142+
}
143+
144+
public function testSubmitSymfonyForm(): void
145+
{
146+
$this->client->request('GET', '/register');
147+
$this->submitSymfonyForm('registration_form', [
148+
'[email]' => 'jane_doe@gmail.com',
149+
'[password]' => '123456',
150+
'[agreeTerms]' => true,
151+
]);
152+
$this->assertResponseRedirects('/dashboard');
153+
}
154+
}

tests/ConsoleAssertionsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Codeception\Module\Symfony\ConsoleAssertionsTrait;
8+
use Tests\Support\CodeceptTestCase;
9+
10+
final class ConsoleAssertionsTest extends CodeceptTestCase
11+
{
12+
use ConsoleAssertionsTrait;
13+
14+
public function testRunSymfonyConsoleCommand(): void
15+
{
16+
$this->assertStringContainsString('No option', $this->runSymfonyConsoleCommand('app:test-command'));
17+
$this->assertStringContainsString('Option selected', $this->runSymfonyConsoleCommand('app:test-command', ['--opt' => true]));
18+
$this->assertStringContainsString('Option selected', $this->runSymfonyConsoleCommand('app:test-command', ['-o' => true]));
19+
$this->assertSame('', $this->runSymfonyConsoleCommand('app:test-command', ['-q']));
20+
}
21+
}

tests/DoctrineAssertionsTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
8+
use Tests\App\Entity\User;
9+
use Tests\App\Repository\UserRepository;
10+
use Tests\App\Repository\UserRepositoryInterface;
11+
use Tests\Support\CodeceptTestCase;
12+
13+
final class DoctrineAssertionsTest extends CodeceptTestCase
14+
{
15+
use DoctrineAssertionsTrait;
16+
17+
public function testGrabNumRecords(): void
18+
{
19+
$this->assertSame(1, $this->grabNumRecords(User::class));
20+
}
21+
22+
public function testGrabRepository(): void
23+
{
24+
$this->assertInstanceOf(UserRepository::class, $this->grabRepository(User::class));
25+
$this->assertInstanceOf(UserRepository::class, $this->grabRepository(UserRepository::class));
26+
$this->assertInstanceOf(UserRepository::class, $this->grabRepository($this->grabRepository(User::class)->findOneBy(['email' => 'john_doe@gmail.com'])));
27+
$this->assertInstanceOf(UserRepository::class, $this->grabRepository(UserRepositoryInterface::class));
28+
}
29+
30+
public function testSeeNumRecords(): void
31+
{
32+
$this->seeNumRecords(1, User::class);
33+
}
34+
}

tests/DomCrawlerAssertionsTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
8+
use Tests\Support\CodeceptTestCase;
9+
10+
final class DomCrawlerAssertionsTest extends CodeceptTestCase
11+
{
12+
use DomCrawlerAssertionsTrait;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
$this->client->request('GET', '/test_page');
18+
}
19+
20+
public function testAssertCheckboxChecked(): void
21+
{
22+
$this->assertCheckboxChecked('exampleCheckbox', 'The checkbox should be checked.');
23+
}
24+
25+
public function testAssertCheckboxNotChecked(): void
26+
{
27+
$this->assertCheckboxNotChecked('nonExistentCheckbox', 'This checkbox should not be checked.');
28+
}
29+
30+
public function testAssertInputValueNotSame(): void
31+
{
32+
$this->assertInputValueNotSame('exampleInput', 'Wrong Value', 'The input value should not be "Wrong Value".');
33+
}
34+
35+
public function testAssertInputValueSame(): void
36+
{
37+
$this->assertInputValueSame('exampleInput', 'Expected Value', 'The input value should be "Expected Value".');
38+
}
39+
40+
public function testAssertPageTitleContains(): void
41+
{
42+
$this->assertPageTitleContains('Test', 'The page title should contain "Test".');
43+
}
44+
45+
public function testAssertPageTitleSame(): void
46+
{
47+
$this->assertPageTitleSame('Test Page', 'The page title should be "Test Page".');
48+
}
49+
50+
public function testAssertSelectorExists(): void
51+
{
52+
$this->assertSelectorExists('h1', 'The <h1> element should be present.');
53+
}
54+
55+
public function testAssertSelectorNotExists(): void
56+
{
57+
$this->assertSelectorNotExists('.non-existent-class', 'This selector should not exist.');
58+
}
59+
60+
public function testAssertSelectorTextContains(): void
61+
{
62+
$this->assertSelectorTextContains('h1', 'Test', 'The <h1> tag should contain "Test".');
63+
}
64+
65+
public function testAssertSelectorTextNotContains(): void
66+
{
67+
$this->assertSelectorTextNotContains('h1', 'Error', 'The <h1> tag should not contain "Error".');
68+
}
69+
70+
public function testAssertSelectorTextSame(): void
71+
{
72+
$this->assertSelectorTextSame('h1', 'Test Page', 'The text in the <h1> tag should be exactly "Test Page".');
73+
}
74+
}

tests/EventsAssertionsTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Codeception\Module\Symfony\EventsAssertionsTrait;
8+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
9+
use Symfony\Component\HttpKernel\Kernel;
10+
use Tests\App\Event\TestEvent;
11+
use Tests\App\Listener\TestEventListener;
12+
use Tests\Support\CodeceptTestCase;
13+
14+
final class EventsAssertionsTest extends CodeceptTestCase
15+
{
16+
use EventsAssertionsTrait;
17+
18+
public function testDontSeeEvent(): void
19+
{
20+
$this->client->request('GET', '/dispatch-orphan-event');
21+
$this->dontSeeEvent(TestEvent::class);
22+
}
23+
24+
public function testDontSeeEventListenerIsCalled(): void
25+
{
26+
$this->client->request('GET', '/dispatch-orphan-event');
27+
$this->dontSeeEventListenerIsCalled(TestEventListener::class);
28+
}
29+
30+
#[IgnoreDeprecations]
31+
public function testDontSeeEventTriggered(): void
32+
{
33+
$this->client->request('GET', '/dispatch-orphan-event');
34+
$this->dontSeeEventTriggered(TestEventListener::class);
35+
}
36+
37+
public function testDontSeeOrphanEvent(): void
38+
{
39+
if (Kernel::VERSION_ID < 60000) {
40+
$this->markTestSkipped('Orphan event detection requires Symfony 6.0+');
41+
}
42+
$this->client->request('GET', '/dispatch-event');
43+
$this->dontSeeOrphanEvent();
44+
}
45+
46+
public function testSeeEvent(): void
47+
{
48+
$this->client->request('GET', '/dispatch-event');
49+
$this->seeEvent(TestEvent::class);
50+
}
51+
52+
public function testSeeEventListenerIsCalled(): void
53+
{
54+
$this->client->request('GET', '/dispatch-event');
55+
$this->seeEventListenerIsCalled(TestEventListener::class, TestEvent::class);
56+
57+
$this->client->request('GET', '/dispatch-named-event');
58+
$this->seeEventListenerIsCalled(TestEventListener::class, 'named.event');
59+
}
60+
61+
#[IgnoreDeprecations]
62+
public function testSeeEventTriggered(): void
63+
{
64+
$this->client->request('GET', '/dispatch-event');
65+
$this->seeEventTriggered(TestEventListener::class);
66+
}
67+
68+
public function testSeeOrphanEvent(): void
69+
{
70+
$this->client->request('GET', '/dispatch-orphan-event');
71+
$this->seeOrphanEvent('orphan.event');
72+
}
73+
}

0 commit comments

Comments
 (0)