1 | <?php |
||
16 | class TokenManagerTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
20 | */ |
||
21 | private $factory; |
||
22 | |||
23 | /** |
||
24 | * @var TokenRepositoryInterface|ObjectProphecy |
||
25 | */ |
||
26 | private $repository; |
||
27 | |||
28 | /** |
||
29 | * @var InformationGuesserInterface|ObjectProphecy |
||
30 | */ |
||
31 | private $informationGuesser; |
||
32 | |||
33 | /** |
||
34 | * @var UserManagerInterface|ObjectProphecy |
||
35 | */ |
||
36 | private $userManager; |
||
37 | |||
38 | protected function setUp() |
||
39 | { |
||
40 | $this->factory = $this->createMock(TokenFactoryInterface::class); |
||
41 | $this->repository = $this->prophesize(TokenRepositoryInterface::class); |
||
42 | $this->informationGuesser = $this->prophesize(InformationGuesserInterface::class); |
||
43 | $this->userManager = $this->prophesize(UserManagerInterface::class); |
||
44 | } |
||
45 | |||
46 | protected function tearDown() |
||
47 | { |
||
48 | unset( |
||
49 | $this->factory, |
||
50 | $this->repository, |
||
51 | $this->informationGuesser, |
||
52 | $this->userManager |
||
53 | ); |
||
54 | } |
||
55 | |||
56 | protected function manager() |
||
57 | { |
||
58 | return new TokenManager( |
||
59 | $this->factory, |
||
60 | $this->repository->reveal(), |
||
61 | $this->informationGuesser->reveal(), |
||
62 | $this->userManager->reveal() |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @test |
||
68 | */ |
||
69 | public function it_get_token_from_repository() |
||
70 | { |
||
71 | $this->repository->get('unique-token', 'forgot_password') |
||
72 | ->shouldBeCalledTimes(1) |
||
73 | ->willReturn($expected = $this->prophesize(Token::class)->reveal()); |
||
74 | |||
75 | $token = $this->manager()->get('forgot_password', 'unique-token'); |
||
76 | |||
77 | self::assertSame($expected, $token); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @test |
||
82 | */ |
||
83 | public function it_create_unique_token() |
||
84 | { |
||
85 | $token1 = new Token('string', 'jdoe','unique-token-1', 'reset-password', '+1 day', []); |
||
86 | $token2 = new Token('string', 'jdoe','unique-token-2', 'reset-password', '+1 day', []); |
||
87 | |||
88 | $this->factory->expects($this->exactly(2)) |
||
89 | ->method('create') |
||
90 | ->will($this->onConsecutiveCalls($token1, $token2)); |
||
91 | |||
92 | $this->repository->exists('unique-token-1', 'forgot_password') |
||
93 | ->shouldBeCalledTimes(1) |
||
94 | ->willReturn(true); |
||
95 | $this->repository->exists('unique-token-2', 'forgot_password') |
||
96 | ->shouldBeCalledTimes(1) |
||
97 | ->willReturn(false); |
||
98 | |||
99 | $this->repository->create($token1) |
||
100 | ->shouldNotBeCalled(); |
||
101 | $this->repository->create($token2) |
||
102 | ->shouldBeCalledTimes(1); |
||
103 | |||
104 | $token = $this->manager()->create('forgot_password', 'john-doe'); |
||
105 | |||
106 | self::assertSame($token2, $token); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @test |
||
111 | */ |
||
112 | public function it_consume_token() |
||
113 | { |
||
114 | $token = new Token('string', 'jdoe','unique-token', 'reset-password', '+1 day', []); |
||
115 | |||
116 | $this->informationGuesser->get() |
||
117 | ->shouldBeCalledTimes(1) |
||
118 | ->willReturn(['some', 'precious', 'information']); |
||
119 | |||
120 | $this->repository->update($token) |
||
121 | ->shouldBeCalledTimes(1); |
||
122 | |||
123 | $this->manager()->setUsed($token); |
||
124 | |||
125 | self::assertSame(['some', 'precious', 'information'], $token->getUsedInformation()); |
||
126 | self::assertInstanceOf(\DateTime::class, $token->getUsedAt()); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @test |
||
131 | */ |
||
132 | public function it_extract_user_from_token() |
||
133 | { |
||
134 | $token = new Token('string', 'jdoe','unique-token', 'reset-password', '+1 day', []); |
||
135 | |||
136 | $this->userManager->get('string', 'jdoe') |
||
137 | ->shouldBeCalledTimes(1) |
||
138 | ->willReturn('john doe'); |
||
139 | |||
140 | $user = $this->manager()->getUser($token); |
||
141 | |||
142 | self::assertSame('john doe', $user); |
||
143 | } |
||
144 | } |
||
145 |