1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Tests\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
8
|
|
|
use Yokai\SecurityTokenBundle\Manager\UserManager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Yann Eugoné <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class UserManagerTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var EntityManagerInterface|ObjectProphecy |
17
|
|
|
*/ |
18
|
|
|
private $entityManager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ClassMetadata|ObjectProphecy |
22
|
|
|
*/ |
23
|
|
|
private $classMetadata; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->entityManager = $this->prophesize(EntityManagerInterface::class); |
28
|
|
|
$this->classMetadata = $this->prophesize(ClassMetadata::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function tearDown() |
32
|
|
|
{ |
33
|
|
|
unset( |
34
|
|
|
$this->entityManager, |
35
|
|
|
$this->classMetadata |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function manager() |
40
|
|
|
{ |
41
|
|
|
return new UserManager($this->entityManager->reveal()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function user($id) |
45
|
|
|
{ |
46
|
|
|
return new class($id) { |
47
|
|
|
private $id; |
48
|
|
|
|
49
|
|
|
public function __construct($id) |
50
|
|
|
{ |
51
|
|
|
$this->id = $id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getId() |
55
|
|
|
{ |
56
|
|
|
return $this->id; |
57
|
|
|
} |
58
|
|
|
}; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
*/ |
64
|
|
|
public function it_get_user() |
65
|
|
|
{ |
66
|
|
|
$expected = $this->user('jdoe'); |
67
|
|
|
|
68
|
|
|
$this->entityManager->find(get_class($expected), 'jdoe') |
|
|
|
|
69
|
|
|
->shouldBeCalledTimes(1) |
70
|
|
|
->willReturn($expected); |
71
|
|
|
|
72
|
|
|
$user = $this->manager()->get(get_class($expected), 'jdoe'); |
73
|
|
|
|
74
|
|
|
self::assertSame($expected, $user); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
*/ |
80
|
|
|
public function it_get_user_class() |
81
|
|
|
{ |
82
|
|
|
$expected = $this->user('jdoe'); |
83
|
|
|
|
84
|
|
|
$class = $this->manager()->getClass($expected); |
85
|
|
|
|
86
|
|
|
self::assertSame(get_class($expected), $class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function it_get_user_id() |
93
|
|
|
{ |
94
|
|
|
$expected = $this->user('jdoe'); |
95
|
|
|
|
96
|
|
|
$this->entityManager->getClassMetadata(get_class($expected)) |
|
|
|
|
97
|
|
|
->shouldBeCalledTimes(1) |
98
|
|
|
->willReturn($this->classMetadata->reveal()); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->classMetadata->getIdentifierValues($expected) |
|
|
|
|
101
|
|
|
->shouldBeCalledTimes(1) |
102
|
|
|
->willReturn(['id' => 'jdoe']); |
103
|
|
|
|
104
|
|
|
$id = $this->manager()->getId($expected); |
105
|
|
|
|
106
|
|
|
self::assertSame('jdoe', $id); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: