TestCase.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Testing;
  12. use Mockery as m;
  13. use Throwable;
  14. use function Hyperf\Support\class_basename;
  15. use function Hyperf\Support\class_uses_recursive;
  16. /**
  17. * @internal
  18. * @coversNothing
  19. */
  20. abstract class TestCase extends \PHPUnit\Framework\TestCase
  21. {
  22. use Concerns\InteractsWithContainer;
  23. use Concerns\InteractsWithModelFactory;
  24. use Concerns\MakesHttpRequests;
  25. use Concerns\RunTestsInCoroutine;
  26. use Concerns\InteractsWithDatabase;
  27. /**
  28. * The callbacks that should be run after the application is created.
  29. */
  30. protected array $afterApplicationCreatedCallbacks = [];
  31. /**
  32. * The callbacks that should be run before the application is destroyed.
  33. */
  34. protected array $beforeApplicationDestroyedCallbacks = [];
  35. /**
  36. * The exception thrown while running an application destruction callback.
  37. */
  38. protected ?Throwable $callbackException = null;
  39. protected function setUp(): void
  40. {
  41. $this->refreshContainer();
  42. $this->setUpTraits();
  43. foreach ($this->afterApplicationCreatedCallbacks as $callback) {
  44. $callback();
  45. }
  46. }
  47. protected function tearDown(): void
  48. {
  49. $this->flushContainer();
  50. $this->callBeforeApplicationDestroyedCallbacks();
  51. try {
  52. m::close();
  53. } catch (Throwable $e) {
  54. }
  55. if ($this->callbackException) {
  56. throw $this->callbackException;
  57. }
  58. }
  59. /**
  60. * Boot the testing helper traits.
  61. *
  62. * @return array
  63. */
  64. protected function setUpTraits()
  65. {
  66. $uses = array_flip(class_uses_recursive(static::class));
  67. foreach ($uses as $trait) {
  68. if (method_exists($this, $method = 'setUp' . class_basename($trait))) {
  69. $this->{$method}();
  70. }
  71. if (method_exists($this, $method = 'tearDown' . class_basename($trait))) {
  72. $this->beforeApplicationDestroyed(fn () => $this->{$method}());
  73. }
  74. }
  75. return $uses;
  76. }
  77. /**
  78. * Register a callback to be run before the application is destroyed.
  79. */
  80. protected function beforeApplicationDestroyed(callable $callback)
  81. {
  82. $this->beforeApplicationDestroyedCallbacks[] = $callback;
  83. }
  84. /**
  85. * Execute the application's pre-destruction callbacks.
  86. */
  87. protected function callBeforeApplicationDestroyedCallbacks()
  88. {
  89. foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
  90. try {
  91. $callback();
  92. } catch (Throwable $e) {
  93. if (! $this->callbackException) {
  94. $this->callbackException = $e;
  95. }
  96. }
  97. }
  98. }
  99. }