Completed
Push — feature/service-wrapper-for-sw... ( 6a2d19...b59526 )
by Samuel
19:43 queued 12:41
created

SwaggerStrategyTest::callProcessMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6667
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/**
3
 * SwaggerStrategyTest
4
 */
5
6
namespace Graviton\ProxyBundle\Tests\Definition\Loader\DispersalStrategy;
7
8
use Graviton\ProxyBundle\Definition\ApiDefinition;
9
use Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy;
10
11
/**
12
 * tests for the SwaggerStrategy 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 SwaggerStrategyTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var SwaggerStrategy
22
     */
23
    private $sut;
24
25
    /**
26
     * @var /stdClass
27
     */
28
    private $swagger;
0 ignored issues
show
Unused Code introduced by
The property $swagger is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
30
    /**
31
     * @inheritDoc
32
     *
33
     * @return void
34
     */
35
    protected function setUp()
36
    {
37
        $swaggerParserMock = $this->getMockBuilder('\Swagger\Document')
38
            ->disableOriginalConstructor()
39
            ->setMethods(['setDocument'])
40
            ->getMock();
41
        $this->sut = new SwaggerStrategy($swaggerParserMock);
42
43
44
        /*$this->swagger = new \stdClass();
45
        $this->swagger->swagger = "2.0";
46
        $this->swagger->paths = new \stdClass();
47
        $this->swagger->definitions = new \stdClass();
48
        $this->swagger->info = new \stdClass();
49
        $this->swagger->info->title = "test swagger";
50
        $this->swagger->info->version = "1.0.0";
51
        $this->swagger->basePath = "/api/prefix";
52
        $this->swagger->host = "testapi.local";*/
53
    }
54
55
    /**
56
     * Provides data sets for testSupported()
57
     *
58
     * @return array
59
     */
60
    public function swaggerJsonDataProvider()
61
    {
62
        $basePath = dirname(__FILE__).'/../../../resources/';
63
64
        return array(
65
            array(false, file_get_contents($basePath.'not-supported-swagger.json')),
66
            array(true, file_get_contents($basePath.'simple-swagger.json')),
67
        );
68
    }
69
70
    /**
71
     * test the supports method
72
     *
73
     * @param mixed $result  result
74
     * @param mixed $swagger swagger
75
     *
76
     * @dataProvider swaggerJsonDataProvider
77
     *
78
     * @return void
79
     */
80
    public function testSupported($result, $swagger)
81
    {
82
        $this->assertEquals($result, $this->sut->supports($swagger));
83
    }
84
85
    /**
86
     * test missing fallback data
87
     *
88
     * @expectedException        RuntimeException
89
     * @expectedExceptionMessage Missing mandatory key (host) in fallback data set.
90
     *
91
     * @return void
92
     */
93
    public function testMissingFallbackData()
94
    {
95
        $this->sut->process('{}', array());
96
    }
97
98
    /**
99
     * processing swagger
100
     *
101
     * @return void
102
     */
103
    public function testProcessSwagger()
104
    {
105
        $content = file_get_contents(dirname(__FILE__).'/../../../resources/simple-swagger.json');
106
        $swagger = json_decode($content);
107
        $orderPath = '/order/{orderId}';
108
109
        $swaggerParserMock = $this->getMockBuilder('\Swagger\Document')
110
            ->disableOriginalConstructor()
111
            ->setMethods(['getBasePath', 'setDocument', 'getOperationsById', 'getSchemaResolver'])
112
            ->getMock();
113
114
        $statusCode = 200;
115
        $responseSchema = $swagger->paths->$orderPath->get->responses->$statusCode->schema;
116
        $orderDefinition = $swagger->definitions->Order;
117
        $userDefinition = $swagger->definitions->User;
118
119
        $responseSchemaMock = $this->getMockBuilder('\Swagger\Object\AbstractObject')
120
            ->disableOriginalConstructor()
121
            ->setMethods(['getDocument'])
122
            ->getMockForAbstractClass();
123
        $responseSchemaMock->expects($this->any())
124
            ->method('getDocument')
125
            ->will($this->onConsecutiveCalls($responseSchema, $orderDefinition, $userDefinition));
126
127
        $responseMock = $this->getMockBuilder('\Swagger\Object\Response')
128
            ->disableOriginalConstructor()
129
            ->setMethods(['getSchema'])
130
            ->getMock();
131
        $responseMock->expects($this->once())
132
            ->method('getSchema')
133
            ->willReturn($responseSchemaMock);
134
135
        $responsesMock = $this->getMockBuilder('\Swagger\Object\Responses')
136
            ->disableOriginalConstructor()
137
            ->setMethods(['getHttpStatusCode'])
138
            ->getMock();
139
        $responsesMock->expects($this->once())
140
            ->method('getHttpStatusCode')
141
            ->with($this->equalTo($statusCode))
142
            ->willReturn($responseMock);
143
144
145
        $operationMock = $this->getMockBuilder('\Swagger\Object\Operation')
146
            ->disableOriginalConstructor()
147
            ->setMethods(['getDocumentObjectProperty', 'getResponses'])
148
            ->getMock();
149
        $operationMock->expects($this->once())
150
            ->method('getResponses')
151
            ->willReturn($responsesMock);
152
153
154
        $serviceMock = $this->getMockBuilder('\Swagger\OperationReference')
155
            ->disableOriginalConstructor()
156
            ->setMethods(['getPath', 'getMethod', 'getOperation'])
157
            ->getMock();
158
        $serviceMock->expects($this->exactly(3))
159
            ->method('getPath')
160
            ->will($this->onConsecutiveCalls('/user', '/user', '/order/{orderId}'));
161
        $serviceMock->expects($this->exactly(5))
162
            ->method('getMethod')
163
            ->will($this->onConsecutiveCalls('POST', 'DELETE', 'GET', 'POST', 'GET'));
164
        $serviceMock->expects($this->exactly(2))
165
            ->method('getOperation')
166
            ->willReturn($operationMock);
167
168
169
        $operations = [$serviceMock, $serviceMock, $serviceMock];
170
171
        $schemaResolverMock = $this->getMockBuilder('\Swagger\SchemaResolver')
172
            ->disableOriginalConstructor()
173
            ->setMethods(['resolveReference'])
174
            ->getMock();
175
        $schemaResolverMock->expects($this->exactly(2))
176
            ->method('resolveReference')
177
            ->withAnyParameters()
178
            ->willReturn($responseSchemaMock);
179
180
        $swaggerParserMock
181
            ->expects($this->once())
182
            ->method('getOperationsById')
183
            ->willReturn($operations);
184
        $swaggerParserMock
185
            ->expects($this->exactly(2))
186
            ->method('getSchemaResolver')
187
            ->willReturn($schemaResolverMock);
188
189
        $this->sut = new SwaggerStrategy($swaggerParserMock);
190
191
        $apiDefinition = $this->sut->process($content, array('host' => 'localhost'));
192
        $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $apiDefinition);
193
        $this->assertCount(2, $apiDefinition->getEndpoints());
194
    }
195
}
196