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

orchestral / testbench-core / 12324437332

13 Dec 2024 11:13PM UTC coverage: 91.777% (-0.5%) from 92.296%
12324437332

Pull #278

github

web-flow
Merge 82ecf708a into b1b7dd073
Pull Request #278: [7.x] Fixes database seeding using `WithWorkbench` and `RefreshDatabase`

6 of 14 new or added lines in 1 file covered. (42.86%)

25 existing lines in 8 files now uncovered.

1250 of 1362 relevant lines covered (91.78%)

64.18 hits per line

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

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

3
namespace Orchestra\Testbench\Concerns;
4

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

15
use function Orchestra\Testbench\laravel_or_fail;
16

17
/**
18
 * @internal
19
 *
20
 * @phpstan-import-type TTestingFeature from \Orchestra\Testbench\PHPUnit\AttributeParser
21
 * @phpstan-import-type TAttributes from \Orchestra\Testbench\PHPUnit\AttributeParser
22
 */
23
trait InteractsWithTestCase
24
{
25
    /**
26
     * The cached uses for test case.
27
     *
28
     * @var array<class-string, class-string>|null
29
     */
30
    protected static $cachedTestCaseUses;
31

32
    /**
33
     * The method attributes for test case.
34
     *
35
     * @var array<int, array{key: class-string, instance: object}>
36
     *
37
     * @phpstan-var array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>
38
     */
39
    protected static $testCaseTestingFeatures = [];
40

41
    /**
42
     * Determine if the trait is using given trait (or default to \Orchestra\Testbench\Concerns\Testing trait).
43
     *
44
     * @param  class-string|null  $trait
45
     * @return bool
46
     */
47
    public static function usesTestingConcern(?string $trait = null): bool
48
    {
49
        return isset(static::cachedUsesForTestCase()[$trait ?? Testing::class]);
164✔
50
    }
51

52
    /**
53
     * Determine if the trait is using \Illuminate\Foundation\Testing\LazilyRefreshDatabase or \Illuminate\Foundation\Testing\RefreshDatabase trait.
54
     *
55
     * @return bool
56
     */
57
    public static function usesRefreshDatabaseTestingConcern(): bool
58
    {
59
        return static::usesTestingConcern(LazilyRefreshDatabase::class) || static::usesTestingConcern(RefreshDatabase::class);
12✔
60
    }
61

62
    /**
63
     * Define or get the cached uses for test case.
64
     *
65
     * @return array<class-string, class-string>
66
     */
67
    public static function cachedUsesForTestCase(): array
68
    {
69
        if (\is_null(static::$cachedTestCaseUses)) {
164✔
70
            /** @var array<class-string, class-string> $uses */
71
            $uses = array_flip(class_uses_recursive(static::class));
3✔
72

73
            static::$cachedTestCaseUses = $uses;
3✔
74
        }
75

76
        return static::$cachedTestCaseUses;
164✔
77
    }
78

79
    /**
80
     * Uses testing feature (attribute) on the current test.
81
     *
82
     * @param  object  $attribute
83
     * @return void
84
     *
85
     * @phpstan-param TAttributes $attribute
86
     */
87
    public static function usesTestingFeature($attribute): void
88
    {
89
        if (! AttributeParser::validAttribute($attribute)) {
32✔
UNCOV
90
            return;
×
91
        }
92

93
        $attribute = $attribute instanceof ResolvableContract ? $attribute->resolve() : $attribute;
32✔
94

95
        if (\is_null($attribute)) {
32✔
UNCOV
96
            return;
×
97
        }
98

99
        /** @var class-string<TTestingFeature> $name */
100
        $name = \get_class($attribute);
32✔
101

102
        static::$testCaseTestingFeatures[] = [
32✔
103
            'key' => $name,
32✔
104
            'instance' => $attribute,
32✔
105
        ];
32✔
106
    }
107

108
    /**
109
     * Resolve PHPUnit method attributes for specific method.
110
     *
111
     * @phpunit-overrides
112
     *
113
     * @param  class-string  $className
114
     * @param  string|null  $methodName
115
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
116
     *
117
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
118
     */
119
    abstract protected static function resolvePhpUnitAttributesForMethod(string $className, ?string $methodName = null): Collection;
120

121
    /**
122
     * Prepare the testing environment before the running the test case.
123
     *
124
     * @return void
125
     */
126
    protected function setUpTheTestEnvironmentUsingTestCase(): void
127
    {
128
        $app = laravel_or_fail($this->app);
158✔
129

130
        $this->resolvePhpUnitAttributes()
158✔
131
            ->flatten()
158✔
132
            ->filter(static fn ($instance) => $instance instanceof BeforeEachContract)
158✔
133
            ->map(static function ($instance) use ($app) {
158✔
134
                $instance->beforeEach($app);
4✔
135
            });
158✔
136
    }
137

138
    /**
139
     * Prepare the testing environment before the running the test case.
140
     *
141
     * @return void
142
     */
143
    protected function tearDownTheTestEnvironmentUsingTestCase(): void
144
    {
145
        $app = laravel_or_fail($this->app);
158✔
146

147
        $this->resolvePhpUnitAttributes()
158✔
148
            ->flatten()
158✔
149
            ->filter(static fn ($instance) => $instance instanceof AfterEachContract)
158✔
150
            ->map(static function ($instance) use ($app) {
158✔
151
                $instance->afterEach($app);
4✔
152
            });
158✔
153
    }
154

155
    /**
156
     * Prepare the testing environment before the running the test case.
157
     *
158
     * @return void
159
     *
160
     * @codeCoverageIgnore
161
     */
162
    public static function setUpBeforeClassUsingTestCase(): void
163
    {
164
        static::resolvePhpUnitAttributesForMethod(static::class)
165
            ->flatten()
166
            ->filter(static fn ($instance) => $instance instanceof BeforeAllContract)
167
            ->map(static function ($instance) {
168
                $instance->beforeAll();
169
            });
170
    }
171

172
    /**
173
     * Clean up the testing environment before the next test case.
174
     *
175
     * @return void
176
     *
177
     * @codeCoverageIgnore
178
     */
179
    public static function tearDownAfterClassUsingTestCase(): void
180
    {
181
        static::resolvePhpUnitAttributesForMethod(static::class)
182
            ->flatten()
183
            ->filter(static fn ($instance) => $instance instanceof AfterAllContract)
184
            ->map(static function ($instance) {
185
                $instance->afterAll();
186
            });
187

188
        /** @phpstan-ignore-next-line */
189
        static::$latestResponse = null;
190
        static::$testCaseTestingFeatures = [];
191
    }
192
}
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

© 2025 Coveralls, Inc