Completed
Push — master ( 9672ef...b72795 )
by Ivannis Suárez
02:30
created

ThenResolverTests   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 89
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testResolve() 0 58 1
A testReject() 0 21 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\RejectedPromise;
15
16
/**
17
 * Then Resolver Tests class.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 */
21
class ThenResolverTests extends ObservableResolverTests
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function testResolve()
27
    {
28
        parent::testResolve();
29
30
        $this
31
            ->given(
32
                $deferred = new Deferred(),
33
                $onFulfilled = $this->delegateMockWithReturn($deferred->promise()),
34
                /** @var \Cubiche\Core\Async\Promise\ThenResolver $resolver */
35
                $resolver = $this->newTestedInstance($onFulfilled),
36
                $onFulfilledPromise = $this->delegateMock(),
37
                $onNotifyPromise = $this->delegateMock()
38
            )
39
            ->when(function () use ($resolver, $onFulfilledPromise, $onNotifyPromise) {
40
                $resolver->resolve();
41
                $resolver->promise()->then($onFulfilledPromise, null, $onNotifyPromise);
42
            })
43
            ->then()
44
                ->delegateCall($onFulfilledPromise)
45
                    ->never()
46
                ->delegateCall($onNotifyPromise)
47
                    ->never()
48
        ;
49
50
        $this
51
            ->when($deferred->notify('state'))
52
            ->then()
53
                ->delegateCall($onNotifyPromise)
54
                    ->withArguments('state')
55
                    ->once()
56
        ;
57
58
        $this
59
            ->when($deferred->resolve('foo'))
60
            ->then()
61
                ->delegateCall($onFulfilledPromise)
62
                    ->withArguments('foo')
63
                    ->once()
64
        ;
65
66
        $this
67
            ->given(
68
                $reason = new \Exception(),
69
                $onFulfilled = $this->delegateMockWithReturn(new RejectedPromise($reason)),
70
                /** @var \Cubiche\Core\Async\Promise\ThenResolver $resolver */
71
                $resolver = $this->newTestedInstance($onFulfilled),
72
                $onRejectedPromise = $this->delegateMock()
73
            )
74
            ->when(function () use ($resolver, $onRejectedPromise) {
75
                $resolver->resolve();
76
                $resolver->promise()->then(null, $onRejectedPromise);
77
            })
78
            ->then()
79
                ->delegateCall($onRejectedPromise)
80
                    ->withArguments($reason)
81
                    ->once()
82
        ;
83
    }
84
85
    /**
86
     * Test reject.
87
     */
88
    public function testReject()
89
    {
90
        parent::testReject();
91
92
        $this
93
            ->given(
94
                $reason = new \Exception(),
95
                $onRejected = $this->delegateMockWithException($reason),
96
                /** @var \Cubiche\Core\Async\Promise\ThenResolver $resolver */
97
                $resolver = $this->newTestedInstance(null, $onRejected),
98
                $onRejectedPromise = $this->delegateMock()
99
            )
100
            ->when(function () use ($resolver, $onRejectedPromise) {
101
                $resolver->reject();
102
                $resolver->promise()->then(null, $onRejectedPromise);
103
            })
104
            ->then()
105
                ->delegateCall($onRejectedPromise)
106
                    ->withArguments($reason)
107
                    ->once();
108
    }
109
}
110