CallbackPromiseSpec.php 2.6 KB

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