Skip to content

Commit

Permalink
fetchExternalData tests (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Aug 11, 2022
1 parent 1ee1e46 commit 6c1c2eb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function getPlatformInfo()
*/
public function fetchExternalData($url, $data = null, $blocking = true, $stream = null, $fullData = false, $headers = [], $files = [], $method = 'POST')
{
// TODO add tests for this function with all possible parameters
@set_time_limit(0);
$options = [
'timeout' => !empty($blocking) ? 30 : 0.01,
Expand All @@ -70,29 +69,24 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream

$client = new Client(config('hh5p.guzzle'));
try {

if ($data !== null) {
// Post
$options['form_params'] = $data;
$response = $client->request('POST', $url, $options);

} else {
$response = $client->request('GET', $url, $options);
}

if ($response->getStatusCode() === 200) {
$contents = null;
$body = empty($response->getBody()) ? null : $response->getBody()->getContents();
$body = empty($response->getBody()) ? null : $response->getBody()->getContents();

if ($contents) {
return $fullData ? ['status' => $response->getStatusCode(), 'data' => json_decode($contents)] : $contents;
}
if ($body) {
return $fullData ? ['status' => $response->getStatusCode(), 'data' => json_decode($body)] : $body;
}

return true;
} else {
return;
return false;
}
} catch (RequestException $e) {
return false;
Expand Down
76 changes: 76 additions & 0 deletions tests/Repositories/H5PRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace EscolaLms\HeadlessH5P\Tests\Repositories;

use EscolaLms\HeadlessH5P\Repositories\H5PRepository;
use EscolaLms\HeadlessH5P\Tests\TestCase;
use GuzzleHttp\Psr7\Response;
use H5PHubEndpoints;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class H5PRepositoryTest extends TestCase
{
use DatabaseTransactions;

private H5PRepository $repository;

protected function setUp(): void
{
parent::setUp();
$this->mock->reset();
$this->repository = app(H5PRepository::class);
}

public function testFetchExternalDataShouldReturnFullData(): void
{
$this->mock->append(new Response(200, [], json_encode(['var1' => 'val1'])));

$url = H5PHubEndpoints::createURL('example/url');
$result = $this->repository->fetchExternalData($url, null, false, null, true);

$this->assertArrayHasKey('status', $result);
$this->assertArrayHasKey('data', $result);
$this->assertSame(200, $result['status']);
$this->assertSame('val1', $result['data']->var1);
}

public function testFetchExternalDataShouldReturnDataInStringJson(): void
{
$this->mock->append(new Response(200, [], json_encode(['var1' => 'val1'])));

$url = H5PHubEndpoints::createURL('example/url');
$result = $this->repository->fetchExternalData($url, ['param1' => 'value1'], false, null, false);

$this->assertEquals('{"var1":"val1"}', $result);
}

public function testFetchExternalDataBlockingTrue(): void
{
$this->mock->append(new Response(200, [], json_encode(['var1' => 'val1'])));

$url = H5PHubEndpoints::createURL('example/url');
$result = $this->repository->fetchExternalData($url, null, true, null, false);

$this->assertEquals('{"var1":"val1"}', $result);
}

public function testFetchExternalDataShouldReturnFalseWhenStatusIs400(): void
{
$this->mock->append(new Response(400, [], json_encode(['var1' => 'val1'])));

$url = H5PHubEndpoints::createURL('example/url');
$result = $this->repository->fetchExternalData($url, null, false, null, false);

$this->assertFalse($result);
}

public function testFetchExternalDataShouldReturnFalseWhenStatusIsDifferentFrom200(): void
{
$this->mock->append(new Response(302, [], json_encode(['var1' => 'val1'])));

$url = H5PHubEndpoints::createURL('example/url');
$result = $this->repository->fetchExternalData($url, null, false, null, false);

$this->assertFalse($result);
}
}

0 comments on commit 6c1c2eb

Please sign in to comment.