CallbackSpec.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace spec\SM\Callback;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Argument;
  5. use SM\Event\TransitionEvent;
  6. use SM\StateMachine\StateMachineInterface;
  7. class CallbackSpec extends ObjectBehavior
  8. {
  9. protected $specs = array();
  10. protected $callable;
  11. protected $sm;
  12. function let(StateMachineInterface $sm)
  13. {
  14. $sm->getState()->willReturn('checkout');
  15. $this->beConstructedWith($this->specs, $this->callable);
  16. }
  17. function it_is_initializable()
  18. {
  19. $this->shouldHaveType('SM\Callback\Callback');
  20. }
  21. function it_satisfies_simple_on(TransitionEvent $event)
  22. {
  23. $specs = array('on' => 'tested-transition');
  24. $this->beConstructedWith($specs, $this->callable);
  25. $event->getConfig()->willReturn($this->getConfig(array('dummy'), 'dummy'));
  26. $event->getTransition()->willReturn('tested-transition');
  27. $event->getState()->willReturn('dummy');
  28. $this->isSatisfiedBy($event)->shouldReturn(true);
  29. }
  30. function it_doesnt_satisfies_simple_on(TransitionEvent $event)
  31. {
  32. $specs = array('on' => 'tested-transition');
  33. $this->beConstructedWith($specs, $this->callable);
  34. $event->getConfig()->willReturn($this->getConfig(array('dummy'), 'dummy'));
  35. $event->getTransition()->willReturn('tested-transition-not-matching');
  36. $this->isSatisfiedBy($event)->shouldReturn(false);
  37. }
  38. function it_satisfies_simple_from(TransitionEvent $event)
  39. {
  40. $specs = array('from' => 'tested-state');
  41. $this->beConstructedWith($specs, $this->callable);
  42. $event->getConfig()->willReturn($this->getConfig(array('tested-state'), 'dummy'));
  43. $event->getTransition()->willReturn('dummy');
  44. $event->getState()->willReturn('tested-state');
  45. $this->isSatisfiedBy($event)->shouldReturn(true);
  46. }
  47. function it_doesnt_satisfies_simple_from(TransitionEvent $event)
  48. {
  49. $specs = array('from' => 'tested-state');
  50. $this->beConstructedWith($specs, $this->callable);
  51. $event->getConfig()->willReturn($this->getConfig(array('tested-state-not-matching'), 'dummy'));
  52. $event->getTransition()->willReturn('dummy');
  53. $event->getState()->willReturn('tested-state-not-matching');
  54. $this->isSatisfiedBy($event)->shouldReturn(false);
  55. }
  56. function it_satisfies_simple_to(TransitionEvent $event)
  57. {
  58. $specs = array('to' => 'tested-state');
  59. $this->beConstructedWith($specs, $this->callable);
  60. $event->getConfig()->willReturn($this->getConfig(array('dummy'), 'tested-state'));
  61. $event->getTransition()->willReturn('dummy');
  62. $event->getState()->willReturn('dummy');
  63. $this->isSatisfiedBy($event)->shouldReturn(true);
  64. }
  65. function it_doesnt_satisfies_simple_to(TransitionEvent $event)
  66. {
  67. $specs = array('from' => 'tested-state');
  68. $this->beConstructedWith($specs, $this->callable);
  69. $event->getConfig()->willReturn($this->getConfig(array('tested-state-not-matching'), 'dummy'));
  70. $event->getTransition()->willReturn('dummy');
  71. $event->getState()->willReturn('dummy');
  72. $this->isSatisfiedBy($event)->shouldReturn(false);
  73. }
  74. function it_satisfies_complex_specs(TransitionEvent $event)
  75. {
  76. $specs = array('to' => 'tested-state-to', 'from' => 'tested-state-from', 'on' => 'tested-transition');
  77. $this->beConstructedWith($specs, $this->callable);
  78. $event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'tested-state-to'));
  79. $event->getTransition()->willReturn('tested-transition');
  80. $event->getState()->willReturn('tested-state-from');
  81. $this->isSatisfiedBy($event)->shouldReturn(true);
  82. }
  83. function it_doesnt_satisfies_wrong_from(TransitionEvent $event)
  84. {
  85. $specs = array('to' => 'tested-state-to', 'from' => 'tested-wrong', 'on' => 'tested-transition');
  86. $this->beConstructedWith($specs, $this->callable);
  87. $event->getConfig()->willReturn($this->getConfig(array('dummy'), 'tested-state-to'));
  88. $event->getTransition()->willReturn('tested-transition');
  89. $event->getState()->willReturn('dummy');
  90. $this->isSatisfiedBy($event)->shouldReturn(false);
  91. }
  92. function it_doesnt_satisfies_wrong_to(TransitionEvent $event)
  93. {
  94. $specs = array('to' => 'tested-wrong', 'from' => 'tested-state-from', 'on' => 'tested-transition');
  95. $this->beConstructedWith($specs, $this->callable);
  96. $event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'dummy'));
  97. $event->getTransition()->willReturn('tested-transition');
  98. $event->getState()->willReturn('tested-state-from');
  99. $this->isSatisfiedBy($event)->shouldReturn(false);
  100. }
  101. function it_doesnt_satisfies_wrong_on(TransitionEvent $event)
  102. {
  103. $specs = array('to' => 'tested-state-to', 'from' => 'tested-state-from', 'on' => 'tested-wrong');
  104. $this->beConstructedWith($specs, $this->callable);
  105. $event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'tested-state-to'));
  106. $event->getTransition()->willReturn('dummy');
  107. $event->getState()->willReturn('tested-state-from');
  108. $this->isSatisfiedBy($event)->shouldReturn(false);
  109. }
  110. function it_doesnt_satisfies_excluded_from(TransitionEvent $event)
  111. {
  112. $specs = array('to' => 'tested-state-to', 'excluded_from' => 'tested-state-from');
  113. $this->beConstructedWith($specs, $this->callable);
  114. $event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'tested-state-to'));
  115. $event->getTransition()->willReturn('dummy');
  116. $event->getState()->willReturn('tested-state-from');
  117. $this->isSatisfiedBy($event)->shouldReturn(false);
  118. }
  119. protected function getConfig($from = array(), $to)
  120. {
  121. return array('from' => $from, 'to' => $to);
  122. }
  123. }