Completed
Push — master ( 9d18b4...301d75 )
by Russell
03:01
created

JSONTextBasicTest::testgetValueAsIterable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
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
        'invalid'   => 'tests/fixtures/json/invalid.json'
22
    ];
23
24
    /**
25
     * JSONTextTest constructor.
26
     * 
27
     * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
28
     */
29
    public function __construct()
30
    {
31 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...
32
            $this->fixtures[$name] = MODULE_DIR . '/' . $path;
33
        }
34
    }
35
36
    public function testgetValueAsIterable()
37
    {
38
        $field = JSONText\Fields\JSONText::create('MyJSON');
39
        $field->setValue($this->getFixture('invalid'));
40
        $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...
41
        $this->assertEquals(['chinese' => 'great wall'], $field->getJSONStore());
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...
42
43
        $field = JSONText\Fields\JSONText::create('MyJSON');
44
        $field->setValue('');
45
        $this->assertEquals([], $field->getJSONStore());
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...
46
    }
47
48 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...
49
    {
50
        // Test: Source data is simple JSON array
51
        // Return type: Array
52
        $field = JSONText\Fields\JSONText::create('MyJSON');
53
        $field->setReturnType('array');
54
        $field->setValue($this->getFixture('array'));
55
        $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...
56
        $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...
57
        $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...
58
59
        // Test: Source data is simple JSON array
60
        // Return type: JSON
61
        $field = JSONText\Fields\JSONText::create('MyJSON');
62
        $field->setReturnType('json');
63
        $field->setValue($this->getFixture('array'));
64
        $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...
65
        $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...
66
67
        // Test: Source data is simple JSON array
68
        // Return type: SilverStripe
69
        $field = JSONText\Fields\JSONText::create('MyJSON');
70
        $field->setReturnType('silverstripe');
71
        $field->setValue($this->getFixture('array'));
72
        $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...
73
        $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...
74
        $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...
75
76
        // Test: Empty
77
        $field = JSONText\Fields\JSONText::create('MyJSON');
78
        $field->setReturnType('array');
79
        $field->setValue('');
80
        $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...
81
        $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...
82
83
        // Test: Invalid
84
        $field = JSONText\Fields\JSONText::create('MyJSON');
85
        $field->setReturnType('array');
86
        $field->setValue('{');
87
        $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...
88
        $field->first();
89
    }
90
91 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...
92
    {
93
        // Test: Source data is simple JSON array
94
        // Return type: Array
95
        $field = JSONText\Fields\JSONText::create('MyJSON');
96
        $field->setReturnType('array');
97
        $field->setValue($this->getFixture('array'));
98
        $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...
99
        $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...
100
        $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...
101
102
        // Test: Source data is simple JSON array
103
        // Return type: JSON
104
        $field = JSONText\Fields\JSONText::create('MyJSON');
105
        $field->setReturnType('json');
106
        $field->setValue($this->getFixture('array'));
107
        $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...
108
        $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...
109
110
        // Test: Source data is simple JSON array
111
        // Return type: SilverStripe
112
        $field = JSONText\Fields\JSONText::create('MyJSON');
113
        $field->setReturnType('silverstripe');
114
        $field->setValue($this->getFixture('array'));
115
        $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...
116
        $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...
117
        $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...
118
119
        // Test: Empty
120
        $field = JSONText\Fields\JSONText::create('MyJSON');
121
        $field->setReturnType('array');
122
        $field->setValue('');
123
        $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...
124
        $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...
125
126
        // Test: Invalid
127
        $field = JSONText\Fields\JSONText::create('MyJSON');
128
        $field->setReturnType('array');
129
        $field->setValue('{');
130
        $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...
131
        $field->last();
132
    }
133
134
    public function testNth()
135
    {
136
        // Test: Source data is simple JSON array
137
        // Return type: Array
138
        $field = JSONText\Fields\JSONText::create('MyJSON');
139
        $field->setReturnType('array');
140
        $field->setValue($this->getFixture('array'));
141
        $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...
142
        $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...
143
        $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...
144
145
        // Test: Source data is simple JSON array
146
        // Return type: JSON
147
        $field = JSONText\Fields\JSONText::create('MyJSON');
148
        $field->setReturnType('json');
149
        $field->setValue($this->getFixture('array'));
150
        $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...
151
        $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...
152
153
        // Test: Source data is simple JSON array
154
        // Return type: SilverStripe
155
        $field = JSONText\Fields\JSONText::create('MyJSON');
156
        $field->setReturnType('silverstripe');
157
        $field->setValue($this->getFixture('array'));
158
        $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...
159
        $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...
160
        $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...
161
162
        // Test: Empty
163
        $field = JSONText\Fields\JSONText::create('MyJSON');
164
        $field->setReturnType('array');
165
        $field->setValue('');
166
        $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...
167
        $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...
168
169
        // Test: Invalid
170
        $field = JSONText\Fields\JSONText::create('MyJSON');
171
        $field->setReturnType('array');
172
        $field->setValue('{');
173
        $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...
174
        $field->nth(1);
175
    }
176
    
177
    /**
178
     * Get the contents of a fixture
179
     * 
180
     * @param string $fixture
181
     * @return string
182
     */
183
    private function getFixture($fixture)
184
    {
185
        $files = $this->fixtures;
186
        return file_get_contents($files[$fixture]);
187
    }
188
189
}
190