Completed
Push — master ( b3ac2d...7fdc5c )
by Joschi
02:17
created

FrontMarkTest::testSequenceAggregate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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