1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Tests\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
9
|
|
|
use Yokai\SecurityTokenBundle\Manager\UserManager; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Yann Eugoné <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class UserManagerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EntityManagerInterface|ObjectProphecy |
18
|
|
|
*/ |
19
|
|
|
private $entityManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ClassMetadata|ObjectProphecy |
23
|
|
|
*/ |
24
|
|
|
private $classMetadata; |
25
|
|
|
|
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->entityManager = $this->prophesize(EntityManagerInterface::class); |
29
|
|
|
$this->classMetadata = $this->prophesize(ClassMetadata::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function tearDown() |
33
|
|
|
{ |
34
|
|
|
unset( |
35
|
|
|
$this->entityManager, |
36
|
|
|
$this->classMetadata |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function manager() |
41
|
|
|
{ |
42
|
|
|
return new UserManager($this->entityManager->reveal()); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function user($id) |
46
|
|
|
{ |
47
|
|
|
return new class($id) { |
48
|
|
|
private $id; |
49
|
|
|
|
50
|
|
|
public function __construct($id) |
51
|
|
|
{ |
52
|
|
|
$this->id = $id; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getId() |
56
|
|
|
{ |
57
|
|
|
return $this->id; |
58
|
|
|
} |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
*/ |
65
|
|
|
public function it_get_user() |
66
|
|
|
{ |
67
|
|
|
$expected = $this->user('jdoe'); |
68
|
|
|
|
69
|
|
|
$this->entityManager->find(get_class($expected), 'jdoe') |
|
|
|
|
70
|
|
|
->shouldBeCalledTimes(1) |
71
|
|
|
->willReturn($expected); |
72
|
|
|
|
73
|
|
|
$user = $this->manager()->get(get_class($expected), 'jdoe'); |
74
|
|
|
|
75
|
|
|
self::assertSame($expected, $user); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function it_get_user_class() |
82
|
|
|
{ |
83
|
|
|
$expected = $this->user('jdoe'); |
84
|
|
|
|
85
|
|
|
$class = $this->manager()->getClass($expected); |
86
|
|
|
|
87
|
|
|
self::assertSame(get_class($expected), $class); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function it_get_user_id() |
94
|
|
|
{ |
95
|
|
|
$expected = $this->user('jdoe'); |
96
|
|
|
|
97
|
|
|
$this->entityManager->getClassMetadata(get_class($expected)) |
|
|
|
|
98
|
|
|
->shouldBeCalledTimes(1) |
99
|
|
|
->willReturn($this->classMetadata->reveal()); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$this->classMetadata->getIdentifierValues($expected) |
|
|
|
|
102
|
|
|
->shouldBeCalledTimes(1) |
103
|
|
|
->willReturn(['id' => 'jdoe']); |
104
|
|
|
|
105
|
|
|
$id = $this->manager()->getId($expected); |
106
|
|
|
|
107
|
|
|
self::assertSame('jdoe', $id); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: