CommandRetryTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.6.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core\Retry;
  15. use Cake\Core\Retry\CommandRetry;
  16. use Cake\Core\Retry\RetryStrategyInterface;
  17. use Cake\TestSuite\TestCase;
  18. use Exception;
  19. /**
  20. * Tests for the CommandRetry class
  21. */
  22. class CommandRetryTest extends TestCase
  23. {
  24. /**
  25. * Simple retry test
  26. *
  27. * @return void
  28. */
  29. public function testRetry()
  30. {
  31. $count = 0;
  32. $exception = new Exception('this is failing');
  33. $action = function () use (&$count, $exception) {
  34. $count++;
  35. if ($count < 4) {
  36. throw $exception;
  37. }
  38. return $count;
  39. };
  40. $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
  41. $strategy
  42. ->expects($this->exactly(3))
  43. ->method('shouldRetry')
  44. ->will($this->returnCallback(function ($e, $c) use ($exception, &$count) {
  45. $this->assertSame($e, $exception);
  46. $this->assertEquals($c + 1, $count);
  47. return true;
  48. }));
  49. $retry = new CommandRetry($strategy, 5);
  50. $this->assertEquals(4, $retry->run($action));
  51. }
  52. /**
  53. * Test attempts exceeded
  54. *
  55. * @return void
  56. */
  57. public function testExceedAttempts()
  58. {
  59. $exception = new Exception('this is failing');
  60. $action = function () use ($exception) {
  61. throw $exception;
  62. };
  63. $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
  64. $strategy
  65. ->expects($this->exactly(4))
  66. ->method('shouldRetry')
  67. ->will($this->returnCallback(function ($e) use ($exception) {
  68. return true;
  69. }));
  70. $retry = new CommandRetry($strategy, 3);
  71. $this->expectException(Exception::class);
  72. $this->expectExceptionMessage('this is failing');
  73. $retry->run($action);
  74. }
  75. /**
  76. * Test that the strategy is respected
  77. *
  78. * @return void
  79. */
  80. public function testRespectStrategy()
  81. {
  82. $action = function () {
  83. throw new Exception('this is failing');
  84. };
  85. $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
  86. $strategy
  87. ->expects($this->once())
  88. ->method('shouldRetry')
  89. ->will($this->returnCallback(function () {
  90. return false;
  91. }));
  92. $retry = new CommandRetry($strategy, 3);
  93. $this->expectException(Exception::class);
  94. $this->expectExceptionMessage('this is failing');
  95. $retry->run($action);
  96. }
  97. }