Completed
Push — master ( 298e09...565401 )
by Ivannis Suárez
02:36
created

PromiseTests   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 130
Duplicated Lines 45.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 3
dl 59
loc 130
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultConstructorArguments() 0 14 1
A resolve() 0 4 1
A reject() 0 4 1
A notify() 0 4 1
B testConstruct() 26 26 1
A testNotify() 17 17 1
A promiseDataProvider() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Delegate\Delegate;
14
use Cubiche\Core\Async\Promise\Promise;
15
use Cubiche\Core\Async\Promise\State;
16
17
/**
18
 * Promise Tests class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
class PromiseTests extends PromiseInterfaceTestCase
24
{
25
    /**
26
     * @var Delegate
27
     */
28
    protected $resolve;
29
30
    /**
31
     * @var Delegate
32
     */
33
    protected $reject;
34
35
    /**
36
     * @var Delegate
37
     */
38
    protected $notify;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function defaultConstructorArguments()
44
    {
45
        return array(
46
            function (callable $callable) {
47
                $this->resolve = new Delegate($callable);
48
            },
49
            function (callable $callable) {
50
                $this->reject = new Delegate($callable);
51
            },
52
            function (callable $callable) {
53
                $this->notify = new Delegate($callable);
54
            },
55
        );
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function resolve($value = null)
62
    {
63
        $this->resolve->__invoke($value);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function reject($reason = null)
70
    {
71
        $this->reject->__invoke($reason);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function notify($state = null)
78
    {
79
        $this->notify->__invoke($state);
80
    }
81
82
    /**
83
     * Test __construct.
84
     */
85 View Code Duplication
    public function testConstruct()
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...
86
    {
87
        $this
88
            ->given(
89
                $resolve = $this->delegateMock(),
90
                $reject = $this->delegateMock(),
91
                $notify = $this->delegateMock()
92
            )
93
            ->when($this->newTestedInstance($resolve, $reject, $notify))
94
            ->then()
95
                ->delegateCall($resolve)
96
                    ->once()
97
                ->delegateCall($reject)
98
                    ->once()
99
                ->delegateCall($notify)
100
                    ->once()
101
        ;
102
103
        $this
104
            /* @var \Cubiche\Core\Async\Promise\PromiseInterface $promise */
105
            ->given($promise = $this->newDefaultTestedInstance())
106
            ->then()
107
                ->boolean($promise->state()->equals(State::PENDING()))
108
                    ->isTrue()
109
        ;
110
    }
111
112
    /**
113
     * Test notify.
114
     */
115 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...
116
    {
117
        $this
118
            ->given(
119
                $promise = $this->newDefaultTestedInstance(),
120
                $onNotify = $this->delegateMock()
121
            )
122
            ->when(function () use ($promise, $onNotify) {
123
                $promise->then(null, null, $onNotify);
124
                $this->notify('foo');
125
            })
126
            ->then()
127
                ->delegateCall($onNotify)
128
                    ->withArguments('foo')
129
                    ->once()
130
        ;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 View Code Duplication
    protected function promiseDataProvider()
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...
137
    {
138
        $pending = $this->newDefaultTestedInstance();
139
140
        $fulfilled = $this->newDefaultTestedInstance();
141
        $this->resolve($this->defaultResolveValue());
142
143
        $rejected = $this->newDefaultTestedInstance();
144
        $this->reject($this->defaultRejectReason());
145
146
        return array(
147
            'pending' => array($pending),
148
            'fulfilled' => array($fulfilled),
149
            'rejected' => array($rejected),
150
        );
151
    }
152
}
153