FactoryTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
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\Dev\Tests\AbstractTest;
40
use Apparat\Resource\Infrastructure\Io\File\Writer as FileWriter;
41
use Apparat\Resource\Infrastructure\Io\InMemory\Writer as InMemoryWriter;
42
use Apparat\Resource\Infrastructure\Model\Resource\CommonMarkResource;
43
use Apparat\Resource\Infrastructure\Model\Resource\FrontMarkResource;
44
use Apparat\Resource\Infrastructure\Model\Resource\JsonResource;
45
use Apparat\Resource\Infrastructure\Model\Resource\TextResource;
46
use Apparat\Resource\Infrastructure\Model\Resource\YamlResource;
47
use Apparat\Resource\Ports\InvalidArgumentException;
48
use Apparat\Resource\Ports\Resource;
49
50
/**
51
 * Resource factory tests
52
 *
53
 * @package     Apparat\Resource
54
 * @subpackage  Apparat\Resource\Tests
55
 */
56
class FactoryTest extends AbstractTest
57
{
58
    /**
59
     * Example text file
60
     *
61
     * @var string
62
     */
63
    const TXT_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'cc0.txt';
64
    /**
65
     * Example JSON file
66
     *
67
     * @var string
68
     */
69
    const JSON_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.json';
70
    /**
71
     * Example YAML file
72
     *
73
     * @var string
74
     */
75
    const YAML_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.yaml';
76
    /**
77
     * Example CommonMark file
78
     *
79
     * @var string
80
     */
81
    const COMMONMARK_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'commonmark.md';
82
    /**
83
     * Example FrontMark file with YAML front matter
84
     *
85
     * @var string
86
     */
87
    const YAML_FRONTMARK_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'yaml-frontmark.md';
88
    /**
89
     * Example FrontMark file with JSON front matter
90
     *
91
     * @var string
92
     */
93
    const JSON_FRONTMARK_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'json-frontmark.md';
94
    /**
95
     * Example text data
96
     *
97
     * @var string
98
     */
99
    protected $text = null;
100
    /**
101
     * Example JSON data
102
     *
103
     * @var string
104
     */
105
    protected $json = null;
106
    /**
107
     * Example YAML data
108
     *
109
     * @var string
110
     */
111
    protected $yaml = null;
112
    /**
113
     * Example CommonMark data
114
     *
115
     * @var string
116
     */
117
    protected $commonMark = null;
118
    /**
119
     * Example FrontMark data with YAML front matter
120
     *
121
     * @var string
122
     */
123
    protected $yamlFrontMark = null;
124
    /**
125
     * Example FrontMark file with JSON front matter
126
     *
127
     * @var string
128
     */
129
    protected $jsonFrontMark = null;
130
131
    /**
132
     * Test the text resource factory with a string literal input (in-memory reader)
133
     */
134
    public function testTextFactoryStringReader()
135
    {
136
        $randString = md5(rand());
137
        $textResource = Resource::text($randString);
138
        $this->assertInstanceOf(TextResource::class, $textResource);
139
        $this->assertEquals($randString, $textResource->get());
140
        $this->assertEquals($randString, strval($textResource));
141
    }
142
143
    /**
144
     * Test the text resource factory with a file path input (file reader)
145
     */
146
    public function testTextFactoryFileReader()
147
    {
148
        $textResource = Resource::text('file://'.self::TXT_FILE);
149
        $this->assertInstanceOf(TextResource::class, $textResource);
150
        $this->assertEquals($this->text, $textResource->get());
151
    }
152
153
    /**
154
     * Test the text resource factory with an invalid reader stream-wrapper
155
     *
156
     * @expectedException InvalidArgumentException
157
     * @expectedExceptionCode 1448493550
158
     */
159
    public function testTextFactoryInvalidReader()
160
    {
161
        Resource::text('foo://'.self::TXT_FILE);
162
    }
163
164
    /**
165
     * Test the text resource factory with a string output (in-memory writer)
166
     */
167
    public function testTextFactoryStringWriter()
168
    {
169
        $textResource = Resource::text($this->text);
170
        /** @var InMemoryWriter $writer */
171
        $writer = $textResource->toTarget('');
172
        $this->assertInstanceOf(InMemoryWriter::class, $writer);
173
        $this->assertEquals($this->text, $writer->getData());
174
    }
175
176
    /**
177
     * Test the text resource factory with a file path (file reader)
178
     */
179
    public function testTextFactoryFileWriter()
180
    {
181
        $tempFileName = $this->createTemporaryFileName();
182
        $textResource = Resource::text($this->text);
183
        $writer = $textResource->toTarget('file://'.$tempFileName, FileWriter::FILE_CREATE);
184
        $this->assertInstanceOf(FileWriter::class, $writer);
185
        $this->assertStringEqualsFile($tempFileName, $this->text);
186
    }
187
188
    /**
189
     * Test the text resource factory with an invalid writer stream-wrapper
190
     *
191
     * @expectedException InvalidArgumentException
192
     * @expectedExceptionCode 1448493564
193
     */
194
    public function testTextFactoryInvalidWriter()
195
    {
196
        Resource::text($this->text)->toTarget('foo://'.$this->createTemporaryFileName());
197
    }
198
199
    /**
200
     * Test the JSON resource factory with a file path input (file reader)
201
     */
202
    public function testJsonFactoryFileReader()
203
    {
204
        $jsonResource = Resource::json('file://'.self::JSON_FILE);
205
        $this->assertInstanceOf(JsonResource::class, $jsonResource);
206
        $this->assertEquals($this->json, $jsonResource->get());
207
    }
208
209
    /**
210
     * Test the YAML resource factory with a file path input (file reader)
211
     */
212
    public function testYamlFactoryFileReader()
213
    {
214
        $yamlResource = Resource::yaml('file://'.self::YAML_FILE);
215
        $this->assertInstanceOf(YamlResource::class, $yamlResource);
216
        $this->assertEquals($this->yaml, $yamlResource->get());
217
    }
218
219
    /**
220
     * Test the CommonMark resource factory with a file path input (file reader)
221
     */
222
    public function testCommonMarkFactoryFileReader()
223
    {
224
        $commonMarkResource = Resource::commonMark('file://'.self::COMMONMARK_FILE);
225
        $this->assertInstanceOf(CommonMarkResource::class, $commonMarkResource);
226
        $this->assertEquals($this->commonMark, $commonMarkResource->get());
227
    }
228
229
    /**
230
     * Test the YAML FrontMark resource factory with a file path input (file reader)
231
     */
232
    public function testYamlFrontMarkFactoryFileReader()
233
    {
234
        $yamlFMResource = Resource::frontMark('file://'.self::YAML_FRONTMARK_FILE);
235
        $this->assertInstanceOf(FrontMarkResource::class, $yamlFMResource);
236
        $this->assertEquals($this->yamlFrontMark, strval($yamlFMResource));
237
    }
238
239
    /**
240
     * Test the JSON FrontMark resource factory with a file path input (file reader)
241
     */
242
    public function testJsonFrontMarkFactoryFileReader()
243
    {
244
        $jsonFMResource = Resource::frontMark('file://'.self::JSON_FRONTMARK_FILE);
245
        $this->assertInstanceOf(FrontMarkResource::class, $jsonFMResource);
246
        $this->assertEquals($this->jsonFrontMark, strval($jsonFMResource));
247
    }
248
249
    /**
250
     * Sets up the fixture
251
     */
252
    protected function setUp()
253
    {
254
        parent::setUp();
255
        $this->text = file_get_contents(self::TXT_FILE);
256
        $this->json = file_get_contents(self::JSON_FILE);
257
        $this->yaml = file_get_contents(self::YAML_FILE);
258
        $this->commonMark = file_get_contents(self::COMMONMARK_FILE);
259
        $this->yamlFrontMark = file_get_contents(self::YAML_FRONTMARK_FILE);
260
        $this->jsonFrontMark = file_get_contents(self::JSON_FRONTMARK_FILE);
261
    }
262
}
263