Failed Conditions
Branch master (01ba0d)
by Arnold
06:58 queued 01:45
created

IterableSortKeysTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 10
1
<?php
2
3
namespace Jasny\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use function Jasny\iterable_sort_keys;
7
8
/**
9
 * @covers \Jasny\iterable_sort_keys
10
 */
11
class IterableSortKeysTest extends TestCase
12
{
13
    use ProvideIterablesTrait;
14
    use LazyExecutionIteratorTrait;
15
16
    protected $sorted = [
17
        "Alpha",
18
        "Bravo",
19
        "Charlie",
20
        "Delta",
21
        "Echo",
22
        "Foxtrot",
23
        "Golf",
24
        "Hotel",
25
        "India",
26
        "Juliet",
27
        "Kilo",
28
        "Lima",
29
        "Mike",
30
        "November",
31
        "Oscar",
32
        "Papa",
33
        "Quebec",
34
        "Romeo",
35
        "Sierra",
36
        "Tango",
37
        "Uniform",
38
        "Victor",
39
        "Whiskey",
40
        "X-ray",
41
        "Yankee",
42
        "Zulu"
43
    ];
44
45
    public function provider()
46
    {
47
        $keys = $this->sorted;
48
        shuffle($keys);
49
50
        $values = array_fill_keys($keys, null);
51
52
        return $this->provideIterables($values, false, false);
53
    }
54
55
    /**
56
     * @dataProvider provider
57
     */
58
    public function test($values)
59
    {
60
        $iterator = iterable_sort_keys($values);
61
        $result = iterator_to_array($iterator);
62
63
        $this->assertSame($this->sorted, array_keys($result));
64
    }
65
66
    public function testKey()
67
    {
68
        $values = [
69
            'India' => 'one',
70
            'Zulu' => 'two',
71
            'Papa' => 'three',
72
            'Bravo' => 'four'
73
        ];
74
75
        $iterator = iterable_sort_keys($values);
76
        $result = iterator_to_array($iterator);
77
78
        $expected = [
79
            'Bravo' => 'four',
80
            'India' => 'one',
81
            'Papa' => 'three',
82
            'Zulu' => 'two'
83
        ];
84
85
        $this->assertSame($expected, $result);
86
    }
87
88
    public function testGenerator()
89
    {
90
        $keys = [['i' => 7], ['i' => 2], null, ['i' => 42], ['i' => -2]];
91
92
        $loop = function($keys) {
93
            foreach ($keys as $i => $key) {
94
                yield $key => $i;
95
            }
96
        };
97
98
        $generator = $loop($keys);
99
        $iterator = iterable_sort_keys($generator, function($a, $b) {
100
            return ($a['i'] ?? 0) <=> ($b['i'] ?? 0);
101
        });
102
103
        $resultKeys = [];
104
        $resultValues = [];
105
106
        foreach ($iterator as $key => $value) {
107
            $resultKeys[] = $key;
108
            $resultValues[] = $value;
109
        }
110
111
        $this->assertSame([4, 2, 1, 0, 3], $resultValues);
112
        $this->assertSame([$keys[4], $keys[2], $keys[1], $keys[0], $keys[3]], $resultKeys);
113
    }
114
115
    public function testCallback()
116
    {
117
        $compare = function($a, $b) {
118
            return (strlen($a) <=> strlen($b)) ?: $a <=> $b;
119
        };
120
121
        $inner = new \ArrayIterator(array_fill_keys($this->sorted, null));
122
123
        $iterator = iterable_sort_keys($inner, $compare);
124
        $result = iterator_to_array($iterator);
125
126
        $expected = array_fill_keys($this->sorted, null);
127
        uksort($expected, $compare);
128
129
        $this->assertSame($expected, $result);
130
    }
131
    
132
    public function testEmpty()
133
    {
134
        $iterator = iterable_sort_keys(new \EmptyIterator());
135
136
        $result = iterator_to_array($iterator);
137
138
        $this->assertSame([], $result);
139
    }
140
141
    /**
142
     * Test that nothing happens when not iterating
143
     */
144
    public function testLazyExecution()
145
    {
146
        $iterator = $this->createLazyExecutionIterator();
147
148
        iterable_sort_keys($iterator);
149
150
        $this->assertTrue(true, "No warning");
151
    }
152
}
153