Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Testing;
4
 
5
use Carbon\CarbonImmutable;
6
use Illuminate\Console\Application as Artisan;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Foundation\Bootstrap\HandleExceptions;
9
use Illuminate\Queue\Queue;
10
use Illuminate\Support\Carbon;
11
use Illuminate\Support\Facades\Facade;
12
use Illuminate\Support\Facades\ParallelTesting;
13
use Illuminate\Support\Str;
14
use Illuminate\View\Component;
15
use Mockery;
16
use Mockery\Exception\InvalidCountException;
17
use PHPUnit\Framework\TestCase as BaseTestCase;
18
use PHPUnit\Util\Annotation\Registry;
19
use Throwable;
20
 
21
abstract class TestCase extends BaseTestCase
22
{
23
    use Concerns\InteractsWithContainer,
24
        Concerns\MakesHttpRequests,
25
        Concerns\InteractsWithAuthentication,
26
        Concerns\InteractsWithConsole,
27
        Concerns\InteractsWithDatabase,
28
        Concerns\InteractsWithDeprecationHandling,
29
        Concerns\InteractsWithExceptionHandling,
30
        Concerns\InteractsWithSession,
31
        Concerns\InteractsWithTime,
32
        Concerns\InteractsWithViews,
33
        Concerns\MocksApplicationServices;
34
 
35
    /**
36
     * The Illuminate application instance.
37
     *
38
     * @var \Illuminate\Foundation\Application
39
     */
40
    protected $app;
41
 
42
    /**
43
     * The callbacks that should be run after the application is created.
44
     *
45
     * @var array
46
     */
47
    protected $afterApplicationCreatedCallbacks = [];
48
 
49
    /**
50
     * The callbacks that should be run before the application is destroyed.
51
     *
52
     * @var array
53
     */
54
    protected $beforeApplicationDestroyedCallbacks = [];
55
 
56
    /**
57
     * The exception thrown while running an application destruction callback.
58
     *
59
     * @var \Throwable
60
     */
61
    protected $callbackException;
62
 
63
    /**
64
     * Indicates if we have made it through the base setUp function.
65
     *
66
     * @var bool
67
     */
68
    protected $setUpHasRun = false;
69
 
70
    /**
71
     * Creates the application.
72
     *
73
     * Needs to be implemented by subclasses.
74
     *
75
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
76
     */
77
    abstract public function createApplication();
78
 
79
    /**
80
     * Setup the test environment.
81
     *
82
     * @return void
83
     */
84
    protected function setUp(): void
85
    {
86
        static::$latestResponse = null;
87
 
88
        Facade::clearResolvedInstances();
89
 
90
        if (! $this->app) {
91
            $this->refreshApplication();
92
 
93
            ParallelTesting::callSetUpTestCaseCallbacks($this);
94
        }
95
 
96
        $this->setUpTraits();
97
 
98
        foreach ($this->afterApplicationCreatedCallbacks as $callback) {
99
            $callback();
100
        }
101
 
102
        Model::setEventDispatcher($this->app['events']);
103
 
104
        $this->setUpHasRun = true;
105
    }
106
 
107
    /**
108
     * Refresh the application instance.
109
     *
110
     * @return void
111
     */
112
    protected function refreshApplication()
113
    {
114
        $this->app = $this->createApplication();
115
    }
116
 
117
    /**
118
     * Boot the testing helper traits.
119
     *
120
     * @return array
121
     */
122
    protected function setUpTraits()
123
    {
124
        $uses = array_flip(class_uses_recursive(static::class));
125
 
126
        if (isset($uses[RefreshDatabase::class])) {
127
            $this->refreshDatabase();
128
        }
129
 
130
        if (isset($uses[DatabaseMigrations::class])) {
131
            $this->runDatabaseMigrations();
132
        }
133
 
399 lars 134
        if (isset($uses[DatabaseTruncation::class])) {
135
            $this->truncateDatabaseTables();
136
        }
137
 
148 lars 138
        if (isset($uses[DatabaseTransactions::class])) {
139
            $this->beginDatabaseTransaction();
140
        }
141
 
142
        if (isset($uses[WithoutMiddleware::class])) {
143
            $this->disableMiddlewareForAllTests();
144
        }
145
 
146
        if (isset($uses[WithoutEvents::class])) {
147
            $this->disableEventsForAllTests();
148
        }
149
 
150
        if (isset($uses[WithFaker::class])) {
151
            $this->setUpFaker();
152
        }
153
 
154
        foreach ($uses as $trait) {
155
            if (method_exists($this, $method = 'setUp'.class_basename($trait))) {
156
                $this->{$method}();
157
            }
158
 
159
            if (method_exists($this, $method = 'tearDown'.class_basename($trait))) {
160
                $this->beforeApplicationDestroyed(fn () => $this->{$method}());
161
            }
162
        }
163
 
164
        return $uses;
165
    }
166
 
167
    /**
168
     * Clean up the testing environment before the next test.
169
     *
170
     * @return void
171
     *
172
     * @throws \Mockery\Exception\InvalidCountException
173
     */
174
    protected function tearDown(): void
175
    {
176
        if ($this->app) {
177
            $this->callBeforeApplicationDestroyedCallbacks();
178
 
179
            ParallelTesting::callTearDownTestCaseCallbacks($this);
180
 
181
            $this->app->flush();
182
 
183
            $this->app = null;
184
        }
185
 
186
        $this->setUpHasRun = false;
187
 
188
        if (property_exists($this, 'serverVariables')) {
189
            $this->serverVariables = [];
190
        }
191
 
192
        if (property_exists($this, 'defaultHeaders')) {
193
            $this->defaultHeaders = [];
194
        }
195
 
196
        if (class_exists('Mockery')) {
197
            if ($container = Mockery::getContainer()) {
198
                $this->addToAssertionCount($container->mockery_getExpectationCount());
199
            }
200
 
201
            try {
202
                Mockery::close();
203
            } catch (InvalidCountException $e) {
204
                if (! Str::contains($e->getMethodName(), ['doWrite', 'askQuestion'])) {
205
                    throw $e;
206
                }
207
            }
208
        }
209
 
210
        if (class_exists(Carbon::class)) {
211
            Carbon::setTestNow();
212
        }
213
 
214
        if (class_exists(CarbonImmutable::class)) {
215
            CarbonImmutable::setTestNow();
216
        }
217
 
218
        $this->afterApplicationCreatedCallbacks = [];
219
        $this->beforeApplicationDestroyedCallbacks = [];
220
 
221
        $this->originalExceptionHandler = null;
222
        $this->originalDeprecationHandler = null;
223
 
224
        Artisan::forgetBootstrappers();
225
        Component::flushCache();
226
        Component::forgetComponentsResolver();
227
        Component::forgetFactory();
228
        Queue::createPayloadUsing(null);
229
        HandleExceptions::forgetApp();
230
 
231
        if ($this->callbackException) {
232
            throw $this->callbackException;
233
        }
234
    }
235
 
236
    /**
237
     * Clean up the testing environment before the next test case.
238
     *
239
     * @return void
240
     */
241
    public static function tearDownAfterClass(): void
242
    {
243
        static::$latestResponse = null;
244
 
245
        (function () {
246
            $this->classDocBlocks = [];
247
            $this->methodDocBlocks = [];
248
        })->call(Registry::getInstance());
249
    }
250
 
251
    /**
252
     * Register a callback to be run after the application is created.
253
     *
254
     * @param  callable  $callback
255
     * @return void
256
     */
257
    public function afterApplicationCreated(callable $callback)
258
    {
259
        $this->afterApplicationCreatedCallbacks[] = $callback;
260
 
261
        if ($this->setUpHasRun) {
262
            $callback();
263
        }
264
    }
265
 
266
    /**
267
     * Register a callback to be run before the application is destroyed.
268
     *
269
     * @param  callable  $callback
270
     * @return void
271
     */
272
    protected function beforeApplicationDestroyed(callable $callback)
273
    {
274
        $this->beforeApplicationDestroyedCallbacks[] = $callback;
275
    }
276
 
277
    /**
278
     * Execute the application's pre-destruction callbacks.
279
     *
280
     * @return void
281
     */
282
    protected function callBeforeApplicationDestroyedCallbacks()
283
    {
284
        foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
285
            try {
286
                $callback();
287
            } catch (Throwable $e) {
288
                if (! $this->callbackException) {
289
                    $this->callbackException = $e;
290
                }
291
            }
292
        }
293
    }
294
 
295
    /**
296
     * This method is called when a test method did not execute successfully.
297
     *
298
     * @param  \Throwable  $exception
299
     * @return void
300
     */
301
    protected function onNotSuccessfulTest(Throwable $exception): void
302
    {
303
        parent::onNotSuccessfulTest(
304
            is_null(static::$latestResponse)
305
                ? $exception
306
                : static::$latestResponse->transformNotSuccessfulException($exception)
307
        );
308
    }
309
}