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

orchestral / testbench-core / 13641823135

03 Mar 2025 10:34PM UTC coverage: 93.108% (+0.08%) from 93.033%
13641823135

push

github

web-flow
Use `UsesVendor` attribute on `defineCachedRoutes()` tests (#319)

* Use `UsesVendor` attribute on `defineCachedRoutes()` tests

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

* wip

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

---------

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

17 of 17 new or added lines in 3 files covered. (100.0%)

3 existing lines in 1 file now uncovered.

1378 of 1480 relevant lines covered (93.11%)

62.8 hits per line

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

92.31
/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 name.
50
     *
51
     * @internal
52
     *
53
     * @return string|null
54
     */
55
    public function resolvePhpUnitTestName(): ?string
56
    {
57
        return $this instanceof PHPUnitTestCase
163✔
58
            ? $this->getName(false)
163✔
59
            : null;
163✔
60
    }
61

62
    /**
63
     * Resolve PHPUnit method annotations.
64
     *
65
     * @phpunit-overrides
66
     *
67
     * @return \Illuminate\Support\Collection<string, mixed>
68
     */
69
    protected function resolvePhpUnitAnnotations(): Collection
70
    {
71
        $instance = new ReflectionClass($this);
163✔
72
        $methodName = $this->resolvePhpUnitTestName();
163✔
73

74
        if (! $this instanceof PHPUnitTestCase || $instance->isAnonymous() || \is_null($methodName)) {
163✔
75
            return new Collection;
1✔
76
        }
77

78
        /** @var array<string, mixed> $annotations */
79
        $annotations = rescue(
162✔
80
            fn () => PHPUnit9Registry::getInstance()->forMethod($instance->getName(), $methodName)->symbolAnnotations(),
162✔
81
            [],
162✔
82
            false
162✔
83
        );
162✔
84

85
        return Collection::make($annotations);
162✔
86
    }
87

88
    /**
89
     * Resolve PHPUnit method attributes.
90
     *
91
     * @phpunit-overrides
92
     *
93
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
94
     *
95
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
96
     */
97
    protected function resolvePhpUnitAttributes(): Collection
98
    {
99
        $instance = new ReflectionClass($this);
163✔
100
        $methodName = $this->resolvePhpUnitTestName();
163✔
101

102
        if (! $this instanceof PHPUnitTestCase || $instance->isAnonymous() || \is_null($methodName)) {
163✔
103
            return new Collection; /** @phpstan-ignore return.type */
1✔
104
        }
105

106
        return static::resolvePhpUnitAttributesForMethod($instance->getName(), $methodName);
162✔
107
    }
108

109
    /**
110
     * Resolve PHPUnit method attributes for specific method.
111
     *
112
     * @phpunit-overrides
113
     *
114
     * @param  class-string  $className
115
     * @param  string|null  $methodName
116
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
117
     *
118
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
119
     */
120
    protected static function resolvePhpUnitAttributesForMethod(string $className, ?string $methodName = null): Collection
121
    {
122
        if (! isset(static::$cachedTestCaseClassAttributes[$className])) {
162✔
UNCOV
123
            static::$cachedTestCaseClassAttributes[$className] = rescue(static function () use ($className) {
×
UNCOV
124
                return AttributeParser::forClass($className);
×
UNCOV
125
            }, [], false);
×
126
        }
127

128
        if (! \is_null($methodName) && ! isset(static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"])) {
162✔
129
            static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] = rescue(static function () use ($className, $methodName) {
147✔
130
                return AttributeParser::forMethod($className, $methodName);
147✔
131
            }, [], false);
147✔
132
        }
133

134
        /** @var \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>> $attributes */
135
        $attributes = Collection::make(array_merge(
162✔
136
            static::$testCaseTestingFeatures,
162✔
137
            static::$cachedTestCaseClassAttributes[$className],
162✔
138
            static::$testCaseMethodTestingFeatures,
162✔
139
            ! \is_null($methodName) ? static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] : [],
162✔
140
        ))->groupBy('key')
162✔
141
            ->map(static function ($attributes) {
162✔
142
                /** @var \Illuminate\Support\Collection<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}> $attributes */
143
                return $attributes->map(static function ($attribute) {
74✔
144
                    /** @var array{key: class-string<TTestingFeature>, instance: TTestingFeature} $attribute */
145
                    return $attribute['instance'];
74✔
146
                });
74✔
147
            });
162✔
148

149
        return $attributes;
162✔
150
    }
151

152
    /**
153
     * Prepare the testing environment before the running the test case.
154
     *
155
     * @return void
156
     *
157
     * @codeCoverageIgnore
158
     */
159
    public static function setUpBeforeClassUsingPHPUnit(): void
160
    {
161
        static::cachedUsesForTestCase();
162
    }
163

164
    /**
165
     * Clean up the testing environment before the next test case.
166
     *
167
     * @return void
168
     *
169
     * @codeCoverageIgnore
170
     */
171
    public static function tearDownAfterClassUsingPHPUnit(): void
172
    {
173
        static::$cachedTestCaseUses = null;
174
        static::$cachedTestCaseClassAttributes = [];
175
        static::$cachedTestCaseMethodAttributes = [];
176

177
        (function () {
178
            $this->classDocBlocks = [];
179
            $this->methodDocBlocks = [];
180
        })->call(PHPUnit9Registry::getInstance());
181
    }
182
}
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