Completed
Push — master ( d4cdf1...85bc2e )
by
unknown
11s
created

testNon200ErrorCodesAreHandled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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(
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<BringYourOwnIdeas...portedAddonsLoaderTest>.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<BringYourOwnIdeas...portedAddonsLoaderTest>.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<BringYourOwnIdeas...portedAddonsLoaderTest>.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<BringYourOwnIdeas...portedAddonsLoaderTest>.

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.

Loading history...
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