/
FirstTest.php
110 lines (96 loc) · 3.33 KB
/
FirstTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @package Functional-php
* @author Lars Strojny <lstrojny@php.net>
* @copyright 2011-2021 Lars Strojny
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/
namespace Functional\Tests;
use ArrayIterator;
use Functional\Exceptions\InvalidArgumentException;
use Traversable;
class FirstTest extends AbstractTestCase
{
/** @var string[] */
private $badArray;
/** @var Traversable */
private $badIterator;
public function getAliases(): array
{
return [
['Functional\first'],
['Functional\head'],
];
}
protected function setUp(): void
{
parent::setUp();
$this->list = ['first', 'second', 'third'];
$this->listIterator = new ArrayIterator($this->list);
$this->badArray = ['foo', 'bar', 'baz'];
$this->badIterator = new ArrayIterator($this->badArray);
}
/**
* @dataProvider getAliases
*/
public function test(string $functionName): void
{
$callback = function ($v, $k, $collection) {
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
return $v == 'second' && $k == 1;
};
self::assertSame('second', $functionName($this->list, $callback));
self::assertSame('second', $functionName($this->listIterator, $callback));
self::assertNull($functionName($this->badArray, $callback));
self::assertNull($functionName($this->badIterator, $callback));
}
/**
* @dataProvider getAliases
*/
public function testWithoutCallback($functionName): void
{
self::assertSame('first', $functionName($this->list));
self::assertSame('first', $functionName($this->list, null));
self::assertSame('first', $functionName($this->listIterator));
self::assertSame('first', $functionName($this->listIterator, null));
self::assertSame('foo', $functionName($this->badArray));
self::assertSame('foo', $functionName($this->badArray, null));
self::assertSame('foo', $functionName($this->badIterator));
self::assertSame('foo', $functionName($this->badIterator, null));
}
/**
* @dataProvider getAliases
*/
public function testPassNonCallable($functionName): void
{
$this->expectCallableArgumentError($functionName, 2);
$functionName($this->list, 'undefinedFunction');
}
/**
* @dataProvider getAliases
*/
public function testExceptionIsThrownInArray($functionName): void
{
$this->expectException('DomainException');
$this->expectExceptionMessage('Callback exception');
$functionName($this->list, [$this, 'exception']);
}
/**
* @dataProvider getAliases
*/
public function testExceptionIsThrownInCollection($functionName): void
{
$this->expectException('DomainException');
$this->expectExceptionMessage('Callback exception');
$functionName($this->listIterator, [$this, 'exception']);
}
/**
* @dataProvider getAliases
*/
public function testPassNoCollection($functionName): void
{
$this->expectArgumentError(\sprintf('%s() expects parameter 1 to be array or instance of Traversable', $functionName));
$functionName('invalidCollection', 'strlen');
}
}