1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Tests\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
9
|
|
|
use Yokai\SecurityTokenBundle\Entity\Token; |
10
|
|
|
use Yokai\SecurityTokenBundle\Repository\DoctrineORMTokenRepository; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Yann Eugoné <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class DoctrineORMTokenRepositoryTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EntityManager|ObjectProphecy |
19
|
|
|
*/ |
20
|
|
|
private $manager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EntityRepository|ObjectProphecy |
24
|
|
|
*/ |
25
|
|
|
private $repository; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->manager = $this->prophesize(EntityManager::class); |
30
|
|
|
$this->repository = $this->prophesize(EntityRepository::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function tearDown() |
34
|
|
|
{ |
35
|
|
|
unset( |
36
|
|
|
$this->manager, |
37
|
|
|
$this->repository |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function repository() |
42
|
|
|
{ |
43
|
|
|
return new DoctrineORMTokenRepository($this->manager->reveal(), $this->repository->reveal()); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
* @expectedException \Yokai\SecurityTokenBundle\Exception\TokenNotFoundException |
49
|
|
|
*/ |
50
|
|
|
public function it_throw_exception_if_token_not_found() |
51
|
|
|
{ |
52
|
|
|
$this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
|
|
|
|
53
|
|
|
->shouldBeCalledTimes(1) |
54
|
|
|
->willReturn(null); |
55
|
|
|
|
56
|
|
|
$this->repository()->get('unique', 'init_password'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
* @expectedException \Yokai\SecurityTokenBundle\Exception\TokenExpiredException |
62
|
|
|
*/ |
63
|
|
|
public function it_throw_exception_if_token_expired() |
64
|
|
|
{ |
65
|
|
|
$token = new Token('string', 'jdoe','unique', 'init_password', '-1 day', []); |
66
|
|
|
|
67
|
|
|
$this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
|
|
|
|
68
|
|
|
->shouldBeCalledTimes(1) |
69
|
|
|
->willReturn($token); |
70
|
|
|
|
71
|
|
|
$this->repository()->get('unique', 'init_password'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* @expectedException \Yokai\SecurityTokenBundle\Exception\TokenUsedException |
77
|
|
|
*/ |
78
|
|
|
public function it_throw_exception_if_token_used() |
79
|
|
|
{ |
80
|
|
|
$token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
81
|
|
|
$token->setUsedAt(new \DateTime()); |
82
|
|
|
$token->setUsedInformation(['info']); |
83
|
|
|
|
84
|
|
|
$this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
|
|
|
|
85
|
|
|
->shouldBeCalledTimes(1) |
86
|
|
|
->willReturn($token); |
87
|
|
|
|
88
|
|
|
$this->repository()->get('unique', 'init_password'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function it_get_valid_token() |
95
|
|
|
{ |
96
|
|
|
$token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
97
|
|
|
|
98
|
|
|
$this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
|
|
|
|
99
|
|
|
->shouldBeCalledTimes(1) |
100
|
|
|
->willReturn($token); |
101
|
|
|
|
102
|
|
|
$got = $this->repository()->get('unique', 'init_password'); |
103
|
|
|
|
104
|
|
|
self::assertSame($token, $got); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function it_create_token() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
113
|
|
|
|
114
|
|
|
$this->manager->persist($token) |
|
|
|
|
115
|
|
|
->shouldBeCalledTimes(1); |
116
|
|
|
$this->manager->flush($token) |
|
|
|
|
117
|
|
|
->shouldBeCalledTimes(1); |
118
|
|
|
|
119
|
|
|
$this->repository()->create($token); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @test |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function it_update_token() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
128
|
|
|
|
129
|
|
|
$this->manager->persist($token) |
|
|
|
|
130
|
|
|
->shouldBeCalledTimes(1); |
131
|
|
|
$this->manager->flush($token) |
|
|
|
|
132
|
|
|
->shouldBeCalledTimes(1); |
133
|
|
|
|
134
|
|
|
$this->repository()->update($token); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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: