1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Prophecy\Promise; |
4
|
|
|
|
5
|
|
|
use PhpSpec\Exception\Example\SkippingException; |
6
|
|
|
use PhpSpec\ObjectBehavior; |
7
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
9
|
|
|
|
10
|
|
|
class ThrowPromiseSpec extends ObjectBehavior |
11
|
|
|
{ |
12
|
|
|
function let() |
13
|
|
|
{ |
14
|
|
|
$this->beConstructedWith('RuntimeException'); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function it_is_promise() |
18
|
|
|
{ |
19
|
|
|
$this->shouldBeAnInstanceOf('Prophecy\Promise\PromiseInterface'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function it_instantiates_and_throws_exception_from_provided_classname(ObjectProphecy $object, MethodProphecy $method) |
23
|
|
|
{ |
24
|
|
|
$this->beConstructedWith('InvalidArgumentException'); |
25
|
|
|
|
26
|
|
|
$this->shouldThrow('InvalidArgumentException') |
27
|
|
|
->duringExecute(array(), $object, $method); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function it_instantiates_exceptions_with_required_arguments(ObjectProphecy $object, MethodProphecy $method) |
31
|
|
|
{ |
32
|
|
|
$this->beConstructedWith('spec\Prophecy\Promise\RequiredArgumentException'); |
33
|
|
|
|
34
|
|
|
$this->shouldThrow('spec\Prophecy\Promise\RequiredArgumentException') |
35
|
|
|
->duringExecute(array(), $object, $method); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function it_throws_provided_exception(ObjectProphecy $object, MethodProphecy $method) |
39
|
|
|
{ |
40
|
|
|
$this->beConstructedWith($exc = new \RuntimeException('Some exception')); |
41
|
|
|
|
42
|
|
|
$this->shouldThrow($exc)->duringExecute(array(), $object, $method); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function it_throws_error_instances(ObjectProphecy $object, MethodProphecy $method) |
46
|
|
|
{ |
47
|
|
|
if (!class_exists('\Error')) { |
48
|
|
|
throw new SkippingException('The class Error, introduced in PHP 7, does not exist'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->beConstructedWith($exc = new \Error('Error exception')); |
52
|
|
|
|
53
|
|
|
$this->shouldThrow($exc)->duringExecute(array(), $object, $method); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
function it_throws_errors_by_class_name() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (!class_exists('\Error')) { |
59
|
|
|
throw new SkippingException('The class Error, introduced in PHP 7, does not exist'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->beConstructedWith('\Error'); |
63
|
|
|
|
64
|
|
|
$this->shouldNotThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function it_does_not_throw_something_that_is_not_throwable_by_class_name() |
68
|
|
|
{ |
69
|
|
|
$this->beConstructedWith('\stdClass'); |
70
|
|
|
|
71
|
|
|
$this->shouldThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function it_does_not_throw_something_that_is_not_throwable_by_instance() |
75
|
|
|
{ |
76
|
|
|
$this->beConstructedWith(new \stdClass()); |
77
|
|
|
|
78
|
|
|
$this->shouldThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
function it_throws_an_exception_by_class_name() |
82
|
|
|
{ |
83
|
|
|
$this->beConstructedWith('\Exception'); |
84
|
|
|
|
85
|
|
|
$this->shouldNotThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
function it_throws_an_extension_of_throwable_by_class_name() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if (!interface_exists('\Throwable')) { |
91
|
|
|
throw new SkippingException('The interface Throwable, introduced in PHP 7, does not exist'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->beConstructedWith('\Fixtures\Prophecy\ThrowableInterface'); |
95
|
|
|
|
96
|
|
|
$this->shouldNotThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
function it_throws_a_throwable_by_class_name() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if (!interface_exists('\Throwable')) { |
102
|
|
|
throw new SkippingException('The interface Throwable, introduced in PHP 7, does not exist'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->beConstructedWith('\Throwable'); |
106
|
|
|
|
107
|
|
|
$this->shouldNotThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
class RequiredArgumentException extends \Exception |
112
|
|
|
{ |
113
|
|
|
final public function __construct($message, $code) {} |
114
|
|
|
} |
115
|
|
|
|
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.