FrontMarkTest::testJsonResource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 16
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\Kernel\Ports\Kernel;
41
use Apparat\Resource\Application\Service\JsonUtility;
42
use Apparat\Resource\Domain\Model\Hydrator\HydratorInterface;
43
use Apparat\Resource\Domain\Model\Part\InvalidArgumentException;
44
use Apparat\Resource\Domain\Model\Part\OutOfBoundsException;
45
use Apparat\Resource\Domain\Model\Part\PartChoice;
46
use Apparat\Resource\Domain\Model\Part\PartSequence;
47
use Apparat\Resource\Infrastructure\Io\InMemory\Reader;
48
use Apparat\Resource\Infrastructure\Model\Hydrator\FrontMatterHydrator;
49
use Apparat\Resource\Infrastructure\Model\Hydrator\JsonHydrator;
50
use Apparat\Resource\Infrastructure\Model\Hydrator\YamlHydrator;
51
use Apparat\Resource\Infrastructure\Model\Resource\FrontMarkResource;
52
use Symfony\Component\Yaml\Yaml;
53
54
/**
55
 * FrontMark file tests
56
 *
57
 * @package     Apparat\Resource
58
 * @subpackage  Apparat\Resource\Tests
59
 */
60
class FrontMarkTest extends AbstractTest
61
{
62
    /**
63
     * Example YAML file
64
     *
65
     * @var string
66
     */
67
    const YAML_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.yaml';
68
    /**
69
     * Example JSON file
70
     *
71
     * @var string
72
     */
73
    const JSON_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'invoice.json';
74
    /**
75
     * Example CommonMark file
76
     *
77
     * @var string
78
     */
79
    const COMMONMARK_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'commonmark.md';
80
    /**
81
     * Example FrontMark file with YAML front matter
82
     *
83
     * @var string
84
     */
85
    const YAML_FRONTMARK_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'yaml-frontmark.md';
86
    /**
87
     * Example FrontMark file with JSON front matter
88
     *
89
     * @var string
90
     */
91
    const JSON_FRONTMARK_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'json-frontmark.md';
92
    /**
93
     * Example front matter YAML file
94
     *
95
     * @var string
96
     */
97
    const YAML_FRONTMATTER_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'frontmatter.yaml';
98
    /**
99
     * Example front matter JSON file
100
     *
101
     * @var string
102
     */
103
    const JSON_FRONTMATTER_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'frontmatter.json';
104
    /**
105
     * Example CommonMark data
106
     *
107
     * @var string
108
     */
109
    protected $commonMark = null;
110
    /**
111
     * Example FrontMark data with YAML front matter
112
     *
113
     * @var string
114
     */
115
    protected $yamlFrontMark = null;
116
    /**
117
     * Example FrontMark file with JSON front matter
118
     *
119
     * @var string
120
     */
121
    protected $jsonFrontMark = null;
122
123
    /**
124
     * Test a FrontMark file
125
     */
126
    public function testFrontMarkResource()
127
    {
128
        $frontMarkResource = Kernel::create(FrontMarkResource::class, [null]);
129
        $this->assertInstanceOf(FrontMarkResource::class, $frontMarkResource);
130
        $this->assertEquals(null, $frontMarkResource->getMediaTypePart());
131
132
    }
133
134
    /**
135
     * Test YAML part with too few part identifiers
136
     *
137
     * @expectedException InvalidArgumentException
138
     * @expectedExceptionCode 1448051332
139
     */
140
    public function testYamlTooFewPartIdentifiers()
141
    {
142
        $frontMarkResource = Kernel::create(
143
            FrontMarkResource::class,
144
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
145
        );
146
        $frontMarkResource->getPart('/0');
147
    }
148
149
    /**
150
     * Test YAML part with invalid occurrence index
151
     *
152
     * @expectedException InvalidArgumentException
153
     * @expectedExceptionCode 1448051596
154
     */
155
    public function testYamlInvalidOccurrenceIndex()
156
    {
157
        $frontMarkResource = Kernel::create(
158
            FrontMarkResource::class,
159
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
160
        );
161
        $frontMarkResource->getPart('/abc/123');
162
    }
163
164
    /**
165
     * Test YAML part with occurrence index out of bounds
166
     *
167
     * @expectedException OutOfBoundsException
168
     * @expectedExceptionCode 1448052094
169
     */
170
    public function testYamlOccurrenceIndexOutOfBounds()
171
    {
172
        $frontMarkResource = Kernel::create(
173
            FrontMarkResource::class,
174
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
175
        );
176
        $frontMarkResource->getPart('/123/abc');
177
    }
178
179
    /**
180
     * Test YAML part with occurrence index out of bounds
181
     *
182
     * @expectedException InvalidArgumentException
183
     * @expectedExceptionCode 1447364401
184
     */
185
    public function testYamlInvalidSubpartIdentifier()
186
    {
187
        $frontMarkResource = Kernel::create(
188
            FrontMarkResource::class,
189
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
190
        );
191
        $frontMarkResource->getPart('/123/~');
192
    }
193
194
    /**
195
     * Test YAML part with unknown identifier
196
     *
197
     * @expectedException InvalidArgumentException
198
     * @expectedExceptionCode 1447876475
199
     */
200
    public function testYamlUnknownSubpartIdentifier()
201
    {
202
        $frontMarkResource = Kernel::create(
203
            FrontMarkResource::class,
204
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
205
        );
206
        $frontMarkResource->getPart('/0/abc');
207
    }
208
209
    /**
210
     * Test YAML with empty / invalid part
211
     *
212
     * @expectedException InvalidArgumentException
213
     * @expectedExceptionCode 1448053518
214
     */
215
    public function testYamlInvalidPartInstance()
216
    {
217
        $frontMarkResource = Kernel::create(
218
            FrontMarkResourceMock::class,
219
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
220
        );
221
        $frontMarkResource->invalidateCommonMarkPart();
222
        $frontMarkResource->getPart('/0/'.HydratorInterface::STANDARD);
223
    }
224
225
    /**
226
     * Test sequence aggregate
227
     */
228
    public function testSequenceAggregate()
229
    {
230
        $frontMarkResource = Kernel::create(
231
            FrontMarkResourceMock::class,
232
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
233
        );
234
235
        /** @var PartSequence $sequence */
236
        $sequence = $frontMarkResource->getSequence();
237
        $this->assertInstanceOf(PartSequence::class, $sequence);
238
        $this->assertEquals(1, count($sequence));
239
        foreach ($sequence as $occurrenceIndex => $occurrence) {
240
            $this->assertEquals(0, $occurrenceIndex);
241
            $this->assertTrue(is_array($occurrence));
242
        }
243
    }
244
245
    /**
246
     * Test YAML getting the front matter part
247
     */
248
    public function testYamlGetFrontmatterPart()
249
    {
250
        $expectedData = Yaml::parse(file_get_contents(self::YAML_FRONTMATTER_FILE), Yaml::PARSE_DATETIME);
251
        $frontMarkResource = Kernel::create(
252
            FrontMarkResource::class,
253
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
254
        );
255
        $actualData = Yaml::parse(
256
            $frontMarkResource->getPart('/0/'.FrontMatterHydrator::FRONTMATTER),
257
            Yaml::PARSE_DATETIME
258
        );
259
        $this->assertArrayEquals($expectedData, $actualData);
260
    }
261
262
    /**
263
     * Test YAML setting the front matter part
264
     */
265
    public function testYamlSetFrontmatterPart()
266
    {
267
        $yaml = file_get_contents(self::YAML_FILE);
268
        $expectedData = Yaml::parse($yaml, Yaml::PARSE_DATETIME);
269
        $frontMarkResource = Kernel::create(
270
            FrontMarkResource::class,
271
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
272
        );
273
        $frontMarkResource->setPart($yaml, '/0/'.FrontMatterHydrator::FRONTMATTER);
274
        $actualData = Yaml::parse(
275
            $frontMarkResource->getPart('/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.YamlHydrator::YAML),
276
            Yaml::PARSE_DATETIME
277
        );
278
        $this->assertArrayEquals($expectedData, $actualData);
279
    }
280
281
    /**
282
     * Test JSON FrontMark file
283
     */
284
    public function testJsonFrontMarkResource()
285
    {
286
        $frontMarkResource = Kernel::create(
287
            FrontMarkResource::class,
288
            [Kernel::create(Reader::class, [$this->jsonFrontMark])]
289
        );
290
        $this->assertStringEqualsFile(
291
            self::JSON_FRONTMATTER_FILE,
292
            $frontMarkResource->getPart('/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.JsonHydrator::JSON)
293
        );
294
    }
295
296
    /**
297
     * Test JSON file
298
     */
299
    public function testJsonResource()
300
    {
301
        $frontMarkResource = Kernel::create(
302
            FrontMarkResource::class,
303
            [
304
                Kernel::create(
305
                    Reader::class,
306
                    [file_get_contents(self::JSON_FRONTMATTER_FILE)]
307
                )
308
            ]
309
        );
310
        $this->assertStringEqualsFile(
311
            self::JSON_FRONTMATTER_FILE,
312
            $frontMarkResource->getPart('/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.JsonHydrator::JSON)
313
        );
314
    }
315
316
    /**
317
     * Test JSON FrontMark frontmatter wildcard
318
     */
319
    public function testJsonFrontMarkFrontmatterWildcard()
320
    {
321
        $frontMarkResource = Kernel::create(
322
            FrontMarkResource::class,
323
            [Kernel::create(Reader::class, [$this->jsonFrontMark])]
324
        );
325
        $this->assertStringEqualsFile(
326
            self::JSON_FRONTMATTER_FILE,
327
            $frontMarkResource->getPart('/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.PartChoice::WILDCARD)
328
        );
329
    }
330
331
    /**
332
     * Test JSON FrontMark get frontmatter data
333
     */
334
    public function testJsonFrontMarkFrontmatterGetData()
335
    {
336
        $frontMarkResource = Kernel::create(
337
            FrontMarkResource::class,
338
            [Kernel::create(Reader::class, [$this->jsonFrontMark])]
339
        );
340
        $this->assertArrayEquals(
341
            JsonUtility::decode(file_get_contents(self::JSON_FRONTMATTER_FILE), true),
342
            $frontMarkResource->getData()
343
        );
344
    }
345
346
    /**
347
     * Test JSON FrontMark set frontmatter data
348
     */
349
    public function testJsonFrontMarkFrontmatterSetData()
350
    {
351
        $expectedJson = JsonUtility::decode(file_get_contents(self::JSON_FILE), true);
352
        $frontMarkResource = Kernel::create(
353
            FrontMarkResource::class,
354
            [Kernel::create(Reader::class, [$this->jsonFrontMark])]
355
        );
356
        $frontMarkResource->setData($expectedJson);
357
        $this->assertArrayEquals(
358
            $expectedJson,
359
            $frontMarkResource->getDataPart('/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.PartChoice::WILDCARD)
360
        );
361
    }
362
363
    /**
364
     * Test YAML set / get CommonMark
365
     */
366
    public function testYamlSetGetCommonMark()
367
    {
368
        $randomSet = md5(rand());
369
        $frontMarkResource = Kernel::create(
370
            FrontMarkResource::class,
371
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
372
        );
373
        $frontMarkResource->set($randomSet);
374
        $this->assertEquals($randomSet, $frontMarkResource->getPart('/0/'.HydratorInterface::STANDARD));
375
        $this->assertEquals($randomSet, $frontMarkResource->get());
376
    }
377
378
    /**
379
     * Test YAML append CommonMark
380
     */
381
    public function testYamlAppendCommonMark()
382
    {
383
        $randomAppend = md5(rand());
384
        $frontMarkResource = Kernel::create(
385
            FrontMarkResource::class,
386
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
387
        );
388
        $frontMarkResource->append($randomAppend);
389
        $this->assertEquals($this->commonMark.$randomAppend, $frontMarkResource->get());
390
    }
391
392
    /**
393
     * Test YAML prepend CommonMark
394
     */
395
    public function testYamlPrependCommonMark()
396
    {
397
        $randomPrepend = md5(rand());
398
        $frontMarkResource = Kernel::create(
399
            FrontMarkResource::class,
400
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
401
        );
402
        $frontMarkResource->prepend($randomPrepend);
403
        $this->assertEquals($randomPrepend.$this->commonMark, $frontMarkResource->get());
404
    }
405
406
    /**
407
     * Test YAML get CommonMark HTL
408
     */
409
    public function testYamlGetCommonMarkHtml()
410
    {
411
        $expectedHtml = $this->normalizeHtml(
412
            file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'commonmark.html')
413
        );
414
        $frontMarkResource = Kernel::create(
415
            FrontMarkResource::class,
416
            [Kernel::create(Reader::class, [$this->yamlFrontMark])]
417
        );
418
        $this->assertEquals($expectedHtml, $this->normalizeHtml($frontMarkResource->getHtml()));
419
    }
420
421
    /**
422
     * Sets up the fixture
423
     */
424
    protected function setUp()
425
    {
426
        parent::setUp();
427
        $this->commonMark = file_get_contents(self::COMMONMARK_FILE);
428
        $this->yamlFrontMark = file_get_contents(self::YAML_FRONTMARK_FILE);
429
        $this->jsonFrontMark = file_get_contents(self::JSON_FRONTMARK_FILE);
430
    }
431
}
432