Test Failed
Branch master (9f179d)
by Randy
01:57
created

OptionalTest::testSomeByRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
eloc 5
nc 1
nop 0
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use function Dgame\Optional\maybe;
5
use function Dgame\Optional\none;
6
use function Dgame\Optional\some;
7
8
class OptionalTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    public function testSome()
11
    {
12
        $some = some(42);
13
        $this->assertTrue($some->isSome());
14
        $this->assertEquals(42, $some->unwrap());
15
    }
16
17
    public function testSomeByRef()
18
    {
19
        $some = some(42);
20
        $this->assertTrue($some->isSome($value));
21
        $this->assertFalse($some->isNone());
22
        $this->assertEquals(42, $value);
23
    }
24
25
    public function testNone()
26
    {
27
        $none = none();
28
        $this->assertTrue($none->isNone());
29
        $c = 42;
30
        $this->assertFalse($none->isSome($c));
31
        $this->assertNull($c);
32
    }
33
34
    public function testNoneByRef()
35
    {
36
        $none = none();
37
        $this->assertTrue($none->isNone());
38
        $this->assertFalse($none->isSome($value));
39
        $this->assertNull($value);
40
    }
41
42
    public function testMaybe()
43
    {
44
        $maybe = maybe(null);
45
        $this->assertTrue($maybe->isNone());
46
47
        $maybe = maybe(false)->ensureNotFalse();
48
        $this->assertTrue($maybe->isNone());
49
50
        $maybe = maybe(42);
51
        $this->assertTrue($maybe->isSome());
52
        $this->assertEquals(42, $maybe->unwrap());
53
    }
54
55
    public function testChain()
56
    {
57
        $a = new class
58
        {
59
            public function test() : int
60
            {
61
                return 42;
62
            }
63
        };
64
65
        $some = some($a);
66
        $this->assertEquals(42, $some->unwrap()->test());
67
    }
68
69
    public function testEnsure()
70
    {
71
        $result = some(0)->ensure(function($value) {
72
            return $value > 0;
73
        });
74
        $this->assertTrue($result->isNone());
75
        $result = maybe(null)->ensure(function($value) {
76
            return $value > 0;
77
        });
78
        $this->assertTrue($result->isNone());
79
    }
80
}