JSONTextTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 32.79 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 40
loc 122
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A testIsValidExpression() 16 16 1
A testIsValidJson() 16 16 1
A testIsValidDBValue() 0 11 1
A testToSSTypes() 0 22 1
A getFixture() 0 5 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
/**
4
 * @package silverstripe-jsontext
5
 * @subpackage fields
6
 * @author Russell Michell <[email protected]>
7
 */
8
9
use PhpTek\JSONText\ORM\FieldType\JSONText;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\ORM\FieldType\DBFloat;
12
use SilverStripe\ORM\FieldType\DBBoolean;
13
use SilverStripe\ORM\FieldType\DBVarchar;
14
use SilverStripe\ORM\FieldType\DBInt;
15
16
class JSONTextTest extends SapphireTest
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $fixtures = [
22
        'array'     => 'fixtures/json/array.json',
23
        'object'    => 'fixtures/json/object.json'
24
    ];
25
26
    /**
27
     * JSONTextTest constructor.
28
     *
29
     * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
30
     */
31 View Code Duplication
    public function __construct()
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...
32
    {
33
        foreach($this->fixtures as $name => $path) {
34
            $this->fixtures[$name] = realpath(__DIR__) . '/' . $path;
35
        }
36
        
37
        parent::__construct();
38
    }
39
40
    /**
41
     * @todo There are a ton more permutations of a JSONPath regex
42
     * See the trace() method in JSONPath for more examples to work from
43
     */
44 View Code Duplication
    public function testIsValidExpression()
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...
45
    {
46
        $field = JSONText::create('MyJSON');
47
48
        $this->assertTrue($field->isValidExpression('$..'));
49
        $this->assertTrue($field->isValidExpression('*'));
50
        $this->assertTrue($field->isValidExpression('$.[2]'));
51
        $this->assertTrue($field->isValidExpression('$.cars.american[*]'));
52
        $this->assertTrue($field->isValidExpression('[0:1:1]'));
53
        $this->assertFalse($field->isValidExpression('[0:1:]'));
54
        $this->assertFalse($field->isValidExpression('[0:1:1'));
55
        $this->assertFalse($field->isValidExpression(''));
56
        $this->assertFalse($field->isValidExpression('$.1.cars.american[*]'));
57
        $this->assertFalse($field->isValidExpression('$'));
58
        $this->assertFalse($field->isValidExpression('$[2]'));
59
    }
60
61
    /**
62
     * @return void
63
     */
64 View Code Duplication
    public function testIsValidJson()
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...
65
    {
66
        $field = JSONText::create('MyJSON');
67
68
        $this->assertFalse($field->isValidJson(''));
69
        $this->assertTrue($field->isValidJson('true'));
70
        $this->assertTrue($field->isValidJson('false'));
71
        $this->assertFalse($field->isValidJson('null'));
72
        $this->assertFalse($field->isValidJson("['one']"));
73
        $this->assertFalse($field->isValidJson('["one]'));
74
        $this->assertFalse($field->isValidJson('{{{'));
75
        $this->assertTrue($field->isValidJson('[]'));
76
        $this->assertTrue($field->isValidJson('["one"]'));
77
        $this->assertTrue($field->isValidJson('["one","two"]'));
78
        $this->assertTrue($field->isValidJson('{"cars":{"american":["buick","oldsmobile"]}}'));
79
    }
80
81
82
    /**
83
     * Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need
84
     * to allow otherwise invalid JSON by means of an optional 2nd param
85
     *
86
     * @return void
87
     */
88
    public function testIsValidDBValue()
89
    {
90
        $field = JSONText::create('MyJSON');
91
92
        $this->assertFalse($field->isValidDBValue('true'));
93
        $this->assertFalse($field->isValidDBValue('false'));
94
        $this->assertFalse($field->isValidDBValue('null'));
95
        $this->assertTrue($field->isValidDBValue(''));
96
        $this->assertTrue($field->isValidDBValue('["one","two"]'));
97
        $this->assertTrue($field->isValidDBValue('{"cars":{"american":["buick","oldsmobile"]}}'));
98
    }
99
100
    /**
101
     * Properly excercise our internal SS type conversion.
102
     */
103
    public function testToSSTypes()
104
    {
105
        $field = JSONText::create('MyJSON');
106
        $field->setValue($this->getFixture('array'));
107
        $field->setReturnType('silverstripe');
108
109
        $data = $field->last()[6];
110
        $this->assertInstanceOf(DBFloat::class, $data);
111
112
        $data = $field->first()[0];
113
        $this->assertInstanceOf(DBVarchar::class, $data);
114
115
        $data = $field->nth(5)[5];
116
        $this->assertInstanceOf(DBInt::class, $data);
117
118
        $data = $field->nth(1)[1];
119
        $this->assertInstanceOf(DBBoolean::class, $data);
120
121
        $field->setValue('["true"]');
122
        $data = $field->first()[0];
123
        $this->assertInstanceOf(DBVarchar::class, $data);
124
    }
125
126
    /**
127
     * Get the contents of a fixture
128
     *
129
     * @param string $fixture
130
     * @return string
131
     */
132
    private function getFixture($fixture)
133
    {
134
        $files = $this->fixtures;
135
        return file_get_contents($files[$fixture]);
136
    }
137
}
138