Completed
Push — master ( d068c5...99fa9d )
by Antonio
03:22 queued 58s
created

ArrayHelperTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetValueFromObject() 0 10 1
A testRemove() 0 9 1
B testGetValue() 0 27 1
B valueProvider() 0 28 1
1
<?php
2
namespace Da\Mailer\Test\Helper;
3
4
use Da\Mailer\Helper\ArrayHelper;
5
use PHPUnit_Framework_TestCase;
6
7
class Post
8
{
9
    public $id = 'VII';
10
    public $title = 'Star Wars: The Force Awakens';
11
}
12
13
class ArrayHelperTest extends PHPUnit_Framework_TestCase {
14
15
    public function testGetValueFromObject()
16
    {
17
        $post = new Post();
18
19
        $id = ArrayHelper::getValue($post, 'id');
20
        $title = ArrayHelper::getValue($post, 'title');
21
22
        $this->assertEquals($post->id, $id);
23
        $this->assertEquals($post->title, $title);
24
    }
25
26
    public function testRemove()
27
    {
28
        $array = ['name' => 'b', 'age' => 3];
29
        $name = ArrayHelper::remove($array, 'name');
30
        $this->assertEquals($name, 'b');
31
        $this->assertEquals($array, ['age' => 3]);
32
        $default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue');
33
        $this->assertEquals('defaultValue', $default);
34
    }
35
36
    /**
37
     * @dataProvider valueProvider
38
     */
39
    public function testGetValue($key, $expected, $default = null)
40
    {
41
        $array = [
42
            'name' => 'test',
43
            'date' => '31-12-2116',
44
            'post' => [
45
                'id' => 5,
46
                'author' => [
47
                    'name' => 'Darth',
48
                    'profile' => [
49
                        'title' => 'Sith',
50
                    ],
51
                ],
52
            ],
53
            'admin.firstname' => 'Obiwoan',
54
            'admin.lastname' => 'Kenovi',
55
            'admin' => [
56
                'lastname' => 'Vader',
57
            ],
58
            'version' => [
59
                '1.0' => [
60
                    'status' => 'released'
61
                ]
62
            ],
63
        ];
64
        $this->assertEquals($expected, ArrayHelper::getValue($array, $key, $default));
65
    }
66
67
    public function valueProvider()
68
    {
69
        return [
70
            ['name', 'test'],
71
            ['noname', null],
72
            ['noname', 'test', 'test'],
73
            ['post.id', 5],
74
            ['post.id', 5, 'test'],
75
            ['nopost.id', null],
76
            ['nopost.id', 'test', 'test'],
77
            ['post.author.name', 'Darth'],
78
            ['post.author.noname', null],
79
            ['post.author.noname', 'test', 'test'],
80
            ['post.author.profile.title', 'Sith'],
81
            ['admin.firstname', 'Obiwoan'],
82
            ['admin.firstname', 'Obiwoan', 'test'],
83
            ['admin.lastname', 'Kenovi'],
84
            [
85
                function ($array, $defaultValue) {
86
                    return $array['date'] . $defaultValue;
87
                },
88
                '31-12-2116test',
89
                'test'
90
            ],
91
            [['version', '1.0', 'status'], 'released'],
92
            [['version', '1.0', 'date'], 'defaultValue', 'defaultValue'],
93
        ];
94
    }
95
}
96