1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Prophecy\Promise; |
4
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
6
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
8
|
|
|
|
9
|
|
|
class CallbackPromiseSpec extends ObjectBehavior |
10
|
|
|
{ |
11
|
|
|
function let() |
12
|
|
|
{ |
13
|
|
|
$this->beConstructedWith('get_class'); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function it_is_promise() |
17
|
|
|
{ |
18
|
|
|
$this->shouldBeAnInstanceOf('Prophecy\Promise\PromiseInterface'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
function it_should_execute_closure_callback(ObjectProphecy $object, MethodProphecy $method) |
22
|
|
|
{ |
23
|
|
|
$firstArgumentCallback = function ($args) { |
24
|
|
|
return $args[0]; |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
$this->beConstructedWith($firstArgumentCallback); |
28
|
|
|
|
29
|
|
|
$this->execute(array('one', 'two'), $object, $method)->shouldReturn('one'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
function it_should_execute_static_array_callback(ObjectProphecy $object, MethodProphecy $method) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$firstArgumentCallback = array('spec\Prophecy\Promise\ClassCallback', 'staticCallbackMethod'); |
35
|
|
|
|
36
|
|
|
$this->beConstructedWith($firstArgumentCallback); |
37
|
|
|
|
38
|
|
|
$this->execute(array('one', 'two'), $object, $method)->shouldReturn('one'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
function it_should_execute_instance_array_callback(ObjectProphecy $object, MethodProphecy $method) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$class = new ClassCallback(); |
44
|
|
|
$firstArgumentCallback = array($class, 'callbackMethod'); |
45
|
|
|
|
46
|
|
|
$this->beConstructedWith($firstArgumentCallback); |
47
|
|
|
|
48
|
|
|
$this->execute(array('one', 'two'), $object, $method)->shouldReturn('one'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
function it_should_execute_string_function_callback(ObjectProphecy $object, MethodProphecy $method) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$firstArgumentCallback = 'spec\Prophecy\Promise\functionCallbackFirstArgument'; |
54
|
|
|
|
55
|
|
|
$this->beConstructedWith($firstArgumentCallback); |
56
|
|
|
|
57
|
|
|
$this->execute(array('one', 'two'), $object, $method)->shouldReturn('one'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Class used to test callbackpromise |
64
|
|
|
* |
65
|
|
|
* @param array |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
class ClassCallback |
69
|
|
|
{ |
70
|
|
|
/** |
71
|
|
|
* @param array $args |
72
|
|
|
*/ |
73
|
|
|
function callbackMethod($args) |
74
|
|
|
{ |
75
|
|
|
return $args[0]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $args |
80
|
|
|
*/ |
81
|
|
|
static function staticCallbackMethod($args) |
82
|
|
|
{ |
83
|
|
|
return $args[0]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Callback function used to test callbackpromise |
89
|
|
|
* |
90
|
|
|
* @param array |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
function functionCallbackFirstArgument($args) |
94
|
|
|
{ |
95
|
|
|
return $args[0]; |
96
|
|
|
} |
97
|
|
|
|
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.