|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: leo108 |
|
5
|
|
|
* Date: 2016/9/29 |
|
6
|
|
|
* Time: 09:53 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Leo108\CAS\Repositories; |
|
9
|
|
|
|
|
10
|
|
|
use Exception; |
|
11
|
|
|
use Leo108\CAS\Exceptions\CAS\CasException; |
|
12
|
|
|
use Leo108\CAS\Models\Service; |
|
13
|
|
|
use Leo108\CAS\Models\Ticket; |
|
14
|
|
|
use Leo108\CAS\Services\TicketGenerator; |
|
15
|
|
|
use Mockery; |
|
16
|
|
|
use TestCase; |
|
17
|
|
|
use User; |
|
18
|
|
|
|
|
19
|
|
|
class TicketRepositoryTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testApplyTicket() |
|
22
|
|
|
{ |
|
23
|
|
|
//test if service url is not valid |
|
24
|
|
|
$user = Mockery::mock(User::class) |
|
25
|
|
|
->shouldReceive('getAttribute') |
|
26
|
|
|
->withArgs(['id']) |
|
27
|
|
|
->andReturn(1) |
|
28
|
|
|
->shouldReceive('getEloquentModel') |
|
29
|
|
|
->andReturn(Mockery::self()) |
|
30
|
|
|
->getMock(); |
|
31
|
|
|
|
|
32
|
|
|
$serviceRepository = Mockery::mock(ServiceRepository::class) |
|
33
|
|
|
->shouldReceive('getServiceByUrl') |
|
34
|
|
|
->andReturn(false) |
|
35
|
|
|
->getMock(); |
|
36
|
|
|
app()->instance(ServiceRepository::class, $serviceRepository); |
|
37
|
|
|
try { |
|
38
|
|
|
app()->make(TicketRepository::class)->applyTicket($user, 'what ever'); |
|
39
|
|
|
} catch (Exception $e) { |
|
40
|
|
|
$this->assertInstanceOf(CasException::class, $e); |
|
41
|
|
|
$this->assertEquals($e->getCasErrorCode(), CasException::INVALID_SERVICE); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
//test if get available ticket failed |
|
45
|
|
|
$service = Mockery::mock(Service::class) |
|
46
|
|
|
->shouldReceive('getAttribute') |
|
47
|
|
|
->withArgs(['id']) |
|
48
|
|
|
->andReturn(1) |
|
49
|
|
|
->getMock(); |
|
50
|
|
|
$serviceRepository = Mockery::mock(ServiceRepository::class) |
|
51
|
|
|
->shouldReceive('getServiceByUrl') |
|
52
|
|
|
->andReturn($service) |
|
53
|
|
|
->getMock(); |
|
54
|
|
|
app()->instance(ServiceRepository::class, $serviceRepository); |
|
55
|
|
|
$ticketRepository = $this->initTicketRepository() |
|
56
|
|
|
->makePartial() |
|
57
|
|
|
->shouldAllowMockingProtectedMethods() |
|
58
|
|
|
->shouldReceive('getAvailableTicket') |
|
59
|
|
|
->andReturn(false) |
|
60
|
|
|
->getMock(); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$ticketRepository->applyTicket($user, 'what ever'); |
|
64
|
|
|
} catch (Exception $e) { |
|
65
|
|
|
$this->assertInstanceOf(CasException::class, $e); |
|
66
|
|
|
$this->assertEquals($e->getCasErrorCode(), CasException::INTERNAL_ERROR); |
|
67
|
|
|
$this->assertEquals($e->getMessage(), 'apply ticket failed'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
//normal |
|
71
|
|
|
$ticketStr = 'ST-abc'; |
|
72
|
|
|
$serviceUrl = 'what ever'; |
|
73
|
|
|
$service = Mockery::mock(Service::class) |
|
74
|
|
|
->shouldReceive('getAttribute') |
|
75
|
|
|
->withArgs(['id']) |
|
76
|
|
|
->andReturn(1) |
|
77
|
|
|
->getMock(); |
|
78
|
|
|
$serviceRepository = Mockery::mock(ServiceRepository::class) |
|
79
|
|
|
->shouldReceive('getServiceByUrl') |
|
80
|
|
|
->andReturn($service) |
|
81
|
|
|
->getMock(); |
|
82
|
|
|
app()->instance(ServiceRepository::class, $serviceRepository); |
|
83
|
|
|
$ticket = Mockery::mock(Ticket::class) |
|
84
|
|
|
->shouldReceive('newInstance') |
|
85
|
|
|
->andReturnUsing( |
|
86
|
|
|
function ($param) { |
|
87
|
|
|
$obj = Mockery::mock(); |
|
88
|
|
|
$obj->shouldReceive('user->associate'); |
|
89
|
|
|
$obj->shouldReceive('service->associate'); |
|
90
|
|
|
$obj->shouldReceive('save'); |
|
91
|
|
|
$obj->ticket = $param['ticket']; |
|
92
|
|
|
$obj->service_url = $param['service_url']; |
|
93
|
|
|
|
|
94
|
|
|
return $obj; |
|
95
|
|
|
} |
|
96
|
|
|
) |
|
97
|
|
|
->getMock(); |
|
98
|
|
|
app()->instance(Ticket::class, $ticket); |
|
99
|
|
|
$ticketRepository = $this->initTicketRepository() |
|
100
|
|
|
->makePartial() |
|
101
|
|
|
->shouldAllowMockingProtectedMethods() |
|
102
|
|
|
->shouldReceive('getAvailableTicket') |
|
103
|
|
|
->andReturn($ticketStr) |
|
104
|
|
|
->getMock(); |
|
105
|
|
|
|
|
106
|
|
|
$record = $ticketRepository->applyTicket($user, $serviceUrl); |
|
107
|
|
|
$this->assertEquals($ticketStr, $record->ticket); |
|
108
|
|
|
$this->assertEquals($serviceUrl, $record->service_url); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testGetByTicket() |
|
112
|
|
|
{ |
|
113
|
|
|
$ticket = Mockery::mock(Ticket::class); |
|
114
|
|
|
$ticket->shouldReceive('where->first')->andReturn(null); |
|
115
|
|
|
app()->instance(Ticket::class, $ticket); |
|
116
|
|
|
$this->assertNull(app()->make(TicketRepository::class)->getByTicket('what ever')); |
|
117
|
|
|
|
|
118
|
|
|
$mockTicket = Mockery::mock(Ticket::class) |
|
119
|
|
|
->shouldReceive('isExpired') |
|
120
|
|
|
->andReturnValues([false, true]) |
|
121
|
|
|
->getMock(); |
|
122
|
|
|
|
|
123
|
|
|
$ticket = Mockery::mock(Ticket::class); |
|
124
|
|
|
$ticket->shouldReceive('where->first')->andReturn($mockTicket); |
|
125
|
|
|
app()->instance(Ticket::class, $ticket); |
|
126
|
|
|
$this->assertNotNull(app()->make(TicketRepository::class)->getByTicket('what ever', false)); |
|
127
|
|
|
$this->assertNotNull(app()->make(TicketRepository::class)->getByTicket('what ever')); |
|
128
|
|
|
$this->assertNull(app()->make(TicketRepository::class)->getByTicket('what ever')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testInvalidTicket() |
|
132
|
|
|
{ |
|
133
|
|
|
$mockTicket = Mockery::mock(Ticket::class) |
|
134
|
|
|
->shouldReceive('delete') |
|
135
|
|
|
->andReturn(true) |
|
136
|
|
|
->getMock(); |
|
137
|
|
|
$this->assertTrue(app()->make(TicketRepository::class)->invalidTicket($mockTicket)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testGetAvailableTicket() |
|
141
|
|
|
{ |
|
142
|
|
|
$length = 32; |
|
143
|
|
|
$prefix = 'ST-'; |
|
144
|
|
|
$ticket = 'ticket string'; |
|
145
|
|
|
$ticketGenerator = Mockery::mock(TicketGenerator::class) |
|
146
|
|
|
->shouldReceive('generate') |
|
147
|
|
|
->andReturnUsing( |
|
148
|
|
|
function ($totalLength, $paramPrefix, callable $checkFunc, $maxRetry) use ($length, $prefix, $ticket) { |
|
149
|
|
|
$this->assertEquals($length, $totalLength); |
|
150
|
|
|
$this->assertEquals($prefix, $paramPrefix); |
|
151
|
|
|
$this->assertEquals('getByTicket called', call_user_func_array($checkFunc, [$ticket])); |
|
152
|
|
|
$this->assertEquals(10, $maxRetry); |
|
153
|
|
|
|
|
154
|
|
|
return 'generate called'; |
|
155
|
|
|
} |
|
156
|
|
|
) |
|
157
|
|
|
->once() |
|
158
|
|
|
->getMock(); |
|
159
|
|
|
app()->instance(TicketGenerator::class, $ticketGenerator); |
|
160
|
|
|
$ticketRepository = $this->initTicketRepository() |
|
161
|
|
|
->makePartial() |
|
162
|
|
|
->shouldReceive('getByTicket') |
|
163
|
|
|
->with($ticket, false) |
|
164
|
|
|
->andReturn('getByTicket called') |
|
165
|
|
|
->once() |
|
166
|
|
|
->getMock(); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertEquals( |
|
169
|
|
|
'generate called', |
|
170
|
|
|
self::getNonPublicMethod($ticketRepository, 'getAvailableTicket')->invoke( |
|
171
|
|
|
$ticketRepository, |
|
172
|
|
|
$length, |
|
173
|
|
|
$prefix |
|
174
|
|
|
) |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return Mockery\MockInterface |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function initTicketRepository() |
|
182
|
|
|
{ |
|
183
|
|
|
return Mockery::mock( |
|
184
|
|
|
TicketRepository::class, |
|
185
|
|
|
[app(Ticket::class), app(ServiceRepository::class), app(TicketGenerator::class)] |
|
186
|
|
|
); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|