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

orchestral / testbench-core / 6661306552

27 Oct 2023 12:30AM UTC coverage: 92.024% (+0.09%) from 91.935%
6661306552

Pull #147

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Pull Request #147: Add `Orchestra\Testbench\Concerns\ApplicationTestingHooks`

58 of 58 new or added lines in 3 files covered. (100.0%)

923 of 1003 relevant lines covered (92.02%)

40.75 hits per line

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

88.68
/src/Concerns/Testing.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Carbon\Carbon;
6
use Carbon\CarbonImmutable;
7
use Illuminate\Console\Application as Artisan;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Foundation\Bootstrap\HandleExceptions;
10
use Illuminate\Foundation\Testing\DatabaseMigrations;
11
use Illuminate\Foundation\Testing\DatabaseTransactions;
12
use Illuminate\Foundation\Testing\DatabaseTruncation;
13
use Illuminate\Foundation\Testing\RefreshDatabase;
14
use Illuminate\Foundation\Testing\WithFaker;
15
use Illuminate\Foundation\Testing\WithoutEvents;
16
use Illuminate\Foundation\Testing\WithoutMiddleware;
17
use Illuminate\Queue\Queue;
18
use Illuminate\Support\Facades\ParallelTesting;
19
use Illuminate\Support\LazyCollection;
20
use Illuminate\View\Component;
21
use Mockery;
22
use PHPUnit\Framework\TestCase;
23
use Throwable;
24

25
trait Testing
26
{
27
    use ApplicationTestingHooks;
28
    use CreatesApplication;
29
    use HandlesAnnotations;
30
    use HandlesAttributes;
31
    use HandlesDatabases;
32
    use HandlesRoutes;
33
    use InteractsWithMigrations;
34
    use WithFactories;
35

36
    /**
37
     * The Illuminate application instance.
38
     *
39
     * @var \Illuminate\Foundation\Application|null
40
     */
41
    protected $app;
42

43
    /**
44
     * The callbacks that should be run after the application is created.
45
     *
46
     * @var array<int, callable():void>
47
     */
48
    protected $afterApplicationCreatedCallbacks = [];
49

50
    /**
51
     * The callbacks that should be run after the application is refreshed.
52
     *
53
     * @var array<int, callable():void>
54
     */
55
    protected $afterApplicationRefreshedCallbacks = [];
56

57
    /**
58
     * The callbacks that should be run before the application is destroyed.
59
     *
60
     * @var array<int, callable():void>
61
     */
62
    protected $beforeApplicationDestroyedCallbacks = [];
63

64
    /**
65
     * The exception thrown while running an application destruction callback.
66
     *
67
     * @var \Throwable|null
68
     */
69
    protected $callbackException;
70

71
    /**
72
     * Indicates if we have made it through the base setUp function.
73
     *
74
     * @var bool
75
     */
76
    protected $setUpHasRun = false;
77

78
    /**
79
     * Setup the test environment.
80
     *
81
     * @internal
82
     *
83
     * @return void
84
     */
85
    final protected function setUpTheTestEnvironment(): void
86
    {
87
        $this->setUpTheApplicationTestingHooks(function () {
100✔
88
            $this->setUpTraits();
100✔
89
        });
100✔
90

91
        $this->setUpHasRun = true;
100✔
92
    }
93

94
    /**
95
     * Clean up the testing environment before the next test.
96
     *
97
     * @internal
98
     *
99
     * @return void
100
     */
101
    final protected function tearDownTheTestEnvironment(): void
102
    {
103
        $this->tearDownTheApplicationTestingHooks(function () {
100✔
104
            $this->setUpHasRun = false;
100✔
105

106
            if (property_exists($this, 'serverVariables')) {
100✔
107
                $this->serverVariables = [];
100✔
108
            }
109

110
            if (property_exists($this, 'defaultHeaders')) {
100✔
111
                $this->defaultHeaders = [];
100✔
112
            }
113

114
            if (property_exists($this, 'originalExceptionHandler')) {
100✔
115
                $this->originalExceptionHandler = null;
100✔
116
            }
117

118
            if (property_exists($this, 'originalDeprecationHandler')) {
100✔
119
                $this->originalDeprecationHandler = null;
100✔
120
            }
121
        });
100✔
122
    }
123

124
    /**
125
     * Boot the testing helper traits.
126
     *
127
     * @internal
128
     *
129
     * @param  array<class-string, class-string>  $uses
130
     * @return array<class-string, class-string>
131
     */
132
    final protected function setUpTheTestEnvironmentTraits(array $uses): array
133
    {
134
        if (isset($uses[WithWorkbench::class])) {
100✔
135
            /** @phpstan-ignore-next-line */
136
            $this->setUpWithWorkbench();
11✔
137
        }
138

139
        $this->setUpDatabaseRequirements(function () use ($uses) {
100✔
140
            if (isset($uses[RefreshDatabase::class])) {
100✔
141
                /** @phpstan-ignore-next-line */
142
                $this->refreshDatabase();
2✔
143
            }
144

145
            if (isset($uses[DatabaseMigrations::class])) {
100✔
146
                /** @phpstan-ignore-next-line */
147
                $this->runDatabaseMigrations();
×
148
            }
149

150
            if (isset($uses[DatabaseTruncation::class])) {
100✔
151
                /** @phpstan-ignore-next-line */
152
                $this->truncateDatabaseTables();
×
153
            }
154
        });
100✔
155

156
        if (isset($uses[DatabaseTransactions::class])) {
100✔
157
            /** @phpstan-ignore-next-line */
158
            $this->beginDatabaseTransaction();
×
159
        }
160

161
        if (isset($uses[WithoutMiddleware::class])) {
100✔
162
            /** @phpstan-ignore-next-line */
163
            $this->disableMiddlewareForAllTests();
×
164
        }
165

166
        if (isset($uses[WithoutEvents::class])) {
100✔
167
            /** @phpstan-ignore-next-line */
168
            $this->disableEventsForAllTests();
×
169
        }
170

171
        if (isset($uses[WithFaker::class])) {
100✔
172
            /** @phpstan-ignore-next-line */
173
            $this->setUpFaker();
1✔
174
        }
175

176
        LazyCollection::make(static function () use ($uses) {
100✔
177
            foreach ($uses as $use) {
100✔
178
                yield $use;
100✔
179
            }
180
        })
100✔
181
            ->reject(function ($use) {
100✔
182
                /** @var class-string $use */
183
                return $this->setUpTheTestEnvironmentTraitToBeIgnored($use);
100✔
184
            })->map(static function ($use) {
100✔
185
                /** @var class-string $use */
186
                return class_basename($use);
100✔
187
            })->each(function ($traitBaseName) {
100✔
188
                /** @var string $traitBaseName */
189
                if (method_exists($this, $method = 'setUp'.$traitBaseName)) {
100✔
190
                    $this->{$method}();
16✔
191
                }
192

193
                if (method_exists($this, $method = 'tearDown'.$traitBaseName)) {
100✔
194
                    $this->beforeApplicationDestroyed(function () use ($method) {
14✔
195
                        $this->{$method}();
14✔
196
                    });
14✔
197
                }
198
            });
100✔
199

200
        return $uses;
100✔
201
    }
202

203
    /**
204
     * Determine trait should be ignored from being autoloaded.
205
     *
206
     * @param  class-string  $use
207
     * @return bool
208
     */
209
    protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
210
    {
211
        return false;
×
212
    }
213

214
    /**
215
     * Reload the application instance with cached routes.
216
     */
217
    protected function reloadApplication(): void
218
    {
219
        $this->tearDownTheTestEnvironment();
1✔
220
        $this->setUpTheTestEnvironment();
1✔
221
    }
222

223
    /**
224
     * Boot the testing helper traits.
225
     *
226
     * @return array<class-string, class-string>
227
     */
228
    abstract protected function setUpTraits();
229
}
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