Passed
Push — master ( 66adc4...f8e97d )
by Alexander
10:22
created

ArraySorterTest::testMultisort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 55
rs 9.296
c 1
b 0
f 0
cc 1
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
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays\Tests;
6
7
use InvalidArgumentException;
8
use stdClass;
9
use PHPUnit\Framework\TestCase;
10
use Yiisoft\Arrays\ArraySorter;
11
12
final class ArraySorterTest extends TestCase
13
{
14
    public function testMultisort(): void
15
    {
16
        // empty key
17
        $dataEmpty = [];
18
        ArraySorter::multisort($dataEmpty, '');
19
        $this->assertEquals([], $dataEmpty);
20
21
        // single key
22
        $array = [
23
            ['name' => 'b', 'age' => 3],
24
            ['name' => 'a', 'age' => 1],
25
            ['name' => 'c', 'age' => 2],
26
        ];
27
        ArraySorter::multisort($array, 'name');
28
        $this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
29
        $this->assertEquals(['name' => 'b', 'age' => 3], $array[1]);
30
        $this->assertEquals(['name' => 'c', 'age' => 2], $array[2]);
31
32
        // multiple keys
33
        $array = [
34
            ['name' => 'b', 'age' => 3],
35
            ['name' => 'a', 'age' => 2],
36
            ['name' => 'a', 'age' => 1],
37
        ];
38
        ArraySorter::multisort($array, ['name', 'age']);
39
        $this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
40
        $this->assertEquals(['name' => 'a', 'age' => 2], $array[1]);
41
        $this->assertEquals(['name' => 'b', 'age' => 3], $array[2]);
42
43
        // case-insensitive
44
        $array = [
45
            ['name' => 'a', 'age' => 3],
46
            ['name' => 'b', 'age' => 2],
47
            ['name' => 'B', 'age' => 4],
48
            ['name' => 'A', 'age' => 1],
49
        ];
50
51
        ArraySorter::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING, SORT_REGULAR]);
52
        $this->assertEquals(['name' => 'A', 'age' => 1], $array[0]);
53
        $this->assertEquals(['name' => 'B', 'age' => 4], $array[1]);
54
        $this->assertEquals(['name' => 'a', 'age' => 3], $array[2]);
55
        $this->assertEquals(['name' => 'b', 'age' => 2], $array[3]);
56
57
        ArraySorter::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING | SORT_FLAG_CASE, SORT_REGULAR]);
58
        $this->assertEquals(['name' => 'A', 'age' => 1], $array[0]);
59
        $this->assertEquals(['name' => 'a', 'age' => 3], $array[1]);
60
        $this->assertEquals(['name' => 'b', 'age' => 2], $array[2]);
61
        $this->assertEquals(['name' => 'B', 'age' => 4], $array[3]);
62
63
        ArraySorter::multisort($array, fn ($item) => ['age', 'name'], SORT_DESC);
64
65
        $this->assertEquals(['name' => 'B', 'age' => 4], $array[0]);
66
        $this->assertEquals(['name' => 'a', 'age' => 3], $array[1]);
67
        $this->assertEquals(['name' => 'b', 'age' => 2], $array[2]);
68
        $this->assertEquals(['name' => 'A', 'age' => 1], $array[3]);
69
    }
70
71
    public function testMultisortNestedObjects(): void
72
    {
73
        $obj1 = new stdClass();
74
        $obj1->type = 'def';
75
        $obj1->owner = $obj1;
76
77
        $obj2 = new stdClass();
78
        $obj2->type = 'abc';
79
        $obj2->owner = $obj2;
80
81
        $obj3 = new stdClass();
82
        $obj3->type = 'abc';
83
        $obj3->owner = $obj3;
84
85
        $models = [
86
            $obj1,
87
            $obj2,
88
            $obj3,
89
        ];
90
91
        $this->assertEquals($obj2, $obj3);
92
93
        ArraySorter::multisort($models, 'type', SORT_ASC);
94
        $this->assertEquals($obj2, $models[0]);
95
        $this->assertEquals($obj3, $models[1]);
96
        $this->assertEquals($obj1, $models[2]);
97
98
        ArraySorter::multisort($models, 'type', SORT_DESC);
99
        $this->assertEquals($obj1, $models[0]);
100
        $this->assertEquals($obj2, $models[1]);
101
        $this->assertEquals($obj3, $models[2]);
102
    }
103
104
    public function testMultisortUseSort(): void
105
    {
106
        // single key
107
        $orders = [
108
            'name' => SORT_ASC,
109
        ];
110
111
        $array = [
112
            ['name' => 'b', 'age' => 3],
113
            ['name' => 'a', 'age' => 1],
114
            ['name' => 'c', 'age' => 2],
115
        ];
116
        ArraySorter::multisort($array, array_keys($orders), array_values($orders));
117
        $this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
118
        $this->assertEquals(['name' => 'b', 'age' => 3], $array[1]);
119
        $this->assertEquals(['name' => 'c', 'age' => 2], $array[2]);
120
121
        // multiple keys
122
        $orders = [
123
            'name' => SORT_ASC,
124
            'age' => SORT_DESC,
125
        ];
126
127
        $array = [
128
            ['name' => 'b', 'age' => 3],
129
            ['name' => 'a', 'age' => 2],
130
            ['name' => 'a', 'age' => 1],
131
        ];
132
        ArraySorter::multisort($array, array_keys($orders), array_values($orders));
133
        $this->assertEquals(['name' => 'a', 'age' => 2], $array[0]);
134
        $this->assertEquals(['name' => 'a', 'age' => 1], $array[1]);
135
        $this->assertEquals(['name' => 'b', 'age' => 3], $array[2]);
136
    }
137
138
    public function testMultisortClosure(): void
139
    {
140
        $changelog = [
141
            '- Enh #123: test1',
142
            '- Bug #125: test2',
143
            '- Bug #123: test2',
144
            '- Enh: test3',
145
            '- Bug: test4',
146
        ];
147
        $i = 0;
148
        ArraySorter::multisort(
149
            $changelog,
150
            static function ($line) use (&$i) {
151
                if (preg_match('/^- (Enh|Bug)( #\d+)?: .+$/', $line, $m)) {
152
                    $o = ['Bug' => 'C', 'Enh' => 'D'];
153
                    return $o[$m[1]] . ' ' . (!empty($m[2]) ? $m[2] : 'AAAA' . $i++);
154
                }
155
156
                return 'B' . $i++;
157
            },
158
            SORT_ASC,
159
            SORT_NATURAL
160
        );
161
        $this->assertEquals(
162
            [
163
                '- Bug #123: test2',
164
                '- Bug #125: test2',
165
                '- Bug: test4',
166
                '- Enh #123: test1',
167
                '- Enh: test3',
168
            ],
169
            $changelog
170
        );
171
    }
172
173
    public function testMultisortInvalidArgumentExceptionDirection(): void
174
    {
175
        $this->expectException(InvalidArgumentException::class);
176
        $data = ['foo' => 'bar'];
177
        ArraySorter::multisort($data, ['foo'], []);
178
    }
179
180
    public function testMultisortInvalidArgumentExceptionSortFlag(): void
181
    {
182
        $this->expectException(InvalidArgumentException::class);
183
        $data = ['foo' => 'bar'];
184
        ArraySorter::multisort($data, ['foo'], ['foo'], []);
185
    }
186
}
187