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

JSONTextIntegrationTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 64 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 3
c 1
b 1
f 1
lcom 0
cbo 1
dl 48
loc 75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B testSetValueOnNestedArray() 24 24 1
B 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 JSONText\Fields;
10
use JSONText\Exceptions;
11
12
class JSONTextIntegrationTest 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
    /**
16
     * @var string
17
     */
18
    protected static $fixture_file = '/tests/fixtures/yml/MyAwesomeJSONModel.yml';
19
20
    /**
21
     * Modifies fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
22
     */
23
    public function __construct()
24
    {
25
        self::$fixture_file = MODULE_DIR . '/tests/fixtures/yml/MyAwesomeJSONModel.yml';
26
    }
27
28
    /**
29
     * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a nested JSON array
30
     * on a saved DataObject record.
31
     */
32 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...
33
    {
34
        $model = $this->objFromFixture('MyAwesomeJSONModel', 'json-as-object');
35
        $expression = '$.cars.american.[0]';
36
        $field = $model->dbObject('MyJSON');
37
        
38
        // What's the value at $expression now? (JSON as return type is the default)
39
        $this->assertEquals('["buick"]', $field->query($expression));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextIntegrationTest>.

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...
40
        
41
        // How about now?
42
        $field->setValue('ford', null, $expression);
43
        $model->setField('MyJSON', $field->getValue());
44
        $model->write();
45
        $this->assertEquals('["ford"]', $field->query($expression));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextIntegrationTest>.

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
        // And now? (With chaining)
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
        $field
49
            ->setValue('chrysler', null, $expression)
50
            ->setReturnType('array');
51
52
        $model->setField('MyJSON', $field->getValue());
53
        $model->write();
54
        $this->assertEquals(['chrysler'], $field->query($expression));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextIntegrationTest>.

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...
55
    }
56
57
    /**
58
     * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a simple, un-nested JSON array
59
     * on a saved DataObject record.
60
     */
61 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...
62
    {
63
        $model = $this->objFromFixture('MyAwesomeJSONModel', 'json-as-array');
64
        $expression = '$.[0]';
65
        $field = $model->dbObject('MyJSON');
66
67
        // What's the value at $expression now? (JSON as return type is the default)
68
        $this->assertEquals('["buick"]', $field->query($expression));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextIntegrationTest>.

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...
69
70
        // How about now?
71
        $field->setValue('ford', null, $expression);
72
        $model->setField('MyJSON', $field->getValue());
73
        $model->write();
74
        $this->assertEquals('["ford"]', $field->query($expression));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextIntegrationTest>.

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
        // And now? (With chaining)
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
        $field
78
            ->setValue('chrysler', null, $expression)
79
            ->setReturnType('array');
80
        
81
        $model->setField('MyJSON', $field->getValue());
82
        $model->write();
83
        $this->assertEquals(['chrysler'], $field->query($expression));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<JSONTextIntegrationTest>.

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...
84
    }
85
86
}
87
88
/**
89
 * @package silverstripe-jsontext
90
 */
91
class MyAwesomeJSONModel extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
92
{
93
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
94
        'MyJSON' => '\JSONText\Fields\JSONText'
95
    ];
96
}