Test that all sources are parsed correctly

This commit is contained in:
Andrea Marco Sartori 2023-06-13 22:53:47 +02:00
parent 840469a24f
commit 3eec5fc8fb
2 changed files with 9 additions and 13 deletions

View File

@ -10,11 +10,11 @@ use Generator;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as Psr7Request; use GuzzleHttp\Psr7\Request as Psr7Request;
use GuzzleHttp\Psr7\Response as Psr7Response; use GuzzleHttp\Psr7\Response as Psr7Response;
use GuzzleHttp\Psr7\Stream;
use Illuminate\Http\Client\Request; use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\Response; use Illuminate\Http\Client\Response;
use Mockery; use Mockery;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/** /**
* The dataset provider. * The dataset provider.
@ -462,21 +462,15 @@ final class Dataset
*/ */
public static function forSources(): Generator public static function forSources(): Generator
{ {
$parsed = require fixture('parsing/simple_array.php');
$path = fixture('json/simple_array.json'); $path = fixture('json/simple_array.json');
$json = file_get_contents($path); $json = file_get_contents($path);
$size = strlen($json); $size = strlen($json);
$request = new Psr7Request('GET', 'foo'); $request = new Psr7Request('GET', 'foo');
$stream = Mockery::mock(StreamInterface::class)
->shouldReceive([
'getSize' => $size,
'isReadable' => true,
])
->getMock();
$response = Mockery::mock(ResponseInterface::class) $response = Mockery::mock(ResponseInterface::class)
->shouldReceive('getBody') ->shouldReceive('getBody')
->andReturn($stream) ->andReturnUsing(fn () => new Stream(fopen($path, 'rb')))
->getMock(); ->getMock();
$client = Mockery::mock(Client::class) $client = Mockery::mock(Client::class)
@ -500,7 +494,7 @@ final class Dataset
$psr7Response = Mockery::mock(Psr7Response::class) $psr7Response = Mockery::mock(Psr7Response::class)
->shouldReceive('getBody') ->shouldReceive('getBody')
->andReturn($stream) ->andReturn(new Stream(fopen($path, 'rb')))
->getMock(); ->getMock();
$psr7Request = Mockery::mock(Sources\Psr7Request::class, [$request]) $psr7Request = Mockery::mock(Sources\Psr7Request::class, [$request])
@ -522,11 +516,11 @@ final class Dataset
new Sources\LaravelClientResponse(new Response($psr7Response)), new Sources\LaravelClientResponse(new Response($psr7Response)),
new Sources\Psr7Message($response), new Sources\Psr7Message($response),
$psr7Request, $psr7Request,
new Sources\Psr7Stream($stream), new Sources\Psr7Stream(new Stream(fopen($path, 'rb'))),
]; ];
foreach ($sources as $source) { foreach ($sources as $source) {
yield [$source, $size]; yield [$source, $size, $parsed];
} }
} }
} }

View File

@ -17,9 +17,11 @@ it('throws an exception when Guzzle is required but not installed', function (So
->toThrow(GuzzleRequiredException::class, 'Guzzle is required to load JSON from endpoints'); ->toThrow(GuzzleRequiredException::class, 'Guzzle is required to load JSON from endpoints');
})->with(Dataset::forSourcesRequiringGuzzle()); })->with(Dataset::forSourcesRequiringGuzzle());
it('supports multiple sources', function (Source $source, int $size) { it('supports multiple sources', function (Source $source, int $size, array $parsed) {
expect($source) expect($source)
->getIterator()->toBeInstanceOf(Traversable::class) ->getIterator()->toBeInstanceOf(Traversable::class)
->matches()->toBeTrue() ->matches()->toBeTrue()
->size()->toBe($size); ->size()->toBe($size);
expect(new JsonParser($source))->toParseTo($parsed);
})->with(Dataset::forSources()); })->with(Dataset::forSources());