Skip to content

Commit

Permalink
Adds the stress function for declaring stress tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeraymonddowning committed Dec 9, 2023
1 parent 7494c7d commit 9508f1f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
20 changes: 19 additions & 1 deletion src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Function Pest\Stressless\stress() has parameter $test with a nullable type declaration.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Function Pest\Stressless\stress() has parameter $test with null as default value.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Function Pest\Stressless\stress() has parameter $test with a nullable type declaration.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Function Pest\Stressless\stress() has parameter $test with null as default value.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Function Pest\Stressless\stress() has parameter $test with a nullable type declaration.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Function Pest\Stressless\stress() has parameter $test with null as default value.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Function Pest\Stressless\stress() has parameter $test with a nullable type declaration.

Check failure on line 15 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Function Pest\Stressless\stress() has parameter $test with null as default value.
{
return func_num_args() > 1
? test($description, func_get_arg(1))->group('stress')

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Support\HigherOrderTapProxy::group().

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Parameter #2 $closure of function test expects Closure|null, mixed given.

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Support\HigherOrderTapProxy::group().

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Parameter #2 $closure of function test expects Closure|null, mixed given.

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Support\HigherOrderTapProxy::group().

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Parameter #2 $closure of function test expects Closure|null, mixed given.

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Support\HigherOrderTapProxy::group().

Check failure on line 18 in src/Autoload.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Parameter #2 $closure of function test expects Closure|null, mixed given.
: visit($description);
}

/**
* Performs a stress test on the given URL.
*/
function visit(string $url): Factory
{
return Factory::make($url);
}
8 changes: 8 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/StressTestFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use function Pest\Stressless\stress;

stress('the homepage', function (): void {
stress('example.com');

expect(test()->groups()[0])->toBe('stress');
});
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
24 changes: 13 additions & 11 deletions tests/Unit/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9508f1f

Please sign in to comment.