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

orchestral / testbench-core / 17228981544

26 Aug 2025 05:36AM UTC coverage: 92.525% (-0.05%) from 92.577%
17228981544

push

github

crynobone
fix tests

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

1510 of 1632 relevant lines covered (92.52%)

75.08 hits per line

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

94.44
/src/Concerns/InteractsWithTestCase.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Attribute;
6
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
7
use Illuminate\Foundation\Testing\RefreshDatabase;
8
use Illuminate\Support\Collection;
9
use Orchestra\Testbench\Contracts\Attributes\AfterAll as AfterAllContract;
10
use Orchestra\Testbench\Contracts\Attributes\AfterEach as AfterEachContract;
11
use Orchestra\Testbench\Contracts\Attributes\BeforeAll as BeforeAllContract;
12
use Orchestra\Testbench\Contracts\Attributes\BeforeEach as BeforeEachContract;
13
use Orchestra\Testbench\Contracts\Attributes\Resolvable as ResolvableContract;
14
use Orchestra\Testbench\PHPUnit\AttributeParser;
15

16
use function Orchestra\Testbench\laravel_or_fail;
17

18
/**
19
 * @phpstan-import-type TTestingFeature from \Orchestra\Testbench\PHPUnit\AttributeParser
20
 * @phpstan-import-type TAttributes from \Orchestra\Testbench\PHPUnit\AttributeParser
21
 */
22
trait InteractsWithTestCase
23
{
24
    /**
25
     * The cached application bootstrap file.
26
     *
27
     * @var string|null
28
     */
29
    protected static string|bool|null $cacheApplicationBootstrapFile = null;
30

31
    /**
32
     * The cached uses for test case.
33
     *
34
     * @var array<class-string, class-string>|null
35
     */
36
    protected static ?array $cachedTestCaseUses = null;
37

38
    /**
39
     * The method attributes for test case.
40
     *
41
     * @var array<int, array{key: class-string, instance: object}>
42
     *
43
     * @phpstan-var array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>
44
     */
45
    protected static array $testCaseTestingFeatures = [];
46

47
    /**
48
     * The method attributes for test case's method.
49
     *
50
     * @var array<int, array{key: class-string, instance: object}>
51
     *
52
     * @phpstan-var array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>
53
     */
54
    protected static array $testCaseMethodTestingFeatures = [];
55

56
    /**
57
     * Determine if the trait is using given trait (or default to \Orchestra\Testbench\Concerns\Testing trait).
58
     *
59
     * @api
60
     *
61
     * @param  class-string|null  $trait
62
     * @return bool
63
     */
64
    public static function usesTestingConcern(?string $trait = null): bool
65
    {
66
        return isset(static::cachedUsesForTestCase()[$trait ?? Testing::class]);
192✔
67
    }
68

69
    /**
70
     * Determine if the trait is using \Illuminate\Foundation\Testing\LazilyRefreshDatabase or \Illuminate\Foundation\Testing\RefreshDatabase trait.
71
     *
72
     * @return bool
73
     */
74
    public static function usesRefreshDatabaseTestingConcern(): bool
75
    {
76
        return static::usesTestingConcern(LazilyRefreshDatabase::class) || static::usesTestingConcern(RefreshDatabase::class);
13✔
77
    }
78

79
    /**
80
     * Define or get the cached uses for test case.
81
     *
82
     * @internal
83
     *
84
     * @return array<class-string, class-string>
85
     */
86
    public static function cachedUsesForTestCase(): array
87
    {
88
        if (\is_null(static::$cachedTestCaseUses)) {
192✔
89
            /** @var array<class-string, class-string> $uses */
90
            $uses = array_flip(class_uses_recursive(static::class));
3✔
91

92
            static::$cachedTestCaseUses = $uses;
3✔
93
        }
94

95
        return static::$cachedTestCaseUses;
192✔
96
    }
97

98
    /**
99
     * Uses testing feature (attribute) on the current test.
100
     *
101
     * @api
102
     *
103
     * @param  object  $attribute
104
     * @param  int  $flag
105
     *
106
     * @phpstan-param TAttributes $attribute
107
     *
108
     * @return void
109
     */
110
    public static function usesTestingFeature($attribute, int $flag = Attribute::TARGET_CLASS): void
111
    {
112
        if (! AttributeParser::validAttribute($attribute)) {
93✔
113
            return;
×
114
        }
115

116
        $attribute = $attribute instanceof ResolvableContract ? $attribute->resolve() : $attribute;
93✔
117

118
        if (\is_null($attribute)) {
93✔
119
            return;
×
120
        }
121

122
        if ($flag & Attribute::TARGET_CLASS) {
93✔
123
            static::$testCaseTestingFeatures[] = [
89✔
124
                'key' => $attribute::class,
89✔
125
                'instance' => $attribute,
89✔
126
            ];
89✔
127
        } elseif ($flag & Attribute::TARGET_METHOD) {
4✔
128
            static::$testCaseMethodTestingFeatures[] = [
4✔
129
                'key' => $attribute::class,
4✔
130
                'instance' => $attribute,
4✔
131
            ];
4✔
132
        }
133
    }
134

135
    /**
136
     * Resolve PHPUnit method attributes for specific method.
137
     *
138
     * @phpunit-overrides
139
     *
140
     * @param  class-string  $className
141
     * @param  string|null  $methodName
142
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
143
     *
144
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
145
     */
146
    abstract protected static function resolvePhpUnitAttributesForMethod(string $className, ?string $methodName = null): Collection;
147

148
    /**
149
     * Prepare the testing environment before the running the test case.
150
     *
151
     * @internal
152
     *
153
     * @return void
154
     */
155
    protected function setUpTheTestEnvironmentUsingTestCase(): void
156
    {
157
        $app = laravel_or_fail($this->app);
186✔
158

159
        $this->resolvePhpUnitAttributes()
186✔
160
            ->flatten()
186✔
161
            ->filter(static fn ($instance) => $instance instanceof BeforeEachContract)
186✔
162
            ->map(static function ($instance) use ($app) {
186✔
163
                $instance->beforeEach($app);
12✔
164
            });
186✔
165
    }
166

167
    /**
168
     * Prepare the testing environment before the running the test case.
169
     *
170
     * @internal
171
     *
172
     * @return void
173
     */
174
    protected function tearDownTheTestEnvironmentUsingTestCase(): void
175
    {
176
        $app = laravel_or_fail($this->app);
186✔
177

178
        $this->resolvePhpUnitAttributes()
186✔
179
            ->flatten()
186✔
180
            ->filter(static fn ($instance) => $instance instanceof AfterEachContract)
186✔
181
            ->map(static function ($instance) use ($app) {
186✔
182
                $instance->afterEach($app);
13✔
183
            });
186✔
184

185
        static::$testCaseMethodTestingFeatures = [];
186✔
186
    }
187

188
    /**
189
     * Prepare the testing environment before the running the test case.
190
     *
191
     * @internal
192
     *
193
     * @return void
194
     *
195
     * @codeCoverageIgnore
196
     */
197
    public static function setUpBeforeClassUsingTestCase(): void
198
    {
199
        static::resolvePhpUnitAttributesForMethod(static::class)
200
            ->flatten()
201
            ->filter(static fn ($instance) => $instance instanceof BeforeAllContract)
202
            ->map(static function ($instance) {
203
                $instance->beforeAll();
204
            });
205
    }
206

207
    /**
208
     * Clean up the testing environment before the next test case.
209
     *
210
     * @internal
211
     *
212
     * @return void
213
     *
214
     * @codeCoverageIgnore
215
     */
216
    public static function tearDownAfterClassUsingTestCase(): void
217
    {
218
        static::resolvePhpUnitAttributesForMethod(static::class)
219
            ->flatten()
220
            ->filter(static fn ($instance) => $instance instanceof AfterAllContract)
221
            ->map(static function ($instance) {
222
                $instance->afterAll();
223
            });
224

225
        static::$testCaseTestingFeatures = [];
226
        static::$cacheApplicationBootstrapFile = null;
227
    }
228
}
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