Completed
Push — master ( c121e3...39204a )
by Russell
02:23
created

JSONTextTest::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package silverstripe-jsontext
5
 * @subpackage fields
6
 * @author Russell Michell <[email protected]>
7
 */
8
9
use JSONText\Fields\JSONText;
10
11
class JSONTextTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $fixtures = [
17
        'array'     => 'tests/fixtures/json/array.json',
18
        'object'    => 'tests/fixtures/json/object.json'
19
    ];
20
    
21
    /**
22
     * JSONTextTest constructor.
23
     * 
24
     * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
25
     */
26
    public function __construct()
27
    {
28
        foreach($this->fixtures as $name => $path) {
29
            $this->fixtures[$name] = MODULE_DIR . '/' . $path;
30
        }
31
    }
32
    
33
    /**
34
     * @todo There are a ton more permutations of a JSONPath regex
35
     * See the trace() method in JSONPath for more examples to work from
36
     */
37 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...
38
    {
39
        $field = JSONText::create('MyJSON');
40
        
41
        $this->assertTrue($field->isValidExpression('$..'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        $this->assertTrue($field->isValidExpression('*'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        $this->assertTrue($field->isValidExpression('$.[2]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        $this->assertTrue($field->isValidExpression('$.cars.american[*]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        $this->assertTrue($field->isValidExpression('[0:1:1]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        $this->assertFalse($field->isValidExpression('[0:1:]'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $this->assertFalse($field->isValidExpression('[0:1:1'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        $this->assertFalse($field->isValidExpression(''));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        $this->assertFalse($field->isValidExpression('$.1.cars.american[*]'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
        $this->assertFalse($field->isValidExpression('$'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $this->assertFalse($field->isValidExpression('$[2]'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
    }
53
54
    /**
55
     * @return void
56
     */
57 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...
58
    {
59
        $field = JSONText::create('MyJSON');
60
61
        $this->assertFalse($field->isValidJson(''));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        $this->assertTrue($field->isValidJson('true'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        $this->assertTrue($field->isValidJson('false'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        $this->assertFalse($field->isValidJson('null'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
        $this->assertFalse($field->isValidJson("['one']"));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        $this->assertFalse($field->isValidJson('["one]'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        $this->assertFalse($field->isValidJson('{{{'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        $this->assertTrue($field->isValidJson('[]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        $this->assertTrue($field->isValidJson('["one"]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        $this->assertTrue($field->isValidJson('["one","two"]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
        $this->assertTrue($field->isValidJson('{"cars":{"american":["buick","oldsmobile"]}}'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
    }
73
74
75
    /**
76
     * Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need
77
     * to allow otherwise invalid JSON by means of an optional 2nd param
78
     *
79
     * @return void
80
     */
81
    public function testIsValidDBValue()
82
    {
83
        $field = JSONText::create('MyJSON');
84
        
85
        $this->assertFalse($field->isValidDBValue('true'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        $this->assertFalse($field->isValidDBValue('false'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        $this->assertFalse($field->isValidDBValue('null'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        $this->assertTrue($field->isValidDBValue(''));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        $this->assertTrue($field->isValidDBValue('["one","two"]'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $this->assertTrue($field->isValidDBValue('{"cars":{"american":["buick","oldsmobile"]}}'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
    }
92
    
93
    /**
94
     * Properly excercise our internal SS type conversion.
95
     */
96
    public function testToSSTypes()
97
    {
98
        $field = JSONText::create('MyJSON');
99
        $field->setValue($this->getFixture('array'));
100
        $field->setReturnType('silverstripe');
101
        
102
        $data = $field->last()[6];
103
        $this->assertInstanceOf('Float', $data);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        
105
        $data = $field->first()[0];
106
        $this->assertInstanceOf('Varchar', $data);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        
108
        $data = $field->nth(5)[5];
109
        $this->assertInstanceOf('Int', $data);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        
111
        $data = $field->nth(1)[1];
112
        $this->assertInstanceOf('Boolean', $data);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
        
114
        $field->setValue('["true"]');
115
        $data = $field->first()[0];
116
        $this->assertInstanceOf('Varchar', $data);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
    }
118
        
119
    /**
120
     * Get the contents of a fixture
121
     * 
122
     * @param string $fixture
123
     * @return string
124
     */
125
    private function getFixture($fixture)
126
    {
127
        $files = $this->fixtures;
128
        return file_get_contents($files[$fixture]);
129
    }
130
}
131