Completed
Pull Request — develop (#15)
by Narcotic
13:08
created

testValidateJsonDefinitionWithErrors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
/**
3
 * ValidatorTest class file
4
 */
5
6
namespace Graviton\JsonSchemaBundle\Tests\Validator;
7
8
use Graviton\JsonSchemaBundle\Exception\ValidationExceptionError;
9
use Graviton\JsonSchemaBundle\Validator\Validator;
10
11
/**
12
 * Test Validator
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class ValidatorTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * Test Validator::validateJsonDefinition() with malformed JSON
22
     *
23
     * @return void
24
     * @expectedException \Graviton\JsonSchemaBundle\Validator\InvalidJsonException
25
     * @expectedExceptionMessage Malformed JSON
26
     */
27 View Code Duplication
    public function testValidateJsonDefinitionMalformedJson()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $schema = new \stdClass();
30
        $json = '[}';
31
32
        $validator = $this->getMockBuilder('JsonSchema\Validator')
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
        $validator->expects($this->never())
36
            ->method('isValid');
37
        $validator->expects($this->never())
38
            ->method('getErrors');
39
        $validator->expects($this->never())
40
            ->method('check');
41
42
        $sut = new Validator($validator, $schema);
43
        $this->assertEquals([], $sut->validateJsonDefinition($json));
44
    }
45
46
    /**
47
     * Test Validator::validateJsonDefinition() with non-object JSON
48
     *
49
     * @return void
50
     * @expectedException \Graviton\JsonSchemaBundle\Validator\InvalidJsonException
51
     * @expectedExceptionMessage JSON value must be an object
52
     */
53 View Code Duplication
    public function testValidateJsonDefinitionNonObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $schema = new \stdClass();
56
        $json = '[]';
57
58
        $validator = $this->getMockBuilder('JsonSchema\Validator')
59
            ->disableOriginalConstructor()
60
            ->getMock();
61
        $validator->expects($this->never())
62
            ->method('isValid');
63
        $validator->expects($this->never())
64
            ->method('getErrors');
65
        $validator->expects($this->never())
66
            ->method('check');
67
68
        $sut = new Validator($validator, $schema);
69
        $this->assertEquals([], $sut->validateJsonDefinition($json));
70
    }
71
72
    /**
73
     * Test Validator::validateJsonDefinition() with errors
74
     *
75
     * @return void
76
     */
77
    public function testValidateJsonDefinitionWithErrors()
78
    {
79
        $schema = new \stdClass();
80
        $json = '{"a":"b"}';
81
        $errors = [['message' => 'error']];
82
        $returnErrors = [new ValidationExceptionError($errors[0])];
83
84
        $validator = $this->getMockBuilder('JsonSchema\Validator')
85
            ->disableOriginalConstructor()
86
            ->getMock();
87
        $validator->expects($this->once())
88
            ->method('isValid')
89
            ->willReturn(false);
90
        $validator->expects($this->once())
91
            ->method('getErrors')
92
            ->willReturn($errors);
93
        $validator->expects($this->once())
94
                  ->method('reset')
95
                  ->willReturn(true);
96
        $validator->expects($this->once())
97
            ->method('check')
98
            ->with(json_decode($json), $schema);
99
100
        $sut = new Validator($validator, $schema);
101
        $this->assertEquals($returnErrors, $sut->validateJsonDefinition($json));
102
    }
103
104
105
    /**
106
     * Test Validator::validateJsonDefinition() without errors
107
     *
108
     * @return void
109
     */
110
    public function testValidateJsonDefinitionWithoutErrors()
111
    {
112
        $schema = new \stdClass();
113
        $json = '{"a":"b"}';
114
115
        $validator = $this->getMockBuilder('JsonSchema\Validator')
116
            ->disableOriginalConstructor()
117
            ->getMock();
118
        $validator->expects($this->once())
119
            ->method('check')
120
            ->with(json_decode($json), $schema)
121
            ->willReturn(true);
122
        $validator->expects($this->once())
123
                  ->method('isValid')
124
                  ->willReturn(true);
125
        $validator->expects($this->never())
126
            ->method('getErrors');
127
128
        $sut = new Validator($validator, $schema);
129
        $this->assertEquals([], $sut->validateJsonDefinition($json));
130
    }
131
}
132