CrudRelationsExtensionTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
use Cake\Utility\Hash;
20
21
/**
22
 * Class CrudRelationsExtensionTest
23
 *
24
 * @package CakeDC\Api\Test\TestCase\Integration\Service\Extension
25
 */
26
class CrudRelationsExtensionTest extends IntegrationTestCase
27
{
28
29
    use ConfigTrait;
30
    use FixturesTrait;
31
32
    /**
33
     * setUp
34
     *
35
     * @return void
36
     */
37
    public function setUp()
38
    {
39
        parent::setUp();
40
        Configure::write('App.fullBaseUrl', 'http://example.com');
41
        $this->_tokenAccess();
42
        $this->_loadDefaultExtensions('CakeDC/Api.CrudRelations');
43
        $this->_loadDefaultExtensions('CakeDC/Api.Paginate');
44
        $this->getDefaultUser(Settings::USER1);
45
    }
46
47
    /**
48
     * tearDown
49
     *
50
     * @return void
51
     */
52
    public function tearDown()
53
    {
54
        parent::tearDown();
55
        Configure::write('Test.Api.Extension', null);
56
    }
57
58
    public function testNoInclude()
59
    {
60
        $this->sendRequest('/articles/1', 'GET', ['include_direct' => false]);
61
        $result = $this->getJsonResponse();
62
        $this->assertSuccess($result);
63
        $expected = [
64
            'id' => 1,
65
            'author_id' => 1,
66
            'title' => 'First Article',
67
            'body' => 'First Article Body',
68
            'published' => 'Y'
69
        ];
70
        $this->assertEquals($expected, Hash::get($result, 'data'));
71
    }
72
73
    public function testIncludeDirect()
74
    {
75
        $this->sendRequest('/articles/1', 'GET', ['include_direct' => true]);
76
        $result = $this->getJsonResponse();
77
        $this->assertSuccess($result);
78
        $expected = [
79
            'id' => 1,
80
            'author_id' => 1,
81
            'title' => 'First Article',
82
            'body' => 'First Article Body',
83
            'published' => 'Y',
84
            'author' => [
85
                'id' => 1,
86
                'first_name' => 'Electra',
87
                'last_name' => 'Cronos'
88
            ]
89
        ];
90
        $this->assertEquals($expected, Hash::get($result, 'data'));
91
    }
92
93
    public function testIncludeNoRelations()
94
    {
95
        $this->sendRequest('/articles/1', 'GET', ['include_relations' => '']);
96
        $result = $this->getJsonResponse();
97
        $this->assertSuccess($result);
98
        $expected = [
99
            'id' => 1,
100
            'author_id' => 1,
101
            'title' => 'First Article',
102
            'body' => 'First Article Body',
103
            'published' => 'Y',
104
        ];
105
        $this->assertEquals($expected, Hash::get($result, 'data'));
106
    }
107
108
    public function testIncludeAuthorRelations()
109
    {
110
        $this->sendRequest('/articles/1', 'GET', ['include_relations' => 'authors']);
111
        $result = $this->getJsonResponse();
112
        $this->assertSuccess($result);
113
        $expected = [
114
            'id' => 1,
115
            'author_id' => 1,
116
            'title' => 'First Article',
117
            'body' => 'First Article Body',
118
            'published' => 'Y',
119
            'author' => [
120
                'id' => 1,
121
                'first_name' => 'Electra',
122
                'last_name' => 'Cronos'
123
            ]
124
        ];
125
        $this->assertEquals($expected, Hash::get($result, 'data'));
126
    }
127
}
128