Completed
Push — master ( 7ee214...32fd8d )
by Alberto
19s
created

testDecorateOverriddenCallsFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A MokaMockingStrategyTestCase::testDecorateWithMethodMultipleCallsSuccess() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Tests;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Strategy\MockingStrategyInterface;
8
use PHPUnit\Framework\TestCase;
9
use Tests\AbstractTestClass;
10
use Tests\BarTestClass;
11
use Tests\FooTestClass;
12
use Tests\TestInterface;
13
14
abstract class MokaMockingStrategyTestCase extends TestCase
15
{
16
    /**
17
     * @var MockingStrategyInterface
18
     */
19
    protected $strategy;
20
21
    /**
22
     * @var object
23
     */
24
    protected $mock;
25
26
    /**
27
     * @var array
28
     */
29
    protected $methodsWithValues;
30
31
    /**
32
     * @var string
33
     */
34
    private $className;
35
36
    final public function testGetMockTypeSuccess()
37
    {
38
        $this->assertInternalType('string', $this->strategy->getMockType());
39
    }
40
41
    final public function testBuildClassSuccess()
42
    {
43
        $this->assertInstanceOf($this->strategy->getMockType(), $this->mock);
44
    }
45
46
    final public function testBuildInterfaceSuccess()
47
    {
48
        $mock = $this->strategy->build(TestInterface::class);
49
50
        $this->assertInstanceOf($this->strategy->getMockType(), $mock);
51
    }
52
53
    final public function testBuildAbstractClassSuccess()
54
    {
55
        $mock = $this->strategy->build(AbstractTestClass::class);
56
57
        $this->assertInstanceOf($this->strategy->getMockType(), $mock);
58
    }
59
60
    final public function testDecorateFailure()
61
    {
62
        $this->expectException(InvalidArgumentException::class);
63
64
        $this->strategy->decorate(new \stdClass(), $this->methodsWithValues);
65
    }
66
67
    final public function testDecorateWrongTypeHintFailure()
68
    {
69
        $this->strategy->decorate($this->mock, [
70
            'getSelf' => mt_rand()
71
        ]);
72
73
        $this->expectException(\TypeError::class);
74
        $this->strategy->get($this->mock)->getSelf();
75
    }
76
77
    final public function testDecorateWithPropertySuccess()
78
    {
79
        $this->strategy->decorate($this->mock, [
80
            '$property' => 1138
81
        ]);
82
83
        $this->assertEquals(
84
            1138,
85
            $this->strategy->get($this->mock)->property
86
        );
87
    }
88
89
    final public function testDecorateWithMethodSingleCallSuccess()
90
    {
91
        $this->assertSame($this->methodsWithValues['isTrue'], $this->strategy->get($this->mock)->isTrue());
92
93
        $this->expectException(\Exception::class);
94
        $this->expectExceptionMessage($this->methodsWithValues['throwException']->getMessage());
95
        $this->strategy->get($this->mock)->throwException();
96
    }
97
98
    final public function testDecorateWithMethodMultipleCallsSuccess()
99
    {
100
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
101
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
102
    }
103
104
    final public function testDecorateWithMethodOverriddenCallsFailure()
105
    {
106
        $this->strategy->decorate($this->mock, [
107
            'getInt' => mt_rand(),
108
            'throwException' => mt_rand()
109
        ]);
110
111
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
112
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
113
114
        $this->expectException(\Exception::class);
115
        $this->expectExceptionMessage($this->methodsWithValues['throwException']->getMessage());
116
        $this->strategy->get($this->mock)->throwException();
117
    }
118
119
    final public function testDecorateWithMethodCallWithArgumentSuccess()
120
    {
121
        $this->assertSame($this->methodsWithValues['withArgument'], $this->strategy->get($this->mock)->withArgument(mt_rand()));
122
    }
123
124
    final public function testDecorateWithMethodCallWithMissingArgumentFailure()
125
    {
126
        $this->expectException(\Error::class);
127
128
        $this->strategy->get($this->mock)->withArgument();
129
    }
130
131
    final public function testDecorateWithMethodCallWithWrongArgumentFailure()
132
    {
133
        $this->expectException(\TypeError::class);
134
135
        $this->strategy->get($this->mock)->withArgument('string');
136
    }
137
138
    final public function testGetSuccess()
139
    {
140
        $this->assertInstanceOf($this->className, $this->strategy->get($this->mock));
141
    }
142
143
    final public function testGetFailure()
144
    {
145
        $this->expectException(InvalidArgumentException::class);
146
147
        $this->strategy->get(new \stdClass());
148
    }
149
150
    protected function setUp()
151
    {
152
        $this->className = [
153
            FooTestClass::class,
154
            BarTestClass::class
155
        ][random_int(0, 1)];
156
157
        $this->methodsWithValues = [
158
            'isTrue' => (bool)random_int(0, 1),
159
            'getInt' => mt_rand(),
160
            'withArgument' => mt_rand(),
161
            'throwException' => new \Exception('' . mt_rand())
162
        ];
163
164
        $this->mock = $this->strategy->build($this->className);
165
166
        $this->strategy->decorate($this->mock, $this->methodsWithValues);
167
    }
168
169
    final protected function setStrategy(MockingStrategyInterface $strategy)
170
    {
171
        $this->strategy = $strategy;
172
    }
173
174
    final protected function getRandomFQCN()
175
    {
176
        return 'foo_' . mt_rand();
177
    }
178
}
179