Completed
Push — master ( ff7a5a...3d4150 )
by Nicolas
03:12
created

JsonTest::it_sets_a_key_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Nwidart\Modules\Tests;
2
3
use Nwidart\Modules\Json;
4
5
class JsonTest extends BaseTestCase
6
{
7
    /**
8
     * @var Json
9
     */
10
    private $json;
11
12
    public function setUp()
13
    {
14
        parent::setUp();
15
        $path = __DIR__ . '/stubs/module.json';
16
        $this->json = new Json($path, $this->app['files']);
17
    }
18
19
    /** @test */
20
    public function it_gets_the_file_path()
21
    {
22
        $path = __DIR__ . '/stubs/module.json';
23
24
        $this->assertEquals($path, $this->json->getPath());
25
    }
26
27
    /** @test */
28
    public function it_gets_attributes_from_json_file()
29
    {
30
        $this->assertEquals('Order', $this->json->get('name'));
31
        $this->assertEquals('order', $this->json->get('alias'));
32
        $this->assertEquals('My demo module', $this->json->get('description'));
33
        $this->assertEquals('0.1', $this->json->get('version'));
34
        $this->assertEquals(['my', 'stub', 'module'], $this->json->get('keywords'));
35
        $this->assertEquals(1, $this->json->get('active'));
36
        $this->assertEquals(1, $this->json->get('order'));
37
    }
38
39
    /** @test */
40
    public function it_reads_attributes_from_magic_get_method()
41
    {
42
        $this->assertEquals('Order', $this->json->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
        $this->assertEquals('order', $this->json->alias);
0 ignored issues
show
Documentation introduced by
The property alias does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
        $this->assertEquals('My demo module', $this->json->description);
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
        $this->assertEquals('0.1', $this->json->version);
0 ignored issues
show
Documentation introduced by
The property version does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
        $this->assertEquals(['my', 'stub', 'module'], $this->json->keywords);
0 ignored issues
show
Documentation introduced by
The property keywords does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
        $this->assertEquals(1, $this->json->active);
0 ignored issues
show
Documentation introduced by
The property active does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
        $this->assertEquals(1, $this->json->order);
0 ignored issues
show
Documentation introduced by
The property order does not exist on object<Nwidart\Modules\Json>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
    }
50
51
    /** @test */
52
    public function it_makes_json_class()
53
    {
54
        $path = __DIR__ . '/stubs/module.json';
55
        $json = Json::make($path, $this->app['files']);
56
57
        $this->assertInstanceOf(Json::class, $json);
58
    }
59
60
    /** @test */
61
    public function it_sets_a_path()
62
    {
63
        $path = __DIR__ . '/stubs/module.json';
64
        $this->assertEquals($path, $this->json->getPath());
65
66
        $this->json->setPath('some/path.json');
67
        $this->assertEquals('some/path.json', $this->json->getPath());
68
    }
69
70
    /** @test */
71
    public function it_decodes_json()
72
    {
73
        $expected = '{
74
    "name": "Order",
75
    "alias": "order",
76
    "description": "My demo module",
77
    "version": "0.1",
78
    "keywords": [
79
        "my",
80
        "stub",
81
        "module"
82
    ],
83
    "active": 1,
84
    "order": 1,
85
    "providers": [
86
        "Modules\\\Order\\\Providers\\\OrderServiceProvider",
87
        "Modules\\\Order\\\Providers\\\EventServiceProvider",
88
        "Modules\\\Order\\\Providers\\\RouteServiceProvider"
89
    ],
90
    "aliases": [],
91
    "files": []
92
}';
93
        $this->assertEquals($expected, $this->json->toJsonPretty());
94
    }
95
96
    /** @test */
97
    public function it_sets_a_key_value()
98
    {
99
        $this->json->set('key', 'value');
100
101
        $this->assertEquals('value', $this->json->get('key'));
102
    }
103
104
    /** @test */
105
    public function it_can_be_casted_to_string()
106
    {
107
        $expected = '{
108
    "name": "Order",
109
    "alias": "order",
110
    "description": "My demo module",
111
    "version": "0.1",
112
    "keywords": [
113
        "my",
114
        "stub",
115
        "module"
116
    ],
117
    "active": 1,
118
    "order": 1,
119
    "providers": [
120
        "Modules\\\Order\\\Providers\\\OrderServiceProvider",
121
        "Modules\\\Order\\\Providers\\\EventServiceProvider",
122
        "Modules\\\Order\\\Providers\\\RouteServiceProvider"
123
    ],
124
    "aliases":{},
125
    "files": [
126
    ]
127
}
128
';
129
        $this->assertEquals($expected, (string)$this->json);
130
    }
131
}
132