Skip to content

Commit 715a125

Browse files
committed
fix
1 parent 068ec7e commit 715a125

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"rector --dry-run",
7979
"pint --test"
8080
],
81-
"test:unit": "pest --parallel --coverage --exactly=83.6",
81+
"test:unit": "pest --parallel --coverage --exactly=77.0",
8282
"test:types": "phpstan",
8383
"test": [
8484
"@test:lint",

playground/tests/Feature/ExampleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
declare(strict_types=1);
44

5+
use Illuminate\Foundation\Testing\RefreshDatabase;
56
use Tests\TestCase;
67

7-
pest()->uses(TestCase::class);
8+
pest()
9+
->uses(TestCase::class, RefreshDatabase::class);
810

911
it('returns a successful response', function () {
1012
$page = page()->goto(route('database'));

src/Autoload.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
declare(strict_types=1);
44

55
use Pest\Browser\Browser;
6-
use Pest\Browser\Playwright\Client;
76
use Pest\Browser\Playwright\Page;
87
use Pest\Browser\Playwright\Playwright;
9-
use Pest\Browser\ServerManager;
108
use Pest\Plugin;
119

1210
Plugin::uses(Browser::class);
@@ -20,12 +18,6 @@
2018
*/
2119
function page(?string $url = null, array $options = []): Page
2220
{
23-
ServerManager::instance()->http()->start();
24-
25-
Client::instance()->connectTo(
26-
ServerManager::instance()->playwright()->url().'?browser=chromium',
27-
);
28-
2921
$browser = Playwright::chromium()->launch();
3022
$page = $browser->newContext($options)->newPage();
3123

src/Plugin.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Pest\Browser;
66

7+
use Pest\Browser\Playwright\Client;
78
use Pest\Browser\Support\Screenshot;
89
use Pest\Contracts\Plugins\Bootable;
910
use Pest\Contracts\Plugins\Terminable;
@@ -12,29 +13,64 @@
1213
/**
1314
* @internal
1415
*/
15-
final readonly class Plugin implements Bootable, Terminable // @pest-arch-ignore-line
16+
final class Plugin implements Bootable, Terminable // @pest-arch-ignore-line
1617
{
18+
/**
19+
* Indicates whether the plugin is used in the current test suite.
20+
*/
21+
private bool $usesPlugin = false;
22+
1723
/**
1824
* Boots the plugin.
1925
*/
2026
public function boot(): void
2127
{
28+
if (($this->usesPlugin = $this->usesPlugin()) === false) {
29+
return;
30+
}
31+
2232
if (Parallel::isWorker() === false) {
2333
ServerManager::instance()->playwright()->start();
2434

2535
Screenshot::cleanup();
2636
}
37+
38+
Client::instance()->connectTo(
39+
ServerManager::instance()->playwright()->url().'?browser=chromium',
40+
);
41+
42+
if (Parallel::isEnabled() === false || Parallel::isWorker()) {
43+
$http = ServerManager::instance()->http();
44+
45+
$http->start();
46+
47+
$_ENV['APP_URL'] = "http://{$http->url()}";
48+
}
2749
}
2850

2951
/**
3052
* Terminates the plugin.
3153
*/
3254
public function terminate(): void
3355
{
56+
if ($this->usesPlugin === false) {
57+
return;
58+
}
59+
3460
if (Parallel::isWorker() === false) {
3561
ServerManager::instance()->playwright()->stop();
3662
}
3763

3864
ServerManager::instance()->http()->stop();
3965
}
66+
67+
/**
68+
* Checks if the plugin is used in the current test suite.
69+
*/
70+
private function usesPlugin(): bool
71+
{
72+
// check if any of the tests uses the page() function...
73+
74+
return true;
75+
}
4076
}

0 commit comments

Comments
 (0)