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

orchestral / testbench-core / 11968182539

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

Pull #265

github

crynobone
wip

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

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

88.24
/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\Exceptions\ApplicationNotAvailableException;
14
use Orchestra\Testbench\PHPUnit\AttributeParser;
15

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

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

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

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

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

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

75
        return static::$cachedTestCaseUses;
157✔
76
    }
77

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

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

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

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

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

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

120
    /**
121
     * Prepare the testing environment before the running the test case.
122
     *
123
     * @return void
124
     */
125
    protected function setUpTheTestEnvironmentUsingTestCase(): void
126
    {
127
        /** @phpstan-ignore-next-line */
128
        if (\is_null($app = $this->app)) {
151✔
UNCOV
129
            throw ApplicationNotAvailableException::make(__METHOD__);
×
130
        }
131

132
        $this->resolvePhpUnitAttributes()
151✔
133
            ->flatten()
151✔
134
            ->filter(static function ($instance) {
151✔
135
                return $instance instanceof BeforeEachContract;
65✔
136
            })->map(function ($instance) use ($app) {
151✔
137
                $instance->beforeEach($app);
4✔
138
            });
151✔
139
    }
140

141
    /**
142
     * Prepare the testing environment before the running the test case.
143
     *
144
     * @return void
145
     */
146
    protected function tearDownTheTestEnvironmentUsingTestCase(): void
147
    {
148
        /** @phpstan-ignore-next-line */
149
        if (\is_null($app = $this->app)) {
151✔
UNCOV
150
            throw ApplicationNotAvailableException::make(__METHOD__);
×
151
        }
152

153
        $this->resolvePhpUnitAttributes()
151✔
154
            ->flatten()
151✔
155
            ->filter(static function ($instance) {
151✔
156
                return $instance instanceof AfterEachContract;
68✔
157
            })->map(static function ($instance) use ($app) {
151✔
158
                $instance->afterEach($app);
4✔
159
            });
151✔
160
    }
161

162
    /**
163
     * Prepare the testing environment before the running the test case.
164
     *
165
     * @return void
166
     *
167
     * @codeCoverageIgnore
168
     */
169
    public static function setUpBeforeClassUsingTestCase(): void
170
    {
171
        static::resolvePhpUnitAttributesForMethod(static::class)
172
            ->flatten()
173
            ->filter(static function ($instance) {
174
                return $instance instanceof BeforeAllContract;
175
            })->map(static function ($instance) {
176
                $instance->beforeAll();
177
            });
178
    }
179

180
    /**
181
     * Clean up the testing environment before the next test case.
182
     *
183
     * @return void
184
     *
185
     * @codeCoverageIgnore
186
     */
187
    public static function tearDownAfterClassUsingTestCase(): void
188
    {
189
        static::resolvePhpUnitAttributesForMethod(static::class)
190
            ->flatten()
191
            ->filter(static function ($instance) {
192
                return $instance instanceof AfterAllContract;
193
            })->map(static function ($instance) {
194
                $instance->afterAll();
195
            });
196

197
        /** @phpstan-ignore-next-line */
198
        static::$latestResponse = null;
199
        static::$testCaseTestingFeatures = [];
200
    }
201
}
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