Passed
Push — master ( 8bd994...8bfb27 )
by Théo
02:28
created

  A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\RequirementChecker;
16
17
use Error;
18
use PHPUnit\Framework\TestCase;
19
use stdClass;
20
21
/**
22
 * @covers \KevinGH\RequirementChecker\Requirement
23
 */
24
class RequirementTest extends TestCase
25
{
26
    public function test_it_can_be_created(): void
27
    {
28
        $requirement = new Requirement(
29
            $check = new IsPhpVersionFulfilled('>=5.3'),
30
            'Test message',
31
            'Help message'
32
        );
33
34
        $this->assertSame($check, $requirement->getIsFullfilledChecker());
35
        $this->assertTrue($requirement->isFulfilled());
36
        $this->assertSame('Test message', $requirement->getTestMessage());
37
        $this->assertSame('Help message', $requirement->getHelpText());
38
    }
39
40
    public function test_it_evaluates_the_check_lazily(): void
41
    {
42
        $requirement = new Requirement(
43
            $check = new class implements IsFulfilled {
44
                public function __invoke()
45
                {
46
                    throw new Error();
47
                }
48
            },
49
            'Test message',
50
            'Help message'
51
        );
52
53
        $this->assertSame($check, $requirement->getIsFullfilledChecker());
54
55
        try {
56
            $requirement->isFulfilled();
57
58
            $this->fail('Expected exception to be thrown.');
59
        } catch (Error $error) {
60
            $this->assertTrue(true);
61
        }
62
    }
63
64
    public function test_it_casts_the_fulfilled_result_into_a_boolean(): void
65
    {
66
        $requirement = new Requirement(
67
            new class implements IsFulfilled {
68
                public function __invoke()
69
                {
70
                    return 1;
1 ignored issue
show
Bug Best Practice introduced by
The expression return 1 returns the type integer which is incompatible with the return type mandated by KevinGH\RequirementChecker\IsFulfilled::__invoke() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
71
                }
72
            },
73
            '',
74
            ''
75
        );
76
77
        $this->assertTrue($requirement->isFulfilled());
78
79
        $requirement = new Requirement(
80
            new class implements IsFulfilled {
81
                public function __invoke()
82
                {
83
                    return 0;
1 ignored issue
show
Bug Best Practice introduced by
The expression return 0 returns the type integer which is incompatible with the return type mandated by KevinGH\RequirementChecker\IsFulfilled::__invoke() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
84
                }
85
            },
86
            '',
87
            ''
88
        );
89
90
        $this->assertFalse($requirement->isFulfilled());
91
92
        $requirement = new Requirement(
93
            new class implements IsFulfilled {
94
                public function __invoke()
95
                {
96
                    return new stdClass();
1 ignored issue
show
Bug Best Practice introduced by
The expression return new stdClass() returns the type stdClass which is incompatible with the return type mandated by KevinGH\RequirementChecker\IsFulfilled::__invoke() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
97
                }
98
            },
99
            '',
100
            ''
101
        );
102
103
        $this->assertTrue($requirement->isFulfilled());
104
    }
105
106
    public function test_it_evaluates_the_check_only_once(): void
107
    {
108
        $x = -1;
109
110
        $requirement = new Requirement(
111
            new class($x) implements IsFulfilled {
112
                private $x;
113
114
                public function __construct(&$x)
115
                {
116
                    $this->x = $x;
117
                }
118
119
                public function __invoke()
120
                {
121
                    $this->x++;
122
123
                    return $this->x;
124
                }
125
            },
126
            'Test message',
127
            'Help message'
128
        );
129
130
        $this->assertFalse($requirement->isFulfilled());
131
        $this->assertFalse($requirement->isFulfilled());    // Would have gave `true` if it was evaluated a second time
132
    }
133
}
134