Skip to content

Commit

Permalink
Merge branch 'develop' into 9/php84
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone authored Oct 7, 2024
2 parents 78d9316 + ac33e8f commit cca4a5d
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 227 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/analyse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ jobs:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--prefer-dist --no-cache"

- name: Execute Code Style Analysis
run: vendor/bin/pint --test

- name: Execute Static Code Analysis
run: vendor/bin/phpstan analyse
run: vendor/bin/phpstan analyse --verbose

lint:
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
RAY_ENABLED: false

- name: Upload Failed Screenshots
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure()
with:
name: screenshots
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG-8.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

This changelog references the relevant changes (bug and security fixes) done to `orchestra/testbench-dusk`.

## 8.30.0

Released: 2024-09-25

### Changes

* Update minimum support for Testbench v8.27.0+. ([v8.26.0...v8.27.0](https://github.com/orchestral/testbench/compare/v8.26.0...v8.27.0))
* Update minimum support for Testbench Core v8.28.1+. ([v8.27.0...v8.28.1](https://github.com/orchestral/testbench/compare/v8.27.0...v8.28.1))
* Change `resolveApplicationResolvingCallback()` method visibility from `private` to `protected`.
* Update `Orchestra\Testbench\default_skeleton_path()` to accept `array`.

### Removed

* Removed `Orchestra\Testbench\Dusk\find_test_directory()` function.

## 8.29.0

Released: 2024-08-26
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG-9.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

This changelog references the relevant changes (bug and security fixes) done to `orchestra/testbench-dusk`.

## 9.8.0

Released: 2024-09-25

### Changes

* Update minimum support for Testbench v9.5.0+. ([v9.4.0...v9.5.0](https://github.com/orchestral/testbench/compare/v9.4.0...v9.5.0))
* Update minimum support for Testbench Core v9.5.1+. ([v9.4.0...v9.5.1](https://github.com/orchestral/testbench-core/compare/v9.4.0...v9.5.1))
* Update `Orchestra\Testbench\default_skeleton_path()` to accept `array`.

### Removed

* Removed `Orchestra\Testbench\Dusk\find_test_directory()` function.

## 9.7.1

Released: 2024-09-12

### Changes

* Add `concurrency.php` configuration based on Laravel Framework 11.23.

## 9.7.0

Released: 2024-08-26
Expand Down
189 changes: 8 additions & 181 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,191 +11,18 @@ The package was developed by [Konsulting Ltd](https://github.com/konsulting) and
[![Latest Unstable Version](https://poser.pugx.org/orchestra/testbench-dusk/v/unstable)](https://packagist.org/packages/orchestra/testbench-dusk)
[![License](https://poser.pugx.org/orchestra/testbench-dusk/license)](https://packagist.org/packages/orchestra/testbench-dusk)

* [Version Compatibility](#version-compatibility)
* [Getting Started](#getting-started)
* [Installation](#installation)
* [Usage](#usage)
* [Advanced Usage](#advanced-usage)
* [Troubleshooting](#troubleshooting)
* [Changelog](https://github.com/orchestral/testbench-dusk/releases)
## Official Documentation

## Version Compatibility
Documentation for Testbench Dusk can be found on the [packages.tools/testbench-dusk](https://packages.tools/testbench-dusk.html).

Laravel | Testbench Dusk
:---------|:----------
6.x | 4.x
7.x | 5.x
8.x | 6.x
9.x | 7.x
10.x | 8.x
11.x | 9.x
## Contributing

## Getting Started
Thank you for considering contributing to Testbench! You can read the contribution guide [here](CONTRIBUTING.md).

Before going through the rest of this documentation, please take some time to read the following documentation:
## Code of Conduct

* [Package Development for Laravel](https://laravel.com/docs/packages)
* [Orchestra Testbench Documentation](https://packages.tools/testbench/getting-started/introduction.html)
* [Laravel Dusk Documentation](https://laravel.com/docs/dusk)
In order to ensure that the community is welcoming to all, please review and abide by the [Code of Conduct](CODE_OF_CONDUCT.md).

## Installation
## License

To install through composer, run the following command from terminal:

composer require --dev "orchestra/testbench-dusk"

## Usage

To use Testbench Dusk Component, all you need to do is extend `Orchestra\Testbench\Dusk\TestCase` instead of `Laravel\Dusk\TestCase`. The fixture app booted by `Orchestra\Testbench\Dusk\TestCase` is predefined to follow the base application skeleton of Laravel 7.

```php
<?php

class BrowserTestCase extends Orchestra\Testbench\Dusk\TestCase
{
//
}
```

### Custom Host and Port

By default, Testbench Dusk will start its own PHP server at `http://127.0.0.1:8001`.

You can customize this by replacing the `$baseServeHost` and `$baseServePort` such as below:

```php
<?php

class BrowserTestCase extends Orchestra\Testbench\Dusk\TestCase
{
protected static $baseServeHost = '127.0.0.1';
protected static $baseServePort = 9000;
}
```


### Running with or without UI

Dusk 3.5+ offers the ability to run Dusk tests without UI (the browser window), and this is the default and is normally slightly quicker.
You can switch the behaviour with the following calls:

```php
// To show the UI during testing
\Orchestra\Testbench\Dusk\Options::withUI();

// To hide the UI during testing
\Orchestra\Testbench\Dusk\Options::withoutUI();
```

We recommend you place this in a `tests/bootstrap.php` file, similar to this packages own test setup and use this for PHP Unit.

### Database

By default you can either use `sqlite`, `mysql`, `pgsql` or `sqlsrv` with Testbench Dusk, however do note that it is impossible to use `sqlite` using `:memory:` database as you would with **Testbench** or **Testbench BrowserKit**.

If you opt to use `sqlite`, you might want to set the default database connection to `sqlite` either using `phpunit` configuration or setting it up on `getEnvironmentSetUp()` method.

```php
/**
* Define environment setup.
*
* @param Illuminate\Foundation\Application $app
*
* @return void
*/
protected function defineEnvironment($app)
{
$this->app['config']->set('database.default', 'sqlite');
}
```
> **Note**: In contradiction with laravel documentation you **should not** use `Illuminate\Foundation\Testing\DatabaseMigrations` trait, as testbench-dusk handles rollbacks by its self
To create the sqlite database you just need to run the following code:

```bash
php vendor/orchestra/testbench-dusk/create-sqlite-db
```

## Advanced Usage

### Customising the Laravel App instance used during a test

We use the calling test class to build up the application to be used when serving the request.

Sometimes you will want to make a minor change to the application for a single test (e.g. changing a config item).

This is made possible by using the `tweakApplication` method on the test, and passing in a closure to apply. At the end of the test, you need to call the `removeApplicationTweaks` method to stop the changes being applied to the server.

An example test (`can_tweak_the_application_within_a_test`) is available in the `tests/Browser/RouteTest.php` test file.

### Selectively running Dusk tests

Browser tests can take a while to run, so you could also separate your tests in your `phpunit.xml` file by providing different testsuites, allowing you to run your Browser tests on demand.

For example:

```xml
<testsuites>
<testsuite name="Browser">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
```

Run only your browser tests by running phpunit with the `--testsuite=Browser` option.

You can optionally set the default testsuite with the option `defaultTestSuite="Unit"`.

## Troubleshooting

### Chrome versions

```
Facebook\WebDriver\Exception\SessionNotCreatedException: session not created: Chrome version must be between 70 and 73
```

If tests report following error, run the following command:

./vendor/bin/dusk-updater update

Alternatively you can run the following command to detect installed ChromeDriver and auto update it if neccessary:

./vendor/bin/dusk-updater detect --auto-update

### Running Dusk and standard testbench tests in same suite

You may encounter the error
`PHP Fatal error: Cannot declare class CreateUsersTable, because the name is already in use in...`
when using [`loadLaravelMigrations()`](https://github.com/orchestral/testbench-core/blob/3.9/src/Concerns/WithLaravelMigrations.php) with some of your test extending the Dusk test class `\Orchestra\Testbench\Dusk\TestCase` and others extend the "normal" test class `\Orchestra\Testbench\TestCase`.

The problem arises because migrations are loaded from both packages' "skeletons" during the same test run,
and Laravel's migration classes are not namespaced.

#### Solution

Make sure all integration tests in your test suite use the same Laravel skeleton (the one from `testbench-dusk`),
regardless of the base class they extend by overriding `getBasePath()` in your test classes.
Do the override in your base integration test class, or perhaps in a trait if you need it in multiple classes.

```php
/**
* Make sure all integration tests use the same Laravel "skeleton" files.
* This avoids duplicate classes during migrations.
*
* Overrides \Orchestra\Testbench\Dusk\TestCase::getBasePath
* and \Orchestra\Testbench\Concerns\CreatesApplication::getBasePath
*
* @return string
*/
protected function getBasePath()
{
// Adjust this path depending on where your override is located.
return __DIR__.'/../vendor/orchestra/testbench-dusk/laravel';
}
```
Testbench Dusk is open-sourced software licensed under the [MIT license](LICENSE).
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"php": "^8.2",
"composer-runtime-api": "^2.2",
"laravel/dusk": "^8.1",
"laravel/framework": "^11.26",
"laravel/serializable-closure": "^1.3",
"orchestra/dusk-updater": "^2.6",
"orchestra/testbench": "^9.4",
Expand Down
20 changes: 20 additions & 0 deletions laravel/config/concurrency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Default Concurrency Driver
|--------------------------------------------------------------------------
|
| This option determines the default concurrency driver that will be used
| by Laravel's concurrency functions. By default, concurrent work will
| be sent to isolated PHP processes which will return their results.
|
| Supported: "process", "fork", "sync"
|
*/

'default' => env('CONCURRENCY_DRIVER', 'process'),

];
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<testsuite name="Browser">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
Expand Down
4 changes: 2 additions & 2 deletions src/DuskServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Orchestra\Testbench\Dusk;

use Orchestra\Testbench\Dusk\Exceptions\UnableToStartServer;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

use function Illuminate\Support\php_binary;
use function Orchestra\Testbench\defined_environment_variables;
use function Orchestra\Testbench\join_paths;

Expand Down Expand Up @@ -227,7 +227,7 @@ protected function guardServerStarting(): void
protected function prepareCommand(): array
{
return [
(string) (new PhpExecutableFinder)->find(false),
php_binary(),
'-S',
\sprintf('%s:%s', $this->host, $this->port),
join_paths(__DIR__, 'server.php'),
Expand Down
19 changes: 6 additions & 13 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orchestra\Testbench\Dusk;

use Illuminate\Support\Arr;
use Illuminate\Support\LazyCollection;
use Laravel\Dusk\Browser;

Expand All @@ -10,20 +11,12 @@

/**
* Get the default skeleton path
*
* @param array|string $path
*/
function default_skeleton_path(string $path = ''): string
function default_skeleton_path($path = ''): string
{
$path = $path != '' ? ltrim($path, DIRECTORY_SEPARATOR) : '';

return (string) realpath(join_paths(__DIR__, '..', 'laravel', $path));
}

/**
* Find test directory.
*/
function find_test_directory(): string
{
return package_path(join_paths('tests', 'Browser'));
return (string) realpath(join_paths(__DIR__, '..', 'laravel', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path)));
}

/**
Expand All @@ -35,7 +28,7 @@ function prepare_debug_directories(): void
return;
}

$path = find_test_directory();
$path = package_path(join_paths('tests', 'Browser'));

LazyCollection::make(['screenshots', 'console', 'source'])
->map(static fn ($directory) => join_paths($path, $directory))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace Orchestra\Testbench\Dusk\Tests\Unit;
namespace Orchestra\Testbench\Dusk\Tests\Concerns;

use Exception;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class TestCase extends PHPUnitTestCase
trait InteractsWithServer
{
protected function waitForServerToStart()
{
Expand Down
Loading

0 comments on commit cca4a5d

Please sign in to comment.