Skip to content

Commit

Permalink
Add helpers and custom expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 21, 2024
1 parent 5fb3385 commit c015b5b
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

use Cerbero\LazyJsonPages\Services\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;

/*
|--------------------------------------------------------------------------
| Test Case
Expand All @@ -24,9 +30,26 @@
|
*/

// expect()->extend('toBeOne', function () {
// return $this->toBe(1);
// });
expect()->extend('toLoadItemsViaRequests', function (array $items, array $requests) {
$responses = $transactions = $expectedUris = [];

foreach ($requests as $uri => $fixture) {
$responses[] = new Response(body: file_get_contents(fixture($fixture)));
$expectedUris[] = $uri;
}

$stack = HandlerStack::create(new MockHandler($responses));

$stack->push(Middleware::history($transactions));

Client::configure(['handler' => $stack]);

$this->sequence(...$items);

$actualUris = array_map(fn(array $transaction) => (string) $transaction['request']->getUri(), $transactions);

expect($actualUris)->toBe($expectedUris);
});

/*
|--------------------------------------------------------------------------
Expand All @@ -39,7 +62,6 @@
|
*/

// function something()
// {
// // ..
// }
function fixture(string $filename) {
return __DIR__ . "/fixtures/{$filename}";
}

0 comments on commit c015b5b

Please sign in to comment.