From 9508f1fe613d8fa393148914d7fa46b0eafd6609 Mon Sep 17 00:00:00 2001 From: lukeraymonddowning Date: Sat, 9 Dec 2023 14:03:58 +0000 Subject: [PATCH] Adds the `stress` function for declaring stress tests. --- src/Autoload.php | 20 +++++++++++++++++++- src/Plugin.php | 8 ++++++++ tests/Feature/StressTestFunction.php | 11 +++++++++++ tests/Pest.php | 4 ++-- tests/Unit/Factory.php | 24 +++++++++++++----------- 5 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 tests/Feature/StressTestFunction.php diff --git a/src/Autoload.php b/src/Autoload.php index c2cbaf5..ed6c5e9 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -4,7 +4,25 @@ namespace Pest\Stressless; -function stress(string $url): Factory +use Closure; +use Pest\PendingCalls\TestCall; + +/** + * If passed two parameters, this function creates a test + * with the `stress` group. If only passed one parameter, + * it will perform a stress test on the given URL. + */ +function stress(string $description, ?Closure $test = null): Factory|TestCall +{ + return func_num_args() > 1 + ? test($description, func_get_arg(1))->group('stress') + : visit($description); +} + +/** + * Performs a stress test on the given URL. + */ +function visit(string $url): Factory { return Factory::make($url); } diff --git a/src/Plugin.php b/src/Plugin.php index a13eb0d..7b93387 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -28,6 +28,14 @@ public function __construct() */ public function handleArguments(array $arguments): array { + if ($this->hasArgument('--stress', $arguments)) { + return $this->pushArgument('--group=stress', $this->popArgument('--stress', $arguments)); + } + + if ($this->hasArgument('--exclude-stress', $arguments)) { + return $this->pushArgument('--exclude-group=stress', $this->popArgument('--exclude-stress', $arguments)); + } + if (! array_key_exists(1, $arguments)) { return $arguments; } diff --git a/tests/Feature/StressTestFunction.php b/tests/Feature/StressTestFunction.php new file mode 100644 index 0000000..a3da7aa --- /dev/null +++ b/tests/Feature/StressTestFunction.php @@ -0,0 +1,11 @@ +groups()[0])->toBe('stress'); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 58317da..a61d214 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use function Pest\Stressless\stress; +use function Pest\Stressless\visit; uses()->beforeEach(function (): void { - $this->stress = $_SERVER['stress'] ??= stress('example.com') + $this->stress = $_SERVER['stress'] ??= visit('example.com') ->concurrently(2) ->for(1)->second(); diff --git a/tests/Unit/Factory.php b/tests/Unit/Factory.php index d0814f4..84a136b 100644 --- a/tests/Unit/Factory.php +++ b/tests/Unit/Factory.php @@ -2,71 +2,73 @@ declare(strict_types=1); -it('correctly use get method by default', function (): void { +use function Pest\Stressless\stress; + +stress('correctly use get method by default', function (): void { expect($this->stress->method())->toBe('get'); }); -it('correctly sets the delete method', function (): void { +stress('correctly sets the delete method', function (): void { $this->stress->delete(); expect($this->stress->method())->toBe('delete'); }); -it('correctly sets the get method', function (): void { +stress('correctly sets the get method', function (): void { $this->stress->get(); expect($this->stress->method())->toBe('get'); }); -it('correctly sets the head method', function (): void { +stress('correctly sets the head method', function (): void { $this->stress->head(); expect($this->stress->method())->toBe('head'); }); -it('correctly sets the options method without payload', function (): void { +stress('correctly sets the options method without payload', function (): void { $this->stress->options(); expect($this->stress->method())->toBe('options') ->and($this->stress->payload())->toBe([]); }); -it('correctly sets the options method with payload', function (): void { +stress('correctly sets the options method with payload', function (): void { $this->stress->options(['foo' => 'bar']); expect($this->stress->method())->toBe('options') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); -it('correctly sets the patch method without payload', function (): void { +stress('correctly sets the patch method without payload', function (): void { $this->stress->patch(); expect($this->stress->method())->toBe('patch') ->and($this->stress->payload())->toBe([]); }); -it('correctly sets the patch method with payload', function (): void { +stress('correctly sets the patch method with payload', function (): void { $this->stress->patch(['foo' => 'bar']); expect($this->stress->method())->toBe('patch') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); -it('correctly sets the put method without payload', function (): void { +stress('correctly sets the put method without payload', function (): void { $this->stress->put(); expect($this->stress->method())->toBe('put') ->and($this->stress->payload())->toBe([]); }); -it('correctly sets the put method with payload', function (): void { +stress('correctly sets the put method with payload', function (): void { $this->stress->put(['foo' => 'bar']); expect($this->stress->method())->toBe('put') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); -it('correctly sets the post method', function (): void { +stress('correctly sets the post method', function (): void { $this->stress->post(['foo' => 'bar']); expect($this->stress->method())->toBe('post')