Completed
Push — feature/update-deps ( 7965c1...3f1c64 )
by Narcotic
27:14 queued 22:12
created

HttpLoaderTest::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/**
3
 * HttpLoaderTest
4
 */
5
6
namespace Graviton\ProxyBundle\Tests\Definition\Loader;
7
8
use Graviton\ProxyBundle\Definition\Loader\HttpLoader;
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * tests for the HttpLoader class
13
 *
14
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link    http://swisscom.ch
17
 */
18
class HttpLoaderTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var HttpLoader
22
     */
23
    private $sut;
24
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30
    /**
31
     * setup
32
     *
33
     * @return void
34
     */
35
    public function setup()
36
    {
37
        $response = $this->getMockBuilder('GuzzleHttp\Psr7\Response')
38
            ->getMock();
39
        $response
40
            ->expects($this->any())
41
            ->method("getBody")
42
            ->willReturn("{ 'test': 'bablaba' }");
43
44
        $client = $this->getMockForAbstractClass('Proxy\Adapter\AdapterInterface');
45
        $client->expects($this->any())
46
                ->method("send")
47
                ->withAnyParameters()
48
                ->willReturn($response);
49
50
        $validator = $this->getMockForAbstractClass('Symfony\Component\Validator\Validator\ValidatorInterface');
51
        $this->logger = $this->createMock('Psr\Log\LoggerInterface');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock('Psr\\Log\\LoggerInterface') of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Psr\Log\LoggerInterface> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
53
        $this->sut = new HttpLoader($validator, $client, $this->logger);
54
    }
55
56
    /**
57
     * test the support method
58
     *
59
     * @return void
60
     */
61
    public function testSupports()
62
    {
63
        $client = $this->getMockForAbstractClass('Proxy\Adapter\AdapterInterface');
64
        $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')
65
                          ->getMockForAbstractClass();
66
        $validator
67
            ->expects($this->once())
68
            ->method('validate')
69
            ->willReturn(array());
70
71
        $sut = new HttpLoader($validator, $client, $this->logger);
72
        $this->assertTrue($sut->supports("test/test.json"));
73
74
        $validatorFail = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')
75
            ->getMockForAbstractClass();
76
        $validatorFail
77
            ->expects($this->once())
78
            ->method('validate')
79
            ->willReturn(array("error text"));
80
81
        $sut = new HttpLoader($validatorFail, $client, $this->logger);
82
        $this->assertFalse($sut->supports("test/again.json"));
83
    }
84
85
    /**
86
     * load method return null
87
     *
88
     * @return HttpLoader
0 ignored issues
show
Documentation introduced by
Should the return type not be HttpLoader|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
89
     */
90
    public function testLoadShallNotReturnNull()
91
    {
92
        $url = "http://localhost/test.json";
93
        $this->assertNotNull($this->sut->load($url));
94
95
        $mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy')
96
            ->disableOriginalConstructor()
97
            ->setMethods(['supports'])
98
            ->getMock();
99
        $mock
100
            ->expects($this->once())
101
            ->method("supports")
102
            ->willReturn(false);
103
104
        $this->sut->setDispersalStrategy($mock);
105
        $this->assertNotNull($this->sut->load($url));
106
    }
107
108
    /**
109
     * test the load method
110
     *
111
     * @depends testLoadShallNotReturnNull
112
     *
113
     * @return void
114
     */
115
    public function testLoad()
116
    {
117
        $apiDefinition = $this->getMockBuilder('Graviton\ProxyBundle\Definition\ApiDefinition')->getMock();
118
119
        $mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy')
120
            ->disableOriginalConstructor()
121
            ->setMethods(['supports', 'process'])
122
            ->getMock();
123
        $mock
124
            ->expects($this->once())
125
            ->method("supports")
126
            ->willReturn(true);
127
        $mock
128
            ->expects($this->once())
129
            ->method("process")
130
            ->willReturn($apiDefinition);
131
132
        $this->sut->setDispersalStrategy($mock);
133
        $loadedContent = $this->sut->load("http://localhost/test/url/blub");
134
        $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $loadedContent);
135
    }
136
137
    /**
138
     * test a load with cached content
139
     *
140
     * @return void
141
     */
142
    public function testLoadWithCache()
143
    {
144
        $storeKey = 'testSwagger';
145
        $cachedContent = '{"swagger": "2.0"}';
146
        $apiDefinition = $this->createMock('Graviton\ProxyBundle\Definition\ApiDefinition');
147
148
        $mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy')
149
            ->disableOriginalConstructor()
150
            ->setMethods(['supports', 'process'])
151
            ->getMock();
152
        $mock ->expects($this->once())
153
            ->method("supports")
154
            ->willReturn(true);
155
        $mock->expects($this->once())
156
            ->method("process")
157
            ->with($this->equalTo($cachedContent))
158
            ->willReturn($apiDefinition);
159
        $this->sut->setDispersalStrategy($mock);
160
161
        $cacheMock = $this->getMockBuilder('Doctrine\Common\Cache\FilesystemCache')
162
            ->disableOriginalConstructor()
163
            ->setMethods(['contains', 'fetch'])
164
            ->getMock();
165
        $cacheMock->expects($this->once())
166
            ->method('contains')
167
            ->with($this->equalTo($storeKey))
168
            ->will($this->returnValue(true));
169
        $cacheMock->expects($this->once())
170
            ->method('fetch')
171
            ->with($this->equalTo($storeKey))
172
            ->willReturn($cachedContent);
173
        $this->sut->setCache($cacheMock, 'ProxyBundle', 1234);
174
        $this->sut->setOptions(['prefix' => $storeKey]);
175
176
        $content = $this->sut->load("http://localhost/test/blablabla");
177
        $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $content);
178
    }
179
}
180