Completed
Push — master ( 4ef5f0...ef9d83 )
by Rafael
04:26
created

Array2ObjectTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 6
c 7
b 0
f 0
lcom 1
cbo 7
dl 0
loc 161
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testArray2Object() 0 89 2
B testArray2ObjectWithNestingChildren() 0 39 1
A testPopulateObjectError() 0 5 1
A testCreateObjectError() 0 5 1
A testSettingPropertyMatcher() 0 13 1
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object\Tests;
11
12
use Rafrsr\LibArray2Object\Array2ObjectBuilder;
13
use Rafrsr\LibArray2Object\Parser\CallableParser;
14
use Rafrsr\LibArray2Object\Matcher\MapMatcher;
15
use Rafrsr\LibArray2Object\Tests\Fixtures\Team;
16
17
class Array2ObjectTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testArray2Object()
20
    {
21
        $teamArray = [
22
            'id' => 1,//read-only
23
            'name' => 'Dream Team',
24
            'Manager' => [
25
                'name' => 'Big Manager',
26
                'salary' => '$10000'
27
            ],
28
            'createdAt' => '2016-01-01',
29
            'points' => '25',
30
            'players' => [
31
                [
32
                    'name' => 'Player 1',
33
                    'number' => '1',
34
                    'height' => '1.80',
35
                    'regular' => 'true'
36
                ],
37
                [
38
                    'name' => 'Player 2',
39
                    'number' => '2',
40
                    'height' => '1.85',
41
                    'regular' => 0
42
                ],
43
                [
44
                    'name' => 'Player 3',
45
                    'number' => '3',
46
                    'height' => '1.78',
47
                    'regular' => 'yes'
48
                ]
49
            ],
50
            'scores' => [
51
                '2016' => '29',
52
                '2015' => '28',
53
                '2014' => '30',
54
            ]
55
        ];
56
57
        //register custom parser
58
        $array2Object = Array2ObjectBuilder::create()->addParser(
59
            new CallableParser(
60
                function ($value, $type, \ReflectionProperty $property, $object) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
                    if ($property->getName() === 'salary') {
62
                        $value = str_replace('$', null, $value);
63
                    }
64
65
                    return $value;
66
                }
67
            )
68
        )->build();
69
70
        /** @var Team $team */
71
        $team = $array2Object->createObject(Team::class, $teamArray);
72
        static::assertNull($team->getId());
73
        static::assertEquals('Dream Team', $team->getName());
74
        static::assertEquals(25, $team->getPoints());
75
        static::assertEquals(29, $team->getScores()[2016]);
76
        static::assertEquals('2016-01-01', $team->getCreatedAt()->format('Y-m-d'));
77
78
        static::assertEquals('Big Manager', $team->getManager()->getName());
79
        static::assertEquals(10000, $team->getManager()->getSalary());
80
81
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
82
        static::assertEquals(1, $team->getPlayers()[0]->getNumber());
83
        static::assertEquals(1.80, $team->getPlayers()[0]->getHeight());
84
        static::assertTrue($team->getPlayers()[0]->isRegular());
85
86
        static::assertEquals('Player 2', $team->getPlayers()[1]->getName());
87
        static::assertEquals(2, $team->getPlayers()[1]->getNumber());
88
        static::assertEquals(1.85, $team->getPlayers()[1]->getHeight());
89
        static::assertFalse($team->getPlayers()[1]->isRegular());
90
91
        static::assertTrue($team->getPlayers()[2]->isRegular());
92
93
        static::assertInternalType('string', $team->getName());
94
        static::assertInternalType('integer', $team->getPoints());
95
        static::assertInternalType('float', $team->getManager()->getSalary());
96
        static::assertInternalType('boolean', $team->getPlayers()[0]->isRegular());
97
        static::assertInternalType('float', $team->getPlayers()[0]->getHeight());
98
        static::assertInternalType('boolean', $team->getPlayers()[2]->isRegular());
99
        static::assertInternalType('integer', $team->getScores()[2016]);
100
101
        $teamArray = [
102
            'name' => 'New Name'
103
        ];
104
105
        $array2Object->populate($team, $teamArray);
106
        static::assertEquals('New Name', $team->getName());
107
    }
108
109
    /**
110
     * @see https://github.com/rafrsr/lib-array2object/issues/1
111
     */
112
    public function testArray2ObjectWithNestingChildren()
113
    {
114
        $teamArray = [
115
            'name' => 'Dream Team',
116
            'players' => [
117
                'player' => [
118
                    [
119
                        'name' => 'Player 1',
120
                        'number' => '1',
121
                        'height' => '1.80',
122
                        'regular' => 'true'
123
                    ],
124
                    [
125
                        'name' => 'Player 2',
126
                        'number' => '2',
127
                        'height' => '1.85',
128
                        'regular' => 0
129
                    ],
130
                ]
131
            ]
132
        ];
133
134
        //register custom parser
135
        $array2Object = Array2ObjectBuilder::create()->build();
136
137
        /** @var Team $team */
138
        $team = $array2Object->createObject(Team::class, $teamArray);
139
        static::assertEquals('Dream Team', $team->getName());
140
141
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
142
        static::assertEquals(1, $team->getPlayers()[0]->getNumber());
143
        static::assertEquals(1.80, $team->getPlayers()[0]->getHeight());
144
        static::assertTrue($team->getPlayers()[0]->isRegular());
145
146
        static::assertEquals('Player 2', $team->getPlayers()[1]->getName());
147
        static::assertEquals(2, $team->getPlayers()[1]->getNumber());
148
        static::assertEquals(1.85, $team->getPlayers()[1]->getHeight());
149
        static::assertFalse($team->getPlayers()[1]->isRegular());
150
    }
151
152
    public function testPopulateObjectError()
153
    {
154
        static::setExpectedException('\InvalidArgumentException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
155
        Array2ObjectBuilder::create()->build()->populate(Team::class, []);
156
    }
157
158
    public function testCreateObjectError()
159
    {
160
        static::setExpectedException('\InvalidArgumentException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
161
        Array2ObjectBuilder::create()->build()->createObject(new Team(), []);
162
    }
163
164
    public function testSettingPropertyMatcher()
165
    {
166
        $array2Object = Array2ObjectBuilder::create()->setMatcher(
167
            new MapMatcher(
168
                [
169
                    'name' => 'nombre'
170
                ]
171
            )
172
        )->build();
173
        /** @var Team $team */
174
        $team = $array2Object->createObject(Team::class, ['nombre' => 'Team']);
175
        static::assertEquals('Team', $team->getName());
176
    }
177
}
178