JSONTextBasicTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 162
Duplicated Lines 45.68 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 74
loc 162
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A setUp() 0 6 1
A testGetValueAsJSONStore() 0 7 1
A testFirst() 33 33 1
A testLast() 33 33 1
A testNth() 0 33 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
 * @todo Add 'object' fixture to each
8
 */
9
10
use PhpTek\JSONText\ORM\FieldType\JSONText;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\ORM\FieldType\DBVarchar;
13
14
class JSONTextBasicTest extends SapphireTest
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $fixtures = [
20
        'array'     => 'fixtures/json/array.json',
21
        'object'    => 'fixtures/json/object.json'
22
    ];
23
24
    /**
25
     * @var \JSONText\ORM\FieldType\JSONText
26
     */
27
    protected $sut;
28
29
    /**
30
     * JSONTextTest constructor.
31
     *
32
     * Modifies fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
33
     */
34 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...
35
    {
36
        foreach($this->fixtures as $name => $path) {
37
            $this->fixtures[$name] = realpath(__DIR__) . '/' . $path;
38
        }
39
        
40
        parent::__construct();
41
    }
42
43
    /**
44
     * Setup the System Under Test for this test suite.
45
     */
46
    public function setUp()
47
    {
48
        parent::setUp();
49
50
        $this->sut = JSONText::create('MyJSON');
0 ignored issues
show
Documentation Bug introduced by
It seems like \PhpTek\JSONText\ORM\Fie...NText::create('MyJSON') of type object<PhpTek\JSONText\ORM\FieldType\JSONText> is incompatible with the declared type object<JSONText\ORM\FieldType\JSONText> of property $sut.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
    }
52
53
    public function testGetValueAsJSONStore()
54
    {
55
        $field = $this->sut;
56
57
        $field->setValue('');
58
        $this->assertEquals([], $field->getStoreAsArray());
59
    }
60
61 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...
62
    {
63
        $field = $this->sut;
64
65
        // Test: Source data is simple JSON array
66
        // Return type: Array
67
        $field->setReturnType('array');
68
        $field->setValue($this->getFixture('array'));
69
        $this->assertInternalType('array', $field->first());
70
        $this->assertCount(1, $field->first());
71
        $this->assertEquals([0 => 'great wall'], $field->first());
72
73
        // Test: Source data is simple JSON array
74
        // Return type: JSON
75
        $field->setReturnType('json');
76
        $field->setValue($this->getFixture('array'));
77
        $this->assertInternalType('string', $field->first());
78
        $this->assertEquals('["great wall"]', $field->first());
79
80
        // Test: Source data is simple JSON array
81
        // Return type: SilverStripe
82
        $field->setReturnType('silverstripe');
83
        $field->setValue($this->getFixture('array'));
84
        $this->assertInternalType('array', $field->first());
85
        $this->assertInstanceOf('\SilverStripe\ORM\FieldType\DBVarchar', $field->first()[0]);
86
        $this->assertEquals('great wall', $field->first()[0]->getValue());
87
88
        // Test: Empty
89
        $field->setReturnType('array');
90
        $field->setValue('');
91
        $this->assertInternalType('array', $field->first());
92
        $this->assertCount(0, $field->first());
93
    }
94
95 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...
96
    {
97
        $field = $this->sut;
98
99
        // Test: Source data is simple JSON array
100
        // Return type: Array
101
        $field->setReturnType('array');
102
        $field->setValue($this->getFixture('array'));
103
        $this->assertInternalType('array', $field->last());
104
        $this->assertCount(1, $field->last());
105
        $this->assertEquals([6 => 33.3333], $field->last());
106
107
        // Test: Source data is simple JSON array
108
        // Return type: JSON
109
        $field->setReturnType('json');
110
        $field->setValue($this->getFixture('array'));
111
        $this->assertInternalType('string', $field->last());
112
        $this->assertEquals('{"6":33.3333}', $field->last());
113
114
        // Test: Source data is simple JSON array
115
        // Return type: SilverStripe
116
        $field->setReturnType('silverstripe');
117
        $field->setValue($this->getFixture('array'));
118
        $this->assertInternalType('array', $field->last());
119
        $this->assertInstanceOf('\SilverStripe\ORM\FieldType\DBFloat', $field->last()[6]);
120
        $this->assertEquals(33.3333, $field->last()[6]->getValue());
121
122
        // Test: Empty
123
        $field->setReturnType('array');
124
        $field->setValue('');
125
        $this->assertInternalType('array', $field->last());
126
        $this->assertCount(0, $field->last());
127
    }
128
129
    public function testNth()
130
    {
131
        $field = $this->sut;
132
133
        // Test: Source data is simple JSON array
134
        // Return type: Array
135
        $field->setReturnType('array');
136
        $field->setValue($this->getFixture('array'));
137
        $this->assertInternalType('array', $field->nth(1));
138
        $this->assertCount(1, $field->nth(1));
139
        $this->assertEquals([1 => true], $field->nth(1));
140
141
        // Test: Source data is simple JSON array
142
        // Return type: JSON
143
        $field->setReturnType('json');
144
        $field->setValue($this->getFixture('array'));
145
        $this->assertInternalType('string', $field->nth(1));
146
        $this->assertEquals('{"1":true}', $field->nth(1));
147
148
        // Test: Source data is simple JSON array
149
        // Return type: SilverStripe
150
        $field->setReturnType('silverstripe');
151
        $field->setValue($this->getFixture('array'));
152
        $this->assertInternalType('array', $field->nth(1));
153
        $this->assertInstanceOf('\SilverStripe\ORM\FieldType\DBBoolean', $field->nth(1)[1]);
154
        $this->assertEquals(true, $field->nth(1)[1]->getValue());
155
156
        // Test: Empty
157
        $field->setReturnType('array');
158
        $field->setValue('');
159
        $this->assertInternalType('array', $field->nth(1));
160
        $this->assertCount(0, $field->nth(1));
161
    }
162
163
    /**
164
     * Get the contents of a fixture
165
     *
166
     * @param string $fixture
167
     * @return string
168
     */
169
    private function getFixture($fixture)
170
    {
171
        $files = $this->fixtures;
172
        return file_get_contents($files[$fixture]);
173
    }
174
175
}
176