Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Better integrates Pest #1082

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Console/Concerns/InteractsWithTestingFrameworks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Laravel\Dusk\Console\Concerns;

trait InteractsWithTestingFrameworks
{
/**
* Determine if Pest is being used by the application.
*
* @return bool
*/
protected function usingPest()
{
return function_exists('\Pest\\version') && file_exists(base_path('tests').'/Pest.php');
}
}
9 changes: 5 additions & 4 deletions src/Console/DuskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

class DuskCommand extends Command
{
use Concerns\InteractsWithTestingFrameworks;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dusk
{--browse : Open a browser instead of using headless mode}
{--without-tty : Disable output to TTY}
{--pest : Run the tests using Pest}';
{--without-tty : Disable output to TTY}';

/**
* The console command description.
Expand Down Expand Up @@ -101,7 +102,7 @@ protected function binary()
{
$binaryPath = 'vendor/phpunit/phpunit/phpunit';

if ($this->option('pest')) {
if ($this->usingPest()) {
$binaryPath = 'vendor/pestphp/pest/bin/pest';
}

Expand Down Expand Up @@ -172,7 +173,7 @@ protected function env()
*/
protected function shouldUseCollisionPrinter()
{
return ! $this->option('pest')
return ! $this->usingPest()
&& class_exists(EnsurePrinterIsRegisteredSubscriber::class)
&& version_compare(Version::id(), '10.0', '>=');
}
Expand Down
22 changes: 21 additions & 1 deletion src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class InstallCommand extends Command
{
use Concerns\InteractsWithTestingFrameworks;

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -50,12 +52,30 @@ public function handle()
}

$stubs = [
'ExampleTest.stub' => base_path('tests/Browser/ExampleTest.php'),
'HomePage.stub' => base_path('tests/Browser/Pages/HomePage.php'),
'DuskTestCase.stub' => base_path('tests/DuskTestCase.php'),
'Page.stub' => base_path('tests/Browser/Pages/Page.php'),
];

if ($this->usingPest()) {
$stubs['ExampleTest.pest.stub'] = base_path('tests/Browser/ExampleTest.php');

$contents = file_get_contents(base_path('tests/Pest.php'));

$contents = str_replace('<?php', <<<EOT
<?php

uses(
Tests\DuskTestCase::class,
// Illuminate\Foundation\Testing\DatabaseMigrations::class,
)->in('Browser');
EOT, $contents);

file_put_contents(base_path('tests/Pest.php'), $contents);
} else {
$stubs['ExampleTest.stub'] = base_path('tests/Browser/ExampleTest.php');
}

foreach ($stubs as $stub => $file) {
if (! is_file($file)) {
copy(__DIR__.'/../../stubs/'.$stub, $file);
Expand Down
6 changes: 5 additions & 1 deletion src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class MakeCommand extends GeneratorCommand
{
use Concerns\InteractsWithTestingFrameworks;

/**
* The console command name.
*
Expand Down Expand Up @@ -35,7 +37,9 @@ class MakeCommand extends GeneratorCommand
*/
protected function getStub()
{
return __DIR__.'/stubs/test.stub';
return $this->usingPest()
? __DIR__.'/stubs/test.pest.stub'
: __DIR__.'/stubs/test.stub';
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Console/stubs/test.pest.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Laravel\Dusk\Browser;

test('example', function () {
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Laravel');
});
});
10 changes: 10 additions & 0 deletions stubs/ExampleTest.pest.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Laravel\Dusk\Browser;

test('basic example', function () {
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Laravel');
});
});
Loading