1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the hogosha-monitor package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Client; |
17
|
|
|
|
18
|
|
|
use GuzzleHttp\HandlerStack; |
19
|
|
|
use GuzzleHttp\Handler\MockHandler; |
20
|
|
|
use GuzzleHttp\Psr7\Response; |
21
|
|
|
use Hogosha\Monitor\Model\Result; |
22
|
|
|
use Hogosha\Monitor\Model\ResultCollection; |
23
|
|
|
use Hogosha\Monitor\Model\UrlInfo; |
24
|
|
|
use Hogosha\Monitor\Model\UrlProvider; |
25
|
|
|
use Hogosha\Monitor\Runner\Runner; |
26
|
|
|
use Hogosha\Monitor\Validator\Validator; |
27
|
|
|
use Webmozart\Console\IO\BufferedIO; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Guillaume Cavana <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class RunnerTest extends \PHPUnit_Framework_TestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var BufferedIO |
36
|
|
|
*/ |
37
|
|
|
private $io; |
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->io = new BufferedIO(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* testRunner. |
46
|
|
|
*/ |
47
|
|
|
public function testRunnerWithValidator() |
48
|
|
|
{ |
49
|
|
|
$urlProvider = new UrlProvider([ |
50
|
|
|
'urls' => [ |
51
|
|
|
'google' => [ |
52
|
|
|
'url' => 'https://www.google.fr', |
53
|
|
|
'method' => 'GET', |
54
|
|
|
'headers' => [], |
55
|
|
|
'timeout' => 1, |
56
|
|
|
'validator' => ['type' => 'html', 'match' => '/test/'], |
57
|
|
|
'status_code' => 200, |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$client = GuzzleClient::createClient($this->io, ['handler' => $this->mockClient()]); |
63
|
|
|
$runner = new Runner($urlProvider, $client); |
64
|
|
|
$resultCollection = $runner->run(); |
65
|
|
|
|
66
|
|
|
$this->assertCount(1, $urlProvider->getUrls()); |
67
|
|
|
$this->assertInstanceOf(UrlInfo::class, $urlProvider->getUrls()['google']); |
68
|
|
|
$this->assertInstanceOf(ResultCollection::class, $resultCollection); |
69
|
|
|
$this->assertInstanceOf(Result::class, $resultCollection['google']); |
70
|
|
|
$this->assertEquals((new Result($this->createUrlInfo(), 200, 0, null, true)), $resultCollection['google']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testRunnerWithoutValidator() |
74
|
|
|
{ |
75
|
|
|
$urlProvider = new UrlProvider([ |
76
|
|
|
'urls' => [ |
77
|
|
|
'google' => [ |
78
|
|
|
'url' => 'https://www.google.fr', |
79
|
|
|
'method' => 'GET', |
80
|
|
|
'headers' => [], |
81
|
|
|
'timeout' => 1, |
82
|
|
|
'validator' => [], |
83
|
|
|
'status_code' => 200, |
84
|
|
|
], |
85
|
|
|
], |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$client = GuzzleClient::createClient($this->io, ['handler' => $this->mockClient()]); |
89
|
|
|
$runner = new Runner($urlProvider, $client); |
90
|
|
|
$resultCollection = $runner->run(); |
91
|
|
|
|
92
|
|
|
$this->assertCount(1, $urlProvider->getUrls()); |
93
|
|
|
$this->assertInstanceOf(UrlInfo::class, $urlProvider->getUrls()['google']); |
94
|
|
|
$this->assertInstanceOf(ResultCollection::class, $resultCollection); |
95
|
|
|
$this->assertInstanceOf(Result::class, $resultCollection['google']); |
96
|
|
|
$this->assertEquals((new Result($this->createUrlInfo(false), 200, 0, null, null)), $resultCollection['google']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* mockClient. |
101
|
|
|
* |
102
|
|
|
* @return MockHandler |
103
|
|
|
*/ |
104
|
|
|
private function mockClient() |
105
|
|
|
{ |
106
|
|
|
$mock = new MockHandler([ |
107
|
|
|
new Response(200, [], 'test'), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
return HandlerStack::create($mock); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function createUrlInfo($validator = true) |
114
|
|
|
{ |
115
|
|
|
return new UrlInfo( |
116
|
|
|
'google', |
117
|
|
|
'https://www.google.fr', |
118
|
|
|
'GET', |
119
|
|
|
[], |
120
|
|
|
1, |
121
|
|
|
200, |
122
|
|
|
$validator ? |
123
|
|
|
(new Validator(['type' => 'html', 'match' => '/test/'])) : |
124
|
|
|
(new Validator()), |
125
|
|
|
null, |
126
|
|
|
null |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|