Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
/vendor/
/composer.lock
/framework-tests
/.php-cs-fixer.cache
/.php-cs-fixer.cache
tests/_output/
tests/_support/_generated/
tests/_support/FunctionalTester.php
var/

7 changes: 7 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace: Tests
actor_suffix: Tester
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
"require": {
"php": "^8.2",
"ext-json": "*",
"codeception/codeception": "^5.3",
"codeception/lib-innerbrowser": "^3.1 | ^4.0"
},
"require-dev": {
"codeception/codeception": "^5.3",
"codeception/module-asserts": "^3.0",
"codeception/module-doctrine": "^3.1",
"doctrine/orm": "^3.5",
"friendsofphp/php-cs-fixer": "^3.85",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.5",
"symfony/browser-kit": "^5.4 | ^6.4 | ^7.3",
"symfony/cache": "^5.4 | ^6.4 | ^7.3",
"symfony/config": "^5.4 | ^6.4 | ^7.3",
Expand Down Expand Up @@ -72,6 +73,17 @@
"Codeception\\": "src/Codeception/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": [
"tests/_app/TestKernel.php",
"tests/_app/HelloCommand.php",
"tests/_app/TestUser.php",
"tests/_app/ValidEntity.php"
]
},
"config": {
"sort-packages": true
},
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Codeception Module Symfony

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

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

To use the connector without Codeception, require the package and instantiate
`Codeception\\Lib\\Connector\\Symfony` with your kernel.

## Documentation

See [the module documentation](https://codeception.com/docs/modules/Symfony).
Expand Down
6 changes: 4 additions & 2 deletions src/Codeception/Lib/Connector/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profiler;

use function codecept_debug;
use function function_exists;

/**
* @property KernelInterface $kernel
Expand Down Expand Up @@ -73,7 +73,9 @@ public function rebootKernel(): void
try {
$this->container->set($name, $service);
} catch (InvalidArgumentException $e) {
codecept_debug("[Symfony] Can't set persistent service {$name}: {$e->getMessage()}");
if (function_exists('codecept_debug')) {
codecept_debug("[Symfony] Can't set persistent service {$name}: {$e->getMessage()}");
}
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/Codeception/Module/Symfony/BrowserAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public function rebootClientKernel(): void
public function seePageIsAvailable(?string $url = null): void
{
if ($url !== null) {
$this->amOnPage($url);
$this->seeInCurrentUrl($url);
$this->getClient()->request('GET', $url);
$this->assertStringContainsString($url, $this->getClient()->getRequest()->getRequestUri());
}

$this->assertResponseIsSuccessful();
Expand All @@ -328,12 +328,12 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
{
$client = $this->getClient();
$client->followRedirects(false);
$this->amOnPage($page);
$client->request('GET', $page);

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

$client->followRedirect();
$this->seeInCurrentUrl($redirectsTo);
$this->assertStringContainsString($redirectsTo, $client->getRequest()->getRequestUri());
}

/**
Expand Down Expand Up @@ -363,9 +363,16 @@ public function submitSymfonyForm(string $name, array $fields): void
$params[$fixedKey] = $value;
}

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

$this->submitForm($selector, $params, $button);
$node = $this->getClient()->getCrawler()->filter($selector);
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $selector));
$form = $node->form();
$this->getClient()->submit($form, $params);
}

protected function assertThatForClient(Constraint $constraint, string $message = ''): void
Expand Down
5 changes: 2 additions & 3 deletions src/Codeception/Module/Symfony/RouterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ private function getCurrentRouteMatch(string $routeName): array
{
$this->assertRouteExists($routeName);

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

/** @var array<string, mixed> $match */
Expand Down Expand Up @@ -143,7 +142,7 @@ private function assertRouteExists(string $routeName): void
/** @param array<string, mixed> $params */
private function openRoute(string $routeName, array $params = []): void
{
$this->amOnPage($this->grabRouterService()->generate($routeName, $params));
$this->getClient()->request('GET', $this->grabRouterService()->generate($routeName, $params));
}

protected function grabRouterService(): RouterInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Module/Symfony/SessionAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function dontSeeInSession(string $attribute, mixed $value = null): void
*/
public function goToLogoutPath(): void
{
$this->amOnPage($this->getLogoutUrlGenerator()->getLogoutPath());
$this->getClient()->request('GET', $this->getLogoutUrlGenerator()->getLogoutPath());
}

/**
Expand Down
69 changes: 69 additions & 0 deletions tests/BrowserAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Tests;

use Codeception\Module\Symfony\BrowserAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\BrowserKit\Cookie;

class BrowserAssertionsTest extends KernelTestCase
{
use BrowserAssertionsTrait;

private KernelBrowser $client;

protected function setUp(): void
{
self::bootKernel();
$this->client = new KernelBrowser(self::$kernel);
$this->client->getCookieJar()->set(new Cookie('browser_cookie', 'value'));
}

protected function tearDown(): void
{
parent::tearDown();
restore_exception_handler();
}

protected function getClient(): KernelBrowser
{
return $this->client;
}

protected static function getKernelClass(): string
{
return \TestKernel::class;
}

public function testBrowserAssertions(): void
{
$this->client->request('GET', '/sample');

$this->assertBrowserHasCookie('browser_cookie');
$this->assertBrowserCookieValueSame('browser_cookie', 'value');
$this->assertBrowserNotHasCookie('missing_cookie');

$this->assertRequestAttributeValueSame('foo', 'bar');

$this->assertResponseHasCookie('response_cookie');
$this->assertResponseCookieValueSame('response_cookie', 'yum');
$this->assertResponseNotHasCookie('other_cookie');

$this->assertResponseHasHeader('X-Test');
$this->assertResponseHeaderSame('X-Test', '1');
$this->assertResponseHeaderNotSame('X-Test', '2');
$this->assertResponseNotHasHeader('X-None');

$this->assertResponseFormatSame('html');
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(200);
$this->assertRouteSame('sample');

$this->seePageIsAvailable('/sample');
$this->seePageRedirectsTo('/redirect', '/sample');

$this->client->request('GET', '/unprocessable');
$this->assertResponseIsUnprocessable();
}
}
26 changes: 26 additions & 0 deletions tests/BrowserKitConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests;

use Codeception\Lib\Connector\Symfony as SymfonyConnector;
use PHPUnit\Framework\TestCase;
class BrowserKitConnectorTest extends TestCase
{
public function testRequestReturnsSuccessfulResponse(): void
{
$kernel = new \TestKernel('test', true);
$kernel->boot();
$browser = new SymfonyConnector($kernel);

$browser->request('GET', '/');

$this->assertSame(200, $browser->getResponse()->getStatusCode());
$this->assertSame('OK', $browser->getResponse()->getContent());
}

protected function tearDown(): void
{
restore_exception_handler();
parent::tearDown();
}
}
38 changes: 38 additions & 0 deletions tests/ConsoleAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tests;

use Codeception\Module\Symfony\ConsoleAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ConsoleAssertionsTest extends KernelTestCase
{
use ConsoleAssertionsTrait;

protected static function getKernelClass(): string
{
return \TestKernel::class;
}

protected function grabService(string $serviceId): object
{
return self::getContainer()->get($serviceId);
}

protected function unpersistService(string $serviceName): void
{
// no-op for tests
}

public function testRunSymfonyConsoleCommand(): void
{
$output = $this->runSymfonyConsoleCommand('app:hello', ['name' => 'Codeception']);
$this->assertStringContainsString('Hello Codeception', $output);
}

protected function tearDown(): void
{
restore_exception_handler();
parent::tearDown();
}
}
52 changes: 52 additions & 0 deletions tests/DomCrawlerAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests;

use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class DomCrawlerAssertionsTest extends KernelTestCase
{
use DomCrawlerAssertionsTrait;

private KernelBrowser $client;

protected function setUp(): void
{
self::bootKernel();
$this->client = new KernelBrowser(self::$kernel);
$this->client->request('GET', '/sample');
}

protected function tearDown(): void
{
parent::tearDown();
restore_exception_handler();
}

protected function getClient(): KernelBrowser
{
return $this->client;
}

protected static function getKernelClass(): string
{
return \TestKernel::class;
}

public function testDomCrawlerAssertions(): void
{
$this->assertCheckboxChecked('agree');
$this->assertCheckboxNotChecked('subscribe');
$this->assertInputValueSame('username', 'john');
$this->assertInputValueNotSame('username', 'doe');
$this->assertPageTitleContains('Test');
$this->assertPageTitleSame('Test Page');
$this->assertSelectorExists('#greeting');
$this->assertSelectorNotExists('#missing');
$this->assertSelectorTextContains('#greeting', 'Hello');
$this->assertSelectorTextNotContains('#greeting', 'Bye');
$this->assertSelectorTextSame('#greeting', 'Hello World');
}
}
43 changes: 43 additions & 0 deletions tests/FormAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests;

use Codeception\Module\Symfony\FormAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class FormAssertionsTest extends KernelTestCase
{
use FormAssertionsTrait;

private KernelBrowser $client;

protected function setUp(): void
{
self::bootKernel();
$this->client = new KernelBrowser(self::$kernel);
$this->client->request('GET', '/sample');
}

protected function tearDown(): void
{
parent::tearDown();
restore_exception_handler();
}

protected function getClient(): KernelBrowser
{
return $this->client;
}

protected static function getKernelClass(): string
{
return \TestKernel::class;
}

public function testFormAssertions(): void
{
$this->assertFormValue('#testForm', 'field1', 'value1');
$this->assertNoFormValue('#testForm', 'missing_field');
}
}
Loading
Loading