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

orchestral / testbench-core / 13655288030

04 Mar 2025 01:55PM UTC coverage: 93.05% (-0.05%) from 93.099%
13655288030

Pull #322

github

web-flow
Merge 6d8541bd7 into 85d5a3d50
Pull Request #322: Rename `resolvePhpUnitTestName()` to `resolvePhpUnitTestMethodName()`

14 of 15 new or added lines in 1 file covered. (93.33%)

4 existing lines in 1 file now uncovered.

1379 of 1482 relevant lines covered (93.05%)

62.9 hits per line

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

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

3
namespace Orchestra\Testbench\Concerns;
4

5
use Illuminate\Support\Collection;
6
use Orchestra\Testbench\PHPUnit\AttributeParser;
7
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
8
use PHPUnit\Util\Annotation\Registry as PHPUnit9Registry;
9
use ReflectionClass;
10

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

20
    /**
21
     * The cached class attributes for test case.
22
     *
23
     * @var array<string, array<int, array{key: class-string, instance: object}>>
24
     *
25
     * @phpstan-var array<string, array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>>
26
     */
27
    protected static array $cachedTestCaseClassAttributes = [];
28

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

38
    /**
39
     * Determine if the trait is used within testing.
40
     *
41
     * @return bool
42
     */
43
    public function isRunningTestCase(): bool
44
    {
45
        return $this instanceof PHPUnitTestCase || static::usesTestingConcern();
168✔
46
    }
47

48
    /**
49
     * Resolve PHPUnit test class name.
50
     *
51
     * @internal
52
     *
53
     * @return class-string|null
54
     */
55
    public function resolvePhpUnitTestClassName(): ?string
56
    {
57
        $instance = new ReflectionClass($this);
163✔
58

59
        if (! $this instanceof PHPUnitTestCase || $instance->isAnonymous()) {
163✔
60
            return null;
1✔
61
        }
62

63
        return $instance->getName();
162✔
64
    }
65

66
    /**
67
     * Resolve PHPUnit test method name.
68
     *
69
     * @internal
70
     *
71
     * @return string|null
72
     */
73
    public function resolvePhpUnitTestMethodName(): ?string
74
    {
75
        if (! $this instanceof PHPUnitTestCase) {
163✔
NEW
UNCOV
76
            return null;
×
77
        }
78

79
        return $this->getName(false);
163✔
80
    }
81

82
    /**
83
     * Resolve PHPUnit method annotations.
84
     *
85
     * @phpunit-overrides
86
     *
87
     * @return \Illuminate\Support\Collection<string, mixed>
88
     */
89
    protected function resolvePhpUnitAnnotations(): Collection
90
    {
91
        $className = $this->resolvePhpUnitTestClassName();
163✔
92
        $methodName = $this->resolvePhpUnitTestMethodName();
163✔
93

94
        if (\is_null($className) || \is_null($methodName)) {
163✔
95
            return new Collection;
1✔
96
        }
97

98
        /** @var array<string, mixed> $annotations */
99
        $annotations = rescue(
162✔
100
            fn () => PHPUnit9Registry::getInstance()->forMethod($className, $methodName)->symbolAnnotations(),
162✔
101
            [],
162✔
102
            false
162✔
103
        );
162✔
104

105
        return Collection::make($annotations);
162✔
106
    }
107

108
    /**
109
     * Resolve PHPUnit method attributes.
110
     *
111
     * @phpunit-overrides
112
     *
113
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
114
     *
115
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
116
     */
117
    protected function resolvePhpUnitAttributes(): Collection
118
    {
119
        $className = $this->resolvePhpUnitTestClassName();
163✔
120
        $methodName = $this->resolvePhpUnitTestMethodName();
163✔
121

122
        if (\is_null($className) || \is_null($methodName)) {
163✔
123
            return new Collection; /** @phpstan-ignore return.type */
1✔
124
        }
125

126
        return static::resolvePhpUnitAttributesForMethod($className, $methodName);
162✔
127
    }
128

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

148
        if (! \is_null($methodName) && ! isset(static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"])) {
162✔
149
            static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] = rescue(static function () use ($className, $methodName) {
147✔
150
                return AttributeParser::forMethod($className, $methodName);
147✔
151
            }, [], false);
147✔
152
        }
153

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

169
        return $attributes;
162✔
170
    }
171

172
    /**
173
     * Prepare the testing environment before the running the test case.
174
     *
175
     * @return void
176
     *
177
     * @codeCoverageIgnore
178
     */
179
    public static function setUpBeforeClassUsingPHPUnit(): void
180
    {
181
        static::cachedUsesForTestCase();
182
    }
183

184
    /**
185
     * Clean up the testing environment before the next test case.
186
     *
187
     * @return void
188
     *
189
     * @codeCoverageIgnore
190
     */
191
    public static function tearDownAfterClassUsingPHPUnit(): void
192
    {
193
        static::$cachedTestCaseUses = null;
194
        static::$cachedTestCaseClassAttributes = [];
195
        static::$cachedTestCaseMethodAttributes = [];
196

197
        (function () {
198
            $this->classDocBlocks = [];
199
            $this->methodDocBlocks = [];
200
        })->call(PHPUnit9Registry::getInstance());
201
    }
202
}
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