1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\DataFixtures; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use App\Entity\UserProfile; |
7
|
|
|
use App\Entity\UserProfileContacts; |
8
|
|
|
use Doctrine\Bundle\FixturesBundle\Fixture; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
10
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
11
|
|
|
|
12
|
|
|
class UsersFixtures extends Fixture |
13
|
|
|
{ |
14
|
|
|
const TESTER_EMAIL = '[email protected]'; |
15
|
|
|
const TESTER_USERNAME = 'tester_fixture'; |
16
|
|
|
const TESTER_PASSWORD = '123456'; |
17
|
|
|
const TESTER_API_TOKEN = 'tester_api_token'; |
18
|
|
|
|
19
|
|
|
private $encoder; |
20
|
|
|
|
21
|
|
|
public function __construct(UserPasswordEncoderInterface $encoder) |
22
|
|
|
{ |
23
|
|
|
$this->encoder = $encoder; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function load(ObjectManager $manager): void |
27
|
|
|
{ |
28
|
|
|
$user = new User(); |
29
|
|
|
$user->username = 'tester_fixture'; |
30
|
|
|
$user->email = '[email protected]'; |
31
|
|
|
$user->setPassword('123456', $this->encoder); |
32
|
|
|
|
33
|
|
|
$profile = $user->getProfile(); |
34
|
|
|
$profile->first_name = 'First'; |
35
|
|
|
$profile->last_name = 'Last'; |
36
|
|
|
|
37
|
|
|
for ($i = 3; $i--> 0;) { |
38
|
|
|
$profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$manager->persist($user); |
42
|
|
|
$manager->flush(); |
43
|
|
|
|
44
|
|
|
$this->createTestApiToken($user, self::TESTER_API_TOKEN, $manager); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function createTestApiToken(User $user, string $token, ObjectManager $manager): void |
48
|
|
|
{ |
49
|
|
|
$manager->getConnection()->exec("INSERT INTO users_api_tokens (id, user_id, token) VALUES (NEXTVAL('users_api_tokens_id_seq'), {$user->getId()}, '{$token}');"); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
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: