DeferredInterfaceTestCase::invalidActionTest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 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\Core\Async\Tests\Units\Promise;
12
13
use Cubiche\Core\Async\Promise\Deferred;
14
use Cubiche\Core\Async\Promise\DeferredInterface;
15
use Cubiche\Core\Async\Promise\State;
16
17
/**
18
 * Deferred Interface Test Case class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
abstract class DeferredInterfaceTestCase extends PromisorInterfaceTestCase
24
{
25
    /**
26
     * Test class.
27
     */
28
    public function testClass()
29
    {
30
        $this
31
            ->testedClass
32
                ->implements(DeferredInterface::class)
33
        ;
34
    }
35
36
    /**
37
     * Test __constructor.
38
     */
39
    public function testCreate()
40
    {
41
        $this
42
            ->given(
43
                /** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */
44
                $deferred = $this->newDefaultTestedInstance()
45
            )
46
            ->then()
47
                ->boolean($deferred->promise()->state()->equals(State::PENDING()))
48
                    ->isTrue()
49
        ;
50
    }
51
52
    /**
53
     * Test resolve.
54
     */
55
    public function testResolve()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $this
58
            ->given(
59
                /** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */
60
                $deferred = $this->newDefaultTestedInstance(),
61
                $value = 'foo'
62
            )
63
            ->when($deferred->resolve($value))
64
            ->then()
65
                ->boolean($deferred->promise()->state()->equals(State::FULFILLED()))
66
                    ->isTrue()
67
        ;
68
69
        $this
70
            ->given($onFulfilled = $this->delegateMock())
71
            ->when($deferred->promise()->then($onFulfilled))
72
            ->then()
73
                ->delegateCall($onFulfilled)
74
                    ->withArguments($value)
75
                    ->once()
76
        ;
77
78
        $this->invalidActionTest($deferred);
79
    }
80
81
    /**
82
     * Test reject.
83
     */
84
    public function testReject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $this
87
            ->given(
88
                /** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */
89
                $deferred = $this->newDefaultTestedInstance(),
90
                $reason = new \Exception()
91
            )
92
            ->when($deferred->reject($reason))
93
            ->then()
94
                ->boolean($deferred->promise()->state()->equals(State::REJECTED()))
95
                    ->isTrue()
96
            ;
97
98
        $this
99
            ->given($onRejected = $this->delegateMock())
100
            ->when($deferred->promise()->then(null, $onRejected))
101
            ->then()
102
                ->delegateCall($onRejected)
103
                    ->withArguments($reason)
104
                     ->once()
105
        ;
106
107
        $this->invalidActionTest($deferred);
108
    }
109
110
    /**
111
     * Test notify.
112
     */
113 View Code Duplication
    public function testNotify()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $this
116
            ->given(
117
                /** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */
118
                $deferred = $this->newDefaultTestedInstance(),
119
                $onNotify = $this->delegateMock()
120
            )
121
            ->when(function () use ($deferred, $onNotify) {
122
                $deferred->promise()->then(null, null, $onNotify);
123
                $deferred->notify('foo');
124
            })
125
            ->then()
126
                ->delegateCall($onNotify)
127
                    ->withArguments('foo')
128
                    ->once()
129
            ;
130
    }
131
132
    /**
133
     * Test cancel.
134
     */
135
    public function testCancel()
136
    {
137
        $this
138
        ->given(
139
            /** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */
140
            $deferred = $this->newDefaultTestedInstance()
141
        )
142
        ->when($canceled = $deferred->cancel())
143
        ->then()
144
            ->boolean($canceled)
145
                ->isTrue()
146
        ;
147
148
        $this
149
            ->given($onRejected = $this->delegateMock())
150
            ->when($deferred->promise()->then(null, $onRejected))
151
            ->then()
152
                ->delegateCall($onRejected)
153
                    ->once()
154
        ;
155
156
        $this
157
            ->given(
158
                /** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */
159
                $deferred = $this->newDefaultTestedInstance()
160
            )
161
            ->if($deferred->resolve('foo'))
162
            ->when($canceled = $deferred->cancel())
163
            ->then()
164
                ->boolean($canceled)
165
                    ->isFalse()
166
        ;
167
    }
168
169
    /**
170
     * @param DeferredInterface $deferred
171
     */
172
    protected function invalidActionTest(DeferredInterface $deferred)
173
    {
174
        $this
175
            ->given($deferred)
176
            ->exception(function () use ($deferred) {
177
                $deferred->resolve();
178
            })
179
                ->isInstanceOf(\LogicException::class)
180
            ->exception(function () use ($deferred) {
181
                $deferred->reject();
182
            })
183
                ->isInstanceOf(\LogicException::class)
184
            ->exception(function () use ($deferred) {
185
                $deferred->notify();
186
            })
187
                ->isInstanceOf(\LogicException::class)
188
        ;
189
    }
190
}
191