CrudExtensionTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 5 1
A testIndex() 0 7 1
A testView() 0 14 1
A testAdd() 0 21 1
A testEdit() 0 21 1
A testDelete() 0 10 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\Integration\Service\Action\Extension;
13
14
use CakeDC\Api\TestSuite\IntegrationTestCase;
15
use CakeDC\Api\Test\ConfigTrait;
16
use CakeDC\Api\Test\FixturesTrait;
17
use CakeDC\Api\Test\Settings;
18
use Cake\Core\Configure;
19
20
class CrudExtensionTest extends IntegrationTestCase
21
{
22
23
    use ConfigTrait;
24
    use FixturesTrait;
25
26
    /**
27
     * setUp
28
     *
29
     * @return void
30
     */
31
    public function setUp()
32
    {
33
        parent::setUp();
34
        Configure::write('App.fullBaseUrl', 'http://example.com');
35
        $this->_tokenAccess();
36
        $this->_loadDefaultExtensions([]);
37
        $this->getDefaultUser(Settings::USER1);
38
    }
39
40
    /**
41
     * tearDown
42
     *
43
     * @return void
44
     */
45
    public function tearDown()
46
    {
47
        parent::tearDown();
48
        Configure::write('Test.Api.Extension', null);
49
    }
50
51
    public function testIndex()
52
    {
53
        $this->sendRequest('/articles', 'GET');
54
        $result = $this->getJsonResponse();
55
        $this->assertSuccess($result);
56
        $this->assertEquals(15, count($result['data']));
57
    }
58
59
    public function testView()
60
    {
61
        $this->sendRequest('/articles/1', 'GET');
62
        $result = $this->getJsonResponse();
63
        $this->assertSuccess($result);
64
        $article = [
65
            'id' => 1,
66
            'author_id' => 1,
67
            'title' => 'First Article',
68
            'body' => 'First Article Body',
69
            'published' => 'Y'
70
        ];
71
        $this->assertEquals($article, $result['data']);
72
    }
73
74
    public function testAdd()
75
    {
76
        $article = [
77
            'author_id' => 15,
78
            'title' => 'New Article',
79
            'body' => 'New Article Body',
80
            'published' => 'Y'
81
        ];
82
        $this->sendRequest('/articles', 'POST', $article);
83
        $result = $this->getJsonResponse();
84
        $this->assertSuccess($result);
85
        $this->assertArrayHasKey('id', $result['data']);
86
        $id = $result['data']['id'];
87
88
        $this->sendRequest('/articles/' . $id, 'GET');
89
        $result = $this->getJsonResponse();
90
        $this->assertSuccess($result);
91
92
        $res = array_intersect_key($article, $result['data']);
93
        $this->assertEquals($article, $res);
94
    }
95
96
    public function testEdit()
97
    {
98
        $article = [
99
            'author_id' => 15,
100
            'title' => 'New Article',
101
            'body' => 'New Article Body',
102
            'published' => 'Y'
103
        ];
104
        $this->sendRequest('/articles/1', 'PUT', $article);
105
        $result = $this->getJsonResponse();
106
        $this->assertSuccess($result);
107
        $this->assertArrayHasKey('id', $result['data']);
108
        $this->assertEquals(1, $result['data']['id']);
109
110
        $this->sendRequest('/articles/1', 'GET');
111
        $result = $this->getJsonResponse();
112
        $this->assertSuccess($result);
113
114
        $res = array_intersect_key($article, $result['data']);
115
        $this->assertEquals($article, $res);
116
    }
117
118
    public function testDelete()
119
    {
120
        $this->sendRequest('/articles/1', 'DELETE');
121
        $result = $this->getJsonResponse();
122
        $this->assertSuccess($result);
123
124
        $this->sendRequest('/articles/1', 'GET');
125
        $result = $this->getJsonResponse();
126
        $this->assertError($result, 404);
127
    }
128
}
129