ThrowablePatchSpec.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace spec\Prophecy\Doubler\ClassPatch;
  3. use PhpSpec\Exception\Example\SkippingException;
  4. use Prophecy\Doubler\ClassPatch\ThrowablePatch;
  5. use PhpSpec\ObjectBehavior;
  6. use Prophecy\Argument;
  7. use Prophecy\Doubler\Generator\Node\ClassNode;
  8. class ThrowablePatchSpec extends ObjectBehavior
  9. {
  10. function it_is_a_patch()
  11. {
  12. $this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
  13. }
  14. function it_does_not_support_class_that_does_not_implement_throwable(ClassNode $node)
  15. {
  16. if (\PHP_VERSION_ID < 70000) {
  17. throw new SkippingException('Throwable is not defined in PHP 5');
  18. }
  19. $node->getInterfaces()->willReturn(array());
  20. $node->getParentClass()->willReturn('stdClass');
  21. $this->supports($node)->shouldReturn(false);
  22. }
  23. function it_supports_class_that_extends_not_throwable_class(ClassNode $node)
  24. {
  25. if (\PHP_VERSION_ID < 70000) {
  26. throw new SkippingException('Throwable is not defined in PHP 5');
  27. }
  28. $node->getInterfaces()->willReturn(array('Throwable'));
  29. $node->getParentClass()->willReturn('stdClass');
  30. $this->supports($node)->shouldReturn(true);
  31. }
  32. function it_does_not_support_class_that_already_extends_a_throwable_class(ClassNode $node)
  33. {
  34. if (\PHP_VERSION_ID < 70000) {
  35. throw new SkippingException('Throwable is not defined in PHP 5');
  36. }
  37. $node->getInterfaces()->willReturn(array('Throwable'));
  38. $node->getParentClass()->willReturn('InvalidArgumentException');
  39. $this->supports($node)->shouldReturn(false);
  40. }
  41. function it_supports_class_implementing_interface_that_extends_throwable(ClassNode $node)
  42. {
  43. if (\PHP_VERSION_ID < 70000) {
  44. throw new SkippingException('Throwable is not defined in PHP 5');
  45. }
  46. $node->getInterfaces()->willReturn(array('Fixtures\Prophecy\ThrowableInterface'));
  47. $node->getParentClass()->willReturn('stdClass');
  48. $this->supports($node)->shouldReturn(true);
  49. }
  50. function it_sets_the_parent_class_to_exception(ClassNode $node)
  51. {
  52. if (\PHP_VERSION_ID < 70000) {
  53. throw new SkippingException('Throwable is not defined in PHP 5');
  54. }
  55. $node->getParentClass()->willReturn('stdClass');
  56. $node->setParentClass('Exception')->shouldBeCalled();
  57. $node->removeMethod('getMessage')->shouldBeCalled();
  58. $node->removeMethod('getCode')->shouldBeCalled();
  59. $node->removeMethod('getFile')->shouldBeCalled();
  60. $node->removeMethod('getLine')->shouldBeCalled();
  61. $node->removeMethod('getTrace')->shouldBeCalled();
  62. $node->removeMethod('getPrevious')->shouldBeCalled();
  63. $node->removeMethod('getNext')->shouldBeCalled();
  64. $node->removeMethod('getTraceAsString')->shouldBeCalled();
  65. $this->apply($node);
  66. }
  67. function it_throws_error_when_trying_to_double_concrete_class_and_throwable_interface(ClassNode $node)
  68. {
  69. if (\PHP_VERSION_ID < 70000) {
  70. throw new SkippingException('Throwable is not defined in PHP 5');
  71. }
  72. $node->getParentClass()->willReturn('ArrayObject');
  73. $this->shouldThrow('Prophecy\Exception\Doubler\ClassCreatorException')->duringApply($node);
  74. }
  75. }