-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBaseControllerDeprecationTest.php
More file actions
170 lines (141 loc) · 5.32 KB
/
Copy pathBaseControllerDeprecationTest.php
File metadata and controls
170 lines (141 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
namespace Tests\Controller;
use App\Controller\BaseController;
use App\Entity\User;
use App\Enum\UserType;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;
use function restore_error_handler;
use function set_error_handler;
use const E_USER_DEPRECATED;
/**
* Unit tests for the deprecated BaseController helper shims.
*
* Verifies that each shim still delegates correctly and announces its
* deprecation (message built by triggerMethodDeprecation()).
*
* @internal
*/
#[CoversClass(BaseController::class)]
final class BaseControllerDeprecationTest extends TestCase
{
/**
* Messages emitted via triggerMethodDeprecation(). The native
* #[Deprecated] attribute (PHP >= 8.4) emits its own deprecation per
* call, so only "Method ..." messages are collected.
*
* @var list<string>
*/
private array $deprecations = [];
protected function setUp(): void
{
$this->deprecations = [];
set_error_handler(
function (int $errno, string $errstr): bool {
// triggerMethodDeprecation() uses __METHOD__ (no parentheses);
// the native attribute formats the name as method() instead.
if (str_starts_with($errstr, 'Method ') && !str_contains($errstr, '()')) {
$this->deprecations[] = $errstr;
}
return true;
},
E_USER_DEPRECATED,
);
}
protected function tearDown(): void
{
restore_error_handler();
}
public function testIsLoggedInDelegatesAndDeprecates(): void
{
$controller = $this->createController();
self::assertTrue($controller->callIsLoggedIn(new Request()));
self::assertCount(1, $this->deprecations);
self::assertStringContainsString('::isLoggedIn is deprecated', $this->deprecations[0]);
self::assertStringContainsString("use isGranted('IS_AUTHENTICATED_FULLY') directly", $this->deprecations[0]);
}
public function testCheckLoginDelegatesAndDeprecates(): void
{
$controller = $this->createController();
self::assertTrue($controller->callCheckLogin(new Request()));
self::assertCount(1, $this->deprecations);
self::assertStringContainsString('::checkLogin is deprecated', $this->deprecations[0]);
}
public function testHasUserTypeMatchesCurrentUserAndDeprecates(): void
{
$controller = $this->createController();
self::assertTrue($controller->callHasUserType(new Request(), UserType::PL));
self::assertFalse($controller->callHasUserType(new Request(), UserType::DEV));
self::assertCount(2, $this->deprecations);
self::assertStringContainsString('::hasUserType is deprecated', $this->deprecations[0]);
}
public function testIsPlDelegatesToHasUserTypeAndDeprecates(): void
{
$controller = $this->createController();
self::assertTrue($controller->callIsPl(new Request()));
// isPl() delegates to hasUserType(), so both shims announce themselves
self::assertCount(2, $this->deprecations);
self::assertStringContainsString('::isPl is deprecated', $this->deprecations[0]);
}
public function testIsDevDelegatesToHasUserTypeAndDeprecates(): void
{
$controller = $this->createController();
self::assertFalse($controller->callIsDEV(new Request()));
self::assertCount(2, $this->deprecations);
self::assertStringContainsString('::isDEV is deprecated', $this->deprecations[0]);
}
private function createController(): DeprecatedShimBaseController
{
$user = new User();
$user->setUsername('unittest');
$user->setType('PL');
return new DeprecatedShimBaseController($user);
}
}
/**
* Test double exposing the deprecated protected shims with a fixed user.
*
* @internal
*/
final class DeprecatedShimBaseController extends BaseController
{
public function __construct(private readonly User $user)
{
}
public function callIsLoggedIn(Request $request): bool
{
// @phpstan-ignore method.deprecated (the shim under test is deprecated by design)
return $this->isLoggedIn($request);
}
public function callCheckLogin(Request $request): bool
{
// @phpstan-ignore method.deprecated (the shim under test is deprecated by design)
return $this->checkLogin($request);
}
public function callHasUserType(Request $request, UserType $userType): bool
{
// @phpstan-ignore method.deprecated (the shim under test is deprecated by design)
return $this->hasUserType($request, $userType);
}
public function callIsPl(Request $request): bool
{
// @phpstan-ignore method.deprecated (the shim under test is deprecated by design)
return $this->isPl($request);
}
public function callIsDEV(Request $request): bool
{
// @phpstan-ignore method.deprecated (the shim under test is deprecated by design)
return $this->isDEV($request);
}
protected function isGranted(mixed $attribute, mixed $subject = null): bool
{
return true;
}
protected function getUser(): UserInterface
{
return $this->user;
}
}