ValidatorTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 114
Duplicated Lines 31.58 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 36
loc 114
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidateJsonDefinitionMalformedJson() 18 18 1
A testValidateJsonDefinitionNonObject() 18 18 1
A testValidateJsonDefinitionWithErrors() 0 26 1
A testValidateJsonDefinitionWithoutErrors() 0 21 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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  https://opensource.org/licenses/MIT MIT 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('validate');
41
42
        $sut = new Validator($validator, $schema);
0 ignored issues
show
Documentation introduced by
$schema is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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('validate');
67
68
        $sut = new Validator($validator, $schema);
0 ignored issues
show
Documentation introduced by
$schema is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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('validate')
98
            ->with(json_decode($json), $schema);
99
100
        $sut = new Validator($validator, $schema);
0 ignored issues
show
Documentation introduced by
$schema is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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('validate')
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);
0 ignored issues
show
Documentation introduced by
$schema is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
        $this->assertEquals([], $sut->validateJsonDefinition($json));
130
    }
131
}
132