Skip to content

Commit

Permalink
[feature] add Symfony 6 support (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Nov 19, 2021
1 parent a4b8389 commit b153787
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 57 deletions.
21 changes: 3 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,9 @@ on:

jobs:
tests:
name: PHP ${{ matrix.php }}, SF ${{ matrix.symfony }} - ${{ matrix.deps }}
runs-on: ubuntu-latest
strategy:
matrix:
php: [7.4, 8.0]
deps: [hightest]
symfony: [4.4.*, 5.3.*, 5.4.*]
include:
- php: 7.4
deps: lowest
symfony: '*'
steps:
- uses: zenstruck/.github@php-test-symfony
with:
php: ${{ matrix.php }}
symfony: ${{ matrix.symfony }}
deps: ${{ matrix.deps }}
phpunit: simple-phpunit
uses: zenstruck/.github/.github/workflows/php-test-symfony.yml@main
with:
phpunit: simple-phpunit

code-coverage:
uses: zenstruck/.github/.github/workflows/php-coverage-codecov.yml@main
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"php": ">=7.4",
"behat/mink": "^1.8",
"mtdowling/jmespath.php": "^2.6",
"symfony/browser-kit": "^4.4.9|^5.0.9",
"symfony/css-selector": "^4.4|^5.0",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/browser-kit": "^4.4.9|^5.0.9|^6.0",
"symfony/css-selector": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/polyfill-php80": "^1.20",
"zenstruck/assert": "^1.0",
"zenstruck/callback": "^1.1"
Expand All @@ -26,8 +26,8 @@
"dbrekelmans/bdi": "^0.3.0",
"symfony/mime": ">=4.4.1",
"symfony/panther": "^1.0",
"symfony/phpunit-bridge": "^5.2",
"symfony/security-bundle": "^4.4|^5.0"
"symfony/phpunit-bridge": "^5.3",
"symfony/security-bundle": "^4.4|^5.0|^6.0"
},
"config": {
"preferred-install": "dist",
Expand Down
12 changes: 8 additions & 4 deletions src/Browser/Test/HasBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ protected function httpBrowser(array $kernelOptions = [], array $pantherOptions
static::bootKernel($kernelOptions);
}

if (static::$container->has('profiler')) {
$browser->setProfiler(static::$container->get('profiler'));
$container = \method_exists(static::class, 'getContainer') ? static::getContainer() : static::$container;

if ($container->has('profiler')) {
$browser->setProfiler($container->get('profiler'));
}
}

Expand Down Expand Up @@ -133,11 +135,13 @@ protected function browser(array $kernelOptions = [], array $server = []): Kerne
// reboot kernel before starting browser
static::bootKernel($kernelOptions);

if (!static::$container->has('test.client')) {
$container = \method_exists(static::class, 'getContainer') ? static::getContainer() : static::$container;

if (!$container->has('test.client')) {
throw new \RuntimeException('The Symfony test client is not enabled.');
}

$client = static::$container->get('test.client');
$client = $container->get('test.client');
$client->setServerParameters($server);

$browser = new $class($client);
Expand Down
83 changes: 60 additions & 23 deletions tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\UserInterface;

/**
Expand Down Expand Up @@ -126,7 +127,15 @@ public function user(?UserInterface $user = null): Response
public function registerBundles(): iterable
{
yield new FrameworkBundle();
yield new SecurityBundle();

if (self::securityEnabled()) {
yield new SecurityBundle();
}
}

public static function securityEnabled(): bool
{
return self::VERSION_ID >= 50300;
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
Expand All @@ -136,30 +145,58 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'router' => ['utf8' => true],
'test' => true,
'profiler' => ['enabled' => true, 'collect' => true],
'session' => ['storage_id' => 'session.storage.mock_file'],
]);
$c->loadFromExtension('security', [
'encoders' => [User::class => 'plaintext'],
'providers' => ['users' => ['memory' => ['users' => ['kevin' => ['password' => 'pass']]]]],
'firewalls' => ['main' => ['anonymous' => true]],
]);
$c->register('logger', NullLogger::class); // disable logging

if (self::securityEnabled()) {
$c->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'password_hashers' => [InMemoryUser::class => 'plaintext'],
'providers' => ['users' => ['memory' => ['users' => ['kevin' => ['password' => 'pass']]]]],
'firewalls' => ['main' => []],
]);

$c->loadFromExtension('framework', [
'session' => ['storage_factory_id' => 'session.storage.factory.mock_file'],
]);
}
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
{
$routes->add('/page1', 'kernel::page1');
$routes->add('/page2', 'kernel::page2');
$routes->add('/text', 'kernel::text');
$routes->add('/submit-form', 'kernel::submitForm');
$routes->add('/http-method', 'kernel::httpMethod');
$routes->add('/exception', 'kernel::exception');
$routes->add('/redirect1', 'kernel::redirect1');
$routes->add('/redirect2', 'kernel::redirect2');
$routes->add('/redirect3', 'kernel::redirect3');
$routes->add('/json', 'kernel::json');
$routes->add('/xml', 'kernel::xml');
$routes->add('/javascript', 'kernel::javascript');
$routes->add('/user', 'kernel::user');
/**
* @param RouteCollectionBuilder|RoutingConfigurator $routes
*/
protected function configureRoutes($routes): void
{
if ($routes instanceof RouteCollectionBuilder) {
$routes->add('/page1', 'kernel::page1');
$routes->add('/page2', 'kernel::page2');
$routes->add('/text', 'kernel::text');
$routes->add('/submit-form', 'kernel::submitForm');
$routes->add('/http-method', 'kernel::httpMethod');
$routes->add('/exception', 'kernel::exception');
$routes->add('/redirect1', 'kernel::redirect1');
$routes->add('/redirect2', 'kernel::redirect2');
$routes->add('/redirect3', 'kernel::redirect3');
$routes->add('/json', 'kernel::json');
$routes->add('/xml', 'kernel::xml');
$routes->add('/javascript', 'kernel::javascript');
$routes->add('/user', 'kernel::user');

return;
}

$routes->add('page1', '/page1')->controller('kernel::page1');
$routes->add('page2', '/page2')->controller('kernel::page2');
$routes->add('text', '/text')->controller('kernel::text');
$routes->add('submit-form', '/submit-form')->controller('kernel::submitForm');
$routes->add('http-method', '/http-method')->controller('kernel::httpMethod');
$routes->add('exception', '/exception')->controller('kernel::exception');
$routes->add('redirect1', '/redirect1')->controller('kernel::redirect1');
$routes->add('redirect2', '/redirect2')->controller('kernel::redirect2');
$routes->add('redirect3', '/redirect3')->controller('kernel::redirect3');
$routes->add('json', '/json')->controller('kernel::json');
$routes->add('xml', '/xml')->controller('kernel::xml');
$routes->add('javascript', '/javascript')->controller('kernel::javascript');
$routes->add('user', '/user')->controller('kernel::user');
}
}
11 changes: 4 additions & 7 deletions tests/KernelBrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Zenstruck\Browser\Tests;

use Symfony\Bundle\FrameworkBundle\KernelBrowser as SymfonyKernelBrowser;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\User;
use Zenstruck\Browser\KernelBrowser;
use Zenstruck\Browser\Tests\Fixture\Kernel;

/**
* @author Kevin Bond <[email protected]>
Expand All @@ -32,15 +31,13 @@ public function can_use_kernel_browser_as_typehint(): void
*/
public function can_act_as_user(): void
{
if (!\method_exists(SymfonyKernelBrowser::class, 'loginUser')) {
$this->markTestSkipped(SymfonyKernelBrowser::class.'::loginUser() is only available in Symfony 5.1+.');
if (!Kernel::securityEnabled()) {
$this->markTestSkipped('Only enable security-related tests in 5.3+');
}

$userClass = \class_exists(InMemoryUser::class) ? InMemoryUser::class : User::class;

$this->browser()
->throwExceptions()
->actingAs(new $userClass('kevin', 'pass'))
->actingAs(new InMemoryUser('kevin', 'pass'))
->visit('/user')
->assertSee('user: kevin/pass')
;
Expand Down

0 comments on commit b153787

Please sign in to comment.