Skip to content

Commit 7caaa6e

Browse files
committed
🎨 remove deprecations
1 parent 194abb3 commit 7caaa6e

File tree

6 files changed

+67
-53
lines changed

6 files changed

+67
-53
lines changed

.github/workflows/build.yaml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@ jobs:
2828
tests:
2929
runs-on: ubuntu-20.04
3030
strategy:
31+
fail-fast: false
3132
matrix:
3233
include:
33-
- description: 'Symfony 4.4'
34+
- description: 'Symfony 5.4'
3435
php: '7.4'
35-
symfony: '4.4.*'
36+
symfony: '5.4.*'
3637
composer_option: '--prefer-lowest'
37-
- description: 'Symfony 5.3'
38-
php: '7.4'
39-
symfony: '5.3.*'
40-
- description: 'Symfony 5.4'
38+
- description: 'Symfony 6.0'
4139
php: '8.0'
42-
symfony: '5.4.*@dev'
40+
symfony: '6.0.*'
4341
name: PHP ${{ matrix.php }} tests (${{ matrix.description }})
4442
steps:
4543
- name: Checkout
@@ -63,11 +61,6 @@ jobs:
6361
run: |
6462
composer config minimum-stability dev
6563
composer config prefer-stable true
66-
- name: remove cs-fixer for Symfony 6
67-
if: contains(matrix.symfony, '6.0.*@dev')
68-
run: |
69-
composer remove --dev friendsofphp/php-cs-fixer --no-update
70-
composer remove --dev symfony/swiftmailer-bundle --no-update
7164
- name: update vendors
7265
run: composer update --no-interaction --no-progress --ansi ${{ matrix.composer_option }}
7366
- name: run tests

composer.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717
"require": {
1818
"php": "^7.4 || ^8.0",
1919
"doctrine/data-fixtures": "^1.5",
20-
"symfony/browser-kit": "^4.4 || ^5.0 || ^6.0",
21-
"symfony/console": "^4.4 || ^5.0 || ^6.0",
22-
"symfony/css-selector": "^4.4 || ^5.0 || ^6.0",
23-
"symfony/dependency-injection": "^4.4 || ^5.0 || ^6.0",
24-
"symfony/dom-crawler": "^4.4 || ^5.0 || ^6.0"
20+
"symfony/browser-kit": "^5.4 || ^6.0",
21+
"symfony/console": "^5.4 || ^6.0",
22+
"symfony/css-selector": "^5.4 || ^6.0",
23+
"symfony/dependency-injection": "^5.4 || ^6.0",
24+
"symfony/dom-crawler": "^5.4 || ^6.0"
2525
},
2626
"require-dev": {
2727
"doctrine/lexer": "^1.2",
2828
"doctrine/orm": "^2.8",
29-
"friendsofphp/php-cs-fixer": "^3.0",
3029
"mikey179/vfsstream": "^1.6",
3130
"phpunit/phpunit": "^9.5",
32-
"symfony/doctrine-bridge": "^4.4 || ^5.0",
33-
"symfony/framework-bundle": "^4.4 || ^5.0",
34-
"symfony/mime": "^4.4 || ^5.0",
35-
"symfony/security-core": "^4.4 || ^5.0",
36-
"symfony/swiftmailer-bundle": "^3.5"
31+
"symfony/doctrine-bridge": "^5.4 || ^6.0",
32+
"symfony/framework-bundle": "^5.4 || ^6.0",
33+
"symfony/mime": "^5.4 || ^6.0",
34+
"symfony/process": "^5.4 || ^6.0",
35+
"symfony/security-core": "^5.4 || ^6.0"
3736
},
3837
"config": {
3938
"bin-dir": "bin",

phpstan-baseline.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ parameters:
1010
count: 1
1111
path: src/Test/WebTestCase.php
1212

13+
-
14+
message: "#SwiftmailerBundle#"
15+
count: 2
16+
path: src/Test/WebTestCase.php

src/DependencyInjection/BeelabTestExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
final class BeelabTestExtension extends Extension
99
{
10+
/**
11+
* @param array<int, mixed> $configs
12+
*/
1013
public function load(array $configs, ContainerBuilder $container): void
1114
{
1215
$configuration = new Configuration();

src/Test/WebTestCase.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Beelab\TestBundle\Test;
44

55
use Doctrine\Common\DataFixtures\AbstractFixture;
6-
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
76
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
87
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
98
use Doctrine\ORM\EntityManagerInterface;
@@ -14,10 +13,12 @@
1413
use Symfony\Component\BrowserKit\Cookie;
1514
use Symfony\Component\Console\Command\Command;
1615
use Symfony\Component\Console\Tester\CommandTester;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
1717
use Symfony\Component\DomCrawler\Crawler;
1818
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
1919
use Symfony\Component\DomCrawler\Form;
2020
use Symfony\Component\HttpFoundation\File\UploadedFile;
21+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2122
use Symfony\Component\Process\Process;
2223
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2324
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
@@ -26,14 +27,17 @@ abstract class WebTestCase extends SymfonyWebTestCase
2627
{
2728
protected static ?EntityManagerInterface $em = null;
2829

29-
protected static ?KernelBrowser $client = null;
30+
protected static KernelBrowser $client;
3031

3132
private ?AbstractFixture $fixture = null;
3233

3334
protected static ?string $authUser = null;
3435

3536
protected static ?string $authPw = null;
3637

38+
/** @var ContainerInterface */
39+
protected static $container = null;
40+
3741
protected function setUp(): void
3842
{
3943
$environment = $_SERVER['APP_ENV'] ?? 'test';
@@ -102,15 +106,15 @@ protected static function saveOutput(bool $delete = true): void
102106
*/
103107
protected static function login(string $username = '[email protected]', ?string $firewall = null, ?string $service = null): void
104108
{
105-
$service = $service ?? static::$container->getParameter('beelab_test.user_service');
109+
$service ??= static::$container->getParameter('beelab_test.user_service');
106110
$object = static::$container->get($service);
107111
$user = \is_callable([$object, 'loadUserByIdentifier']) ? $object->loadUserByIdentifier($username) : $object->loadUserByUsername($username);
108112
if (null === $user) {
109113
throw new \InvalidArgumentException(\sprintf('Username %s not found.', $username));
110114
}
111-
$firewall = $firewall ?? static::$container->getParameter('beelab_test.firewall');
112-
$token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
113-
$session = static::$container->get('session');
115+
$firewall ??= static::$container->getParameter('beelab_test.firewall');
116+
$token = new UsernamePasswordToken($user, $firewall, $user->getRoles());
117+
$session = self::getSession();
114118
$session->set('_security_'.$firewall, \serialize($token));
115119
$session->save();
116120
$cookie = new Cookie($session->getName(), $session->getId());
@@ -270,17 +274,12 @@ protected static function commandTest(
270274

271275
/**
272276
* Get an entity by its fixtures reference name.
273-
*
274-
* @return mixed
275277
*/
276-
protected function getReference(string $name)
278+
protected function getReference(string $name): object
277279
{
278280
if (null === $this->fixture) {
279281
throw new \RuntimeException('Load some fixtures before.');
280282
}
281-
if (!$this->fixture instanceof DependentFixtureInterface) {
282-
throw new \RuntimeException('Fixture is not dependent.');
283-
}
284283
if (!$this->fixture->hasReference($name)) {
285284
throw new \InvalidArgumentException(\sprintf('Reference "%s" not found.', $name));
286285
}
@@ -318,8 +317,7 @@ protected static function postForm(string $name, array $values, array $files = [
318317

319318
protected static function setSessionException(string $msg = 'error...'): void
320319
{
321-
/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
322-
$session = self::$container->get('session');
320+
$session = self::getSession();
323321
$session->set('_security.last_error', new AuthenticationServiceException($msg));
324322
$session->save();
325323
$cookie = new Cookie($session->getName(), $session->getId());
@@ -402,4 +400,15 @@ private function loadFixtureClass(Loader $loader, string $className): void
402400
$loader->addFixture($fixture);
403401
$this->fixture = $fixture;
404402
}
403+
404+
private static function getSession(): SessionInterface
405+
{
406+
if (static::$container->has('session.factory')) {
407+
return static::$container->get('session.factory')->createSession();
408+
}
409+
if (static::$container->has('session')) {
410+
return static::$container->get('session');
411+
}
412+
throw new \UnexpectedValueException('Cannot get session from container.');
413+
}
405414
}

tests/Test/WebTestCaseTest.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Beelab\TestBundle\Tests;
44

55
use Beelab\TestBundle\Test\WebTestCase;
6-
use Doctrine\ORM\EntityManagerInterface;
76
use org\bovigo\vfs\vfsStream;
87
use PHPUnit\Framework\TestCase;
98
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
@@ -15,7 +14,9 @@
1514
use Symfony\Component\DomCrawler\Link;
1615
use Symfony\Component\HttpFoundation\File\UploadedFile;
1716
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\HttpKernel\Profiler\Profile;
17+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
19+
use Symfony\Component\Security\Core\User\UserInterface;
1920
use Symfony\Component\Security\Core\User\UserProviderInterface;
2021

2122
final class WebTestCaseTest extends TestCase
@@ -90,26 +91,28 @@ public function testSaveOutput(): void
9091

9192
public function testLogin(): void
9293
{
93-
$user = $this->getMockBuilder('stdClass')->setMethods(['getRoles', '__toString'])->getMock();
94+
$user = $this->getMockBuilder(UserInterface::class)->getMock();
9495
$user
9596
->expects(self::once())
9697
->method('getRoles')
9798
->willReturn([]);
98-
$user
99-
->method('__toString')
100-
->willReturn('user');
10199

102100
$repository = $this
103101
->getMockBuilder(UserProviderInterface::class)
104-
->setMethods(['loadUserByUsername', 'refreshUser', 'supportsClass'])
102+
->setMethods(['loadUserByIdentifier', 'loadUserByUsername', 'refreshUser', 'supportsClass'])
105103
->getMock()
106104
;
107105
$repository
108106
->expects(self::once())
109-
->method('loadUserByUsername')
107+
->method('loadUserByIdentifier')
110108
->willReturn($user);
111109

112-
$session = $this->getMockBuilder('stdClass')->setMethods(['getId', 'getName', 'set', 'save'])->getMock();
110+
$session = $this->createMock(SessionInterface::class);
111+
112+
self::$container
113+
->method('has')
114+
->withConsecutive(['session.factory'], ['session'])
115+
->will(self::onConsecutiveCalls(false, true));
113116

114117
self::$container
115118
->method('get')
@@ -136,23 +139,26 @@ public function testLoginWithUserNotFound(): void
136139
{
137140
$repository = $this
138141
->getMockBuilder(UserProviderInterface::class)
139-
->setMethods(['loadUserByUsername', 'refreshUser', 'supportsClass'])
142+
->setMethods(['loadUserByIdentifier', 'loadUserByUsername', 'refreshUser', 'supportsClass'])
140143
->getMock()
141144
;
142145
$repository
143146
->expects(self::once())
144-
->method('loadUserByUsername')
145-
->willReturn(null)
147+
->method('loadUserByIdentifier')
148+
->willThrowException(new UserNotFoundException())
146149
;
150+
151+
$session = $this->createMock(SessionInterface::class);
152+
147153
self::$container
148154
->method('get')
149-
->with('beelab_user.manager')
150-
->willReturn($repository)
155+
->withConsecutive(['beelab_user.manager'], ['session'])
156+
->will(self::onConsecutiveCalls($repository, $session))
151157
;
158+
152159
$method = new \ReflectionMethod(self::$mock, 'login');
153160
$method->setAccessible(true);
154-
$this->expectException(\InvalidArgumentException::class);
155-
$this->expectExceptionMessage('Username [email protected] not found.');
161+
$this->expectException(UserNotFoundException::class);
156162
$method->invoke(self::$mock, '[email protected]', 'main', 'beelab_user.manager');
157163
}
158164

0 commit comments

Comments
 (0)