|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overwatch\ServiceBundle\Tests\Expectation; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Handler\MockHandler; |
|
6
|
|
|
use GuzzleHttp\HandlerStack; |
|
7
|
|
|
use GuzzleHttp\Psr7\Response; |
|
8
|
|
|
use Overwatch\ServiceBundle\Expectation\ToContainTextExpectation; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* ToContainTextExpectationTest |
|
12
|
|
|
*/ |
|
13
|
|
|
class ToContainTextExpectationTest extends \PHPUnit_Framework_TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
const URL = 'http://www.example.com/'; |
|
16
|
|
|
const HAYSTACK = 'Trollface skeptical Fry wat me gusta'; |
|
17
|
|
|
const NEEDLE = 'skep'; |
|
18
|
|
|
const NEEDLE_REGEX = '/\bfr\w\b/i'; |
|
19
|
|
|
const BAD_NEEDLE = 'norris'; |
|
20
|
|
|
const BAD_NEEDLE_REGEX = '/wot\b/'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @expectedException \InvalidArgumentException |
|
24
|
|
|
* @expectedExceptionMessage The actual value provided is not a valid URL |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testInvalidUrl() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->createExpectationWithMockedResponse()->run('8.8.8.8'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
public function testBasicStringMatching() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$expectation = $this->createExpectationWithMockedResponse(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals( |
|
36
|
|
|
'Found the text "' . self::NEEDLE . '" on ' . self::URL, |
|
37
|
|
|
$expectation->run(self::URL, self::NEEDLE) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
43
|
|
|
* @expectedExceptionMessage Expected http://www.example.com/ to contain the text "norris", but wasn't found in the response |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testFailedBasicMatch() |
|
46
|
|
|
{ |
|
47
|
|
|
$expectation = $this->createExpectationWithMockedResponse(); |
|
48
|
|
|
|
|
49
|
|
|
$expectation->run(self::URL, self::BAD_NEEDLE); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
public function testRegexMatching() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$expectation = $this->createExpectationWithMockedResponse(); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals( |
|
57
|
|
|
'Found the text "' . self::NEEDLE_REGEX . '" on ' . self::URL, |
|
58
|
|
|
$expectation->run(self::URL, self::NEEDLE_REGEX) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
64
|
|
|
* @expectedExceptionMessage Expected http://www.example.com/ to contain the text "/wot\b/", but wasn't found in the response |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testFailedRegexMatch() |
|
67
|
|
|
{ |
|
68
|
|
|
$expectation = $this->createExpectationWithMockedResponse(); |
|
69
|
|
|
|
|
70
|
|
|
$expectation->run(self::URL, self::BAD_NEEDLE_REGEX); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
public function testHtmlBasicStringMatching() |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$expectation = $this->createExpectationWithMockedResponse( |
|
76
|
|
|
'<!DOCTYPE html><html><body><h1>Memes</h1><p>' . self::HAYSTACK . '</p></body></html>', |
|
77
|
|
|
'text/html' |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals( |
|
81
|
|
|
'Found the text "' . self::NEEDLE . '" on ' . self::URL, |
|
82
|
|
|
$expectation->run(self::URL, self::NEEDLE) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
88
|
|
|
* @expectedExceptionMessage Expected http://www.example.com/ to contain the text "norris", but wasn't found in the textual content of any element |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function testFailedHtmlBasicMatch() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$expectation = $this->createExpectationWithMockedResponse( |
|
93
|
|
|
'<!DOCTYPE html><html><body><h1>Memes</h1><p>' . self::HAYSTACK . '</p></body></html>', |
|
94
|
|
|
'text/html' |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$expectation->run(self::URL, self::BAD_NEEDLE); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
public function testHtmlErrorRegexMatching() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$expectation = $this->createExpectationWithMockedResponse( |
|
103
|
|
|
'<!DOCTYPE html><html><body><h1>Memes</h1><p>' . self::HAYSTACK . '</p></body></html>', |
|
104
|
|
|
'text/html', |
|
105
|
|
|
500, |
|
106
|
|
|
true |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals( |
|
110
|
|
|
'Found the text "' . self::NEEDLE_REGEX . '" on ' . self::URL, |
|
111
|
|
|
$expectation->run(self::URL, self::NEEDLE_REGEX) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @expectedException GuzzleHttp\Exception\ServerException |
|
117
|
|
|
* @expectedExceptionMessage Server error: `GET http://www.example.com/` resulted in a `500 Internal Server Error` response: |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function testHttpErrorWhenNotAllowed() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$expectation = $this->createExpectationWithMockedResponse( |
|
122
|
|
|
'<!DOCTYPE html><html><body><h1>Memes</h1><p>' . self::HAYSTACK . '</p></body></html>', |
|
123
|
|
|
'text/html', |
|
124
|
|
|
500 |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$expectation->run(self::URL, self::NEEDLE); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function createExpectationWithMockedResponse($body = self::HAYSTACK, $contentType = 'text/plain', $result = 200, $allowErrors = false) |
|
131
|
|
|
{ |
|
132
|
|
|
$mock = new MockHandler([ |
|
133
|
|
|
new Response($result, ['Content-Type' => $contentType], $body), |
|
134
|
|
|
]); |
|
135
|
|
|
|
|
136
|
|
|
$handler = HandlerStack::create($mock); |
|
137
|
|
|
|
|
138
|
|
|
return new ToContainTextExpectation([ |
|
139
|
|
|
'allow_errors' => $allowErrors, |
|
140
|
|
|
'crawlable_types' => ['text/html', 'text/xml'], |
|
141
|
|
|
'timeout' => 5 |
|
142
|
|
|
], ['handler' => $handler]); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.