1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Effortless maintenance management (http://juliangut.com/janitor) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/janitor for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/janitor/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Janitor\Test; |
11
|
|
|
|
12
|
|
|
use Janitor\Janitor; |
13
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
14
|
|
|
use Zend\Diactoros\Response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers \Janitor\Janitor |
18
|
|
|
*/ |
19
|
|
|
class JanitorTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
protected $janitor; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$watcher = $this->getMock('Janitor\\Watcher'); |
26
|
|
|
$watcher->expects($this->any())->method('isActive')->will($this->returnValue(false)); |
27
|
|
|
|
28
|
|
|
$excluder = $this->getMock('Janitor\\Excluder'); |
29
|
|
|
$excluder->expects($this->any())->method('isExcluded')->will($this->returnValue(false)); |
30
|
|
|
|
31
|
|
|
$this->janitor = new Janitor([$watcher], [$excluder]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers \Janitor\Janitor::getScheduledTimes |
36
|
|
|
*/ |
37
|
|
|
public function testScheduled() |
38
|
|
|
{ |
39
|
|
|
$start = new \DateTime(); |
40
|
|
|
$start->add(new \DateInterval('P1D')); |
41
|
|
|
$end = clone $start; |
42
|
|
|
$end->add(new \DateInterval('PT2H')); |
43
|
|
|
$scheduledTimes = [['start' => $start, 'end' => $end]]; |
44
|
|
|
|
45
|
|
|
$start = new \DateTime(); |
46
|
|
|
$start->add(new \DateInterval('PT2H')); |
47
|
|
|
$end = clone $start; |
48
|
|
|
$end->add(new \DateInterval('PT1H')); |
49
|
|
|
array_unshift($scheduledTimes, ['start' => $start, 'end' => $end]); |
50
|
|
|
|
51
|
|
|
$start = new \DateTime(); |
52
|
|
|
$start->add(new \DateInterval('P1D')); |
53
|
|
|
$start->add(new \DateInterval('PT1H')); |
54
|
|
|
$end = clone $start; |
55
|
|
|
$end->add(new \DateInterval('PT3H')); |
56
|
|
|
$scheduledTimes[] = ['start' => $start, 'end' => $end]; |
57
|
|
|
$scheduledTimes[] = ['start' => $start, 'end' => $end]; |
58
|
|
|
|
59
|
|
|
foreach ($scheduledTimes as $times) { |
60
|
|
|
$watcher = $this->getMock('Janitor\\ScheduledWatcher'); |
61
|
|
|
$watcher->expects($this->any())->method('isActive')->will($this->returnValue(true)); |
62
|
|
|
$watcher->expects($this->any())->method('isScheduled')->will($this->returnValue(true)); |
63
|
|
|
$watcher->expects($this->any())->method('getScheduledTimes')->will($this->returnValue([$times])); |
64
|
|
|
|
65
|
|
|
$this->janitor->addWatcher($watcher); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->assertEquals($scheduledTimes, $this->janitor->getScheduledTimes()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @covers \Janitor\Janitor::__invoke |
73
|
|
|
* @covers \Janitor\Janitor::getActiveWatcher |
74
|
|
|
* @covers \Janitor\Janitor::isExcluded |
75
|
|
|
*/ |
76
|
|
|
public function testNoActiveWatcher() |
77
|
|
|
{ |
78
|
|
|
$janitor = $this->janitor; |
79
|
|
|
|
80
|
|
|
$response = $janitor( |
81
|
|
|
ServerRequestFactory::fromGlobals(), |
82
|
|
|
new Response('php://temp'), |
83
|
|
|
function ($request, $response) { |
84
|
|
|
return $response->withHeader('janitor', 'tested'); |
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
$this->assertEquals('tested', $response->getHeaderLine('janitor')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @covers \Janitor\Janitor::addWatcher |
92
|
|
|
* @covers \Janitor\Janitor::__invoke |
93
|
|
|
* @covers \Janitor\Janitor::getActiveWatcher |
94
|
|
|
* @covers \Janitor\Janitor::isExcluded |
95
|
|
|
* @covers \Janitor\Janitor::getHandler |
96
|
|
|
*/ |
97
|
|
|
public function testDefaultHandler() |
98
|
|
|
{ |
99
|
|
|
$janitor = $this->janitor; |
100
|
|
|
|
101
|
|
|
$watcher = $this->getMock('Janitor\\Watcher'); |
102
|
|
|
$watcher->expects($this->any())->method('isActive')->will($this->returnValue(true)); |
103
|
|
|
$this->janitor->addWatcher($watcher); |
104
|
|
|
|
105
|
|
|
$request = ServerRequestFactory::fromGlobals(); |
106
|
|
|
$request = $request->withHeader('Accept', 'application/json'); |
107
|
|
|
|
108
|
|
|
$response = $janitor( |
109
|
|
|
$request, |
110
|
|
|
new Response('php://temp'), |
111
|
|
|
function () { |
112
|
|
|
} |
113
|
|
|
); |
114
|
|
|
$this->assertTrue(substr($response->getBody(), 0, 1) === '{'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @covers \Janitor\Janitor::addWatcher |
119
|
|
|
* @covers \Janitor\Janitor::__invoke |
120
|
|
|
* @covers \Janitor\Janitor::setHandler |
121
|
|
|
* @covers \Janitor\Janitor::getActiveWatcher |
122
|
|
|
* @covers \Janitor\Janitor::isExcluded |
123
|
|
|
* @covers \Janitor\Janitor::getHandler |
124
|
|
|
*/ |
125
|
|
|
public function testCustomHandler() |
126
|
|
|
{ |
127
|
|
|
$janitor = $this->janitor; |
128
|
|
|
|
129
|
|
|
$watcher = $this->getMock('Janitor\\Watcher'); |
130
|
|
|
$watcher->expects($this->any())->method('isActive')->will($this->returnValue(true)); |
131
|
|
|
$this->janitor->addWatcher($watcher); |
132
|
|
|
|
133
|
|
|
$this->janitor->setHandler(function ($request, $response, $watcher) { |
134
|
|
|
return $response->withHeader('active_watcher', get_class($watcher)); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
$response = $janitor( |
138
|
|
|
ServerRequestFactory::fromGlobals(), |
139
|
|
|
new Response('php://temp'), |
140
|
|
|
function () { |
141
|
|
|
} |
142
|
|
|
); |
143
|
|
|
$this->assertEquals(get_class($watcher), $response->getHeaderLine('active_watcher')); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @covers \Janitor\Janitor::addWatcher |
148
|
|
|
* @covers \Janitor\Janitor::addExcluder |
149
|
|
|
* @covers \Janitor\Janitor::setAttributeName |
150
|
|
|
* @covers \Janitor\Janitor::getAttributeName |
151
|
|
|
* @covers \Janitor\Janitor::__invoke |
152
|
|
|
* @covers \Janitor\Janitor::getActiveWatcher |
153
|
|
|
* @covers \Janitor\Janitor::isExcluded |
154
|
|
|
*/ |
155
|
|
|
public function testIsExcluded() |
156
|
|
|
{ |
157
|
|
|
$janitor = $this->janitor; |
158
|
|
|
|
159
|
|
|
$watcher = $this->getMock('Janitor\\Watcher'); |
160
|
|
|
$watcher->expects($this->any())->method('isActive')->will($this->returnValue(true)); |
161
|
|
|
$this->janitor->addWatcher($watcher); |
162
|
|
|
|
163
|
|
|
$excluder = $this->getMock('Janitor\\Excluder'); |
164
|
|
|
$excluder->expects($this->once())->method('isExcluded')->will($this->returnValue(true)); |
165
|
|
|
$this->janitor->addExcluder($excluder); |
166
|
|
|
|
167
|
|
|
$customAttributeName = 'custom'; |
168
|
|
|
$this->janitor->setAttributeName($customAttributeName); |
169
|
|
|
$this->assertEquals($customAttributeName, $this->janitor->getAttributeName()); |
170
|
|
|
|
171
|
|
|
$response = $janitor( |
172
|
|
|
ServerRequestFactory::fromGlobals(), |
173
|
|
|
new Response('php://temp'), |
174
|
|
|
function ($request, $response) use ($customAttributeName) { |
175
|
|
|
return $response->withHeader( |
176
|
|
|
$customAttributeName, |
177
|
|
|
get_class($request->getAttribute($customAttributeName)) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
); |
181
|
|
|
$this->assertEquals(get_class($watcher), $response->getHeaderLine($customAttributeName)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|