|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aerophant\RamdaTest; |
|
4
|
|
|
|
|
5
|
|
|
use function Aerophant\Ramda\path; |
|
6
|
|
|
use function Aerophant\Ramda\pathOr; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class PathOrTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testSimplePath() |
|
12
|
|
|
{ |
|
13
|
|
|
$paths = ['data']; |
|
14
|
|
|
$object = ['data' => 'dataValue']; |
|
15
|
|
|
$defaultValue = 'defaultValue'; |
|
16
|
|
|
$expect = 'dataValue'; |
|
17
|
|
|
$actual = pathOr($defaultValue)($paths)($object); |
|
18
|
|
|
$this->assertEquals($expect, $actual); |
|
19
|
|
|
} |
|
20
|
|
|
public function testNestedPath() |
|
21
|
|
|
{ |
|
22
|
|
|
$paths = ['data', 'nestedData']; |
|
23
|
|
|
$object = ['data' => ['nestedData' => 'dataValue']]; |
|
24
|
|
|
$defaultValue = 'defaultValue'; |
|
25
|
|
|
$expect = 'dataValue'; |
|
26
|
|
|
$actual = pathOr($defaultValue)($paths)($object); |
|
27
|
|
|
$this->assertEquals($expect, $actual); |
|
28
|
|
|
} |
|
29
|
|
|
public function testSimplePathAndNotFound() |
|
30
|
|
|
{ |
|
31
|
|
|
$paths = ['data']; |
|
32
|
|
|
$object = ['anotherData' => 'dataValue']; |
|
33
|
|
|
$defaultValue = 'defaultValue'; |
|
34
|
|
|
$actual = pathOr($defaultValue)($paths)($object); |
|
35
|
|
|
$this->assertEquals($defaultValue, $actual); |
|
36
|
|
|
} |
|
37
|
|
|
public function testNestedPathAndNotFound() |
|
38
|
|
|
{ |
|
39
|
|
|
$paths = ['data', 'nestedData']; |
|
40
|
|
|
$object = ['data' => ['anotherNestedData' => 'dataValue']]; |
|
41
|
|
|
$defaultValue = 'defaultValue'; |
|
42
|
|
|
$actual = pathOr($defaultValue)($paths)($object); |
|
43
|
|
|
$this->assertEquals($defaultValue, $actual); |
|
44
|
|
|
} |
|
45
|
|
|
public function testUnsupportedObject() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
48
|
|
|
$paths = ['data', 'nestedData']; |
|
49
|
|
|
$object = new \stdClass(); |
|
50
|
|
|
$defaultValue = 'defaultValue'; |
|
51
|
|
|
pathOr($defaultValue)($paths)($object); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|