From 883d7dfbdd8e2c1097f8a01ef7af5d58f3e1ecbd Mon Sep 17 00:00:00 2001 From: Louis Rose Date: Thu, 21 Mar 2024 15:42:38 +0000 Subject: [PATCH] makes tests compatible with mock --- lib/ApiRequestor.php | 3 +- lib/HttpClient/GuzzleClient.php | 4 ++- tests/ExceptionsTest.php | 12 +++---- tests/FilesTest.php | 16 +++++----- tests/FormatsTest.php | 4 +-- tests/ImportsTest.php | 10 +++--- tests/JobsTest.php | 55 ++++++++++----------------------- tests/LoggingTest.php | 14 ++------- tests/PagingTest.php | 10 +++--- tests/WithClient.php | 29 ++++++++++++++--- tests/files/target/.gitignore | 2 ++ 11 files changed, 78 insertions(+), 81 deletions(-) create mode 100644 tests/files/target/.gitignore diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index a5d68cd..016a19e 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -36,7 +36,8 @@ public function __construct($config) */ public function request($endpoint, $method = 'GET', $params = [], $getFileContent = false) { - $endpoint = Zamzar::API_BASE[$this->config['environment']] . $endpoint; + $base = $this->config['base_url'] ?? Zamzar::API_BASE[$this->config['environment']]; + $endpoint = $base . $endpoint; if ($this->config['debug']) { Zamzar::getLogger()->info("($method) $endpoint params=" . json_encode($params)); diff --git a/lib/HttpClient/GuzzleClient.php b/lib/HttpClient/GuzzleClient.php index f71b42e..07aa9fa 100644 --- a/lib/HttpClient/GuzzleClient.php +++ b/lib/HttpClient/GuzzleClient.php @@ -18,7 +18,9 @@ class GuzzleClient public static function request($config, $endpoint, $method = 'GET', $params = [], $hasLocalFile = false, $getFileContent = false) { $client = new Client([ - 'auth' => [$config['api_key'], ''], + 'headers' => [ + 'Authorization' => 'Bearer ' . $config['api_key'], + ], ]); $response = $client->request($method, $endpoint, self::prepareRequest($config, $method, $params, $hasLocalFile)); diff --git a/tests/ExceptionsTest.php b/tests/ExceptionsTest.php index 620b438..cbe706c 100644 --- a/tests/ExceptionsTest.php +++ b/tests/ExceptionsTest.php @@ -13,14 +13,14 @@ final class ExceptionsTest extends TestCase public function testAuthenticationException(): void { $this->expectException(\Zamzar\Exception\AuthenticationException::class); - $zamzar = new \Zamzar\ZamzarClient('invalid'); + $zamzar = $this->client(['api_key' => 'invalid']); $zamzar->testConnection(); } public function testInvalidArgumentException(): void { $this->expectException(\Zamzar\Exception\InvalidArgumentException::class); - $this->client->jobs->create([ + $this->client()->jobs->create([ 'sour1file' => 'invalid' ]); } @@ -28,22 +28,22 @@ public function testInvalidArgumentException(): void public function testInvalidRequestException(): void { $this->expectException(\Zamzar\Exception\InvalidRequestException::class); - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => 'https://www.zamzar.com/images/zamzar-logo.png', - 'target_format' => 'mpg' + 'target_format' => 'unsupported' ]); } public function testInvalidResourceException(): void { $this->expectException(\Zamzar\Exception\InvalidResourceException::class); - $this->client->files->get(1234); + $this->client()->files->get(1234); } public function testTimeOutException(): void { $this->expectException(\Zamzar\Exception\TimeOutException::class); - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => 'https://www.zamzar.com/images/zamzar-logo.png', 'target_format' => 'pdf' ])->waitForCompletion(0); diff --git a/tests/FilesTest.php b/tests/FilesTest.php index 9b14251..7248d09 100644 --- a/tests/FilesTest.php +++ b/tests/FilesTest.php @@ -12,22 +12,22 @@ final class FilesTest extends TestCase public function testFilesAreListable(): void { - $files = $this->client->files->all(['limit' => 1]); + $files = $this->client()->files->all(['limit' => 1]); $this->assertEquals(count($files->data), 1); } public function testFileIsRetrievable(): void { - $files = $this->client->files->all(['limit' => 1]); + $files = $this->client()->files->all(['limit' => 1]); $fileid = $files->data[0]->getId(); - $file = $this->client->files->get($fileid); + $file = $this->client()->files->get($fileid); $this->assertGreaterThan(0, $file->size); } public function testFileCanBeUploaded(): void { - $file = $this->client->files->create([ + $file = $this->client()->files->create([ 'name' => __DIR__ . '/files/source/test.pdf' ]); $this->assertGreaterThan(0, $file->id); @@ -35,7 +35,7 @@ public function testFileCanBeUploaded(): void public function testFileCanBeDownloaded(): void { - $file = $this->client->files->create([ + $file = $this->client()->files->create([ 'name' => __DIR__ . '/files/source/test.pdf' ]); $file->download(__DIR__ . '/files/target/'); @@ -44,7 +44,7 @@ public function testFileCanBeDownloaded(): void public function testFileCanBeDownloadedWithCustomFilename(): void { - $file = $this->client->files->create([ + $file = $this->client()->files->create([ 'name' => __DIR__ . '/files/source/test.pdf' ]); $file->download(__DIR__ . '/files/target/output.pdf'); @@ -53,7 +53,7 @@ public function testFileCanBeDownloadedWithCustomFilename(): void public function testFileCanBeDeleted(): void { - $file = $this->client->files->create([ + $file = $this->client()->files->create([ 'name' => __DIR__ . '/files/source/test.pdf' ]); $file->delete(); @@ -63,7 +63,7 @@ public function testFileCanBeDeleted(): void public function testFileCanBeConverted(): void { - $file = $this->client->files->create([ + $file = $this->client()->files->create([ 'name' => __DIR__ . '/files/source/test.pdf' ]); diff --git a/tests/FormatsTest.php b/tests/FormatsTest.php index c760da2..6a22bd4 100644 --- a/tests/FormatsTest.php +++ b/tests/FormatsTest.php @@ -12,13 +12,13 @@ final class FormatsTest extends TestCase public function testFormatsAreListable(): void { - $formats = $this->client->formats->all(['limit' => 1]); + $formats = $this->client()->formats->all(['limit' => 1]); $this->assertEquals(count($formats->data), 1); } public function testFormatIsRetrievable(): void { - $format = $this->client->formats->get('png'); + $format = $this->client()->formats->get('png'); $this->assertGreaterThan(0, count($format->targets)); } } diff --git a/tests/ImportsTest.php b/tests/ImportsTest.php index d933e79..0d91944 100644 --- a/tests/ImportsTest.php +++ b/tests/ImportsTest.php @@ -12,22 +12,22 @@ final class ImportsTest extends TestCase public function testImportsAreListable(): void { - $imports = $this->client->imports->all(['limit' => 1]); + $imports = $this->client()->imports->all(['limit' => 1]); $this->assertEquals(count($imports->data), 1); } public function testImportIsRetrievable(): void { - $import = $this->client->imports->create([ + $import = $this->client()->imports->create([ 'url' => 'https://www.zamzar.com/images/zamzar-logo.png' ]); - $import = $this->client->imports->get($import->id); + $import = $this->client()->imports->get($import->id); $this->assertGreaterThan(0, $import->id); } public function testImportCanBeStarted(): void { - $import = $this->client->imports->create([ + $import = $this->client()->imports->create([ 'url' => 'https://www.zamzar.com/images/zamzar-logo.png' ]); $this->assertGreaterThan(0, $import->id); @@ -35,7 +35,7 @@ public function testImportCanBeStarted(): void public function testCanWaitUntilComplete(): void { - $import = $this->client->imports->create([ + $import = $this->client()->imports->create([ 'url' => 'https://www.zamzar.com/images/zamzar-logo.png' ])->waitForCompletion(); $this->assertTrue($import->hasCompleted()); diff --git a/tests/JobsTest.php b/tests/JobsTest.php index 986720b..05ba565 100644 --- a/tests/JobsTest.php +++ b/tests/JobsTest.php @@ -18,24 +18,24 @@ final class JobsTest extends TestCase public function testJobsAreListable(): void { - $jobs = $this->client->jobs->all(['limit' => 1]); + $jobs = $this->client()->jobs->all(['limit' => 1]); $this->assertEquals(count($jobs->data), 1); } public function testJobIsRetrievable(): void { - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => $this->validLocalSourceFile, 'target_format' => $this->validTargetFormat ]); - $job = $this->client->jobs->get($job->id); + $job = $this->client()->jobs->get($job->id); $this->assertGreaterThan(0, $job->id); } public function testJobCanBeSubmittedForLocalFile(): void { - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => $this->validLocalSourceFile, 'target_format' => $this->validTargetFormat, ])->waitForCompletion(); @@ -45,7 +45,7 @@ public function testJobCanBeSubmittedForLocalFile(): void public function testJobCanBeSubmittedForUrlFile(): void { - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => 'https://www.zamzar.com/images/zamzar-logo.png', 'target_format' => 'jpg', ])->waitForCompletion(); @@ -55,11 +55,11 @@ public function testJobCanBeSubmittedForUrlFile(): void public function testJobCanBeSubmittedForZamzarFile(): void { - $file = $this->client->files->create([ + $file = $this->client()->files->create([ 'name' => __DIR__ . '/files/source/test.pdf' ]); - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => $file->id, 'target_format' => 'png', ])->waitForCompletion(); @@ -67,39 +67,18 @@ public function testJobCanBeSubmittedForZamzarFile(): void $this->assertEquals($job->status, Job::STATUS_SUCCESSFUL); } - public function testCanListOnlysuccessfulJobs() + public function testCanListOnlySuccessfulJobs() { - $job = $this->client->jobs->create([ - 'source_file' => 'https://www.zamzar.com/non-existant.png', - 'target_format' => 'jpg', - ])->waitForCompletion(); - - $this->assertEquals(Job::STATUS_FAILED, $job->status); - - $jobs = $this->client->jobs->all(['status' => Job::STATUS_SUCCESSFUL]); + $jobs = $this->client()->jobs->all(['status' => Job::STATUS_SUCCESSFUL]); $this->assertEmpty(array_filter($jobs->data, function ($job) { return $job->status !== Job::STATUS_SUCCESSFUL; })); } - public function testCanPageOnlysuccessfulJobs() + public function testCanPageOnlySuccessfulJobs() { - $job = $this->client->jobs->create([ - 'source_file' => 'https://www.zamzar.com/non-existant.png', - 'target_format' => 'jpg', - ])->waitForCompletion(); - - $this->assertEquals(Job::STATUS_FAILED, $job->status); - - $job = $this->client->jobs->create([ - 'source_file' => 'https://www.zamzar.com/images/zamzar-logo.png', - 'target_format' => 'jpg', - ])->waitForCompletion(); - - $this->assertEquals(Job::STATUS_SUCCESSFUL, $job->status); - - $jobs = $this->client->jobs->all(['status' => Job::STATUS_SUCCESSFUL, 'limit' => 1]); + $jobs = $this->client()->jobs->all(['status' => Job::STATUS_SUCCESSFUL, 'limit' => 1]); $this->assertCount(1, $jobs); $this->assertTrue($jobs[0]->isStatusSuccessful()); @@ -112,24 +91,24 @@ public function testCanPageOnlysuccessfulJobs() public function testFilesCanBeDownloadedAndDeleted() { - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => 'https://www.zamzar.com/images/zamzar-logo.png', 'target_format' => 'pdf' ])->waitForCompletion(); $job->downloadTargetFiles($this->targetFilePath); - $this->assertEquals(file_exists($this->targetFilePath . 'zamzar-logo.pdf'), true); + $this->assertTrue(file_exists($this->targetFilePath . $job->target_files[0]->name)); - $job = $job->deleteTargetFiles($this->targetFilePath); + $job = $job->deleteTargetFiles(); $this->expectException(\Zamzar\Exception\InvalidResourceException::class); - $job = $job->downloadTargetFiles($this->targetFilePath); + $job->downloadTargetFiles($this->targetFilePath); } - public function testJobCanBeCancelledandStatusRefreshed() + public function testJobCanBeCancelledAndStatusRefreshed() { - $job = $this->client->jobs->create([ + $job = $this->client()->jobs->create([ 'source_file' => 'https://www.zamzar.com/images/zamzar-logo.png', 'target_format' => 'pdf' ]); diff --git a/tests/LoggingTest.php b/tests/LoggingTest.php index b1f85c7..6538bf1 100644 --- a/tests/LoggingTest.php +++ b/tests/LoggingTest.php @@ -19,33 +19,25 @@ public function testDebugDefaultsToFalse(): void Zamzar::setLogger($logger); - $this->client->account->get(); + $this->client()->account->get(); $this->assertEmpty($logger->log); } public function testRequestsAreLogged(): void { - $client = new ZamzarClient([ - 'api_key' => $this->apiKey(), - 'debug' => true, - ]); - $logger = $this->createLogger(); Zamzar::setLogger($logger); - $client->account->get(); + $this->client(['debug' => true])->account->get(); $this->assertCount(1, $logger->log); } public function testLoggerCanBeChanged(): void { - $client = new ZamzarClient([ - 'api_key' => $this->apiKey(), - 'debug' => true, - ]); + $client = $this->client(['debug' => true]); $logger1 = $this->createLogger(); $logger2 = $this->createLogger(); diff --git a/tests/PagingTest.php b/tests/PagingTest.php index 55cc78f..a39e96e 100644 --- a/tests/PagingTest.php +++ b/tests/PagingTest.php @@ -13,7 +13,7 @@ final class PagingTest extends TestCase public function testCanChangePageSize() { - $formats = $this->client->formats->all(['limit' => 2]); + $formats = $this->client()->formats->all(['limit' => 2]); $this->assertInstanceOf(Collection::class, $formats); $this->assertCount(2, $formats); @@ -21,7 +21,7 @@ public function testCanChangePageSize() public function testCanRequestNextPage() { - $formats = $this->client->formats->all(['limit' => 2]); + $formats = $this->client()->formats->all(['limit' => 2]); $this->assertInstanceOf(Collection::class, $formats); $this->assertCount(2, $formats); @@ -38,7 +38,7 @@ public function testCanRequestNextPage() public function testCanRequestPreviousPage() { - $formats = $this->client->formats->all(['limit' => 2]); + $formats = $this->client()->formats->all(['limit' => 2]); $this->assertInstanceOf(Collection::class, $formats); $this->assertCount(2, $formats); @@ -51,7 +51,7 @@ public function testCanRequestPreviousPage() public function testCanRequestNextPageAndChangeLimit() { - $formats = $this->client->formats->all(['limit' => 2]); + $formats = $this->client()->formats->all(['limit' => 2]); $this->assertInstanceOf(Collection::class, $formats); $this->assertCount(2, $formats); @@ -64,7 +64,7 @@ public function testCanRequestNextPageAndChangeLimit() public function testCanRequestPreviousPageAndChangeLimit() { - $formats = $this->client->formats->all(); + $formats = $this->client()->formats->all(); $formats = $formats->nextPage(); $formats = $formats->previousPage(['limit' => '5']); diff --git a/tests/WithClient.php b/tests/WithClient.php index e4f20be..5970938 100644 --- a/tests/WithClient.php +++ b/tests/WithClient.php @@ -2,20 +2,41 @@ namespace Tests; +use GuzzleHttp\Client as GuzzleClient; use Zamzar\ZamzarClient; trait WithClient { - /** @var ZamzarClient */ - protected $client; + /** @before */ + protected function resetMock() { + // Construct the URL for to reset the mock + // See: https://github.com/zamzar/zamzar-mock/blob/main/README.md + $parts = parse_url($this->baseUrl()); + $url = $parts['scheme'] . "://" . $parts['host'] . ":" . $parts['port'] . "/__admin/scenarios/reset"; - protected function setUp(): void + // Send a POST request to the mock server via Guzzle + (new GuzzleClient())->post($url); + } + + protected function client(array $configOverrides = []): ZamzarClient { - $this->client = new ZamzarClient(['api_key' => $this->apiKey()]); + $configDefaults = [ + 'api_key' => $this->apiKey(), + 'base_url' => $this->baseUrl(), + ]; + + $config = array_merge($configDefaults, $configOverrides); + + return new ZamzarClient($config); } protected function apiKey() { return getenv('ZAMZAR_API_KEY'); } + + protected function baseUrl() + { + return getenv('ZAMZAR_API_URL'); + } } diff --git a/tests/files/target/.gitignore b/tests/files/target/.gitignore new file mode 100644 index 0000000..b720108 --- /dev/null +++ b/tests/files/target/.gitignore @@ -0,0 +1,2 @@ +* +!test.pdf \ No newline at end of file