Completed
Push — master ( 93d710...74f423 )
by Siwapun
04:08
created

PathOrTest::testNestedPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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