Completed
Push — master ( d54351...6ce5d4 )
by Rafael
03:34
created

Array2ObjectTest::testArray2Object()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 87
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 87
rs 8.6296
cc 2
eloc 58
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Array2Object;
13
use Rafrsr\LibArray2Object\Parser\CallableParser;
14
use Rafrsr\LibArray2Object\PropertyMatcher\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
            'name' => 'Dream Team',
23
            'Manager' => [
24
                'name' => 'Big Manager',
25
                'salary' => '$10000'
26
            ],
27
            'createdAt' => '2016-01-01',
28
            'points' => '25',
29
            'players' => [
30
                [
31
                    'name' => 'Player 1',
32
                    'number' => '1',
33
                    'height' => '1.80',
34
                    'regular' => 'true'
35
                ],
36
                [
37
                    'name' => 'Player 2',
38
                    'number' => '2',
39
                    'height' => '1.85',
40
                    'regular' => 0
41
                ],
42
                [
43
                    'name' => 'Player 3',
44
                    'number' => '3',
45
                    'height' => '1.78',
46
                    'regular' => 'yes'
47
                ]
48
            ],
49
            'scores' => [
50
                '2016' => '29',
51
                '2015' => '28',
52
                '2014' => '30',
53
            ]
54
        ];
55
56
        //register custom parser
57
        Array2Object::registerParser(
58
            new CallableParser(
59
                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...
60
                    if ($property->name === 'salary') {
61
                        $value = str_replace('$', null, $value);
62
                    }
63
64
                    return $value;
65
                }
66
            )
67
        );
68
69
        /** @var Team $team */
70
        $team = Array2Object::createObject(Team::class, $teamArray);
71
        static::assertEquals('Dream Team', $team->getName());
72
        static::assertEquals(25, $team->getPoints());
73
        static::assertEquals(29, $team->getScores()[2016]);
74
        static::assertEquals('2016-01-01', $team->getCreatedAt()->format('Y-m-d'));
75
76
        static::assertEquals('Big Manager', $team->getManager()->getName());
77
        static::assertEquals(10000, $team->getManager()->getSalary());
78
79
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
80
        static::assertEquals(1, $team->getPlayers()[0]->getNumber());
81
        static::assertEquals(1.80, $team->getPlayers()[0]->getHeight());
82
        static::assertTrue($team->getPlayers()[0]->isRegular());
83
84
        static::assertEquals('Player 2', $team->getPlayers()[1]->getName());
85
        static::assertEquals(2, $team->getPlayers()[1]->getNumber());
86
        static::assertEquals(1.85, $team->getPlayers()[1]->getHeight());
87
        static::assertFalse($team->getPlayers()[1]->isRegular());
88
89
        static::assertTrue($team->getPlayers()[2]->isRegular());
90
91
        static::assertInternalType('string', $team->getName());
92
        static::assertInternalType('integer', $team->getPoints());
93
        static::assertInternalType('float', $team->getManager()->getSalary());
94
        static::assertInternalType('boolean', $team->getPlayers()[0]->isRegular());
95
        static::assertInternalType('float', $team->getPlayers()[0]->getHeight());
96
        static::assertInternalType('boolean', $team->getPlayers()[2]->isRegular());
97
        static::assertInternalType('integer', $team->getScores()[2016]);
98
99
        $teamArray = [
100
            'name' => 'New Name'
101
        ];
102
103
        Array2Object::populate($team, $teamArray);
104
        static::assertEquals('New Name', $team->getName());
105
    }
106
107
    public function testPopulateObjectError()
108
    {
109
        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...
110
        Array2Object::populate(Team::class, []);
111
    }
112
113
    public function testCreateObjectError()
114
    {
115
        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...
116
        Array2Object::createObject(new Team(), []);
0 ignored issues
show
Documentation introduced by
new \Rafrsr\LibArray2Object\Tests\Fixtures\Team() is of type object<Rafrsr\LibArray2O...ct\Tests\Fixtures\Team>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
    }
118
119
    public function testSettingPropertyMatcher()
120
    {
121
        Array2Object::setPropertyMatcher(
122
            new MapMatcher(
123
                [
124
                    'name' => 'nombre'
125
                ]
126
            )
127
        );
128
        /** @var Team $team */
129
        $team = Array2Object::createObject(Team::class, ['nombre' => 'Team']);
130
        static::assertEquals('Team', $team->getName());
131
    }
132
}
133