JsonRendererTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 140
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 5 1
A testRendererInitializeByClassName() 0 18 1
A testRendererSuccess() 0 39 1
A testRendererError() 0 31 1
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Test\TestCase\Service\Renderer;
13
14
use CakeDC\Api\Exception\UnauthenticatedException;
15
use CakeDC\Api\Service\Action\Result;
16
use CakeDC\Api\Service\FallbackService;
17
use CakeDC\Api\Service\Renderer\JsonRenderer;
18
use CakeDC\Api\Service\Service;
19
use CakeDC\Api\TestSuite\TestCase;
20
use CakeDC\Api\Test\ConfigTrait;
21
use CakeDC\Api\Test\FixturesTrait;
22
use Cake\Core\Configure;
23
24
class JsonRendererTest extends TestCase
25
{
26
27
    use ConfigTrait;
28
    use FixturesTrait;
29
30
    /**
31
     * @var Service
32
     */
33
    public $Service;
34
35
    /**
36
     * setUp method
37
     *
38
     * @return void
39
     */
40
    public function setUp()
41
    {
42
        parent::setUp();
43
44
        $this->_initializeRequest();
45
    }
46
47
    /**
48
     * tearDown method
49
     *
50
     * @return void
51
     */
52
    public function tearDown()
53
    {
54
        unset($this->Action);
55
        parent::tearDown();
56
    }
57
58
    /**
59
     * Test initialize
60
     *
61
     * @return void
62
     */
63
    public function testRendererInitializeByClassName()
64
    {
65
        $response = $this
66
            ->getMockBuilder('Cake\Http\Response')
67
            ->setMethods(['withStatus', 'withType', 'withStringBody'])
68
            ->getMock();
69
70
        $this->_initializeRequest([], 'GET', ['response' => $response]);
71
        $serviceOptions = [
72
            'version' => null,
73
            'request' => $this->request,
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
            'response' => $response,
75
            'rendererClass' => 'CakeDC/Api.Json'
76
        ];
77
        $this->Service = new FallbackService($serviceOptions);
78
        $renderer = $this->Service->getRenderer();
79
        $this->assertTrue($renderer instanceof JsonRenderer);
80
    }
81
82
    /**
83
     * Test render response
84
     *
85
     * @return void
86
     */
87
    public function testRendererSuccess()
88
    {
89
        Configure::write('debug', 0);
90
        $response = $this
91
            ->getMockBuilder('Cake\Http\Response')
92
            ->setMethods(['withStatus', 'withType', 'withStringBody'])
93
            ->getMock();
94
95
        $this->_initializeRequest([], 'GET', ['response' => $response]);
96
        $serviceOptions = [
97
            'version' => null,
98
            'request' => $this->request,
99
            'response' => $response,
100
            'rendererClass' => 'CakeDC/Api.Json'
101
        ];
102
        $this->Service = new FallbackService($serviceOptions);
103
104
        $result = new Result();
105
        $statusCode = 200;
106
        $result->setCode($statusCode);
107
        $data = ['id' => 1, 'name' => 'alex'];
108
        $result->setData($data);
109
        $renderer = $this->Service->getRenderer();
110
111
        $response->expects($this->once())
112
                 ->method('withStatus')
113
                 ->with($statusCode)
114
                 ->will($this->returnValue($response));
115
        $response->expects($this->once())
116
                 ->method('withStringBody')
117
                ->with('{"id":1,"name":"alex"}')
118
                ->will($this->returnValue($response));
119
        $response->expects($this->once())
120
                 ->method('withType')
121
                 ->with('application/json')
122
                ->will($this->returnValue($response));
123
124
        $renderer->response($result);
125
    }
126
127
    /**
128
     * Test render error
129
     *
130
     * @return void
131
     */
132
    public function testRendererError()
133
    {
134
        $response = $this
135
            ->getMockBuilder('Cake\Http\Response')
136
            ->setMethods(['withStatus', 'withType', 'withStringBody'])
137
            ->getMock();
138
139
        $this->_initializeRequest([], 'GET', ['response' => $response]);
140
        $serviceOptions = [
141
            'version' => null,
142
            'request' => $this->request,
143
            'response' => $response,
144
            'rendererClass' => 'CakeDC/Api.Json'
145
        ];
146
        $this->Service = new FallbackService($serviceOptions);
147
148
        Configure::write('debug', 0);
149
        $error = new UnauthenticatedException();
150
        $renderer = $this->Service->getRenderer();
151
152
        $response->expects($this->once())
153
            ->method('withStringBody')
154
            ->with('{"error":{"code":401,"message":"Unauthenticated"}}')
155
            ->will($this->returnValue($response));
156
        $response->expects($this->once())
157
            ->method('withType')
158
            ->with('application/json')
159
            ->will($this->returnValue($response));
160
161
        $renderer->error($error);
162
    }
163
}
164