Skip to content

Commit

Permalink
refactor: test result
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Feb 14, 2025
1 parent 92d8fcb commit d3cdc39
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 111 deletions.
14 changes: 2 additions & 12 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @internal
*/
final class Compiler
final readonly class Compiler
{
/**
* The path to the tests.
Expand All @@ -20,7 +20,7 @@ final class Compiler
* @param array<int, Operation> $operations
*/
public function __construct(
private readonly array $operations
private array $operations
) {
//
}
Expand All @@ -43,16 +43,6 @@ public function compile(): void
test('runtime', async ({ page }) => {
$content
const response = await page.reload();
test.info().annotations.push({
type: '_response',
description: JSON.stringify({
headers: response.headers(),
status: response.status(),
url: response.url(),
})
});
});
JS,
);
Expand Down
13 changes: 2 additions & 11 deletions src/PendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Pest\Browser;

use Pest\Browser\ValueObjects\TestResult;
use Pest\Browser\ValueObjects\TestResultResponse;
use Pest\Browser\Contracts\Operation;
use Pest\Browser\ValueObjects\TestResult;

/**
* @internal
Expand Down Expand Up @@ -68,18 +67,10 @@ public function assertUrlIs(string $url): self
return $this;
}

/**
* Build and return the final response the test received.
*/
public function response(): TestResultResponse
{
return $this->build()->response();
}

/**
* Compile the JavaScript test file.
*/
public function build(): void
public function compile(): TestResult
{
$compiler = new Compiler($this->operations);

Expand Down
8 changes: 1 addition & 7 deletions src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ public static function isRegex(string $target): bool
return false;
}

// If the first and last characters are not the same, it's not a regex
if (($delimiter = mb_substr($target, 0, 1)) !== mb_substr($target, -1, 1)) {
return false;
}

// If the delimiter is alphanumeric, it's not a regex
if (! preg_match('/[^a-zA-Z0-9]/', $delimiter)) {
return false;
}

return true;
return preg_match('/[^a-zA-Z0-9]/', $delimiter) !== false;
}
}
9 changes: 0 additions & 9 deletions src/ValueObjects/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
public function __construct(
private bool $ok,
private TestResultResponse $response,
) {
//
}
Expand All @@ -26,12 +25,4 @@ public function ok(): bool
{
return $this->ok;
}

/**
* Get the final response from the test result.
*/
public function response(): TestResultResponse
{
return $this->response;
}
}
60 changes: 0 additions & 60 deletions src/ValueObjects/TestResultResponse.php

This file was deleted.

8 changes: 3 additions & 5 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Pest\Browser;

use Pest\Browser\ValueObjects\TestResult;
use Pest\Browser\ValueObjects\TestResultResponse;
use Pest\Support\Arr;
use Symfony\Component\Process\Process;

Expand All @@ -25,12 +24,11 @@ public function run(): TestResult

$output = $process->getOutput();

// @phpstan-ignore-next-line
/** @var array<int, mixed> $outputAsArray */
$outputAsArray = json_decode($output, true);

$ok = Arr::get($outputAsArray, 'suites.0.specs.0.ok');
$annotations = Arr::get($outputAsArray, 'suites.0.specs.0.tests.0.annotations');
$ok = (bool) Arr::get($outputAsArray, 'suites.0.specs.0.ok');

return new TestResult($ok, new TestResultResponse($annotations));
return new TestResult($ok);
}
}
7 changes: 0 additions & 7 deletions tests/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,4 @@
visit('https://laravel.com')
->clickLink('Get Started')
->assertUrlIs('https://laravel.com/docs/11.x');

// -- or --

$browser = visit('https://laravel.com')
->clickLink('Get Started');

expect($browser->response()->url())->toBe('https://laravel.com/docs/11.x');
});

0 comments on commit d3cdc39

Please sign in to comment.