Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Tests & Various Fixes for Symfony 6 #1010

Merged
merged 1 commit into from
Nov 16, 2021
Merged
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
39 changes: 18 additions & 21 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
run: "tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff"

test:
name: "PHP ${{ matrix.php-version }} + symfony/skeleton@${{ matrix.symfony-skeleton-stability }}"
name: "PHP ${{ matrix.php-version }} + @${{ matrix.symfony-version }} ${{ matrix.dependency-versions }} deps"

runs-on: ubuntu-18.04

Expand All @@ -82,28 +82,30 @@ jobs:
ports:
- 1337:1337

continue-on-error: ${{ matrix.allow-failures }}

env:
SYMFONY_SKELETON_STABILITY: ${{ matrix.symfony-skeleton-stability }}
SYMFONY_VERSION: ${{ matrix.symfony-version }}

strategy:
fail-fast: false
matrix:
php-version:
- '7.1.3'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
symfony-skeleton-stability:
- 'stable'
composer-options: ['--no-suggest']
allow-failures: [false]
symfony-version:
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
- '4.4.*'
- '5.3.*'
- '5.4.x-dev'
- '6.0.x-dev'
dependency-versions: ['highest']
include:
- php-version: '8.0'
symfony-skeleton-stability: 'dev'
allow-failures: true
# testing lowest PHP version with LTS
- php-version: '7.1.3'
symfony-version: '4.4.*'
# not important - does not affect each app's dependencies
dependency-versions: 'highest'
# testing lowest php version with highest 5.x stable
- php-version: '7.2.5'
symfony-version: '5.3.*'
dependency-versions: 'lowest'

steps:
- name: "Checkout code"
Expand Down Expand Up @@ -132,17 +134,12 @@ jobs:
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: "Require symfony/flex"
if: contains('stable', env.SYMFONY_SKELETON_STABILITY)
run: composer global require --no-progress --no-scripts --no-plugins symfony/flex

- name: "Require symfony/flex dev-main"
if: contains('dev', env.SYMFONY_SKELETON_STABILITY)
run: composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-main

- name: "Composer install"
uses: "ramsey/composer-install@v1"
with:
composer-options: "${{ matrix.composer-options }}"
dependency-versions: "${{ matrix.dependency-versions }}"

- name: "Composer install php-cs-fixer"
uses: "ramsey/composer-install@v1"
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
->exclude('fixtures')
// the PHP template files are a bit special
->notName('*.tpl.php')
->notPath('src/Console/MigrationDiffFilteredOutput_php8.php')
;

return (new PhpCsFixer\Config())
Expand Down
30 changes: 16 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@
"php": ">=7.1.3",
"doctrine/inflector": "^1.2|^2.0",
"nikic/php-parser": "^4.11",
"symfony/config": "^4.0|^5.0|^6.0",
"symfony/console": "^4.0|^5.0|^6.0",
"symfony/dependency-injection": "^4.0|^5.0|^6.0",
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
"symfony/deprecation-contracts": "^2.2",
"symfony/filesystem": "^4.0|^5.0|^6.0",
"symfony/finder": "^4.0|^5.0|^6.0",
"symfony/framework-bundle": "^4.0|^5.0|^6.0",
"symfony/http-kernel": "^4.0|^5.0|^6.0"
"symfony/filesystem": "^4.4|^5.0|^6.0",
"symfony/finder": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0"
},
"require-dev": {
"composer/semver": "^3.0@dev",
"doctrine/doctrine-bundle": "^1.8|^2.0",
"composer/semver": "^3.0",
"doctrine/doctrine-bundle": "^1.12.3|^2.0",
"doctrine/orm": "^2.3",
"symfony/http-client": "^4.3|^5.0|^6.0",
"symfony/phpunit-bridge": "^4.3|^5.0|^6.0",
"symfony/process": "^4.0|^5.0|^6.0",
"symfony/security-core": "^4.0|^5.0|^6.0",
"symfony/yaml": "^4.0|^5.0|^6.0"
"symfony/http-client": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^4.4|^5.0|^6.0",
"symfony/polyfill-php80": "^1.16.0",
"symfony/process": "^4.4|^5.0|^6.0",
"symfony/security-core": "^4.4|^5.0|^6.0",
"symfony/yaml": "^4.4|^5.0|^6.0",
"twig/twig": "^2.0|^3.0"
},
"config": {
"preferred-install": "dist",
Expand Down
61 changes: 41 additions & 20 deletions src/Console/MigrationDiffFilteredOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,38 @@
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MigrationDiffFilteredOutput implements OutputInterface
if (\PHP_VERSION_ID < 80000
// look for the "string|iterable" type on OutputInterface::write()
|| !(new \ReflectionMethod(OutputInterface::class, 'write'))->getParameters()[0]->getType()) {
class MigrationDiffFilteredOutput implements OutputInterface
{
use BaseMakerMigrationDiffFilteredOuputTrait;

public function write($messages, $newline = false, $options = 0)
{
$this->_write($messages, $newline, $options);
}

public function writeln($messages, $options = 0)
{
$this->_writeln($messages, $options);
}

public function setVerbosity($level)
{
$this->output->setVerbosity($level);
}

public function setDecorated($decorated)
{
$this->output->setDecorated($decorated);
}
}
} else {
require __DIR__.'/MigrationDiffFilteredOutput_php8.php';
}

trait BaseMakerMigrationDiffFilteredOuputTrait
{
private $output;
private $buffer = '';
Expand All @@ -25,56 +56,46 @@ public function __construct(OutputInterface $output)
$this->output = $output;
}

public function write($messages, $newline = false, $options = 0)
public function _write($messages, bool $newline = false, $options = 0)
{
$messages = $this->filterMessages($messages, $newline);

$this->output->write($messages, $newline, $options);
}

public function writeln($messages, $options = 0)
public function _writeln($messages, int $options = 0)
{
$messages = $this->filterMessages($messages, true);

$this->output->writeln($messages, $options);
}

public function setVerbosity($level)
{
$this->output->setVerbosity($level);
}

public function getVerbosity()
public function getVerbosity(): int
{
return $this->output->getVerbosity();
}

public function isQuiet()
public function isQuiet(): bool
{
return $this->output->isQuiet();
}

public function isVerbose()
public function isVerbose(): bool
{
return $this->output->isVerbose();
}

public function isVeryVerbose()
public function isVeryVerbose(): bool
{
return $this->output->isVeryVerbose();
}

public function isDebug()
public function isDebug(): bool
{
return $this->output->isDebug();
}

public function setDecorated($decorated)
{
$this->output->setDecorated($decorated);
}

public function isDecorated()
public function isDecorated(): bool
{
return $this->output->isDecorated();
}
Expand All @@ -84,7 +105,7 @@ public function setFormatter(OutputFormatterInterface $formatter)
$this->output->setFormatter($formatter);
}

public function getFormatter()
public function getFormatter(): OutputFormatterInterface
{
return $this->output->getFormatter();
}
Expand Down
39 changes: 39 additions & 0 deletions src/Console/MigrationDiffFilteredOutput_php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Console;

use Symfony\Component\Console\Output\OutputInterface;

class MigrationDiffFilteredOutput implements OutputInterface
{
use BaseMakerMigrationDiffFilteredOuputTrait;

public function write($messages, bool $newline = false, $options = 0)
{
$this->_write($messages, $newline, $options);
}

public function writeln($messages, int $options = 0)
{
$this->_writeln($messages, $options);
}

public function setVerbosity(int $level)
{
$this->output->setVerbosity($level);
}

public function setDecorated(bool $decorated)
{
$this->output->setDecorated($decorated);
}
}
27 changes: 26 additions & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
use Symfony\Component\Form\Form;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -264,7 +266,10 @@ private function generateAuthenticatorClass(array $securityData, string $authent
$this->generator->generateClass(
$authenticatorClass,
sprintf('authenticator/%sEmptyAuthenticator.tpl.php', $this->useSecurity52 ? 'Security52' : ''),
['provider_key_type_hint' => $this->providerKeyTypeHint()]
[
'provider_key_type_hint' => $this->providerKeyTypeHint(),
'use_legacy_passport_interface' => $this->shouldUseLegacyPassportInterface(),
]
);

return;
Expand All @@ -287,6 +292,7 @@ private function generateAuthenticatorClass(array $securityData, string $authent
'user_needs_encoder' => $this->userClassHasEncoder($securityData, $userClass),
'user_is_entity' => $this->doctrineHelper->isClassAMappedEntity($userClass),
'provider_key_type_hint' => $this->providerKeyTypeHint(),
'use_legacy_passport_interface' => $this->shouldUseLegacyPassportInterface(),
]
);
}
Expand Down Expand Up @@ -411,4 +417,23 @@ private function providerKeyTypeHint(): string

return sprintf('%s ', $type->getName());
}

private function shouldUseLegacyPassportInterface(): bool
{
// only applies to new authenticator security
if (!$this->useSecurity52) {
return false;
}

// legacy: checking for Symfony 5.2 & 5.3 before PassportInterface deprecation
$class = new \ReflectionClass(AuthenticatorInterface::class);
$method = $class->getMethod('authenticate');

// 5.4 where return type is temporarily removed
if (!$method->getReturnType()) {
return false;
}

return PassportInterface::class === $method->getReturnType()->getName();
}
}
16 changes: 15 additions & 1 deletion src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
Expand Down Expand Up @@ -47,6 +48,7 @@
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use Symfony\Component\Validator\Validation;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\Model\VerifyEmailSignatureComponents;
Expand Down Expand Up @@ -78,6 +80,7 @@ final class MakeRegistrationForm extends AbstractMaker
private $firewallName;
private $redirectRouteName;
private $addUniqueEntityConstraint;
private $useNewAuthenticatorSystem = false;

public function __construct(FileManager $fileManager, FormTypeRenderer $formTypeRenderer, RouterInterface $router, DoctrineHelper $doctrineHelper)
{
Expand Down Expand Up @@ -116,6 +119,11 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$securityData = $manipulator->getData();
$providersData = $securityData['security']['providers'] ?? [];

// Determine if we should use new security features introduced in Symfony 5.2
if ($securityData['security']['enable_authenticator_manager'] ?? false) {
$this->useNewAuthenticatorSystem = true;
}

$this->userClass = $interactiveSecurityHelper->guessUserClass(
$io,
$providersData,
Expand Down Expand Up @@ -292,6 +300,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
Response::class,
Route::class,
$passwordHasher,
EntityManagerInterface::class,
];

if ($this->willVerifyEmail) {
Expand All @@ -307,7 +316,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

if ($this->autoLoginAuthenticator) {
$useStatements[] = $this->autoLoginAuthenticator;
$useStatements[] = GuardAuthenticatorHandler::class;
if ($this->useNewAuthenticatorSystem) {
$useStatements[] = UserAuthenticatorInterface::class;
} else {
$useStatements[] = GuardAuthenticatorHandler::class;
}
}

$generator->generateController(
Expand All @@ -328,6 +341,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'email_getter' => $this->emailGetter,
'authenticator_class_name' => $this->autoLoginAuthenticator ? Str::getShortClassName($this->autoLoginAuthenticator) : null,
'authenticator_full_class_name' => $this->autoLoginAuthenticator,
'use_new_authenticator_system' => $this->useNewAuthenticatorSystem,
'firewall_name' => $this->firewallName,
'redirect_route_name' => $this->redirectRouteName,
'password_hasher_class_details' => ($passwordClassDetails = $generator->createClassNameDetails($passwordHasher, '\\')),
Expand Down
Loading