Completed
Push — master ( 2afc86...6ca8f1 )
by Russell
02:59
created

JSONTextSetValueTest::testSetValueOnSourceArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
c 4
b 3
f 1
dl 0
loc 50
rs 9.3333
cc 1
eloc 23
nc 1
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
use JSONText\Exceptions;
11
12
class JSONTextSetValueTest 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...
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $fixtures = [
18
        'array'     => 'tests/fixtures/json/array.json',
19
        'object'    => 'tests/fixtures/json/object.json',
20
        'invalid'   => 'tests/fixtures/json/invalid.json'
21
    ];
22
23
    /**
24
     * JSONTextTest constructor.
25
     * 
26
     * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
27
     */
28
    public function __construct()
29
    {
30 View Code Duplication
        foreach($this->fixtures as $name => $path) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
            $this->fixtures[$name] = MODULE_DIR . '/' . $path;
32
        }
33
    }
34
35
    /**
36
     * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a JSON array
37
     */
38
    public function testSetValueOnSourceArray()
39
    {
40
        // Data Source: Array
41
        // Return Type: ARRAY
42
        // Expression: '$.[2]' The third item
43
        $field = JSONText::create('MyJSON');
44
        $field->setReturnType('array');
45
        $field->setValue($this->getFixture('array'));
46
        // Assert current value
47
        $this->assertEquals(['trabant'], $field->query('$.[2]'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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
        // Now update it...
49
        $field->setValue('lada', null, '$.[2]');
50
        // Assert new value
51
        $this->assertEquals(['lada'], $field->query('$.[2]'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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
        // Data Source: Array
54
        // Return Type: ARRAY
55
        // Expression: '$.[6]' The seventh item
56
        $field = JSONText::create('MyJSON');
57
        $field->setReturnType('array');
58
        $field->setValue($this->getFixture('array'));
59
        // Assert current value
60
        $this->assertEquals([33.3333], $field->query('$.[6]'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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...
61
        // Now update it...
62
        $field->setValue(99.99, null, '$.[6]');
63
        // Assert new value
64
        $this->assertEquals([99.99], $field->query('$.[6]'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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
        
66
        // Invalid #1
67
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextSetValueTest>.

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
        $field->setValue(99.99, null, '$[6]'); // Invalid JSON path expression
69
70
        // Reset expected exception
71
        $this->setExpectedException(null);
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextSetValueTest>.

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
        // Invalid #2
74
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextSetValueTest>.

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...
75
        $field->setValue('true'); // Invalid JSON passed to setValue()
76
77
        // Reset expected exception
78
        $this->setExpectedException(null);
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextSetValueTest>.

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...
79
80
        // Invalid #3
81
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextSetValueTest>.

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...
82
        $field->setValue('{'); // Invalid JSON. Period.
83
84
        // Ensure default SS behaviour is respected with empty strings, evenm though it's invalid JSON
85
        $field->setValue('');
86
        $this->assertEquals('', $field->getValue());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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
    }
88
89
    /**
90
     * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a JSON object
91
     * 
92
     * Tests performing single and multiple updates
93
     */
94
    public function testSetValueOnSourceObject()
95
    {
96
        // Data Source: Object
97
        // Return Type: ARRAY
98
        // Expression: '$.[2]' The third item
99
        $field = JSONText::create('MyJSON');
100
        $field->setReturnType('array');
101
        $field->setValue($this->getFixture('object'));
102
        // Assert we cannot use array accessors at the root level of the source JSON _object_
103
        $this->assertEmpty($field->query('$.[2]'));
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<JSONTextSetValueTest>.

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
        // Assert current types and value
105
        $this->assertInternalType('array', $field->query('$.cars'));
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextSetValueTest>.

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...
106
        $this->assertCount(1, $field->query('$.cars')); // The "cars" key's value is an object returned as a single value array
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextSetValueTest>.

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
        $this->assertCount(3, $field->query('$.cars')[0]); //...with three classifications of car manufacturer by country
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextSetValueTest>.

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...
108
        $this->assertCount(2, $field->query('$.cars')[0]['british']);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextSetValueTest>.

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...
109
        $this->assertEquals('morris', $field->query('$.cars')[0]['british'][1]);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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
        // Now do a multiple update
112
        $newCars = [
113
            'american'  => ['ford', 'tesla'],
114
            'british'   => ['aston martin', 'austin', 'rover']
115
        ];
116
117
        $field->setValue($newCars, null, '$.cars');
118
        
119
        // Assert news types and value
120
        $this->assertInternalType('array', $field->query('$.cars'));
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextSetValueTest>.

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...
121
        $this->assertCount(1, $field->query('$.cars')); // The "cars" key's value is an object returned as a single value array
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextSetValueTest>.

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...
122
        $this->assertCount(2, $field->query('$.cars')[0]); //...with three classifications of car manufacturer by country
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextSetValueTest>.

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...
123
        $this->assertCount(3, $field->query('$.cars')[0]['british']);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextSetValueTest>.

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...
124
        $this->assertEquals('austin', $field->query('$.cars')[0]['british'][1]);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextSetValueTest>.

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...
125
        
126
        // So far we've used JSONPath to identify and update, let's try Postgres operators too
127
        // Now do attempt multiple update
128
        $newerCars = [
129
            'american'   => ['chrysler', 'general motors', 'edsel']
130
        ];
131
132
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextSetValueTest>.

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...
133
        $field->setValue($newerCars, null, '{"cars":"american"}'); // setValue() only takes JSONPath expressions
134
    }
135
    
136
    /**
137
     * Get the contents of a fixture
138
     * 
139
     * @param string $fixture
140
     * @return string
141
     */
142
    private function getFixture($fixture)
143
    {
144
        $files = $this->fixtures;
145
        return file_get_contents($files[$fixture]);
146
    }
147
148
}
149