summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/JsonSchema/SchemaTest.php
blob: 4aae8e5620409806b1b687fce325e54aef0dfae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace OpenCloud\Test\Common\JsonSchema;

use JsonSchema\Validator;
use OpenCloud\Common\JsonSchema\Schema;
use OpenCloud\Test\TestCase;

class SchemaTest extends TestCase
{
    /** @var Schema */
    private $schema;

    /** @var Validator */
    private $validator;
    private $body;

    public function setUp()
    {
        $this->body = [
            'properties' => [
                'foo' => (object)[],
                'bar' => (object)[],
                'baz' => (object)['readOnly' => true],
            ],
        ];

        $this->validator = $this->prophesize(Validator::class);
        $this->schema = new Schema($this->body, $this->validator->reveal());
    }

    public function test_it_gets_errors()
    {
        $this->validator->getErrors()
            ->shouldBeCalled()
            ->willReturn([]);

        $this->assertEquals([], $this->schema->getErrors());
    }

    public function test_it_gets_error_string()
    {
        $this->validator->getErrors()
            ->shouldBeCalled()
            ->willReturn([['property' => 'foo', 'message' => 'bar']]);

        $errorMsg = sprintf("Provided values do not validate. Errors:\n[foo] bar\n");

        $this->assertEquals($errorMsg, $this->schema->getErrorString());
    }

    public function test_it_gets_property_paths()
    {
        $this->assertEquals(['/foo', '/bar', '/baz'], $this->schema->getPropertyPaths());
    }

    public function test_it_ignores_readOnly_attrs()
    {
        $expected = (object)[
            'foo' => true,
            'bar' => false,
        ];

        $subject = (object)[
            'foo' => true,
            'bar' => false,
            'baz' => true,
        ];

        $this->assertEquals((object)$expected, $this->schema->normalizeObject((object)$subject, []));
    }

    public function test_it_stocks_aliases()
    {
        $subject = (object)[
            'fooAlias' => true,
            'bar'      => false,
            'other'    => true,
        ];

        $expected = (object)[
            'foo' => true,
            'bar' => false,
        ];

        $this->assertEquals($expected, $this->schema->normalizeObject($subject, ['foo' => 'fooAlias', 'bar' => 'lol']));
    }

    public function test_it_validates()
    {
        $this->validator->check([], (object) $this->body)->shouldBeCalled();

        $this->schema->validate([]);
    }

    public function test_it_checks_validity()
    {
        $this->validator->isValid()->shouldBeCalled()->willReturn(true);

        $this->schema->isValid();
    }
}