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

orchestral / testbench-core / 11968150625

22 Nov 2024 07:13AM UTC coverage: 91.991% (+0.7%) from 91.321%
11968150625

Pull #265

github

crynobone
[7.x] Allow to configure `tty` via `Orchestra\Testbench\remote()`
function

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Pull Request #265: [7.x] Allow to configure `tty` via `Orchestra\Testbench\remote()`

3 of 4 new or added lines in 1 file covered. (75.0%)

93 existing lines in 25 files now uncovered.

1206 of 1311 relevant lines covered (91.99%)

61.99 hits per line

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

91.43
/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 $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 $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();
157✔
46
    }
47

48
    /**
49
     * Resolve PHPUnit method annotations.
50
     *
51
     * @phpunit-overrides
52
     *
53
     * @return \Illuminate\Support\Collection<string, mixed>
54
     */
55
    protected function resolvePhpUnitAnnotations(): Collection
56
    {
57
        $instance = new ReflectionClass($this);
152✔
58

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

63
        /** @var array<string, mixed> $annotations */
64
        $annotations = rescue(
151✔
65
            fn () => PHPUnit9Registry::getInstance()->forMethod($instance->getName(), $this->getName(false))->symbolAnnotations(),
151✔
66
            [],
151✔
67
            false
151✔
68
        );
151✔
69

70
        return Collection::make($annotations);
151✔
71
    }
72

73
    /**
74
     * Resolve PHPUnit method attributes.
75
     *
76
     * @phpunit-overrides
77
     *
78
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
79
     *
80
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
81
     */
82
    protected function resolvePhpUnitAttributes(): Collection
83
    {
84
        $instance = new ReflectionClass($this);
152✔
85

86
        if (! $this instanceof PHPUnitTestCase || $instance->isAnonymous()) {
152✔
87
            return new Collection; /** @phpstan-ignore return.type */
1✔
88
        }
89

90
        $className = $instance->getName();
151✔
91
        $methodName = $this->getName(false);
151✔
92

93
        return static::resolvePhpUnitAttributesForMethod($className, $methodName);
151✔
94
    }
95

96
    /**
97
     * Resolve PHPUnit method attributes for specific method.
98
     *
99
     * @phpunit-overrides
100
     *
101
     * @param  class-string  $className
102
     * @param  string|null  $methodName
103
     * @return \Illuminate\Support\Collection<class-string, array<int, object>>
104
     *
105
     * @phpstan-return \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>>
106
     */
107
    protected static function resolvePhpUnitAttributesForMethod(string $className, ?string $methodName = null): Collection
108
    {
109
        if (! isset(static::$cachedTestCaseClassAttributes[$className])) {
151✔
UNCOV
110
            static::$cachedTestCaseClassAttributes[$className] = rescue(static function () use ($className) {
×
UNCOV
111
                return AttributeParser::forClass($className);
×
UNCOV
112
            }, [], false);
×
113
        }
114

115
        if (! \is_null($methodName) && ! isset(static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"])) {
151✔
116
            static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] = rescue(static function () use ($className, $methodName) {
141✔
117
                return AttributeParser::forMethod($className, $methodName);
141✔
118
            }, [], false);
141✔
119
        }
120

121
        /** @var \Illuminate\Support\Collection<class-string<TTestingFeature>, array<int, TTestingFeature>> $attributes */
122
        $attributes = Collection::make(array_merge(
151✔
123
            static::$testCaseTestingFeatures,
151✔
124
            static::$cachedTestCaseClassAttributes[$className],
151✔
125
            ! \is_null($methodName) ? static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] : [],
151✔
126
        ))->groupBy('key')
151✔
127
            ->map(static function ($attributes) {
151✔
128
                /** @var \Illuminate\Support\Collection<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}> $attributes */
129
                return $attributes->map(static function ($attribute) {
68✔
130
                    /** @var array{key: class-string<TTestingFeature>, instance: TTestingFeature} $attribute */
131
                    return $attribute['instance'];
68✔
132
                });
68✔
133
            });
151✔
134

135
        return $attributes;
151✔
136
    }
137

138
    /**
139
     * Prepare the testing environment before the running the test case.
140
     *
141
     * @return void
142
     *
143
     * @codeCoverageIgnore
144
     */
145
    public static function setUpBeforeClassUsingPHPUnit(): void
146
    {
147
        static::cachedUsesForTestCase();
148
    }
149

150
    /**
151
     * Clean up the testing environment before the next test case.
152
     *
153
     * @return void
154
     *
155
     * @codeCoverageIgnore
156
     */
157
    public static function tearDownAfterClassUsingPHPUnit(): void
158
    {
159
        static::$cachedTestCaseUses = null;
160
        static::$cachedTestCaseClassAttributes = [];
161
        static::$cachedTestCaseMethodAttributes = [];
162

163
        (function () {
164
            $this->classDocBlocks = [];
165
            $this->methodDocBlocks = [];
166
        })->call(PHPUnit9Registry::getInstance());
167
    }
168
}
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