• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / testbench-core / 11968182539

22 Nov 2024 07:16AM UTC coverage: 91.991% (+0.7%) from 91.321%
11968182539

Pull #265

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Pull Request #265: [7.x] Allow to configure `tty` via `Orchestra\Testbench\remote()` function

3 of 4 new or added lines in 1 file covered. (75.0%)

93 existing lines in 25 files now uncovered.

1206 of 1311 relevant lines covered (91.99%)

61.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.67
/src/Concerns/ApplicationTestingHooks.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Carbon\Carbon;
6
use Carbon\CarbonImmutable;
7
use Closure;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\ParallelTesting;
10
use Orchestra\Testbench\Foundation\Application as Testbench;
11
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
12
use Throwable;
13

14
trait ApplicationTestingHooks
15
{
16
    use InteractsWithMockery;
17
    use InteractsWithPHPUnit;
18
    use InteractsWithTestCase;
19

20
    /**
21
     * The Illuminate application instance.
22
     *
23
     * @var \Illuminate\Foundation\Application|null
24
     */
25
    protected $app;
26

27
    /**
28
     * The callbacks that should be run after the application is created.
29
     *
30
     * @var array<int, callable():void>
31
     */
32
    protected $afterApplicationCreatedCallbacks = [];
33

34
    /**
35
     * The callbacks that should be run after the application is refreshed.
36
     *
37
     * @var array<int, callable():void>
38
     */
39
    protected $afterApplicationRefreshedCallbacks = [];
40

41
    /**
42
     * The callbacks that should be run before the application is destroyed.
43
     *
44
     * @var array<int, callable():void>
45
     */
46
    protected $beforeApplicationDestroyedCallbacks = [];
47

48
    /**
49
     * The exception thrown while running an application destruction callback.
50
     *
51
     * @var \Throwable|null
52
     */
53
    protected $callbackException;
54

55
    /**
56
     * Indicates if we have made it through the base setUp function.
57
     *
58
     * @var bool
59
     */
60
    protected $setUpHasRun = false;
61

62
    /**
63
     * Setup the testing hooks.
64
     *
65
     * @param  (\Closure():(void))|null  $callback
66
     * @return void
67
     */
68
    final protected function setUpTheApplicationTestingHooks(?Closure $callback = null): void
69
    {
70
        if (! $this->app) {
151✔
71
            $this->refreshApplication();
151✔
72

73
            $this->setUpTheTestEnvironmentUsingTestCase();
151✔
74

75
            $this->setUpParallelTestingCallbacks();
151✔
76
        }
77

78
        $this->setUpHasRun = true;
151✔
79

80
        /** @var \Illuminate\Foundation\Application $app */
81
        $app = $this->app;
151✔
82

83
        $this->callAfterApplicationRefreshedCallbacks();
151✔
84

85
        if (! \is_null($callback)) {
151✔
86
            \call_user_func($callback);
151✔
87
        }
88

89
        $this->callAfterApplicationCreatedCallbacks();
151✔
90

91
        Model::setEventDispatcher($app['events']);
151✔
92
    }
93

94
    /**
95
     * Teardown the testing hooks.
96
     *
97
     * @param  (\Closure():(void))|null  $callback
98
     * @return void
99
     *
100
     * @throws \Throwable
101
     */
102
    final protected function tearDownTheApplicationTestingHooks(?Closure $callback = null): void
103
    {
104
        if ($this->app) {
151✔
105
            $this->callBeforeApplicationDestroyedCallbacks();
151✔
106

107
            $this->tearDownTheTestEnvironmentUsingTestCase();
151✔
108

109
            $this->tearDownParallelTestingCallbacks();
151✔
110

111
            $this->app?->flush();
151✔
112

113
            $this->app = null;
151✔
114
        }
115

116
        $this->setUpHasRun = false;
151✔
117

118
        if (! \is_null($callback)) {
151✔
119
            \call_user_func($callback);
151✔
120
        }
121

122
        $this->tearDownTheTestEnvironmentUsingMockery();
151✔
123

124
        Carbon::setTestNow();
151✔
125

126
        if (class_exists(CarbonImmutable::class)) {
151✔
127
            CarbonImmutable::setTestNow();
151✔
128
        }
129

130
        $this->afterApplicationCreatedCallbacks = [];
151✔
131
        $this->beforeApplicationDestroyedCallbacks = [];
151✔
132

133
        Testbench::flushState();
151✔
134

135
        if ($this->callbackException) {
151✔
UNCOV
136
            throw $this->callbackException;
×
137
        }
138
    }
139

140
    /**
141
     * Setup parallel testing callback.
142
     */
143
    protected function setUpParallelTestingCallbacks(): void
144
    {
145
        if (class_exists(ParallelTesting::class) && $this instanceof PHPUnitTestCase) {
151✔
146
            /** @phpstan-ignore staticMethod.notFound, argument.type */
147
            ParallelTesting::callSetUpTestCaseCallbacks($this);
151✔
148
        }
149
    }
150

151
    /**
152
     * Teardown parallel testing callback.
153
     */
154
    protected function tearDownParallelTestingCallbacks(): void
155
    {
156
        if (class_exists(ParallelTesting::class) && $this instanceof PHPUnitTestCase) {
151✔
157
            /** @phpstan-ignore staticMethod.notFound, argument.type */
158
            ParallelTesting::callTearDownTestCaseCallbacks($this);
151✔
159
        }
160
    }
161

162
    /**
163
     * Register a callback to be run after the application is refreshed.
164
     *
165
     * @param  callable():void  $callback
166
     * @return void
167
     */
168
    public function afterApplicationRefreshed(callable $callback): void
169
    {
UNCOV
170
        $this->afterApplicationRefreshedCallbacks[] = $callback;
×
171

UNCOV
172
        if ($this->setUpHasRun) {
×
UNCOV
173
            \call_user_func($callback);
×
174
        }
175
    }
176

177
    /**
178
     * Execute the application's post-refreshed callbacks.
179
     *
180
     * @return void
181
     */
182
    protected function callAfterApplicationRefreshedCallbacks(): void
183
    {
184
        foreach ($this->afterApplicationRefreshedCallbacks as $callback) {
151✔
185
            \call_user_func($callback);
×
186
        }
187
    }
188

189
    /**
190
     * Register a callback to be run after the application is created.
191
     *
192
     * @param  callable():void  $callback
193
     * @return void
194
     */
195
    public function afterApplicationCreated(callable $callback): void
196
    {
197
        $this->afterApplicationCreatedCallbacks[] = $callback;
35✔
198

199
        if ($this->setUpHasRun) {
35✔
200
            \call_user_func($callback);
33✔
201
        }
202
    }
203

204
    /**
205
     * Execute the application's post-creation callbacks.
206
     *
207
     * @return void
208
     */
209
    protected function callAfterApplicationCreatedCallbacks(): void
210
    {
211
        foreach ($this->afterApplicationCreatedCallbacks as $callback) {
151✔
212
            \call_user_func($callback);
34✔
213
        }
214
    }
215

216
    /**
217
     * Register a callback to be run before the application is destroyed.
218
     *
219
     * @param  callable():void  $callback
220
     * @return void
221
     */
222
    public function beforeApplicationDestroyed(callable $callback): void
223
    {
224
        array_unshift($this->beforeApplicationDestroyedCallbacks, $callback);
152✔
225
    }
226

227
    /**
228
     * Execute the application's pre-destruction callbacks.
229
     *
230
     * @return void
231
     */
232
    protected function callBeforeApplicationDestroyedCallbacks(): void
233
    {
234
        foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
151✔
235
            try {
236
                \call_user_func($callback);
151✔
UNCOV
237
            } catch (Throwable $e) {
×
UNCOV
238
                if (! $this->callbackException) {
×
UNCOV
239
                    $this->callbackException = $e;
×
240
                }
241
            }
242
        }
243
    }
244

245
    /**
246
     * Refresh the application instance.
247
     *
248
     * @return void
249
     */
250
    abstract protected function refreshApplication();
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc