Skip to content

Commit

Permalink
makes tests compatible with mock
Browse files Browse the repository at this point in the history
  • Loading branch information
louismrose committed Mar 21, 2024
1 parent f1f244e commit 883d7df
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 81 deletions.
3 changes: 2 additions & 1 deletion lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion lib/HttpClient/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions tests/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ 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'
]);
}

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);
Expand Down
16 changes: 8 additions & 8 deletions tests/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ 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);
}

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/');
Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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'
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/FormatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
10 changes: 5 additions & 5 deletions tests/ImportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ 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);
}

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());
Expand Down
55 changes: 17 additions & 38 deletions tests/JobsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -55,51 +55,30 @@ 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();

$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());
Expand All @@ -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'
]);
Expand Down
14 changes: 3 additions & 11 deletions tests/LoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions tests/PagingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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);
}

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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']);

Expand Down
Loading

0 comments on commit 883d7df

Please sign in to comment.