Completed
Pull Request — master (#1)
by Timothy
08:02
created

JsonSchemaTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 148
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
A testNotIsValidDueToUnspecifiedSchemaFile() 0 6 1
A testNotIsValidDueToInvalidSchemaFile() 0 6 1
A testNotIsValidDueToInvalidSyntax() 0 8 1
A testIsValidEncodedJson() 0 6 1
A testIsValidDecodedJson() 0 6 1
A testNotIsValidDueToSchemaValidation() 0 8 1
A testDefaultMessageFormat() 0 10 1
A testSetFile() 0 10 1
A testSetErrorKeys() 0 14 1
A testSetMessagePrefix() 0 8 1
A testSetMessageSuffix() 0 8 1
A testSetMessageAttributeDelimiter() 0 8 1
1
<?php
2
3
namespace AbacaphiliacTest\Zend\Validator;
4
5
use Abacaphiliac\Zend\Validator\JsonSchema;
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamFile;
8
9
class JsonSchemaTest extends \PHPUnit_Framework_TestCase
10
{
11
    /** @var vfsStreamFile */
12
    private $schema;
13
    
14
    /** @var JsonSchema */
15
    private $sut;
16
17
    protected function setUp()
18
    {
19
        parent::setUp();
20
21
        $this->schema = vfsStream::newFile('schema.json');
22
23
        $schemas = vfsStream::setup('schemas');
24
        $schemas->addChild($this->schema);
25
26
        file_put_contents($this->schema->url(), json_encode(array(
27
            '$schema' => 'http://json-schema.org/draft-04/schema#',
28
            'type' => 'object',
29
            'properties' => array(
30
                'Foo' => array(
31
                    'type' => 'string'
32
                ),
33
            ),
34
        )));
35
36
        $this->sut = new JsonSchema(array(
37
            'file' => $this->schema->url(),
38
        ));
39
    }
40
41
    /**
42
     * @expectedException \Zend\Validator\Exception\RuntimeException
43
     */
44
    public function testNotIsValidDueToUnspecifiedSchemaFile()
45
    {
46
        $this->sut->setFile(null);
47
        
48
        $this->sut->isValid('{"Foo":"Bar"}');
49
    }
50
51
    /**
52
     * @expectedException \Zend\Validator\Exception\RuntimeException
53
     */
54
    public function testNotIsValidDueToInvalidSchemaFile()
55
    {
56
        $this->sut->setFile(__FILE__);
57
        
58
        $this->sut->isValid('{"Foo":"Bar"}');
59
    }
60
61
    public function testNotIsValidDueToInvalidSyntax()
62
    {
63
        $actual = $this->sut->isValid('InvalidJsonString');
64
65
        self::assertFalse($actual);
66
67
        self::assertCount(1, $this->sut->getMessages());
68
    }
69
    
70
    public function testIsValidEncodedJson()
71
    {
72
        $actual = $this->sut->isValid('{"Foo":"Bar"}');
73
74
        self::assertTrue($actual);
75
    }
76
    
77
    public function testIsValidDecodedJson()
78
    {        
79
        $actual = $this->sut->isValid(json_decode('{"Foo":"Bar"}'));
80
81
        self::assertTrue($actual);
82
    }
83
    
84
    public function testNotIsValidDueToSchemaValidation()
85
    {
86
        $actual = $this->sut->isValid('{"Foo":1234}');
87
88
        self::assertFalse($actual);
89
90
        self::assertCount(1, $this->sut->getMessages());
91
    }
92
    
93
    public function testDefaultMessageFormat()
94
    {
95
        $actual = $this->sut->formatError(array(
96
            'property' => 'foo',
97
            'constraint' => 'bar',
98
            'message' => 'error',
99
        ));
100
        
101
        self::assertEquals('[property=foo] [constraint=bar] [message=error]', $actual);
102
    }
103
    
104
    public function testSetFile()
105
    {
106
        $sut = new JsonSchema();
107
        
108
        self::assertNull($sut->getFile());
109
        
110
        $sut->setFile($expected = $this->schema->url());
111
        
112
        self::assertEquals($expected, $sut->getFile());
113
    }
114
    
115
    public function testSetErrorKeys()
116
    {
117
        self::assertArraySubset(
118
            array('property', 'constraint', 'message'),
119
            $this->sut->getErrorKeys()
120
        );
121
        
122
        $this->sut->setErrorKeys(array('foo', 'bar'));
123
124
        self::assertArraySubset(
125
            array('foo', 'bar'),
126
            $this->sut->getErrorKeys()
127
        );
128
    }
129
    
130
    public function testSetMessagePrefix()
131
    {
132
        self::assertEquals('[', $this->sut->getMessagePrefix());
133
        
134
        $this->sut->setMessagePrefix($expected = '{');
135
        
136
        self::assertEquals($expected, $this->sut->getMessagePrefix());
137
    }
138
    
139
    public function testSetMessageSuffix()
140
    {
141
        self::assertEquals(']', $this->sut->getMessageSuffix());
142
        
143
        $this->sut->setMessageSuffix($expected = '}');
144
        
145
        self::assertEquals($expected, $this->sut->getMessageSuffix());
146
    }
147
    
148
    public function testSetMessageAttributeDelimiter()
149
    {
150
        self::assertEquals('] [', $this->sut->getMessageAttributeDelimiter());
151
        
152
        $this->sut->setMessageAttributeDelimiter($expected = '} {');
153
        
154
        self::assertEquals($expected, $this->sut->getMessageAttributeDelimiter());
155
    }
156
}
157