Completed
Pull Request — develop (#273)
by Samuel
19:01 queued 08:39
created

HttpLoaderTest::testLoadWithCache()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 37
rs 8.8571
cc 1
eloc 32
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
10
/**
11
 * tests for the HttpLoader class
12
 *
13
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link    http://swisscom.ch
16
 */
17
class HttpLoaderTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var HttpLoader
21
     */
22
    private $sut;
23
24
    /**
25
     * setup
26
     *
27
     * @return void
28
     */
29
    public function setup()
30
    {
31
        $response = $this->getMockBuilder('Guzzle\Http\Message\Response')
32
            ->setConstructorArgs([200])
33
            ->getMock();
34
        $response
35
            ->expects($this->any())
36
            ->method("getBody")
37
            ->willReturn("{ 'test': 'bablaba' }");
38
        $curlMock = $this->getMock('Guzzle\Common\Collection');
39
        $request = $this->getMockForAbstractClass('Guzzle\Http\Message\RequestInterface');
40
        $request->expects($this->any())
41
            ->method("send")
42
            ->withAnyParameters()
43
            ->willReturn($response);
44
        $request->method('getCurlOptions')
45
            ->willReturn($curlMock);
46
        $client = $this->getMockBuilder('Guzzle\Http\Client')
47
            ->disableOriginalConstructor()
48
            ->getMock();
49
        $client
50
            ->expects($this->any())
51
            ->method('get')
52
            ->willReturn($request);
53
        $validator = $this->getMockForAbstractClass('Symfony\Component\Validator\Validator\ValidatorInterface');
54
55
        $this->sut = new HttpLoader($validator, $client);
56
    }
57
58
    /**
59
     * test the support method
60
     *
61
     * @return void
62
     */
63
    public function testSupports()
64
    {
65
        $client = $this->getMockBuilder('Guzzle\Http\Client')->getMock();
66
        $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')
67
            ->getMockForAbstractClass();
68
        $validator
69
            ->expects($this->once())
70
            ->method('validate')
71
            ->willReturn(array());
72
73
        $sut = new HttpLoader($validator, $client);
74
        $this->assertTrue($sut->supports("test/test.json"));
75
76
        $validatorFail = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')
77
            ->getMockForAbstractClass();
78
        $validatorFail
79
            ->expects($this->once())
80
            ->method('validate')
81
            ->willReturn(array("error text"));
82
83
        $sut = new HttpLoader($validatorFail, $client);
84
        $this->assertFalse($sut->supports("test/again.json"));
85
    }
86
87
    /**
88
     * load method return null
89
     *
90
     * @return HttpLoader
91
     */
92
    public function testLoadReturnNull()
93
    {
94
        $url = "http://localhost/test.json";
95
        $this->assertNull($this->sut->load($url));
96
97
        $mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy')
98
            ->disableOriginalConstructor()
99
            ->setMethods(['supports'])
100
            ->getMock();
101
        $mock
102
            ->expects($this->once())
103
            ->method("supports")
104
            ->willReturn(false);
105
106
        $this->sut->setDispersalStrategy($mock);
107
        $this->assertNull($this->sut->load($url));
108
    }
109
110
    /**
111
     * test the load method
112
     *
113
     * @depends testLoadReturnNull
114
     *
115
     * @return void
116
     */
117
    public function testLoad()
118
    {
119
        $apiDefinition = $this->getMockBuilder('Graviton\ProxyBundle\Definition\ApiDefinition')->getMock();
120
121
        $mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy')
122
            ->disableOriginalConstructor()
123
            ->setMethods(['supports', 'process'])
124
            ->getMock();
125
        $mock
126
            ->expects($this->once())
127
            ->method("supports")
128
            ->willReturn(true);
129
        $mock
130
            ->expects($this->once())
131
            ->method("process")
132
            ->willReturn($apiDefinition);
133
134
        $this->sut->setDispersalStrategy($mock);
135
        $loadedContent = $this->sut->load("http://localhost/test/url/blub");
136
        $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $loadedContent);
137
    }
138
139
    /**
140
     * test a load with cached content
141
     *
142
     * @return void
143
     */
144
    public function testLoadWithCache()
145
    {
146
        $storeKey = 'testSwagger';
147
        $cachedContent = '{"swagger": "2.0"}';
148
        $apiDefinition = $this->getMock('Graviton\ProxyBundle\Definition\ApiDefinition');
149
150
        $mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy')
151
            ->disableOriginalConstructor()
152
            ->setMethods(['supports', 'process'])
153
            ->getMock();
154
        $mock ->expects($this->once())
155
            ->method("supports")
156
            ->willReturn(true);
157
        $mock->expects($this->once())
158
            ->method("process")
159
            ->with($this->equalTo($cachedContent))
160
            ->willReturn($apiDefinition);
161
        $this->sut->setDispersalStrategy($mock);
162
163
        $cacheMock = $this->getMockBuilder('Doctrine\Common\Cache\FilesystemCache')
164
            ->disableOriginalConstructor()
165
            ->setMethods(['contains', 'fetch'])
166
            ->getMock();
167
        $cacheMock->expects($this->once())
168
            ->method('contains')
169
            ->with($this->equalTo($storeKey))
170
            ->will($this->returnValue(true));
171
        $cacheMock->expects($this->once())
172
            ->method('fetch')
173
            ->with($this->equalTo($storeKey))
174
            ->willReturn($cachedContent);
175
        $this->sut->setCache($cacheMock, 'ProxyBundle', 1234);
176
        $this->sut->setOptions(['prefix' => $storeKey]);
177
178
        $content = $this->sut->load("http://localhost/test/blablabla");
179
        $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $content);
180
    }
181
}
182