Completed
Push — master ( b978b7...f16f4e )
by Joschi
04:05
created

JsonTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 93
rs 10
wmc 8
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 4 1
A setUp() 0 5 1
A testJsonResource() 0 6 1
A testJsonResourceReader() 0 5 1
A testJsonResourceHtmlSubparts() 0 5 1
A testJsonResourceGetData() 0 6 1
A testJsonResourceSetData() 0 7 1
A testJsonEncodingDecoding() 0 4 1
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation Fixture (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Tests;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Resource\Application\Service\JsonUtility;
41
use Apparat\Resource\Infrastructure\Io\InMemory\Reader;
42
use Apparat\Resource\Infrastructure\Model\Part\JsonPart;
43
use Apparat\Resource\Infrastructure\Model\Resource\JsonResource;
44
45
/**
46
 * JSON file tests
47
 *
48
 * @package     Apparat\Resource
49
 * @subpackage  Apparat\Resource\Tests
50
 */
51
class JsonTest extends AbstractDataTest
52
{
53
    /**
54
     * Example JSON data
55
     *
56
     * @var string
57
     */
58
    protected $json = null;
59
60
    /**
61
     * Example JSON file
62
     *
63
     * @var string
64
     */
65
    const JSON_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'Fixture' . DIRECTORY_SEPARATOR . 'invoice.json';
66
67
    /**
68
     * Preparations before the first test is run
69
     */
70
    public static function setUpBeforeClass()
71
    {
72
        \date_default_timezone_set('UTC');
73
    }
74
75
    /**
76
     * Sets up the fixture
77
     */
78
    protected function setUp()
79
    {
80
        parent::setUp();
81
        $this->json = file_get_contents(self::JSON_FILE);
82
    }
83
84
    /**
85
     * Test the JSON file constructor
86
     */
87
    public function testJsonResource()
88
    {
89
        $jsonResource = Kernel::create(JsonResource::class, [null]);
90
        $this->assertInstanceOf(JsonResource::class, $jsonResource);
91
        $this->assertEquals(JsonPart::MEDIA_TYPE, $jsonResource->getMediaTypePart());
92
    }
93
94
    /**
95
     * Test the JSON file constructor with reader
96
     */
97
    public function testJsonResourceReader()
98
    {
99
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
100
        $this->assertEquals($this->json, $jsonResource->getPart());
101
    }
102
103
    /**
104
     * Test getting the data of a JSON file with unallowed subparts
105
     *
106
     * @expectedException \Apparat\Resource\Domain\Model\Part\InvalidArgumentException
107
     * @expectedExceptionCode 1447365624
108
     */
109
    public function testJsonResourceHtmlSubparts()
110
    {
111
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
112
        $jsonResource->getDataPart('a/b/c');
113
    }
114
115
    /**
116
     * Test getting the data of a Json file
117
     */
118
    public function testJsonResourceGetData()
119
    {
120
        $expectedData = include __DIR__ . DIRECTORY_SEPARATOR . 'Fixture' . DIRECTORY_SEPARATOR . 'invoice.php';
121
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
122
        $this->assertArrayEquals($expectedData, $jsonResource->getDataPart());
123
    }
124
125
    /**
126
     * Test getting the data of a Json file
127
     */
128
    public function testJsonResourceSetData()
129
    {
130
        $expectedData = $this->getExpectedInvoiceData();
131
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
132
        $jsonResource->setDataPart($expectedData);
133
        $this->assertArrayEquals($expectedData, $jsonResource->getDataPart());
134
    }
135
136
    /**
137
     * Test the JSON Utility
138
     */
139
    public function testJsonEncodingDecoding() {
140
        $array = [(object)['a' => 1], [2, 3]];
141
        $this->assertEquals($array, JsonUtility::decode(JsonUtility::encode($array)));
142
    }
143
}
144