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

MainControllerTest::testVersionHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * functional test for /core/app
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\CoreBundle\Service\CoreUtils;
9
use Graviton\TestBundle\Test\RestTestCase;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Basic functional test for /.
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class MainControllerTest extends RestTestCase
20
{
21
    /**
22
     * @const vendorized app mime type for data
23
     */
24
    const CONTENT_TYPE = 'application/json; charset=UTF-8';
25
    /**
26
     * @const corresponding vendorized schema mime type
27
     */
28
    const SCHEMA_TYPE = 'application/json; charset=UTF-8';
29
30
    /**
31
     * RQL query is ignored
32
     *
33
     * @return void
34
     */
35
    public function testRqlIsIgnored()
36
    {
37
        $client = static::createRestClient();
38
        $client->request('GET', '/?invalidrqlquery');
39
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
40
    }
41
42
    /**
43
     * check if version is returned in header
44
     *
45
     * @return void
46
     */
47
    public function testVersionHeader()
48
    {
49
        $client = static::createRestClient();
50
        $client->request('GET', '/');
51
52
        $composer = new CoreUtils($this->getContainer()->getParameter('graviton.core.version.data'));
53
54
        $response = $client->getResponse();
55
56
        $this->assertEquals($composer->getVersionInHeaderFormat(), $response->headers->get('X-Version'));
57
    }
58
59
    /**
60
     * check for app link in header
61
     *
62
     * @return void
63
     */
64 View Code Duplication
    public function testAppsLink()
65
    {
66
        $client = static::createRestClient();
67
        $client->request('GET', '/');
68
69
        $response = $client->getResponse();
70
71
        $this->assertContains(
72
            '<http://localhost/core/app/>; rel="apps"; type="application/json"',
73
            $response->headers->get('Link')
74
        );
75
    }
76
77
    /**
78
     * check for response contents.
79
     *
80
     * @return void
81
     */
82
    public function testRequestBody()
83
    {
84
        $client = static::createRestClient();
85
        $client->request('GET', '/');
86
87
        $results = $client->getResults();
88
89
        $this->assertEquals(
90
            'Please look at the Link headers of this response for further information.',
91
            $results->message
92
        );
93
94
        $this->assertInternalType('array', $results->services);
95
96
        $refName = '$ref';
97
        $serviceRefs = array_map(
98
            function ($service) use ($refName) {
99
                return $service->$refName;
100
            },
101
            $results->services
102
        );
103
        $this->assertContains('http://localhost/core/app/', $serviceRefs);
104
105
        $profiles = array_map(
106
            function ($service) {
107
                return $service->profile;
108
            },
109
            $results->services
110
        );
111
        $this->assertContains('http://localhost/schema/core/app/collection', $profiles);
112
    }
113
114
    /**
115
     * Verifies the correct behavior of prepareLinkHeader()
116
     *
117
     * @return void
118
     */
119
    public function testPrepareLinkHeader()
120
    {
121
        $routerDouble = $this->getMockBuilder('\Symfony\Component\Routing\Router')
122
            ->disableOriginalConstructor()
123
            ->setMethods(array('generate'))
124
            ->getMock();
125
        $routerDouble
126
            ->expects($this->once())
127
            ->method('generate')
128
            ->with(
129
                $this->equalTo('graviton.core.rest.app.all'),
130
                $this->isType('array'),
131
                $this->isType('boolean')
132
            )
133
            ->will($this->returnValue("http://localhost/core/app"));
134
135
        $responseDouble = $this->getMock('Symfony\Component\HttpFoundation\Response');
136
        $restUtilsDouble = $this->getMock('Graviton\RestBundle\Service\RestUtilsInterface');
137
        $templateDouble = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
138
        $apiLoaderDouble = $this->getMock('Graviton\ProxyBundle\Service\ApiDefinitionLoader');
139
        $configuration = [
140
            'petstore' => [
141
                'prefix' => 'petstore',
142
                'uri' => 'http://petstore.swagger.io/v2/swagger.json'
143
            ]
144
        ];
145
146
        $controller = $this->getProxyBuilder('\Graviton\CoreBundle\Controller\MainController')
147
            ->setConstructorArgs(
148
                [$routerDouble, $responseDouble, $restUtilsDouble, $templateDouble, $apiLoaderDouble, $configuration]
149
            )
150
            ->setMethods(array('prepareLinkHeader'))
151
            ->getProxy();
152
153
        $this->assertEquals(
154
            '<http://localhost/core/app>; rel="apps"; type="application/json"',
155
            $controller->prepareLinkHeader()
156
        );
157
    }
158
159
    /**
160
     * Verifies the correct behavior of determineServices()
161
     *
162
     * @return void
163
     */
164
    public function testDetermineServices()
165
    {
166
        $services = [
167
            [
168
                '$ref'    => 'http://localhost/core/product/',
169
                'profile' => 'http://localhost/schema/core/product/collection'
170
            ],
171
            [
172
                '$ref'    => 'http://localhost/core/app/',
173
                'profile' => 'http://localhost/schema/core/app/collection'
174
            ],
175
        ];
176
177
        $routerDouble = $this->getMockBuilder('\Symfony\Component\Routing\Router')
178
            ->disableOriginalConstructor()
179
            ->setMethods(array('generate'))
180
            ->getMock();
181
        $routerDouble
182
            ->expects($this->exactly(4))
183
            ->method('generate')
184
            ->with(
185
                $this->isType('string'),
186
                $this->isType('array'),
187
                $this->isType('boolean')
188
            )
189
            ->will(
190
                $this->onConsecutiveCalls(
191
                    $this->returnValue($services[0]['$ref']),
192
                    $this->returnValue($services[0]['profile']),
193
                    $this->returnValue($services[1]['$ref']),
194
                    $this->returnValue($services[1]['profile'])
195
                )
196
            );
197
198
        $responseDouble = $this->getMock('Symfony\Component\HttpFoundation\Response');
199
        $restUtilsDouble = $this->getMock('Graviton\RestBundle\Service\RestUtilsInterface');
200
        $templateDouble = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
201
        $apiLoaderDouble = $this->getMock('Graviton\ProxyBundle\Service\ApiDefinitionLoader');
202
        $configuration = [
203
            'petstore' => [
204
                'prefix' => 'petstore',
205
                'uri' => 'http://petstore.swagger.io/v2/swagger.json'
206
            ]
207
        ];
208
209
        $optionRoutes = [
210
            "graviton.core.rest.app.options"     => $routerDouble,
211
            "graviton.core.rest.product.options" => $routerDouble,
212
        ];
213
214
        $controller = $this->getProxyBuilder('\Graviton\CoreBundle\Controller\MainController')
215
            ->setConstructorArgs(
216
                [
217
                    $routerDouble,
218
                    $responseDouble,
219
                    $restUtilsDouble,
220
                    $templateDouble,
221
                    $apiLoaderDouble,
222
                    [],
223
                    [],
224
                    $configuration,
225
                ]
226
            )
227
            ->setMethods(array('determineServices'))
228
            ->getProxy();
229
230
        $this->assertEquals(
231
            [
232
                [
233
                    '$ref'    => 'http://localhost/core/app/',
234
                    'profile' => 'http://localhost/schema/core/app/collection'
235
                ],
236
                [
237
                    '$ref'    => 'http://localhost/core/product/',
238
                    'profile' => 'http://localhost/schema/core/product/collection'
239
                ],
240
            ],
241
            $controller->determineServices($optionRoutes)
242
        );
243
    }
244
245
    /**
246
     * @return void
247
     */
248 View Code Duplication
    public function testOptionsResponse()
249
    {
250
        $client = static::createRestClient();
251
        $client->request('OPTIONS', '/');
252
253
        $response = $client->getResponse();
254
255
        $this->assertContains(
256
            'If-None-Match',
257
            $response->headers->get('Access-Control-Allow-Headers')
258
        );
259
    }
260
}
261