YamlTest::testYamlResourceReader()   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\Infrastructure\Io\InMemory\Reader;
41
use Apparat\Resource\Infrastructure\Model\Part\YamlPart;
42
use Apparat\Resource\Infrastructure\Model\Resource\YamlResource;
43
44
/**
45
 * YAML file tests
46
 *
47
 * @package     Apparat\Resource
48
 * @subpackage  Apparat\Resource\Tests
49
 */
50
class YamlTest extends AbstractDataTest
51
{
52
    /**
53
     * Example YAML file
54
     *
55
     * @var string
56
     */
57
    const YAML_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.yaml';
58
    /**
59
     * Example YAML data
60
     *
61
     * @var string
62
     */
63
    protected $yaml = null;
64
65
    /**
66
     * Preparations before the first test is run
67
     */
68
    public static function setUpBeforeClass()
69
    {
70
        \date_default_timezone_set('UTC');
71
    }
72
73
    /**
74
     * Test the YAML file constructor
75
     */
76
    public function testYamlResource()
77
    {
78
        $yamlResource = Kernel::create(YamlResource::class, [null]);
79
        $this->assertInstanceOf(YamlResource::class, $yamlResource);
80
        $this->assertEquals(YamlPart::MEDIA_TYPE, $yamlResource->getMediaTypePart());
81
    }
82
83
    /**
84
     * Test the YAML file constructor with reader
85
     */
86
    public function testYamlResourceReader()
87
    {
88
        $yamlResource = Kernel::create(YamlResource::class, [Kernel::create(Reader::class, [$this->yaml])]);
89
        $this->assertEquals($this->yaml, $yamlResource->getPart());
90
    }
91
92
    /**
93
     * Test getting the data of a YAML file with unallowed subparts
94
     *
95
     * @expectedException \Apparat\Resource\Domain\Model\Part\InvalidArgumentException
96
     * @expectedExceptionCode 1447365624
97
     */
98
    public function testYamlResourceHtmlSubparts()
99
    {
100
        $yamlResource = Kernel::create(YamlResource::class, [Kernel::create(Reader::class, [$this->yaml])]);
101
        $yamlResource->getDataPart('a/b/c');
102
    }
103
104
    /**
105
     * Test getting the data of a Yaml file
106
     */
107
    public function testYamlResourceGetData()
108
    {
109
        $expectedData = include __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.php';
110
        $yamlResource = Kernel::create(YamlResource::class, [Kernel::create(Reader::class, [$this->yaml])]);
111
        $this->assertArrayEquals($expectedData, $yamlResource->getDataPart());
112
        $this->assertArrayEquals($expectedData, $yamlResource->getData());
113
    }
114
115
    /**
116
     * Test getting the data part of a Yaml file
117
     */
118
    public function testYamlResourceSetDataPart()
119
    {
120
        $expectedData = $this->getExpectedInvoiceData();
121
        $yamlResource = Kernel::create(YamlResource::class, [Kernel::create(Reader::class, [$this->yaml])]);
122
        $yamlResource->setDataPart($expectedData);
123
        $this->assertArrayEquals($expectedData, $yamlResource->getDataPart());
124
    }
125
126
    /**
127
     * Test getting the data of a Yaml file
128
     */
129
    public function testYamlResourceSetData()
130
    {
131
        $expectedData = $this->getExpectedInvoiceData();
132
        $yamlResource = Kernel::create(YamlResource::class, [Kernel::create(Reader::class, [$this->yaml])]);
133
        $yamlResource->setData($expectedData);
134
        $this->assertArrayEquals($expectedData, $yamlResource->getData());
135
    }
136
137
    /**
138
     * Sets up the fixture
139
     */
140
    protected function setUp()
141
    {
142
        parent::setUp();
143
        $this->yaml = file_get_contents(self::YAML_FILE);
144
    }
145
}
146