JSONTextIntegrationTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 56.32 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 49
loc 87
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A testSetValueOnNestedArray() 25 25 1
A testSetValueOnUnNestedArray() 24 24 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
 */
8
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\Dev\TestOnly;
12
use PhpTek\JSONText\Dev\Fixture\MyAwesomeJSONModel;
13
14
class JSONTextIntegrationTest extends SapphireTest
15
{
16
    /**
17
     * @var string
18
     */
19
    protected static $fixture_file;
20
21
    /**
22
     * Modifies fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
23
     */
24
    public function __construct()
25
    {
26
        $dir = realpath(__DIR__);
27
        
28
        self::$fixture_file = $dir . '/fixtures/yml/MyAwesomeJSONModel.yml';
29
        
30
        parent::__construct();
31
    }
32
    
33
    /**
34
     * Allows us the ability to scaffold DB records for {@link TestOnly} implementations.
35
     * @var array
36
     */
37
    protected static $extra_dataobjects = [
38
        MyAwesomeJSONModel::class
39
    ];
40
41
    /**
42
     * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a nested JSON array
43
     * on a saved DataObject record.
44
     */
45 View Code Duplication
    public function testSetValueOnNestedArray()
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...
46
    {
47
        $model = $this->objFromFixture(MyAwesomeJSONModel::class, 'json-as-object');
48
        $expression = '$.cars.american.[0]';
49
        $field = $model->dbObject('MyJSON');
50
51
        // Primary assertion (JSON as return type is the default)
52
        $this->assertEquals('["buick"]', $field->query($expression));
53
        
54
        // How about now?
55
        $field->setValue('ford', null, $expression);
56
        $model->setField('MyJSON', $field->getValue());
57
        $model->write();
58
        $this->assertEquals('["ford"]', $field->query($expression));
59
60
        // Secondary assertion
61
        $field
62
            ->setValue('chrysler', null, $expression)
63
            ->setReturnType('array');
64
65
        // With chaining
66
        $model->setField('MyJSON', $field->getValue());
67
        $model->write();
68
        $this->assertEquals(['chrysler'], $field->query($expression));
69
    }
70
71
    /**
72
     * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a simple, un-nested JSON array
73
     * on a saved DataObject record.
74
     */
75 View Code Duplication
    public function testSetValueOnUnNestedArray()
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...
76
    {
77
        $model = $this->objFromFixture(MyAwesomeJSONModel::class, 'json-as-array');
78
        $expression = '$.[0]';
79
        $field = $model->dbObject('MyJSON');
80
81
        // Primary assertion (JSON as return type is the default)
82
        $this->assertEquals('["buick"]', $field->query($expression));
83
84
        // Secondary assertion
85
        $field->setValue('ford', null, $expression);
86
        $model->setField('MyJSON', $field->getValue());
87
        $model->write();
88
        $this->assertEquals('["ford"]', $field->query($expression));
89
        
90
        // With chaining
91
        $field
92
            ->setValue('chrysler', null, $expression)
93
            ->setReturnType('array');
94
        
95
        $model->setField('MyJSON', $field->getValue());
96
        $model->write();
97
        $this->assertEquals(['chrysler'], $field->query($expression));
98
    }
99
100
}
101