|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Bundle\MakerBundle\Security\SecurityControllerBuilder;
|
16 | 16 | use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
|
| 17 | +use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil; |
17 | 18 |
|
18 | 19 | class SecurityControllerBuilderTest extends TestCase
|
19 | 20 | {
|
20 |
| - public function testAddLoginMethod() |
| 21 | + private $expectedBasePath = __DIR__.'/fixtures/expected'; |
| 22 | + |
| 23 | + public function testLoginMethod(): void |
21 | 24 | {
|
22 |
| - $source = file_get_contents(__DIR__.'/fixtures/source/SecurityController.php'); |
23 |
| - $expectedSource = file_get_contents(__DIR__.'/fixtures/expected/SecurityController_login.php'); |
| 25 | + /* @legacy Can be dropped when PHP 7.x support is dropped in MakerBundle */ |
| 26 | + $this->runMethodTest( |
| 27 | + 'addLoginMethod', |
| 28 | + true, |
| 29 | + sprintf('%s/legacy_add_login_method/%s', $this->expectedBasePath, 'SecurityController_login.php') |
| 30 | + ); |
24 | 31 |
|
25 |
| - $manipulator = new ClassSourceManipulator($source); |
| 32 | + if ((\PHP_VERSION_ID >= 80000)) { |
| 33 | + $this->runMethodTest( |
| 34 | + 'addLoginMethod', |
| 35 | + false, |
| 36 | + sprintf('%s/%s', $this->expectedBasePath, 'SecurityController_login.php') |
| 37 | + ); |
| 38 | + } |
| 39 | + } |
26 | 40 |
|
27 |
| - $securityControllerBuilder = new SecurityControllerBuilder(); |
28 |
| - $securityControllerBuilder->addLoginMethod($manipulator); |
| 41 | + public function testLogoutMethod(): void |
| 42 | + { |
| 43 | + /* @legacy Can be dropped when PHP 7.x support is dropped in MakerBundle */ |
| 44 | + $this->runMethodTest( |
| 45 | + 'addLogoutMethod', |
| 46 | + true, |
| 47 | + sprintf('%s/legacy_add_logout_method/%s', $this->expectedBasePath, 'SecurityController_logout.php') |
| 48 | + ); |
29 | 49 |
|
30 |
| - $this->assertSame($expectedSource, $manipulator->getSourceCode()); |
| 50 | + if ((\PHP_VERSION_ID >= 80000)) { |
| 51 | + $this->runMethodTest( |
| 52 | + 'addLogoutMethod', |
| 53 | + false, |
| 54 | + sprintf('%s/%s', $this->expectedBasePath, 'SecurityController_logout.php') |
| 55 | + ); |
| 56 | + } |
31 | 57 | }
|
32 | 58 |
|
33 |
| - public function testLogoutMethod() |
| 59 | + public function testLoginAndLogoutMethod(): void |
34 | 60 | {
|
35 |
| - $source = file_get_contents(__DIR__.'/fixtures/source/SecurityController.php'); |
36 |
| - $expectedSource = file_get_contents(__DIR__.'/fixtures/expected/SecurityController_logout.php'); |
| 61 | + /** @legacy Can be dropped when PHP 7.x support is dropped in MakerBundle */ |
| 62 | + $builder = $this->getSecurityControllerBuilder(true); |
| 63 | + $csm = $this->getClassSourceManipulator(); |
| 64 | + |
| 65 | + $builder->addLoginMethod($csm); |
| 66 | + $builder->addLogoutMethod($csm); |
| 67 | + |
| 68 | + $this->assertStringEqualsFile( |
| 69 | + sprintf('%s/legacy_add_login_logout_method/%s', $this->expectedBasePath, 'SecurityController_login_logout.php'), |
| 70 | + $csm->getSourceCode() |
| 71 | + ); |
37 | 72 |
|
38 |
| - $manipulator = new ClassSourceManipulator($source); |
| 73 | + if ((\PHP_VERSION_ID >= 80000)) { |
| 74 | + $builder = $this->getSecurityControllerBuilder(false); |
| 75 | + $csm = $this->getClassSourceManipulator(); |
39 | 76 |
|
40 |
| - $securityControllerBuilder = new SecurityControllerBuilder(); |
41 |
| - $securityControllerBuilder->addLogoutMethod($manipulator); |
| 77 | + $builder->addLoginMethod($csm); |
| 78 | + $builder->addLogoutMethod($csm); |
42 | 79 |
|
43 |
| - $this->assertSame($expectedSource, $manipulator->getSourceCode()); |
| 80 | + $this->assertStringEqualsFile( |
| 81 | + sprintf('%s/%s', $this->expectedBasePath, 'SecurityController_login_logout.php'), |
| 82 | + $csm->getSourceCode() |
| 83 | + ); |
| 84 | + } |
44 | 85 | }
|
45 | 86 |
|
46 |
| - public function testLoginAndLogoutMethod() |
| 87 | + private function runMethodTest(string $builderMethod, bool $isLegacyTest, string $expectedFilePath): void |
47 | 88 | {
|
48 |
| - $source = file_get_contents(__DIR__.'/fixtures/source/SecurityController.php'); |
49 |
| - $expectedSource = file_get_contents(__DIR__.'/fixtures/expected/SecurityController_login_logout.php'); |
| 89 | + $builder = $this->getSecurityControllerBuilder($isLegacyTest); |
| 90 | + $csm = $this->getClassSourceManipulator(); |
50 | 91 |
|
51 |
| - $manipulator = new ClassSourceManipulator($source); |
| 92 | + $builder->$builderMethod($csm); |
| 93 | + $this->assertStringEqualsFile($expectedFilePath, $csm->getSourceCode()); |
| 94 | + } |
52 | 95 |
|
53 |
| - $securityControllerBuilder = new SecurityControllerBuilder(); |
54 |
| - $securityControllerBuilder->addLoginMethod($manipulator); |
55 |
| - $securityControllerBuilder->addLogoutMethod($manipulator); |
| 96 | + private function getClassSourceManipulator(): ClassSourceManipulator |
| 97 | + { |
| 98 | + return new ClassSourceManipulator(file_get_contents(__DIR__.'/fixtures/source/SecurityController.php')); |
| 99 | + } |
| 100 | + |
| 101 | + private function getSecurityControllerBuilder(bool $isLegacyTest): SecurityControllerBuilder |
| 102 | + { |
| 103 | + $compatUtil = $this->createMock(PhpCompatUtil::class); |
| 104 | + $compatUtil |
| 105 | + ->method('canUseAttributes') |
| 106 | + ->willReturn(!$isLegacyTest) |
| 107 | + ; |
56 | 108 |
|
57 |
| - $this->assertSame($expectedSource, $manipulator->getSourceCode()); |
| 109 | + return new SecurityControllerBuilder($compatUtil); |
58 | 110 | }
|
59 | 111 | }
|
0 commit comments