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

orchestral / testbench-core / 7147378509

08 Dec 2023 11:33PM UTC coverage: 92.358% (-0.3%) from 92.706%
7147378509

Pull #166

github

web-flow
Merge 1a6ddd0e6 into 4565dc280
Pull Request #166: PestPHP Improvements

76 of 85 new or added lines in 7 files covered. (89.41%)

3 existing lines in 2 files now uncovered.

1136 of 1230 relevant lines covered (92.36%)

45.2 hits per line

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

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

3
namespace Orchestra\Testbench\Concerns;
4

5
use Closure;
6
use Illuminate\Foundation\Testing\DatabaseMigrations;
7
use Illuminate\Foundation\Testing\DatabaseTransactions;
8
use Illuminate\Foundation\Testing\DatabaseTruncation;
9
use Illuminate\Foundation\Testing\RefreshDatabase;
10
use Illuminate\Foundation\Testing\WithFaker;
11
use Illuminate\Foundation\Testing\WithoutEvents;
12
use Illuminate\Foundation\Testing\WithoutMiddleware;
13
use Illuminate\Support\LazyCollection;
14
use Orchestra\Testbench\Pest\WithPest;
15
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
16

17
/**
18
 * @api
19
 */
20
trait Testing
21
{
22
    use ApplicationTestingHooks;
23
    use CreatesApplication;
24
    use HandlesAnnotations;
25
    use HandlesAttributes;
26
    use HandlesDatabases;
27
    use HandlesRoutes;
28
    use InteractsWithMigrations;
29
    use InteractsWithPHPUnit;
30
    use WithFactories;
31

32
    /**
33
     * Setup the test environment.
34
     *
35
     * @internal
36
     *
37
     * @return void
38
     */
39
    final protected function setUpTheTestEnvironment(): void
40
    {
41
        $setUp = function () {
113✔
42
            $this->setUpTheApplicationTestingHooks(function () {
113✔
43
                $this->setUpTraits();
113✔
44
            });
113✔
45
        };
113✔
46

47
        /** @phpstan-ignore-next-line */
48
        if ($this instanceof PHPUnitTestCase && static::usesTestingConcern(WithPest::class)) {
113✔
49
            /** @phpstan-ignore-next-line */
NEW
50
            $this->setUpTheEnvironmentUsingPest();
×
51
        }
52

53
        $this->testCaseSetUpCallback instanceof Closure
113✔
NEW
54
            ? \call_user_func(Closure::bind($this->testCaseSetUpCallback, $this), $setUp)
×
55
            : \call_user_func($setUp);
113✔
56
    }
57

58
    /**
59
     * Clean up the testing environment before the next test.
60
     *
61
     * @internal
62
     *
63
     * @return void
64
     */
65
    final protected function tearDownTheTestEnvironment(): void
66
    {
67
        $tearDown = function () {
113✔
68
            $this->tearDownTheApplicationTestingHooks(function () {
113✔
69
                if (property_exists($this, 'serverVariables')) {
113✔
70
                    $this->serverVariables = [];
113✔
71
                }
72

73
                if (property_exists($this, 'defaultHeaders')) {
113✔
74
                    $this->defaultHeaders = [];
113✔
75
                }
76

77
                if (property_exists($this, 'originalExceptionHandler')) {
113✔
78
                    $this->originalExceptionHandler = null;
113✔
79
                }
80

81
                if (property_exists($this, 'originalDeprecationHandler')) {
113✔
82
                    $this->originalDeprecationHandler = null;
113✔
83
                }
84
            });
113✔
85
        };
113✔
86

87
        /** @phpstan-ignore-next-line */
88
        if ($this instanceof PHPUnitTestCase && static::usesTestingConcern(WithPest::class)) {
113✔
89
            /** @phpstan-ignore-next-line */
NEW
90
            $this->tearDownTheEnvironmentUsingPest();
×
91
        }
92

93
        $this->testCaseTearDownCallback instanceof Closure
113✔
NEW
94
            ? \call_user_func(Closure::bind($this->testCaseTearDownCallback, $this), $tearDown)
×
95
            : \call_user_func($tearDown);
113✔
96

97
        $this->testCaseSetUpCallback = null;
113✔
98
        $this->testCaseTearDownCallback = null;
113✔
99
    }
100

101
    /**
102
     * Boot the testing helper traits.
103
     *
104
     * @internal
105
     *
106
     * @param  array<class-string, class-string>  $uses
107
     * @return array<class-string, class-string>
108
     */
109
    final protected function setUpTheTestEnvironmentTraits(array $uses): array
110
    {
111
        if (isset($uses[WithWorkbench::class])) {
113✔
112
            /** @phpstan-ignore-next-line */
113
            $this->setUpWithWorkbench();
16✔
114
        }
115

116
        $this->setUpDatabaseRequirements(function () use ($uses) {
113✔
117
            if (isset($uses[RefreshDatabase::class])) {
113✔
118
                /** @phpstan-ignore-next-line */
119
                $this->refreshDatabase();
3✔
120
            }
121

122
            if (isset($uses[DatabaseMigrations::class])) {
113✔
123
                /** @phpstan-ignore-next-line */
124
                $this->runDatabaseMigrations();
6✔
125
            }
126

127
            if (isset($uses[DatabaseTruncation::class])) {
113✔
128
                /** @phpstan-ignore-next-line */
129
                $this->truncateDatabaseTables();
×
130
            }
131
        });
113✔
132

133
        if (isset($uses[DatabaseTransactions::class])) {
113✔
134
            /** @phpstan-ignore-next-line */
135
            $this->beginDatabaseTransaction();
×
136
        }
137

138
        if (isset($uses[WithoutMiddleware::class])) {
113✔
139
            /** @phpstan-ignore-next-line */
140
            $this->disableMiddlewareForAllTests();
×
141
        }
142

143
        if (isset($uses[WithoutEvents::class])) {
113✔
144
            /** @phpstan-ignore-next-line */
145
            $this->disableEventsForAllTests();
×
146
        }
147

148
        if (isset($uses[WithFaker::class])) {
113✔
149
            /** @phpstan-ignore-next-line */
150
            $this->setUpFaker();
1✔
151
        }
152

153
        LazyCollection::make(static function () use ($uses) {
113✔
154
            foreach ($uses as $use) {
113✔
155
                yield $use;
113✔
156
            }
157
        })
113✔
158
            ->reject(function ($use) {
113✔
159
                /** @var class-string $use */
160
                return $this->setUpTheTestEnvironmentTraitToBeIgnored($use);
113✔
161
            })->map(static function ($use) {
113✔
162
                /** @var class-string $use */
163
                return class_basename($use);
113✔
164
            })->each(function ($traitBaseName) {
113✔
165
                /** @var string $traitBaseName */
166
                if (method_exists($this, $method = 'setUp'.$traitBaseName)) {
113✔
167
                    $this->{$method}();
18✔
168
                }
169

170
                if (method_exists($this, $method = 'tearDown'.$traitBaseName)) {
113✔
171
                    $this->beforeApplicationDestroyed(function () use ($method) {
14✔
172
                        $this->{$method}();
14✔
173
                    });
14✔
174
                }
175
            });
113✔
176

177
        return $uses;
113✔
178
    }
179

180
    /**
181
     * Determine trait should be ignored from being autoloaded.
182
     *
183
     * @param  class-string  $use
184
     * @return bool
185
     */
186
    protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
187
    {
188
        return false;
×
189
    }
190

191
    /**
192
     * Reload the application instance with cached routes.
193
     */
194
    protected function reloadApplication(): void
195
    {
196
        $this->tearDownTheTestEnvironment();
1✔
197
        $this->setUpTheTestEnvironment();
1✔
198
    }
199

200
    /**
201
     * Boot the testing helper traits.
202
     *
203
     * @return array<class-string, class-string>
204
     */
205
    abstract protected function setUpTraits();
206
}
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