CancellationQueueTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace React\Promise;
  3. class CancellationQueueTest extends TestCase
  4. {
  5. /** @test */
  6. public function acceptsSimpleCancellableThenable()
  7. {
  8. $p = new SimpleTestCancellableThenable();
  9. $cancellationQueue = new CancellationQueue();
  10. $cancellationQueue->enqueue($p);
  11. $cancellationQueue();
  12. $this->assertTrue($p->cancelCalled);
  13. }
  14. /** @test */
  15. public function ignoresSimpleCancellable()
  16. {
  17. $p = new SimpleTestCancellable();
  18. $cancellationQueue = new CancellationQueue();
  19. $cancellationQueue->enqueue($p);
  20. $cancellationQueue();
  21. $this->assertFalse($p->cancelCalled);
  22. }
  23. /** @test */
  24. public function callsCancelOnPromisesEnqueuedBeforeStart()
  25. {
  26. $d1 = $this->getCancellableDeferred();
  27. $d2 = $this->getCancellableDeferred();
  28. $cancellationQueue = new CancellationQueue();
  29. $cancellationQueue->enqueue($d1->promise());
  30. $cancellationQueue->enqueue($d2->promise());
  31. $cancellationQueue();
  32. }
  33. /** @test */
  34. public function callsCancelOnPromisesEnqueuedAfterStart()
  35. {
  36. $d1 = $this->getCancellableDeferred();
  37. $d2 = $this->getCancellableDeferred();
  38. $cancellationQueue = new CancellationQueue();
  39. $cancellationQueue();
  40. $cancellationQueue->enqueue($d2->promise());
  41. $cancellationQueue->enqueue($d1->promise());
  42. }
  43. /** @test */
  44. public function doesNotCallCancelTwiceWhenStartedTwice()
  45. {
  46. $d = $this->getCancellableDeferred();
  47. $cancellationQueue = new CancellationQueue();
  48. $cancellationQueue->enqueue($d->promise());
  49. $cancellationQueue();
  50. $cancellationQueue();
  51. }
  52. /** @test */
  53. public function rethrowsExceptionsThrownFromCancel()
  54. {
  55. $this->setExpectedException('\Exception', 'test');
  56. $mock = $this
  57. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  58. ->getMock();
  59. $mock
  60. ->expects($this->once())
  61. ->method('cancel')
  62. ->will($this->throwException(new \Exception('test')));
  63. $cancellationQueue = new CancellationQueue();
  64. $cancellationQueue->enqueue($mock);
  65. $cancellationQueue();
  66. }
  67. private function getCancellableDeferred()
  68. {
  69. $mock = $this->createCallableMock();
  70. $mock
  71. ->expects($this->once())
  72. ->method('__invoke');
  73. return new Deferred($mock);
  74. }
  75. }