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

[2.x] Adds the stress function for declaring stress tests #16

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
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 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 All @@ -49,27 +57,27 @@

foreach ($arguments as $argument) {
if (str_starts_with($argument, '--duration=')) {
$run->duration((int) str_replace('--duration=', '', $argument));

Check failure on line 60 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::duration().

Check failure on line 60 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::duration().

Check failure on line 60 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::duration().

Check failure on line 60 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::duration().
}

if (str_starts_with($argument, '--concurrency=')) {
$run->concurrently((int) str_replace('--concurrency=', '', $argument));

Check failure on line 64 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::concurrently().

Check failure on line 64 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::concurrently().

Check failure on line 64 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::concurrently().

Check failure on line 64 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::concurrently().
}

if ($argument === '--delete') {
$run->delete();

Check failure on line 68 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::delete().

Check failure on line 68 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::delete().

Check failure on line 68 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::delete().

Check failure on line 68 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::delete().
}

if ($argument === '--get') {
$run->get();

Check failure on line 72 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::get().

Check failure on line 72 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::get().

Check failure on line 72 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::get().

Check failure on line 72 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::get().
}

if ($argument === '--head') {
$run->head();

Check failure on line 76 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::head().

Check failure on line 76 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::head().

Check failure on line 76 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::head().

Check failure on line 76 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::head().
}

if (str_starts_with($argument, '--options=')) {
$run->options($this->extractPayload('options', $argument));

Check failure on line 80 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::options().

Check failure on line 80 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::options().

Check failure on line 80 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-stable)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::options().

Check failure on line 80 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / Static Tests (prefer-lowest)

Call to an undefined method Pest\PendingCalls\TestCall|Pest\Stressless\Factory::options().
} elseif ($argument === '--options') {
$run->options();
}
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
Loading