Passed
Push — master ( 1ccf8a...1e427b )
by Daniel
02:19
created

SchemaLoaderTest::testLoadReturnsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\JsonSchemaApi\Dispatch;
6
7
use Mockery\Adapter\Phpunit\MockeryTestCase;
8
use org\bovigo\vfs\vfsStream;
9
use Teapot\StatusCode;
10
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;
11
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException;
12
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException;
13
14
class SchemaLoaderTest extends MockeryTestCase
15
{
16
    private SchemaLoader $subject;
17
    
18
    public function setUp(): void
19
    {
20
        $this->subject = new SchemaLoader();
21
    }
22
23
    public function testLoadThrowsExceptionIfSchemaWasNotFound(): void
24
    {
25
        $root = vfsStream::setup();
26
        $path = $root->url() . '/some-file';
27
28
        $this->expectException(SchemaNotFoundException::class);
29
        $this->expectExceptionMessage(
30
            sprintf('Schema file `%s` not found', $path)
31
        );
32
        $this->expectExceptionCode(StatusCode::INTERNAL_SERVER_ERROR);
33
34
        $this->subject->load($path);
35
    }
36
37
    public function testLoadThrowsExceptionIfSchemaDoesNotContainValidJson(): void
38
    {
39
        $root = vfsStream::setup();
40
        $content = 'some-content' . PHP_EOL . 'other-content';
41
        $path = $root->url() . '/some-file';
42
43
        file_put_contents($path, $content);
44
45
        $this->expectException(SchemaInvalidException::class);
46
        $this->expectExceptionMessage(
47
            'Schema does not contain valid json (Syntax error)'
48
        );
49
        $this->expectExceptionCode(StatusCode::INTERNAL_SERVER_ERROR);
50
51
        $this->subject->load($path);
52
    }
53
54
    public function testLoadThrowsExceptionIfSchemaIsNotLoadable(): void
55
    {
56
        $root = vfsStream::setup();
57
        $path = $root->url();
58
59
        $this->expectException(SchemaNotLoadableException::class);
60
        $this->expectExceptionMessage(
61
            sprintf('Schema file `%s` not loadable', $path),
62
        );
63
        $this->expectExceptionCode(StatusCode::INTERNAL_SERVER_ERROR);
64
65
66
        $this->subject->load($path);
67
    }
68
69
    public function testLoadReturnsData(): void
70
    {
71
        $root = vfsStream::setup();
72
        $content = ['some' => 'content'];
73
        $path = $root->url() . '/some-file';
74
75
        file_put_contents($path, json_encode($content));
76
77
        static::assertSame(
78
            $content,
79
            (array) $this->subject->load($path)
80
        );
81
    }
82
}
83