Completed
Pull Request — develop (#273)
by Samuel
23:34 queued 09:33
created

HttpLoaderTest::testSupports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 9.0857
cc 1
eloc 18
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
        $request = $this->getMockForAbstractClass('Guzzle\Http\Message\RequestInterface');
39
        $request
40
            ->expects($this->any())
41
            ->method("send")
42
            ->withAnyParameters()
43
            ->willReturn($response);
44
        $client = $this->getMockBuilder('Guzzle\Http\Client')
45
            ->disableOriginalConstructor()
46
            ->getMock();
47
        $client
48
            ->expects($this->any())
49
            ->method('get')
50
            ->willReturn($request);
51
        $validator = $this->getMockForAbstractClass('Symfony\Component\Validator\Validator\ValidatorInterface');
52
53
        $this->sut = new HttpLoader($validator, $client);
54
    }
55
56
    /**
57
     * test the support method
58
     *
59
     * @return void
60
     */
61
    public function testSupports()
62
    {
63
        $client = $this->getMockBuilder('Guzzle\Http\Client')->getMock();
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);
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);
82
        $this->assertFalse($sut->supports("test/again.json"));
83
    }
84
85
    /**
86
     * load method return null
87
     *
88
     * @return HttpLoader
89
     */
90
    public function testLoadReturnNull()
91
    {
92
        $url = "http://localhost/test.json";
93
        $this->assertNull($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->assertNull($this->sut->load($url));
106
    }
107
108
    /**
109
     * test the load method
110
     *
111
     * @depends testLoadReturnNull
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