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