Completed
Push — master ( 85bc2e...01fbb4 )
by Robbie
01:09 queued 01:07
created

testCacheControlSettingsAreRespected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 16
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
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(
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...
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()
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...
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(
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...
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()
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...
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(
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...
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);
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...
78
    }
79
80
    public function testCacheControlSettingsAreRespected()
81
    {
82
        $fakeAddons = ['foo/bar', 'bin/baz'];
83
84
        $cacheMock = $this->getMockBuilder(Zend_Cache_Core::class)
0 ignored issues
show
Bug introduced by
The method getMockBuilder() 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...
85
            ->setMethods(['load', 'save'])
86
            ->getMock();
87
88
        $cacheMock->expects($this->once())->method('load')->will($this->returnValue(false));
0 ignored issues
show
Bug introduced by
The method once() 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...
Bug introduced by
The method returnValue() 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...
89
        $cacheMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() 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...
90
            ->method('save')
91
            ->with($fakeAddons, $this->anything(), [], 5000, $this->anything())
0 ignored issues
show
Bug introduced by
The method anything() 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...
92
            ->will($this->returnValue(true));
0 ignored issues
show
Bug introduced by
The method returnValue() 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...
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)
0 ignored issues
show
Bug introduced by
The method getMockBuilder() 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...
109
            ->setMethods(['load', 'save'])
110
            ->getMock();
111
112
        $cacheMock->expects($this->once())->method('load')->will($this->returnValue($fakeAddons));
0 ignored issues
show
Bug introduced by
The method once() 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...
Bug introduced by
The method returnValue() 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...
113
        $loader = $this->getSupportedAddonsLoader($cacheMock);
114
115
        $mockClient = $this->getMockBuilder(Client::class)->setMethods(['send'])->getMock();
0 ignored issues
show
Bug introduced by
The method getMockBuilder() 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...
116
        $mockClient->expects($this->never())->method('send');
0 ignored issues
show
Bug introduced by
The method never() 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...
117
        $loader->setGuzzleClient($mockClient);
118
119
        $addons = $loader->getAddonNames();
120
121
        $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...
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)
0 ignored issues
show
Bug introduced by
The method getMockBuilder() 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...
142
                ->setMethods(['load', 'save'])
143
                ->getMock();
144
            $cacheMock->expects($this->any())->method('load')->will($this->returnValue(false));
0 ignored issues
show
Bug introduced by
The method any() 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...
Bug introduced by
The method returnValue() 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...
145
            $cacheMock->expects($this->any())->method('save')->will($this->returnValue(true));
0 ignored issues
show
Bug introduced by
The method any() 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...
Bug introduced by
The method returnValue() 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...
146
        }
147
148
        $loader = new SupportedAddonsLoader;
149
        $loader->setCache($cacheMock);
150
151
        return $loader;
152
    }
153
}
154