1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\Maintenance\Tests\Util; |
4
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\Maintenance\Util\SupportedAddonsLoader; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Handler\MockHandler; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use PHPUnit_Framework_TestCase; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use SapphireTest; |
13
|
|
|
use Zend_Cache_Core; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @mixin PHPUnit_Framework_TestCase |
17
|
|
|
*/ |
18
|
|
|
class SupportedAddonsLoaderTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
public function testNon200ErrorCodesAreHandled() |
21
|
|
|
{ |
22
|
|
|
$loader = $this->getSupportedAddonsLoader(); |
23
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response(404))); |
24
|
|
|
|
25
|
|
|
$this->setExpectedException( |
|
|
|
|
26
|
|
|
RuntimeException::class, |
27
|
|
|
'Could not obtain information about supported addons. Error code 404' |
28
|
|
|
); |
29
|
|
|
$loader->getAddonNames(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testNonJsonResponsesAreHandled() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$loader = $this->getSupportedAddonsLoader(); |
35
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
36
|
|
|
200, |
37
|
|
|
['Content-Type' => 'text/html; charset=utf-8'] |
38
|
|
|
))); |
39
|
|
|
|
40
|
|
|
$this->setExpectedException( |
|
|
|
|
41
|
|
|
RuntimeException::class, |
42
|
|
|
'Could not obtain information about supported addons. Response is not JSON' |
43
|
|
|
); |
44
|
|
|
$loader->getAddonNames(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testUnsuccessfulResponsesAreHandled() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$loader = $this->getSupportedAddonsLoader(); |
50
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
51
|
|
|
200, |
52
|
|
|
['Content-Type' => 'application/json'], |
53
|
|
|
json_encode(['success' => 'false']) |
54
|
|
|
))); |
55
|
|
|
|
56
|
|
|
$this->setExpectedException( |
|
|
|
|
57
|
|
|
RuntimeException::class, |
58
|
|
|
'Could not obtain information about supported addons. Response returned unsuccessfully' |
59
|
|
|
); |
60
|
|
|
$loader->getAddonNames(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function testAddonsAreParsedAndReturnedCorrectly() |
65
|
|
|
{ |
66
|
|
|
$fakeAddons = ['foo/bar', 'bin/baz']; |
67
|
|
|
|
68
|
|
|
$loader = $this->getSupportedAddonsLoader(); |
69
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
70
|
|
|
200, |
71
|
|
|
['Content-Type' => 'application/json'], |
72
|
|
|
json_encode(['success' => true, 'addons' => $fakeAddons]) |
73
|
|
|
))); |
74
|
|
|
|
75
|
|
|
$addons = $loader->getAddonNames(); |
76
|
|
|
|
77
|
|
|
$this->assertSame($fakeAddons, $addons); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testCacheControlSettingsAreRespected() |
81
|
|
|
{ |
82
|
|
|
$fakeAddons = ['foo/bar', 'bin/baz']; |
83
|
|
|
|
84
|
|
|
$cacheMock = $this->getMockBuilder(Zend_Cache_Core::class) |
|
|
|
|
85
|
|
|
->setMethods(['load', 'save']) |
86
|
|
|
->getMock(); |
87
|
|
|
|
88
|
|
|
$cacheMock->expects($this->once())->method('load')->will($this->returnValue(false)); |
|
|
|
|
89
|
|
|
$cacheMock->expects($this->once()) |
|
|
|
|
90
|
|
|
->method('save') |
91
|
|
|
->with($fakeAddons, $this->anything(), [], 5000, $this->anything()) |
|
|
|
|
92
|
|
|
->will($this->returnValue(true)); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$loader = $this->getSupportedAddonsLoader($cacheMock); |
95
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
96
|
|
|
200, |
97
|
|
|
['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=5000'], |
98
|
|
|
json_encode(['success' => true, 'addons' => $fakeAddons]) |
99
|
|
|
))); |
100
|
|
|
|
101
|
|
|
$loader->getAddonNames(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testCachedAddonsAreUsedWhenAvailable() |
105
|
|
|
{ |
106
|
|
|
$fakeAddons = ['foo/bar', 'bin/baz']; |
107
|
|
|
|
108
|
|
|
$cacheMock = $this->getMockBuilder(Zend_Cache_Core::class) |
|
|
|
|
109
|
|
|
->setMethods(['load', 'save']) |
110
|
|
|
->getMock(); |
111
|
|
|
|
112
|
|
|
$cacheMock->expects($this->once())->method('load')->will($this->returnValue($fakeAddons)); |
|
|
|
|
113
|
|
|
$loader = $this->getSupportedAddonsLoader($cacheMock); |
114
|
|
|
|
115
|
|
|
$mockClient = $this->getMockBuilder(Client::class)->setMethods(['send'])->getMock(); |
|
|
|
|
116
|
|
|
$mockClient->expects($this->never())->method('send'); |
|
|
|
|
117
|
|
|
$loader->setGuzzleClient($mockClient); |
118
|
|
|
|
119
|
|
|
$addons = $loader->getAddonNames(); |
120
|
|
|
|
121
|
|
|
$this->assertSame($fakeAddons, $addons); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Response $withResponse |
126
|
|
|
* @return Client |
127
|
|
|
*/ |
128
|
|
|
protected function getMockClient(Response $withResponse) |
129
|
|
|
{ |
130
|
|
|
$mock = new MockHandler([ |
131
|
|
|
$withResponse |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
$handler = HandlerStack::create($mock); |
135
|
|
|
return new Client(['handler' => $handler]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function getSupportedAddonsLoader($cacheMock = false) |
139
|
|
|
{ |
140
|
|
|
if (!$cacheMock) { |
141
|
|
|
$cacheMock = $this->getMockBuilder(Zend_Cache_Core::class) |
|
|
|
|
142
|
|
|
->setMethods(['load', 'save']) |
143
|
|
|
->getMock(); |
144
|
|
|
$cacheMock->expects($this->any())->method('load')->will($this->returnValue(false)); |
|
|
|
|
145
|
|
|
$cacheMock->expects($this->any())->method('save')->will($this->returnValue(true)); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$loader = new SupportedAddonsLoader; |
149
|
|
|
$loader->setCache($cacheMock); |
150
|
|
|
|
151
|
|
|
return $loader; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.