Issues (153)

Tests/Unit/Controller/JsonapiControllerTest.php (1 issue)

1
<?php
2
3
4
namespace Aimeos\Aimeos\Tests\Unit\Controller;
5
6
7
class JsonapiControllerTest
8
    extends \TYPO3\CMS\Core\Tests\UnitTestCase
9
{
10
    private $object;
11
    private $request;
12
    private $response;
13
    private $uriBuilder;
14
15
16
    public function setUp()
17
    {
18
        \Aimeos\Aimeos\Base::aimeos(); // initialize autoloader
19
20
        $this->object = $this->getAccessibleMock('Aimeos\\Aimeos\\Controller\\JsonapiController', array('dummy'));
21
22
        $this->request = $this->getMockBuilder('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request')
23
            ->setMethods(array('hasArgument', 'getArgument', 'getMethod'))
24
            ->disableOriginalConstructor()
25
            ->getMock();
26
27
        $this->response = $this->getMockBuilder('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response')
28
            ->setMethods(array('setStatus'))
29
            ->disableOriginalConstructor()
30
            ->getMock();
31
32
        $this->uriBuilder = $this->getMockBuilder('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder')
33
            ->setMethods(array('buildFrontendUri'))
34
            ->disableOriginalConstructor()
35
            ->getMock();
36
37
        if (method_exists($response, 'setRequest')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response seems to be never defined.
Loading history...
38
            $response->setRequest($this->request);
39
        }
40
41
        $this->object->_set('uriBuilder', $this->uriBuilder);
42
        $this->object->_set('response', $this->response);
43
        $this->object->_set('request', $this->request);
44
45
        $this->object->_call('initializeAction');
46
    }
47
48
49
    public function tearDown()
50
    {
51
        unset($this->object, $this->request, $this->response);
52
    }
53
54
55
    /**
56
     * @test
57
     */
58
    public function optionsAction()
59
    {
60
        $this->request->expects($this->exactly(1))->method('getMethod')
61
        ->will($this->returnValue('OPTIONS'));
62
63
        $this->request->expects($this->atLeastOnce())->method('hasArgument')
64
        ->will($this->returnValue(true));
65
66
        $this->request->expects($this->any())->method('getArgument')
67
        ->will($this->onConsecutiveCalls('', 'unittest', null, null, 'unittest', null, null));
68
69
        $this->response->expects($this->exactly(1))->method('setStatus')
70
        ->with($this->equalTo(200));
71
72
        $result = $this->object->indexAction();
73
        $json = json_decode($result, true);
74
75
        $this->assertNotEquals(null, $json);
76
        $this->assertArrayHasKey('meta', $json);
77
        $this->assertArrayHasKey('resources', $json['meta']);
78
        $this->assertGreaterThan(1, count($json['meta']['resources']));
79
        $this->assertArrayHasKey('product', $json['meta']['resources']);
80
    }
81
82
83
    /**
84
     * @test
85
     */
86
    public function getAction()
87
    {
88
        $this->request->expects($this->exactly(1))->method('getMethod')
89
            ->will($this->returnValue('GET'));
90
91
        $this->request->expects($this->atLeastOnce())->method('hasArgument')
92
            ->will($this->returnValue(true));
93
94
        $this->request->expects($this->any())->method('getArgument')
95
            ->will($this->onConsecutiveCalls('product', 'unittest', null, null, 'unittest', null, null));
96
97
        $this->response->expects($this->exactly(1))->method('setStatus')
98
            ->with($this->equalTo(200));
99
100
        $this->uriBuilder->expects($this->any())->method('buildFrontendUri')
101
            ->will($this->returnValue('http://localhost/'));
102
103
        $result = $this->object->indexAction();
104
        $json = json_decode($result, true);
105
106
        $this->assertNotEquals(null, $json);
107
    }
108
109
}