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
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @mixin PHPUnit_Framework_TestCase |
16
|
|
|
*/ |
17
|
|
|
class SupportedAddonsLoaderTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
public function testNon200ErrorCodesAreHandled() |
20
|
|
|
{ |
21
|
|
|
$loader = new SupportedAddonsLoader(); |
22
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response(404))); |
23
|
|
|
|
24
|
|
|
$this->setExpectedException( |
|
|
|
|
25
|
|
|
RuntimeException::class, |
26
|
|
|
'Could not obtain information about supported addons. Error code 404' |
27
|
|
|
); |
28
|
|
|
$loader->getAddonNames(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testNonJsonResponsesAreHandled() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$loader = new SupportedAddonsLoader(); |
34
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
35
|
|
|
200, |
36
|
|
|
['Content-Type' => 'text/html; charset=utf-8'] |
37
|
|
|
))); |
38
|
|
|
|
39
|
|
|
$this->setExpectedException( |
|
|
|
|
40
|
|
|
RuntimeException::class, |
41
|
|
|
'Could not obtain information about supported addons. Response is not JSON' |
42
|
|
|
); |
43
|
|
|
$loader->getAddonNames(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testUnsuccessfulResponsesAreHandled() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$loader = new SupportedAddonsLoader(); |
49
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
50
|
|
|
200, |
51
|
|
|
['Content-Type' => 'application/json'], |
52
|
|
|
json_encode(['success' => 'false']) |
53
|
|
|
))); |
54
|
|
|
|
55
|
|
|
$this->setExpectedException( |
|
|
|
|
56
|
|
|
RuntimeException::class, |
57
|
|
|
'Could not obtain information about supported addons. Response returned unsuccessfully' |
58
|
|
|
); |
59
|
|
|
$loader->getAddonNames(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function testAddonsAreParsedAndReturnedCorrectly() |
64
|
|
|
{ |
65
|
|
|
$fakeAddons = ['foo/bar', 'bin/baz']; |
66
|
|
|
|
67
|
|
|
$loader = new SupportedAddonsLoader(); |
68
|
|
|
$loader->setGuzzleClient($this->getMockClient(new Response( |
69
|
|
|
200, |
70
|
|
|
['Content-Type' => 'application/json'], |
71
|
|
|
json_encode(['success' => true, 'addons' => $fakeAddons]) |
72
|
|
|
))); |
73
|
|
|
|
74
|
|
|
$addons = $loader->getAddonNames(); |
75
|
|
|
|
76
|
|
|
$this->assertSame($fakeAddons, $addons); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Response $withResponse |
81
|
|
|
* @return Client |
82
|
|
|
*/ |
83
|
|
|
protected function getMockClient(Response $withResponse) |
84
|
|
|
{ |
85
|
|
|
$mock = new MockHandler([ |
86
|
|
|
$withResponse |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$handler = HandlerStack::create($mock); |
90
|
|
|
return new Client(['handler' => $handler]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.