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