|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\Attributes\DataProvider; |
15 | 15 | use Symfony\Component\Security\Core\User\InMemoryUser; |
| 16 | +use Symfony\Component\Security\Core\User\UserInterface; |
16 | 17 |
|
17 | 18 | class SecurityTest extends AbstractWebTestCase |
18 | 19 | { |
@@ -85,4 +86,83 @@ public function testLoginUserMultipleTimes() |
85 | 86 | $client->request('GET', '/main/user_profile'); |
86 | 87 | $this->assertEquals('Welcome no-role-username!', $client->getResponse()->getContent()); |
87 | 88 | } |
| 89 | + |
| 90 | + public function testLoginUserThrowsContextualErrorWhenUserGraphIsNotSerializable() |
| 91 | + { |
| 92 | + $user = new SecurityTestUserWithUnserializableField('the-username', ['ROLE_FOO']); |
| 93 | + $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']); |
| 94 | + |
| 95 | + try { |
| 96 | + $client->loginUser($user); |
| 97 | + $this->fail('Expected LogicException was not thrown.'); |
| 98 | + } catch (\LogicException $e) { |
| 99 | + $this->assertStringContainsString(SecurityTestUserWithUnserializableField::class, $e->getMessage()); |
| 100 | + $this->assertStringContainsString('not serializable', $e->getMessage()); |
| 101 | + $this->assertStringContainsString('Implement "__serialize()"/"__unserialize()"', $e->getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public function testLoginUserSuggestsReviewWhenUserAlreadyImplementsSerialize() |
| 106 | + { |
| 107 | + $user = new SecurityTestUserWithBrokenSerialize(); |
| 108 | + $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']); |
| 109 | + |
| 110 | + try { |
| 111 | + $client->loginUser($user); |
| 112 | + $this->fail('Expected LogicException was not thrown.'); |
| 113 | + } catch (\LogicException $e) { |
| 114 | + $this->assertStringContainsString(SecurityTestUserWithBrokenSerialize::class, $e->getMessage()); |
| 115 | + $this->assertStringContainsString('Review the "__serialize()" implementation', $e->getMessage()); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class SecurityTestUserWithUnserializableField implements UserInterface |
| 121 | +{ |
| 122 | + public \SplFileInfo $file; |
| 123 | + |
| 124 | + public function __construct(private string $username, private array $roles = []) |
| 125 | + { |
| 126 | + $this->file = new \SplFileInfo(__FILE__); |
| 127 | + } |
| 128 | + |
| 129 | + public function getRoles(): array |
| 130 | + { |
| 131 | + return $this->roles; |
| 132 | + } |
| 133 | + |
| 134 | + public function eraseCredentials(): void |
| 135 | + { |
| 136 | + } |
| 137 | + |
| 138 | + public function getUserIdentifier(): string |
| 139 | + { |
| 140 | + return $this->username; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +class SecurityTestUserWithBrokenSerialize implements UserInterface |
| 145 | +{ |
| 146 | + public function getRoles(): array |
| 147 | + { |
| 148 | + return ['ROLE_USER']; |
| 149 | + } |
| 150 | + |
| 151 | + public function eraseCredentials(): void |
| 152 | + { |
| 153 | + } |
| 154 | + |
| 155 | + public function getUserIdentifier(): string |
| 156 | + { |
| 157 | + return 'broken-serialize-user'; |
| 158 | + } |
| 159 | + |
| 160 | + public function __serialize(): array |
| 161 | + { |
| 162 | + return ['file' => new \SplFileInfo(__FILE__)]; |
| 163 | + } |
| 164 | + |
| 165 | + public function __unserialize(array $data): void |
| 166 | + { |
| 167 | + } |
88 | 168 | } |
0 commit comments