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

orchestral / testbench-core / 13198403924

07 Feb 2025 10:55AM UTC coverage: 91.657% (-0.4%) from 92.08%
13198403924

push

github

crynobone
wip

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

1516 of 1654 relevant lines covered (91.66%)

73.7 hits per line

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

73.68
/src/Concerns/InteractsWithPHPUnit.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Closure;
6
use Illuminate\Support\Collection;
7
use Orchestra\Testbench\PHPUnit\AttributeParser;
8
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
9
use PHPUnit\Metadata\Annotation\Parser\Registry as PHPUnitRegistry;
10
use ReflectionClass;
11

12
/**
13
 * @internal
14
 *
15
 * @phpstan-import-type TTestingFeature from \Orchestra\Testbench\PHPUnit\AttributeParser
16
 */
17
trait InteractsWithPHPUnit
18
{
19
    use InteractsWithTestCase;
20

21
    /**
22
     * The cached test case setUp resolver.
23
     *
24
     * @var (\Closure(\Closure):(void))|null
25
     */
26
    protected ?Closure $testCaseSetUpCallback = null;
27

28
    /**
29
     * The cached test case tearDown resolver.
30
     *
31
     * @var (\Closure(\Closure):(void))|null
32
     */
33
    protected ?Closure $testCaseTearDownCallback = null;
34

35
    /**
36
     * The cached class attributes for test case.
37
     *
38
     * @var array<string, array<int, array{key: class-string, instance: object}>>
39
     *
40
     * @phpstan-var array<string, array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>>
41
     */
42
    protected static array $cachedTestCaseClassAttributes = [];
43

44
    /**
45
     * The cached method attributes for test case.
46
     *
47
     * @var array<string, array<int, array{key: class-string, instance: object}>>
48
     *
49
     * @phpstan-var array<string, array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>>
50
     */
51
    protected static array $cachedTestCaseMethodAttributes = [];
52

53
    /**
54
     * Determine if the trait is used within testing.
55
     *
56
     * @api
57
     *
58
     * @return bool
59
     */
60
    public function isRunningTestCase(): bool
61
    {
62
        return $this instanceof PHPUnitTestCase || static::usesTestingConcern();
192✔
63
    }
64

65
    /**
66
     * Resolve PHPUnit method annotations.
67
     *
68
     * @internal
69
     *
70
     * @phpunit-overrides
71
     *
72
     * @return \Illuminate\Support\Collection<string, mixed>
73
     *
74
     * @deprecated
75
     */
76
    protected function resolvePhpUnitAnnotations(): Collection
77
    {
78
        $instance = new ReflectionClass($this);
187✔
79

80
        if (
81
            ! $this instanceof PHPUnitTestCase
187✔
82
            || $instance->isAnonymous()
187✔
83
            || ! class_exists(PHPUnitRegistry::class)
187✔
84
        ) {
85
            return new Collection;
187✔
86
        }
87

88
        [$registry, $methodName] = [PHPUnitRegistry::getInstance(), $this->name()];
×
89

90
        /** @var array<string, mixed> $annotations */
91
        $annotations = rescue(
×
92
            fn () => $registry->forMethod($instance->getName(), $methodName)->symbolAnnotations(),
×
93
            [],
×
94
            false
×
95
        );
×
96

97
        return Collection::make($annotations);
×
98
    }
99

100
    /**
101
     * Resolve PHPUnit method attributes.
102
     *
103
     * @internal
104
     *
105
     * @phpunit-overrides
106
     *
107
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
108
     *
109
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
110
     */
111
    protected function resolvePhpUnitAttributes(): Collection
112
    {
113
        $instance = new ReflectionClass($this);
187✔
114

115
        if (! $this instanceof PHPUnitTestCase || $instance->isAnonymous()) {
187✔
116
            return new Collection; /** @phpstan-ignore return.type */
1✔
117
        }
118

119
        $className = $instance->getName();
186✔
120
        $methodName = $this->name();
186✔
121

122
        return static::resolvePhpUnitAttributesForMethod($className, $methodName);
186✔
123
    }
124

125
    /**
126
     * Resolve PHPUnit method attributes for specific method.
127
     *
128
     * @internal
129
     *
130
     * @phpunit-overrides
131
     *
132
     * @param  class-string  $className
133
     * @param  string|null  $methodName
134
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
135
     *
136
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
137
     */
138
    protected static function resolvePhpUnitAttributesForMethod(string $className, ?string $methodName = null): Collection
139
    {
140
        if (! isset(static::$cachedTestCaseClassAttributes[$className])) {
186✔
141
            static::$cachedTestCaseClassAttributes[$className] = rescue(
×
142
                static fn () => AttributeParser::forClass($className), [], false
×
143
            );
×
144
        }
145

146
        if (! \is_null($methodName) && ! isset(static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"])) {
186✔
147
            static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] = rescue(
171✔
148
                static fn () => AttributeParser::forMethod($className, $methodName), [], false
171✔
149
            );
171✔
150
        }
151

152
        /** @var \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>> $attributes */
153
        $attributes = Collection::make(array_merge(
186✔
154
            static::$testCaseTestingFeatures,
186✔
155
            static::$cachedTestCaseClassAttributes[$className],
186✔
156
            ! \is_null($methodName) ? static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] : [],
186✔
157
        ))->groupBy('key')
186✔
158
            ->map(static function ($attributes) {
186✔
159
                /** @var \Illuminate\Support\Collection<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}> $attributes */
160
                return $attributes->map(static function ($attribute) {
136✔
161
                    /** @var array{key: class-string<TTestingFeature>, instance: TTestingFeature} $attribute */
162
                    return $attribute['instance'];
136✔
163
                });
136✔
164
            });
186✔
165

166
        return $attributes;
186✔
167
    }
168

169
    /**
170
     * Define the setUp environment using callback.
171
     *
172
     * @internal
173
     *
174
     * @param  \Closure(\Closure):void  $setUp
175
     * @return void
176
     *
177
     * @codeCoverageIgnore
178
     */
179
    public function setUpTheEnvironmentUsing(Closure $setUp): void
180
    {
181
        $this->testCaseSetUpCallback = $setUp;
182
    }
183

184
    /**
185
     * Define the tearDown environment using callback.
186
     *
187
     * @internal
188
     *
189
     * @param  \Closure(\Closure):void  $tearDown
190
     * @return void
191
     *
192
     * @codeCoverageIgnore
193
     */
194
    public function tearDownTheEnvironmentUsing(Closure $tearDown): void
195
    {
196
        $this->testCaseTearDownCallback = $tearDown;
197
    }
198

199
    /**
200
     * Prepare the testing environment before the running the test case.
201
     *
202
     * @internal
203
     *
204
     * @return void
205
     *
206
     * @codeCoverageIgnore
207
     */
208
    public static function setUpBeforeClassUsingPHPUnit(): void
209
    {
210
        static::cachedUsesForTestCase();
211
    }
212

213
    /**
214
     * Clean up the testing environment before the next test case.
215
     *
216
     * @internal
217
     *
218
     * @return void
219
     *
220
     * @codeCoverageIgnore
221
     */
222
    public static function tearDownAfterClassUsingPHPUnit(): void
223
    {
224
        static::$cachedTestCaseUses = null;
225
        static::$cachedTestCaseClassAttributes = [];
226
        static::$cachedTestCaseMethodAttributes = [];
227

228
        if (class_exists(PHPUnitRegistry::class)) {
229
            $registry = PHPUnitRegistry::getInstance();
230

231
            (function () {
232
                $this->classDocBlocks = [];
233
                $this->methodDocBlocks = [];
234
            })->call($registry);
235
        }
236
    }
237
}
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