-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugTestCase.php
More file actions
119 lines (107 loc) · 3.62 KB
/
PlugTestCase.php
File metadata and controls
119 lines (107 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php declare(strict_types=1);
/**
* HyperPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace modethirteen\Http\Tests;
use modethirteen\Http\Content\JsonContent;
use modethirteen\Http\Content\TextContent;
use modethirteen\Http\Content\XmlContent;
use modethirteen\Http\Exception\PlugUriHostRequiredException;
use modethirteen\Http\Headers;
use modethirteen\Http\Plug;
use modethirteen\Http\Mock\MockPlug;
use modethirteen\Http\Mock\MockRequestMatcher;
use modethirteen\Http\Parser\JsonParser;
use modethirteen\Http\StringUtil;
use modethirteen\Http\XUri;
use modethirteen\XArray\XArray;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class PlugTestCase extends TestCase {
#region Test data providers
/**
* @return array
*/
public static function content_dataProvider() : array {
return [
'text' => [new TextContent('foo')],
'json' => [JsonContent::newFromArray(['foo' => ['bar', 'baz']])],
'xml' => [XmlContent::newFromArray(['foo' => ['bar' => ['@id' => 'qux']]])]
];
}
#endregion
public function setUp() : void {
parent::setUp();
MockRequestMatcher::setIgnoredHeaderNames([
Headers::HEADER_CONTENT_LENGTH
]);
}
public function tearDown() : void {
parent::tearDown();
MockRequestMatcher::setIgnoredHeaderNames([]);
MockRequestMatcher::setIgnoredQueryParamNames([]);
MockPlug::deregisterAll();
}
/**
* @param string $class
* @return MockObject
*/
protected function newMock(string $class) : MockObject {
return $this->getMockBuilder($class)
->addMethods(get_class_methods($class))
->disableOriginalConstructor()
->getMock();
}
/**
* @param string $method
* @param XUri $uri
* @return MockRequestMatcher
*/
protected function newDefaultMockRequestMatcher(string $method, XUri $uri) : MockRequestMatcher {
return new MockRequestMatcher($method, $uri);
}
/**
* Return a new HyperPlug instance configured for httpbin.org
*
* @return Plug
* @throws PlugUriHostRequiredException
*/
protected function newHttpBinPlug() : Plug {
$text = getenv('HTTPBIN_BASEURI');
if($text === false || StringUtil::isNullOrEmpty($text)) {
$text = 'https://httpbin.org';
}
$uri = XUri::tryParse($text);
return (new Plug($uri))->withResultParser(new JsonParser());
}
/**
* Assert that all registered mock plug invocations were called
*/
protected function assertAllMockPlugMocksCalled() {
if(!MockPlug::verifyAll()) {
$this->fail('Failed asserting that all MockPlug mocks were called');
}
}
/**
* Assert that an array contains a specified key with the specified value
*
* @param string $key
* @param mixed $expected
* @param array $array
*/
protected function assertArrayHasKeyValue(string $key, $expected, array $array) {
$this->assertEquals($expected, (new XArray($array))->getVal($key));
}
}