Completed
Push — master ( 7f10cd...099423 )
by Ivannis Suárez
02:07
created

EnumTests::equalityTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Enum\Tests\Units;
12
13
use Cubiche\Core\Enum\Enum;
14
use Cubiche\Core\Enum\Tests\Fixtures\BadDefaultEnumFixture;
15
use Cubiche\Core\Enum\Tests\Fixtures\DefaultEnumFixture;
16
use Cubiche\Core\Enum\Tests\Fixtures\EnumFixture;
17
18
/**
19
 * Enum Tests Class.
20
 *
21
 * @author Ivannis Suárez Jerez <[email protected]>
22
 */
23
class EnumTests extends EnumTestCase
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function newDefaultTestedInstance()
29
    {
30
        return new EnumFixture(EnumFixture::FOO);
31
    }
32
33
    /**
34
     * Test is method.
35
     */
36
    public function testIs()
37
    {
38
        $this
39
            ->given($enum = $this->newDefaultTestedInstance())
40
            ->then()
41
                ->boolean($enum->is(EnumFixture::FOO))
42
                    ->isTrue()
43
                ->boolean($enum->is(EnumFixture::BAR))
44
                    ->isFalse()
45
        ;
46
    }
47
48
    /**
49
     * Test __DEFAULT method.
50
     */
51
    public function testDefault()
52
    {
53
        $this->equalityTest(EnumFixture::__DEFAULT(), EnumFixture::FOO());
54
        $this->equalityTest(DefaultEnumFixture::__DEFAULT(), DefaultEnumFixture::BAR());
55
56
        $this
57
            ->exception(function () {
58
                BadDefaultEnumFixture::__DEFAULT();
59
            })
60
                ->isInstanceof(\UnexpectedValueException::class)
61
            ->exception(function () {
62
                BadDefaultEnumFixture::BAZ();
63
            })
64
                ->isInstanceof(\BadMethodCallException::class)
65
        ;
66
    }
67
68
    /**
69
     * Test ensure method.
70
     */
71
    public function testEnsure()
72
    {
73
        $this->equalityTest(EnumFixture::ensure(EnumFixture::FOO()), EnumFixture::FOO());
74
        $this->equalityTest(EnumFixture::ensure(EnumFixture::FOO(), EnumFixture::BAR()), EnumFixture::FOO());
75
        $this->equalityTest(EnumFixture::ensure(), EnumFixture::__DEFAULT());
76
        $this->equalityTest(EnumFixture::ensure(null, EnumFixture::BAR()), EnumFixture::BAR());
77
78
        $this
79
            ->exception(function () {
80
                EnumFixture::ensure(DefaultEnumFixture::FOO());
81
            })
82
                ->isInstanceof(\InvalidArgumentException::class)
83
        ;
84
    }
85
86
    /**
87
     * @param Enum $enum1
88
     * @param Enum $enum2
89
     */
90
    protected function equalityTest(Enum $enum1, Enum $enum2)
91
    {
92
        $this
93
            ->given($enum1, $enum2)
94
            ->then()
95
                ->boolean($enum1->equals($enum2))
96
                    ->isTrue()
97
        ;
98
    }
99
}
100