Skip to content

Commit

Permalink
feat: adds --without-exception-handling and `without-deprecation-ha…
Browse files Browse the repository at this point in the history
…ndling`
  • Loading branch information
nunomaduro committed Feb 17, 2024
1 parent 8fc3221 commit b120301
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 32 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
matrix:
os: [ubuntu-latest]
php: ['8.1', '8.2', '8.3']
laravel: ['10', '11']
dependency-version: [prefer-lowest, prefer-stable]
parallel: ['', '--parallel']

name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }} - ${{ matrix.parallel }}
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.os }} - ${{ matrix.dependency-version }} - ${{ matrix.parallel }}

steps:
- name: Checkout
Expand All @@ -37,3 +38,6 @@ jobs:

- name: Unit Tests
run: composer test:unit

- name: Unit Options Tests
run: composer test:unit:options
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"license": "MIT",
"require": {
"php": "^8.1.0",
"laravel/framework": "^10.27.0|^11.0",
"pestphp/pest": "^2.22.0"
"laravel/framework": "^10.44.0|^11.0",
"pestphp/pest": "^2.33.6"
},
"autoload": {
"psr-4": {
Expand All @@ -30,8 +30,8 @@
}
},
"require-dev": {
"laravel/dusk": "^7.11.1",
"orchestra/testbench": "^8.13.0",
"laravel/dusk": "^7.12.3",
"orchestra/testbench": "^8.21.1|^9.0.0",
"pestphp/pest-dev-tools": "^2.16.0"
},
"minimum-stability": "dev",
Expand All @@ -48,15 +48,22 @@
"providers": [
"Pest\\Laravel\\PestServiceProvider"
]
},
"pest": {
"plugins": [
"Pest\\Laravel\\Plugin"
]
}
},
"scripts": {
"lint": "pint",
"test:lint": "pint --test",
"test:unit": "pest --colors=always",
"test:unit": "pest --colors=always --exclude-group=options",
"test:unit:options": "pest --colors=always --group=options --without-exception-handling --without-deprecation-handling",
"test": [
"@test:lint",
"@test:unit"
"@test:unit",
"@test:unit:options"
]
}
}
8 changes: 4 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<source>
<include>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
18 changes: 9 additions & 9 deletions src/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @return TestCase
*/
function actingAs(Authenticatable $user, string $driver = null)
function actingAs(Authenticatable $user, ?string $driver = null)
{
return test()->actingAs(...func_get_args());
}
Expand All @@ -22,7 +22,7 @@ function actingAs(Authenticatable $user, string $driver = null)
*
* @return TestCase
*/
function be(Authenticatable $user, string $driver = null)
function be(Authenticatable $user, ?string $driver = null)
{
return test()->be(...func_get_args());
}
Expand All @@ -32,7 +32,7 @@ function be(Authenticatable $user, string $driver = null)
*
* @return TestCase
*/
function assertAuthenticated(string $guard = null)
function assertAuthenticated(?string $guard = null)
{
return test()->assertAuthenticated(...func_get_args());
}
Expand All @@ -42,7 +42,7 @@ function assertAuthenticated(string $guard = null)
*
* @return TestCase
*/
function assertGuest(string $guard = null)
function assertGuest(?string $guard = null)
{
return test()->assertGuest(...func_get_args());
}
Expand All @@ -52,7 +52,7 @@ function assertGuest(string $guard = null)
*
* @return bool
*/
function isAuthenticated(string $guard = null)
function isAuthenticated(?string $guard = null)
{
return test()->isAuthenticated(...func_get_args());
}
Expand All @@ -62,7 +62,7 @@ function isAuthenticated(string $guard = null)
*
* @return TestCase
*/
function assertAuthenticatedAs(Authenticatable $user, string $guard = null)
function assertAuthenticatedAs(Authenticatable $user, ?string $guard = null)
{
return test()->assertAuthenticatedAs(...func_get_args());
}
Expand All @@ -72,7 +72,7 @@ function assertAuthenticatedAs(Authenticatable $user, string $guard = null)
*
* @return TestCase
*/
function assertCredentials(array $credentials, string $guard = null)
function assertCredentials(array $credentials, ?string $guard = null)
{
return test()->assertCredentials(...func_get_args());
}
Expand All @@ -82,15 +82,15 @@ function assertCredentials(array $credentials, string $guard = null)
*
* @return TestCase
*/
function assertInvalidCredentials(array $credentials, string $guard = null)
function assertInvalidCredentials(array $credentials, ?string $guard = null)
{
return test()->assertInvalidCredentials(...func_get_args());
}

/**
* Return true if the credentials are valid, false otherwise.
*/
function hasCredentials(array $credentials, string $guard = null): bool
function hasCredentials(array $credentials, ?string $guard = null): bool
{
return test()->hasCredentials(...func_get_args());
}
6 changes: 3 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ function instance(string $abstract, object $instance): object
/**
* Mock an instance of an object in the container.
*/
function mock(string $abstract, Closure $mock = null): MockInterface
function mock(string $abstract, ?Closure $mock = null): MockInterface
{
return test()->mock(...func_get_args());
}

/**
* Mock a partial instance of an object in the container.
*/
function partialMock(string $abstract, Closure $mock = null): MockInterface
function partialMock(string $abstract, ?Closure $mock = null): MockInterface
{
return test()->partialMock(...func_get_args());
}

/**
* Spy an instance of an object in the container.
*/
function spy(string $abstract, Closure $mock = null): MockInterface
function spy(string $abstract, ?Closure $mock = null): MockInterface
{
return test()->spy(...func_get_args());
}
Expand Down
16 changes: 8 additions & 8 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @return TestCase
*/
function assertDatabaseHas(string $table, array $data, string $connection = null)
function assertDatabaseHas(string $table, array $data, ?string $connection = null)
{
return test()->assertDatabaseHas(...func_get_args());
}
Expand All @@ -23,7 +23,7 @@ function assertDatabaseHas(string $table, array $data, string $connection = null
*
* @return TestCase
*/
function assertDatabaseMissing(string $table, array $data, string $connection = null)
function assertDatabaseMissing(string $table, array $data, ?string $connection = null)
{
return test()->assertDatabaseMissing(...func_get_args());
}
Expand All @@ -33,7 +33,7 @@ function assertDatabaseMissing(string $table, array $data, string $connection =
*
* @return TestCase
*/
function assertDatabaseEmpty(string $table, string $connection = null)
function assertDatabaseEmpty(string $table, ?string $connection = null)
{
return test()->assertDatabaseEmpty(...func_get_args());
}
Expand Down Expand Up @@ -63,7 +63,7 @@ function assertModelMissing(Model $model)
*
* @return TestCase
*/
function assertDatabaseCount(string $table, int $count, string $connection = null)
function assertDatabaseCount(string $table, int $count, ?string $connection = null)
{
return test()->assertDatabaseCount(...func_get_args());
}
Expand All @@ -74,7 +74,7 @@ function assertDatabaseCount(string $table, int $count, string $connection = nul
* @param Model|string $table
* @return TestCase
*/
function assertSoftDeleted($table, array $data = [], string $connection = null, string $deletedAtColumn = 'deleted_at')
function assertSoftDeleted($table, array $data = [], ?string $connection = null, string $deletedAtColumn = 'deleted_at')
{
return test()->assertSoftDeleted(...func_get_args());
}
Expand All @@ -85,7 +85,7 @@ function assertSoftDeleted($table, array $data = [], string $connection = null,
* @param Model|string $table
* @return TestCase
*/
function assertNotSoftDeleted($table, array $data = [], string $connection = null, string $deletedAtColumn = 'deleted_at')
function assertNotSoftDeleted($table, array $data = [], ?string $connection = null, string $deletedAtColumn = 'deleted_at')
{
return test()->assertNotSoftDeleted(...func_get_args());
}
Expand All @@ -103,7 +103,7 @@ function isSoftDeletableModel($model): bool
/**
* Get the database connection.
*/
function getConnection(string $connection = null): Connection
function getConnection(?string $connection = null): Connection
{
return test()->getConnection(...func_get_args());
}
Expand All @@ -123,7 +123,7 @@ function seed(array|string $class = 'Database\\Seeders\\DatabaseSeeder')
*
* @return TestCase
*/
function expectsDatabaseQueryCount(int $excepted, string $connection = null)
function expectsDatabaseQueryCount(int $excepted, ?string $connection = null)
{
return test()->expectsDatabaseQueryCount(...func_get_args());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function json(string $method, string $uri, array $data = [], array $headers = []
*
* @return TestResponse
*/
function call(string $method, string $uri, array $parameters = [], array $cookies = [], array $files = [], array $server = [], string $content = null)
function call(string $method, string $uri, array $parameters = [], array $cookies = [], array $files = [], array $server = [], ?string $content = null)
{
return test()->call(...func_get_args());
}
89 changes: 89 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Pest\Laravel;

use Illuminate\Foundation\Testing\Concerns\InteractsWithDeprecationHandling;
use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugins\Concerns\HandleArguments;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
final class Plugin implements HandlesArguments
{
use HandleArguments;

public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--with-exception-handling', $arguments)) {
$arguments = $this->popArgument('--with-exception-handling', $arguments);

$interactsWithExceptionHandling = function (TestCase $testCase): bool {
return function_exists('trait_uses_recursive') && trait_uses_recursive($testCase, InteractsWithExceptionHandling::class);
};

uses()->beforeEach(function () use ($interactsWithExceptionHandling) {
/** @var TestCase $this */
if ($interactsWithExceptionHandling($this)) {
/** @var TestCase&InteractsWithExceptionHandling $this */
$this->withExceptionHandling();
}
})->in(TestSuite::getInstance()->rootPath);
}

if ($this->hasArgument('--without-exception-handling', $arguments)) {
$arguments = $this->popArgument('--without-exception-handling', $arguments);

$interactsWithExceptionHandling = function (TestCase $testCase): bool {
return function_exists('trait_uses_recursive') && trait_uses_recursive($testCase, InteractsWithExceptionHandling::class);
};

uses()->beforeEach(function () use ($interactsWithExceptionHandling) {
/** @var TestCase $this */
if ($interactsWithExceptionHandling($this)) {
/** @var TestCase&InteractsWithExceptionHandling $this */
$this->withoutExceptionHandling();
}
})->in(TestSuite::getInstance()->rootPath);
}

if ($this->hasArgument('--with-deprecation-handling', $arguments)) {
$arguments = $this->popArgument('--with-deprecation-handling', $arguments);

$interactsWithDeprecationHandling = function (TestCase $testCase): bool {
return function_exists('trait_uses_recursive') && trait_uses_recursive($testCase, InteractsWithDeprecationHandling::class);
};

uses()->beforeEach(function () use ($interactsWithDeprecationHandling) {
/** @var TestCase $this */
if ($interactsWithDeprecationHandling($this)) {
/** @var TestCase&InteractsWithDeprecationHandling $this */
$this->withDeprecationHandling();
}
})->in(TestSuite::getInstance()->rootPath);
}

if ($this->hasArgument('--without-deprecation-handling', $arguments)) {
$arguments = $this->popArgument('--without-deprecation-handling', $arguments);

$interactsWithDeprecationHandling = function (TestCase $testCase): bool {
return function_exists('trait_uses_recursive') && trait_uses_recursive($testCase, InteractsWithDeprecationHandling::class);
};

uses()->beforeEach(function () use ($interactsWithDeprecationHandling) {
/** @var TestCase $this */
if ($interactsWithDeprecationHandling($this)) {
/** @var TestCase&InteractsWithDeprecationHandling $this */
$this->withoutDeprecationHandling();
}
})->in(TestSuite::getInstance()->rootPath);
}

return $arguments;
}
}
13 changes: 13 additions & 0 deletions tests/Options/WithoutDeprecationHandling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Illuminate\Support\Facades\Route;

uses()->group('options');

test('--without-deprecation-handling', function () {
Route::get('/exception', function () {
str_replace(null, null, null);
});

$this->get('/exception');
})->throws(Exception::class, 'str_replace(): Passing null to parameter #1 ($search) of type array|string is deprecated');
13 changes: 13 additions & 0 deletions tests/Options/WithoutExceptionHandling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Illuminate\Support\Facades\Route;

uses()->group('options');

test('--without-exception-handling', function () {
Route::get('/exception', function () {
throw new Exception('Exception message');
});

$this->get('/exception');
})->throws(Exception::class, 'Exception message');

0 comments on commit b120301

Please sign in to comment.