From f081ea716e630584874ac684d93dbd56fa2b7572 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Tue, 13 Jun 2023 17:09:16 +0200 Subject: [PATCH] Add tests for sources --- tests/Dataset.php | 87 +++++++++++++++++++++++++++++++++-- tests/Feature/SourcesTest.php | 7 +++ 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/tests/Dataset.php b/tests/Dataset.php index 18b0545..71d55b2 100644 --- a/tests/Dataset.php +++ b/tests/Dataset.php @@ -3,12 +3,18 @@ namespace Cerbero\JsonParser; use Cerbero\JsonParser\Decoders\DecodedValue; -use Cerbero\JsonParser\Sources\Endpoint; -use Cerbero\JsonParser\Sources\Psr7Request; +use Cerbero\JsonParser\Sources; use Cerbero\JsonParser\Tokens\Parser; use DirectoryIterator; use Generator; +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Request as Psr7Request; +use GuzzleHttp\Psr7\Response as Psr7Response; +use Illuminate\Http\Client\Request; +use Illuminate\Http\Client\Response; use Mockery; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; /** * The dataset provider. @@ -419,7 +425,7 @@ final class Dataset */ public static function forSourcesRequiringGuzzle(): Generator { - $sources = [Endpoint::class, Psr7Request::class]; + $sources = [Sources\Endpoint::class, Sources\Psr7Request::class]; foreach ($sources as $source) { yield Mockery::mock($source) @@ -448,4 +454,79 @@ final class Dataset yield [$decodesToArray, $json, $values[$decodesToArray]]; } } + + /** + * Retrieve the dataset to test sources + * + * @return Generator + */ + public static function forSources(): Generator + { + $path = fixture('json/simple_array.json'); + $json = file_get_contents($path); + $size = strlen($json); + $request = new Psr7Request('GET', 'foo'); + + $stream = Mockery::mock(StreamInterface::class) + ->shouldReceive([ + 'getSize' => $size, + 'isReadable' => true, + ]) + ->getMock(); + + $response = Mockery::mock(ResponseInterface::class) + ->shouldReceive('getBody') + ->andReturn($stream) + ->getMock(); + + $client = Mockery::mock(Client::class) + ->shouldReceive('get', 'sendRequest') + ->andReturn($response) + ->getMock(); + + $endpoint = Mockery::mock(Sources\Endpoint::class, ['https://example.com']) + ->makePartial() + ->shouldAllowMockingProtectedMethods() + ->shouldReceive('guzzle') + ->andReturn($client) + ->getMock(); + + $laravelClientRequest = Mockery::mock(Sources\LaravelClientRequest::class, [new Request($request)]) + ->makePartial() + ->shouldAllowMockingProtectedMethods() + ->shouldReceive('guzzle') + ->andReturn($client) + ->getMock(); + + $psr7Response = Mockery::mock(Psr7Response::class) + ->shouldReceive('getBody') + ->andReturn($stream) + ->getMock(); + + $psr7Request = Mockery::mock(Sources\Psr7Request::class, [$request]) + ->makePartial() + ->shouldAllowMockingProtectedMethods() + ->shouldReceive('guzzle') + ->andReturn($client) + ->getMock(); + + $sources = [ + new Sources\AnySource(new Sources\Json($json)), + new Sources\CustomSource(new Sources\Json($json)), + $endpoint, + new Sources\Filename($path), + new Sources\IterableSource(str_split($json)), + new Sources\Json($json), + new Sources\JsonResource(fopen($path, 'rb')), + $laravelClientRequest, + new Sources\LaravelClientResponse(new Response($psr7Response)), + new Sources\Psr7Message($response), + $psr7Request, + new Sources\Psr7Stream($stream), + ]; + + foreach ($sources as $source) { + yield [$source, $size]; + } + } } diff --git a/tests/Feature/SourcesTest.php b/tests/Feature/SourcesTest.php index ca28f9b..f54801b 100644 --- a/tests/Feature/SourcesTest.php +++ b/tests/Feature/SourcesTest.php @@ -16,3 +16,10 @@ it('throws an exception when Guzzle is required but not installed', function (So expect(fn () => JsonParser::parse($source)->traverse()) ->toThrow(GuzzleRequiredException::class, 'Guzzle is required to load JSON from endpoints'); })->with(Dataset::forSourcesRequiringGuzzle()); + +it('supports multiple sources', function (Source $source, int $size) { + expect($source) + ->getIterator()->toBeInstanceOf(Traversable::class) + ->matches()->toBeTrue() + ->size()->toBe($size); +})->with(Dataset::forSources());