NativeValueObjectTestCase::uniqueNativeValue()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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\Domain\Model\Tests\Units;
12
13
use Cubiche\Domain\Model\NativeValueObjectInterface;
14
use mageekguy\atoum\adapter as Adapter;
15
use mageekguy\atoum\annotations\extractor as Extractor;
16
use mageekguy\atoum\asserter\generator as Generator;
17
use mageekguy\atoum\test\assertion\manager as Manager;
18
19
/**
20
 * NativeValueObjectTestCase class.
21
 *
22
 * @author Ivannis Suárez Jerez <[email protected]>
23
 */
24
abstract class NativeValueObjectTestCase extends TestCase
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function __construct(
30
        Adapter $adapter = null,
31
        Extractor $annotationExtractor = null,
32
        Generator $asserterGenerator = null,
33
        Manager $assertionManager = null,
34
        \Closure $reflectionClassFactory = null
35
    ) {
36
        parent::__construct(
37
            $adapter,
38
            $annotationExtractor,
39
            $asserterGenerator,
40
            $assertionManager,
41
            $reflectionClassFactory
42
        );
43
44
        $this->getAssertionManager()
45
            ->setHandler(
46
                'randomNativeValue',
47
                function () {
48
                    return $this->randomNativeValue();
49
                }
50
            )
51
            ->setHandler(
52
                'invalidNativeValue',
53
                function () {
54
                    return $this->invalidNativeValue();
55
                }
56
            )
57
            ->setHandler(
58
                'uniqueNativeValue',
59
                function () {
60
                    return $this->uniqueNativeValue();
61
                }
62
            )
63
            ->setHandler(
64
                'fromNative',
65
                function ($value) {
66
                    return $this->fromNative($value);
67
                }
68
            )
69
        ;
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    abstract protected function randomNativeValue();
76
77
    /**
78
     * @return mixed
79
     */
80
    abstract protected function invalidNativeValue();
81
82
    /**
83
     * @return mixed
84
     */
85
    abstract protected function uniqueNativeValue();
86
87
    /**
88
     * @param mixed $value
89
     *
90
     * @return mixed
91
     */
92
    abstract protected function fromNative($value);
93
94
    /*
95
     * Test fromNative/toNative.
96
     */
97
    public function testFromNativeToNative()
98
    {
99
        $this
100
            ->given(
101
                $native = $this->randomNativeValue(),
102
                $valueObject = $this->fromNative($native)
103
            )
104
            ->then
105
                ->object($valueObject)
106
                    ->isInstanceOf(NativeValueObjectInterface::class)
107
                ->variable($native)
108
                    ->isEqualTo($valueObject->toNative())
109
        ;
110
111
        $this
112
            ->given($native = $this->invalidNativeValue())
113
            ->then
114
                ->exception(function () use ($native) {
115
                    $this->fromNative($native);
116
                })
117
                ->isInstanceOf(\InvalidArgumentException::class)
118
        ;
119
    }
120
121
    /**
122
     * Test equals method.
123
     */
124
    public function testEquals()
125
    {
126
        $this
127
            ->given(
128
                $native1 = $this->uniqueNativeValue(),
129
                $native2 = $this->randomNativeValue(),
130
                $valueObject1 = $this->fromNative($native1),
131
                $valueObject2 = $this->fromNative($native2)
132
            )
133
            ->then
134
                ->boolean($valueObject1->equals($valueObject1))
135
                    ->isTrue()
136
                ->boolean($valueObject1->equals($valueObject2))
137
                    ->isFalse()
138
                ->string($valueObject1->__toString())
139
                    ->isEqualTo(\strval($native1))
140
        ;
141
    }
142
}
143