JsonTest::testJsonResourceReader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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 file
55
     *
56
     * @var string
57
     */
58
    const JSON_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.json';
59
    /**
60
     * Example JSON data
61
     *
62
     * @var string
63
     */
64
    protected $json = null;
65
66
    /**
67
     * Preparations before the first test is run
68
     */
69
    public static function setUpBeforeClass()
70
    {
71
        \date_default_timezone_set('UTC');
72
    }
73
74
    /**
75
     * Test the JSON file constructor
76
     */
77
    public function testJsonResource()
78
    {
79
        $jsonResource = Kernel::create(JsonResource::class, [null]);
80
        $this->assertInstanceOf(JsonResource::class, $jsonResource);
81
        $this->assertEquals(JsonPart::MEDIA_TYPE, $jsonResource->getMediaTypePart());
82
    }
83
84
    /**
85
     * Test the JSON file constructor with reader
86
     */
87
    public function testJsonResourceReader()
88
    {
89
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
90
        $this->assertEquals($this->json, $jsonResource->getPart());
91
    }
92
93
    /**
94
     * Test getting the data of a JSON file with unallowed subparts
95
     *
96
     * @expectedException \Apparat\Resource\Domain\Model\Part\InvalidArgumentException
97
     * @expectedExceptionCode 1447365624
98
     */
99
    public function testJsonResourceHtmlSubparts()
100
    {
101
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
102
        $jsonResource->getDataPart('a/b/c');
103
    }
104
105
    /**
106
     * Test getting the data of a Json file
107
     */
108
    public function testJsonResourceGetData()
109
    {
110
        $expectedData = include __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.php';
111
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
112
        $this->assertArrayEquals($expectedData, $jsonResource->getDataPart());
113
    }
114
115
    /**
116
     * Test getting the data of a Json file
117
     */
118
    public function testJsonResourceSetData()
119
    {
120
        $expectedData = $this->getExpectedInvoiceData();
121
        $jsonResource = Kernel::create(JsonResource::class, [Kernel::create(Reader::class, [$this->json])]);
122
        $jsonResource->setDataPart($expectedData);
123
        $this->assertArrayEquals($expectedData, $jsonResource->getDataPart());
124
    }
125
126
    /**
127
     * Test the JSON Utility
128
     */
129
    public function testJsonEncodingDecoding()
130
    {
131
        $array = [(object)['a' => 1], [2, 3]];
132
        $this->assertEquals($array, JsonUtility::decode(JsonUtility::encode($array)));
133
    }
134
135
    /**
136
     * Sets up the fixture
137
     */
138
    protected function setUp()
139
    {
140
        parent::setUp();
141
        $this->json = file_get_contents(self::JSON_FILE);
142
    }
143
}
144