Completed
Push — master ( f2c532...9eb09d )
by Russell
02:59
created

JSONTextBasicTest::testGetValueAsJSONStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package silverstripe-jsontext
5
 * @subpackage fields
6
 * @author Russell Michell <[email protected]>
7
 * @todo Add 'object' fixture to each
8
 */
9
10
use JSONText\Fields;
11
use JSONText\Exceptions;
12
13
class JSONTextBasicTest 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...
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $fixtures = [
19
        'array'     => 'tests/fixtures/json/array.json',
20
        'object'    => 'tests/fixtures/json/object.json'
21
    ];
22
23
    /**
24
     * @var \JSONText\Fields\JSONText
25
     */
26
    protected $sut;
27
28
    /**
29
     * JSONTextTest constructor.
30
     * 
31
     * Modifies fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
32
     */
33
    public function __construct()
34
    {
35 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...
36
            $this->fixtures[$name] = MODULE_DIR . '/' . $path;
37
        }
38
    }
39
40
    /**
41
     * Setup the System Under Test for this test suite.
42
     */
43
    public function setUp()
44
    {
45
        parent::setUp();
46
        
47
        $this->sut = JSONText\Fields\JSONText::create('MyJSON');
48
    }
49
50
    public function testGetValueAsJSONStore()
51
    {
52
        $field = $this->sut;
53
        
54
        $field->setValue('');
55
        $this->assertEquals([], $field->getStoreAsArray());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
56
        
57
        $field->setValue('{');
58
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextBasicTest>.

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...
59
        $field->getJSONStore();
60
    }
61
62 View Code Duplication
    public function testFirst()
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...
63
    {
64
        $field = $this->sut;
65
66
        // Test: Source data is simple JSON array
67
        // Return type: Array
68
        $field->setReturnType('array');
69
        $field->setValue($this->getFixture('array'));
70
        $this->assertInternalType('array', $field->first());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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->assertCount(1, $field->first());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextBasicTest>.

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
        $this->assertEquals([0 => 'great wall'], $field->first());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
73
74
        // Test: Source data is simple JSON array
75
        // Return type: JSON
76
        $field->setReturnType('json');
77
        $field->setValue($this->getFixture('array'));
78
        $this->assertInternalType('string', $field->first());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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
        $this->assertEquals('["great wall"]', $field->first());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
80
81
        // Test: Source data is simple JSON array
82
        // Return type: SilverStripe
83
        $field->setReturnType('silverstripe');
84
        $field->setValue($this->getFixture('array'));
85
        $this->assertInternalType('array', $field->first());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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->assertInstanceOf('Varchar', $field->first()[0]);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextBasicTest>.

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->assertEquals('great wall', $field->first()[0]->getValue());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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
89
        // Test: Empty
90
        $field->setReturnType('array');
91
        $field->setValue('');
92
        $this->assertInternalType('array', $field->first());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
93
        $this->assertCount(0, $field->first());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextBasicTest>.

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...
94
95
        // Test: Invalid
96
        $field->setReturnType('array');
97
        $field->setValue('{');
98
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextBasicTest>.

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...
99
        $field->first();
100
    }
101
102 View Code Duplication
    public function testLast()
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...
103
    {
104
        $field = $this->sut;
105
        
106
        // Test: Source data is simple JSON array
107
        // Return type: Array
108
        $field->setReturnType('array');
109
        $field->setValue($this->getFixture('array'));
110
        $this->assertInternalType('array', $field->last());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
111
        $this->assertCount(1, $field->last());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextBasicTest>.

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...
112
        $this->assertEquals([6 => 33.3333], $field->last());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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
        // Test: Source data is simple JSON array
115
        // Return type: JSON
116
        $field->setReturnType('json');
117
        $field->setValue($this->getFixture('array'));
118
        $this->assertInternalType('string', $field->last());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
119
        $this->assertEquals('{"6":33.3333}', $field->last());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
120
121
        // Test: Source data is simple JSON array
122
        // Return type: SilverStripe
123
        $field->setReturnType('silverstripe');
124
        $field->setValue($this->getFixture('array'));
125
        $this->assertInternalType('array', $field->last());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
126
        $this->assertInstanceOf('Float', $field->last()[6]);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextBasicTest>.

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...
127
        $this->assertEquals(33.3333, $field->last()[6]->getValue());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
128
129
        // Test: Empty
130
        $field->setReturnType('array');
131
        $field->setValue('');
132
        $this->assertInternalType('array', $field->last());
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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
        $this->assertCount(0, $field->last());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextBasicTest>.

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...
134
135
        // Test: Invalid
136
        $field->setReturnType('array');
137
        $field->setValue('{');
138
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextBasicTest>.

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...
139
        $field->last();
140
    }
141
142
    public function testNth()
143
    {
144
        $field = $this->sut;
145
        
146
        // Test: Source data is simple JSON array
147
        // Return type: Array
148
        $field->setReturnType('array');
149
        $field->setValue($this->getFixture('array'));
150
        $this->assertInternalType('array', $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
151
        $this->assertCount(1, $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextBasicTest>.

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...
152
        $this->assertEquals([1 => true], $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
153
154
        // Test: Source data is simple JSON array
155
        // Return type: JSON
156
        $field->setReturnType('json');
157
        $field->setValue($this->getFixture('array'));
158
        $this->assertInternalType('string', $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
159
        $this->assertEquals('{"1":true}', $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
160
161
        // Test: Source data is simple JSON array
162
        // Return type: SilverStripe
163
        $field->setReturnType('silverstripe');
164
        $field->setValue($this->getFixture('array'));
165
        $this->assertInternalType('array', $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
166
        $this->assertInstanceOf('Boolean', $field->nth(1)[1]);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<JSONTextBasicTest>.

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...
167
        $this->assertEquals(true, $field->nth(1)[1]->getValue());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextBasicTest>.

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...
168
169
        // Test: Empty
170
        $field->setReturnType('array');
171
        $field->setValue('');
172
        $this->assertInternalType('array', $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<JSONTextBasicTest>.

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...
173
        $this->assertCount(0, $field->nth(1));
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<JSONTextBasicTest>.

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...
174
175
        // Test: Invalid
176
        $field->setReturnType('array');
177
        $field->setValue('{');
178
        $this->setExpectedException('\JSONText\Exceptions\JSONTextException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<JSONTextBasicTest>.

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...
179
        $field->nth(1);
180
    }
181
    
182
    /**
183
     * Get the contents of a fixture
184
     * 
185
     * @param string $fixture
186
     * @return string
187
     */
188
    private function getFixture($fixture)
189
    {
190
        $files = $this->fixtures;
191
        return file_get_contents($files[$fixture]);
192
    }
193
194
}
195