1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\DataFixtures; |
4
|
|
|
|
5
|
|
|
use App\Entity\ConfirmationToken; |
6
|
|
|
use App\Entity\User; |
7
|
|
|
use App\Entity\UserProfile; |
8
|
|
|
use App\Entity\UserProfileContacts; |
9
|
|
|
use Doctrine\Bundle\FixturesBundle\Fixture; |
10
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
11
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
12
|
|
|
|
13
|
|
|
class UsersFixtures extends Fixture |
14
|
|
|
{ |
15
|
|
|
const TESTER_EMAIL = '[email protected]'; |
16
|
|
|
const TESTER_USERNAME = 'tester_fixture'; |
17
|
|
|
const TESTER_PASSWORD = '123456'; |
18
|
|
|
const TESTER_API_TOKEN = 'tester_api_token'; |
19
|
|
|
const TESTER_EMAIL_CONFIRMATION_TOKEN = '11kYJ3ut7aOISPQN0RSqceYDasNnb690'; |
20
|
|
|
|
21
|
|
|
public function load(ObjectManager $manager): void |
22
|
|
|
{ |
23
|
|
|
$user = new User(); |
24
|
|
|
$user->username = self::TESTER_USERNAME; |
25
|
|
|
$user->email = self::TESTER_EMAIL; |
26
|
|
|
$user->setPlainPassword(self::TESTER_PASSWORD); |
27
|
|
|
|
28
|
|
|
$profile = $user->getProfile(); |
29
|
|
|
$profile->first_name = 'First'; |
30
|
|
|
$profile->last_name = 'Last'; |
31
|
|
|
|
32
|
|
|
for ($i = 3; $i--> 0;) { |
33
|
|
|
$profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$manager->persist($user); |
37
|
|
|
$manager->flush(); |
38
|
|
|
|
39
|
|
|
$this->createTestApiToken($user, self::TESTER_API_TOKEN, $manager); |
40
|
|
|
$this->createEmailConfirmationToken($user, self::TESTER_EMAIL_CONFIRMATION_TOKEN, $manager); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function createTestApiToken(User $user, string $token, ObjectManager $manager): void |
44
|
|
|
{ |
45
|
|
|
$manager->getConnection()->exec("INSERT INTO users_api_tokens (id, user_id, token) VALUES (NEXTVAL('users_api_tokens_id_seq'), {$user->getId()}, '{$token}');"); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function createEmailConfirmationToken(User $user, string $token, ObjectManager $manager): void |
49
|
|
|
{ |
50
|
|
|
$type = ConfirmationToken::TYPE_CONFIRM_EMAIl; |
51
|
|
|
$manager->getConnection()->exec("INSERT INTO users_confirmation_tokens (id, user_id, token, type) VALUES (NEXTVAL('users_confirmation_tokens_id_seq'), {$user->getId()}, '{$token}', '{$type}');"); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: