Skip to content

Commit

Permalink
Allow Symfony 7.x and Doctrine ORM 3.x (#58)
Browse files Browse the repository at this point in the history
* Allow Symfony 7.x and Doctrine ORM 3.x

* Fixed missing doctrine-version var in testing matrix

* Fixed PHP version range for Symfony 7.x

* Simplify testing matrix

* Fixed phpstan error ignore config structure

* Fixed remove ClassUtils

* Removed phpspec/prophecy-phpunit

* Fixed static analysis errors in DoctrineUserManager

* Fixed EntityManager::flush argument was removed

* Use ClassUtils again but declare dependency to doctrine/common
  • Loading branch information
yann-eugone authored Jun 3, 2024
1 parent 5f85ac1 commit 105deb5
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 303 deletions.
4 changes: 4 additions & 0 deletions .github/actions/install/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
symfony-version:
required: true
type: string
doctrine-version:
required: true
type: string
coverage-mode:
required: false
type: string
Expand All @@ -29,4 +32,5 @@ runs:
shell: bash
run: |
composer require --no-update "symfony/framework-bundle:${{ inputs.symfony-version }}"
composer require --no-update "doctrine/orm:${{ inputs.doctrine-version }}"
composer update --no-interaction --no-progress --no-suggest
28 changes: 19 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ jobs:
include:
- php-version: '7.4'
symfony-version: '4.4.*'
- php-version: '8.2'
symfony-version: '4.4.*'
doctrine-version: '^2.7'
- php-version: '7.4'
symfony-version: '5.4.*'
- php-version: '8.2'
symfony-version: '5.4.*'
doctrine-version: '^2.7'
- php-version: '8.1'
symfony-version: '6.3.*'
symfony-version: '6.4.*'
doctrine-version: '^2.7'
- php-version: '8.2'
symfony-version: '6.4.*'
doctrine-version: '^3.0'
- php-version: '8.2'
symfony-version: '6.3.*'
symfony-version: '^7.0'
doctrine-version: '^2.7'
- php-version: '8.3'
symfony-version: '^7.0'
doctrine-version: '^3.0'
steps:
- name: "Checkout"
uses: actions/checkout@v2
Expand All @@ -33,6 +39,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
symfony-version: ${{ matrix.symfony-version }}
doctrine-version: ${{ matrix.doctrine-version }}
- name: "Run tests with phpunit/phpunit"
run: vendor/bin/phpunit

Expand All @@ -46,7 +53,8 @@ jobs:
uses: ./.github/actions/install
with:
php-version: '8.1'
symfony-version: '6.1.*'
symfony-version: '6.4.*'
doctrine-version: '^3.0'
- name: "Run static analyzis with phpstan/phpstan"
run: vendor/bin/phpstan analyze

Expand All @@ -60,7 +68,8 @@ jobs:
uses: ./.github/actions/install
with:
php-version: '8.1'
symfony-version: '6.1.*'
symfony-version: '6.4.*'
doctrine-version: '^3.0'
- name: "Run checkstyle with symplify/easy-coding-standard"
run: vendor/bin/ecs

Expand All @@ -74,7 +83,8 @@ jobs:
uses: ./.github/actions/install
with:
php-version: '8.1'
symfony-version: '6.1.*'
symfony-version: '6.4.*'
doctrine-version: '^3.0'
coverage-mode: 'xdebug'
- name: "Run tests with phpunit/phpunit"
env:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"require": {
"php": "^7.4|^8.0",
"ext-openssl": "*",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"doctrine/orm": "^2.7",
"symfony/framework-bundle": "^4.4|^5.0|^6.0|^7.0",
"doctrine/common": "^3.0",
"doctrine/orm": "^2.7|^3.0",
"doctrine/doctrine-bundle": "^2.0",
"yokai/dependency-injection": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpspec/prophecy-phpunit": "^2.0",
"symfony/yaml": "^4.4|^5.2|^6.0",
"symfony/yaml": "^4.4|^5.2|^6.0|^7.0",
"phpstan/phpstan": "^1.7",
"symplify/easy-coding-standard": "^11.3"
},
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ parameters:
count: 1
path: src/Manager/DoctrineUserManager.php

-
message: "#^Method Doctrine\\\\ORM\\\\EntityManager\\:\\:flush\\(\\) invoked with 1 parameter, 0 required\\.$#"
count: 1
path: src/Repository/DoctrineORMTokenRepository.php

5 changes: 3 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ includes:
- phpstan-baseline.neon
parameters:
level: max
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- identifier: missingType.generics
- identifier: missingType.iterableValue
paths:
- src/
4 changes: 1 addition & 3 deletions src/Manager/DoctrineUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ public function get(string $class, string $id)
public function getClass($user): string
{
/** @var object $user */
/** @var class-string $class */
$class = ClassUtils::getClass($user);

return $class;
return ClassUtils::getClass($user);
}

public function getId($user): string
Expand Down
21 changes: 15 additions & 6 deletions src/Repository/DoctrineORMTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function get(string $value, string $purpose): Token
[
'value' => $value,
'purpose' => $purpose,
]
],
);

if (!$token instanceof Token) {
Expand All @@ -67,7 +67,7 @@ public function findExisting(string $userClass, string $userId, string $purpose)
'userClass' => $userClass,
'userId' => $userId,
'purpose' => $purpose,
]
],
);
if (!$token instanceof Token) {
return null;
Expand All @@ -81,14 +81,12 @@ public function findExisting(string $userClass, string $userId, string $purpose)

public function create(Token $token): void
{
$this->manager->persist($token);
$this->manager->flush($token);
$this->save($token);
}

public function update(Token $token): void
{
$this->manager->persist($token);
$this->manager->flush($token);
$this->save($token);
}

public function exists(string $value, string $purpose): bool
Expand All @@ -107,4 +105,15 @@ public function exists(string $value, string $purpose): bool

return intval($result) > 0;
}

private function save(Token $token): void
{
$this->manager->persist($token);

if ((new \ReflectionMethod($this->manager, 'flush'))->getNumberOfParameters() > 0) {
$this->manager->flush($token);
} else {
$this->manager->flush();
}
}
}
21 changes: 10 additions & 11 deletions tests/Command/ArchiveTokenCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Yokai\SecurityTokenBundle\Tests\Command;

use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
Expand All @@ -19,10 +18,8 @@
*/
class ArchiveTokenCommandTest extends KernelTestCase
{
use ProphecyTrait;

/**
* @var ArchivistInterface|ObjectProphecy
* @var MockObject<ArchivistInterface>
*/
private $archivist;

Expand All @@ -33,10 +30,10 @@ class ArchiveTokenCommandTest extends KernelTestCase

protected function setUp(): void
{
$this->archivist = $this->prophesize(ArchivistInterface::class);
$this->archivist = $this->createMock(ArchivistInterface::class);

self::bootKernel();
self::$kernel->getContainer()->set('yokai_security_token.archivist', $this->archivist->reveal());
self::$kernel->getContainer()->set('yokai_security_token.archivist', $this->archivist);

$this->application = new Application(self::$kernel);
}
Expand Down Expand Up @@ -75,8 +72,9 @@ public function it_archive_every_token_when_run_without_options_with_confirmatio
{
$command = $this->command();

$this->archivist->archive(null)
->shouldBeCalledTimes(1)
$this->archivist->expects(self::once())
->method('archive')
->with(null)
->willReturn(10);

$output = $this->runCommand($command);
Expand All @@ -91,8 +89,9 @@ public function it_archive_partial_tokens_when_run_with_options(): void
{
$command = $this->command();

$this->archivist->archive('init_password')
->shouldBeCalledTimes(1)
$this->archivist->expects(self::once())
->method('archive')
->with('init_password')
->willReturn(10);

$output = $this->runCommand($command, ['purpose' => 'init_password']);
Expand Down
17 changes: 6 additions & 11 deletions tests/DependencyInjection/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Generator;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ProphecySubjectInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -23,7 +21,6 @@
use Yokai\SecurityTokenBundle\Archive\ArchivistInterface;
use Yokai\SecurityTokenBundle\Configuration\TokenConfiguration;
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface;
use Yokai\SecurityTokenBundle\Generator\OpenSslTokenGenerator;
use Yokai\SecurityTokenBundle\Generator\TokenGeneratorInterface;
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
use Yokai\SecurityTokenBundle\Manager\TokenManagerInterface;
Expand All @@ -38,8 +35,6 @@
*/
class DependencyInjectionTest extends TestCase
{
use ProphecyTrait;

/**
* @var ContainerBuilder
*/
Expand All @@ -60,7 +55,7 @@ protected function setUp(): void
$this->container->setParameter('kernel.debug', true);
$this->container->setParameter('kernel.bundles', $bundles);
$this->container->setParameter('kernel.environment', 'test');
$this->container->set('logger', $this->prophesize(LoggerInterface::class)->reveal());
$this->container->set('logger', $this->createMock(LoggerInterface::class));
$this->container->setDefinition('doctrine', new Definition(ManagerRegistry::class));
$this->container->setDefinition('doctrine.orm.default_entity_manager', new Definition(EntityManager::class));
$this->container->setDefinition(
Expand All @@ -82,7 +77,7 @@ protected function setUp(): void
'archivist_mock' => ArchivistInterface::class,
];
foreach ($mocks as $id => $class) {
$service = $this->prophesize($class)->reveal();
$service = $this->createMock($class);
$this->container->setDefinition($id, new Definition(get_class($service)));
}

Expand Down Expand Up @@ -173,12 +168,12 @@ public function configurationProvider(): Generator
'names.' . $format,
[
'security_password_init' => [
'generator' => OpenSslTokenGenerator::class,
'generator' => TokenGeneratorInterface::class,
'duration' => '+2 days',
'usages' => 1,
],
'security_password_reset' => [
'generator' => OpenSslTokenGenerator::class,
'generator' => TokenGeneratorInterface::class,
'duration' => '+2 days',
'usages' => 1,
],
Expand All @@ -190,12 +185,12 @@ public function configurationProvider(): Generator
'full.' . $format,
[
'security_password_init' => [
'generator' => ProphecySubjectInterface::class,
'generator' => TokenGeneratorInterface::class,
'duration' => '+1 month',
'usages' => 2,
],
'security_password_reset' => [
'generator' => ProphecySubjectInterface::class,
'generator' => TokenGeneratorInterface::class,
'duration' => '+2 monthes',
'usages' => 3,
],
Expand Down
Loading

0 comments on commit 105deb5

Please sign in to comment.