Completed
Push — feature/EVO-10415-Analytics-wi... ( e14dc6...d15008 )
by Narcotic
130:11 queued 125:50
created

MainControllerTest::testRqlIsIgnored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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