ThrowPromiseSpec.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace spec\Prophecy\Promise;
  3. use PhpSpec\Exception\Example\SkippingException;
  4. use PhpSpec\ObjectBehavior;
  5. use Prophecy\Prophecy\MethodProphecy;
  6. use Prophecy\Prophecy\ObjectProphecy;
  7. class ThrowPromiseSpec extends ObjectBehavior
  8. {
  9. function let()
  10. {
  11. $this->beConstructedWith('RuntimeException');
  12. }
  13. function it_is_promise()
  14. {
  15. $this->shouldBeAnInstanceOf('Prophecy\Promise\PromiseInterface');
  16. }
  17. function it_instantiates_and_throws_exception_from_provided_classname(ObjectProphecy $object, MethodProphecy $method)
  18. {
  19. $this->beConstructedWith('InvalidArgumentException');
  20. $this->shouldThrow('InvalidArgumentException')
  21. ->duringExecute(array(), $object, $method);
  22. }
  23. function it_instantiates_exceptions_with_required_arguments(ObjectProphecy $object, MethodProphecy $method)
  24. {
  25. $this->beConstructedWith('spec\Prophecy\Promise\RequiredArgumentException');
  26. $this->shouldThrow('spec\Prophecy\Promise\RequiredArgumentException')
  27. ->duringExecute(array(), $object, $method);
  28. }
  29. function it_throws_provided_exception(ObjectProphecy $object, MethodProphecy $method)
  30. {
  31. $this->beConstructedWith($exc = new \RuntimeException('Some exception'));
  32. $this->shouldThrow($exc)->duringExecute(array(), $object, $method);
  33. }
  34. function it_throws_error_instances(ObjectProphecy $object, MethodProphecy $method)
  35. {
  36. if (!class_exists('\Error')) {
  37. throw new SkippingException('The class Error, introduced in PHP 7, does not exist');
  38. }
  39. $this->beConstructedWith($exc = new \Error('Error exception'));
  40. $this->shouldThrow($exc)->duringExecute(array(), $object, $method);
  41. }
  42. function it_throws_errors_by_class_name()
  43. {
  44. if (!class_exists('\Error')) {
  45. throw new SkippingException('The class Error, introduced in PHP 7, does not exist');
  46. }
  47. $this->beConstructedWith('\Error');
  48. $this->shouldNotThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation();
  49. }
  50. function it_does_not_throw_something_that_is_not_throwable_by_class_name()
  51. {
  52. $this->beConstructedWith('\stdClass');
  53. $this->shouldThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation();
  54. }
  55. function it_does_not_throw_something_that_is_not_throwable_by_instance()
  56. {
  57. $this->beConstructedWith(new \stdClass());
  58. $this->shouldThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation();
  59. }
  60. function it_throws_an_exception_by_class_name()
  61. {
  62. $this->beConstructedWith('\Exception');
  63. $this->shouldNotThrow('Prophecy\Exception\InvalidArgumentException')->duringInstantiation();
  64. }
  65. }
  66. class RequiredArgumentException extends \Exception
  67. {
  68. final public function __construct($message, $code) {}
  69. }