-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDoctrineUserManagerTest.php
More file actions
152 lines (120 loc) · 3.97 KB
/
Copy pathDoctrineUserManagerTest.php
File metadata and controls
152 lines (120 loc) · 3.97 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
<?php
declare(strict_types=1);
namespace Yokai\SecurityTokenBundle\Tests\Manager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Yokai\SecurityTokenBundle\Manager\DoctrineUserManager;
/**
* @author Yann Eugoné <eugone.yann@gmail.com>
*
* phpcs:ignoreFile PSR1.Methods.CamelCapsMethodName.NotCamelCaps
*/
final class DoctrineUserManagerTest extends TestCase
{
/**
* @var MockObject<ManagerRegistry>
*/
private $registry;
/**
* @var MockObject<EntityManagerInterface>
*/
private $objectManager;
/**
* @var MockObject<ClassMetadata>
*/
private $classMetadata;
protected function setUp(): void
{
$this->registry = $this->createMock(ManagerRegistry::class);
$this->objectManager = $this->createMock(ObjectManager::class);
$this->classMetadata = $this->createMock(ClassMetadata::class);
}
protected function tearDown(): void
{
unset(
$this->registry,
$this->objectManager,
$this->classMetadata,
);
}
protected function manager(): DoctrineUserManager
{
return new DoctrineUserManager($this->registry);
}
protected function user($id)
{
return new class($id) {
private $id;
public function __construct($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
};
}
public function test_it_supports_doctrine_entities(): void
{
$user = $this->user('jdoe');
$this->registry->method('getManagerForClass')
->with(\get_class($user))
->willReturn($this->objectManager);
$manager = $this->manager();
self::assertTrue($manager->supportsClass(\get_class($user)));
self::assertTrue($manager->supportsUser($user));
}
public function test_it_do_not_supports_objects_out_of_doctrine(): void
{
$user = $this->user('jdoe');
$this->registry->method('getManagerForClass')
->with(\get_class($user))
->willReturn(null);
$manager = $this->manager();
self::assertFalse($manager->supportsClass(\get_class($user)));
self::assertFalse($manager->supportsUser($user));
}
public function test_it_get_user(): void
{
$expected = $this->user('jdoe');
$this->registry->expects(self::once())
->method('getManagerForClass')
->with(\get_class($expected))
->willReturn($this->objectManager);
$this->objectManager->expects(self::once())
->method('find')
->with(\get_class($expected), 'jdoe')
->willReturn($expected);
$user = $this->manager()->get(\get_class($expected), 'jdoe');
self::assertSame($expected, $user);
}
public function test_it_get_user_class(): void
{
$expected = $this->user('jdoe');
$class = $this->manager()->getClass($expected);
self::assertSame(\get_class($expected), $class);
}
public function test_it_get_user_id(): void
{
$expected = $this->user('jdoe');
$this->registry->expects(self::once())
->method('getManagerForClass')
->with(\get_class($expected))
->willReturn($this->objectManager);
$this->objectManager->expects(self::once())
->method('getClassMetadata')
->with(\get_class($expected))
->willReturn($this->classMetadata);
$this->classMetadata->expects(self::once())
->method('getIdentifierValues')
->with($expected)
->willReturn(['id' => 'jdoe']);
$id = $this->manager()->getId($expected);
self::assertSame('jdoe', $id);
}
}