-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeadTest.php
68 lines (49 loc) · 1.29 KB
/
HeadTest.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
<?php
use PHPUnit\Framework\TestCase;
use PhatCats\Maybe\Maybe;
class HeadTest extends TestCase {
public function testHeadOnNonEmptyArray() {
$a = ['apples', 'oranges', 'bananas'];
$h = $this->head($a);
$expectedResult = Maybe::fromValue('apples');
$this->assertEquals($h, $expectedResult);
}
public function testHeadOnEmptyArray() {
$a = [];
$h = $this->head($a);
$expectedResult = Maybe::nothing();
$this->assertEquals($h, $expectedResult);
}
public function testHeadOnString() {
$a = "hello";
$h = $this->head($a);
$expectedResult = Maybe::nothing();
$this->assertEquals($h, $expectedResult);
}
public function testHeadOnObject() {
$h = $this->head($this);
$expectedResult = Maybe::nothing();
$this->assertEquals($h, $expectedResult);
}
public function testHeadOnAssociativeArray() {
$a = ['1' => 'apples',
'2' => 'oranges',
'3' => 'bananas'];
$h = $this->head($a);
$expectedResult = Maybe::fromValue('apples');
$this->assertEquals($h, $expectedResult);
}
private function head($array) {
if (is_array($array)) {
if (count($array) > 0) {
$vals = array_values($array);
$h = Maybe::fromValue($vals[0]);
} else {
$h = Maybe::nothing();
}
} else {
$h = Maybe::nothing();
}
return $h;
}
}