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

IterableFindTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
namespace Jasny\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use function Jasny\iterable_find;
7
8
/**
9
 * @covers \Jasny\iterable_find
10
 */
11
class IterableFindTest extends TestCase
12
{
13
    use ProvideIterablesTrait;
14
15
    public function provider()
16
    {
17
        return $this->provideIterables(['one', 'two', 'three'], true);
18
    }
19
20
    /**
21
     * @dataProvider provider
22
     */
23
    public function testValue($values)
24
    {
25
        $result = iterable_find($values, function($value) {
26
            return substr($value, 0, 1) === 't';
27
        });
28
29
        $this->assertEquals('two', $result);
30
    }
31
32
    public function keyProvider()
33
    {
34
        $values = ['one' => 'uno', 'two' => 'dos', 'three' => 'tres'];
35
36
        return $this->provideIterables($values, false, false);
37
    }
38
39
    /**
40
     * @dataProvider keyProvider
41
     */
42
    public function testKey($values)
43
    {
44
        $result = iterable_find($values, function($value, $key) {
45
            return substr($key, 0, 1) === 't';
46
        });
47
48
        $this->assertEquals('dos', $result);
49
    }
50
51
    /**
52
     * @dataProvider provider
53
     */
54
    public function testNotFound($values)
55
    {
56
        $result = iterable_find($values, function() {
57
            return false;
58
        });
59
60
        $this->assertNull($result);
61
    }
62
63
    public function testNoWalk()
64
    {
65
        $iterator = $this->createMock(\Iterator::class);
66
        $iterator->expects($this->any())->method('valid')->willReturn(true);
67
        $iterator->expects($this->exactly(2))->method('current')
68
            ->willReturnOnConsecutiveCalls('one', 'two');
69
70
        $result = iterable_find($iterator, function($value) {
71
            return $value === 'two';
72
        });
73
74
        $this->assertEquals('two', $result);
75
    }
76
77
    public function testEmpty()
78
    {
79
        $result = iterable_find(new \EmptyIterator(), function() {});
80
81
        $this->assertNull($result);
82
    }
83
}
84