From 0dc14c88e83169d93ef1e7463eb1147f880a88d5 Mon Sep 17 00:00:00 2001 From: Ramesh Mhetre Date: Tue, 3 Jul 2018 11:49:43 +0000 Subject: [PATCH 1/2] Apply fixes from StyleCI --- phpunit.php | 2 +- src/Client.php | 119 ++++++++++++++++++++------------------ src/ErrorHandler.php | 14 ++--- src/Http/Client.php | 9 ++- tests/ClientTest.php | 132 +++++++++++++++++++++---------------------- tests/TestHelper.php | 5 +- 6 files changed, 143 insertions(+), 138 deletions(-) diff --git a/phpunit.php b/phpunit.php index 2c9fb1d..a0bb447 100644 --- a/phpunit.php +++ b/phpunit.php @@ -1,3 +1,3 @@ client->request('POST', $this->apiUrl . '/b2_create_bucket', [ + $response = $this->client->request('POST', $this->apiUrl.'/b2_create_bucket', [ 'headers' => [ 'Authorization' => $this->authToken, ], 'json' => [ - 'accountId' => $this->accountId, + 'accountId' => $this->accountId, 'bucketName' => $options['BucketName'], - 'bucketType' => $options['BucketType'] - ] + 'bucketType' => $options['BucketType'], + ], ]); return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']); @@ -71,8 +73,10 @@ public function createBucket(array $options) * Updates the type attribute of a bucket by the given ID. * * @param array $options - * @return Bucket + * * @throws ValidationException + * + * @return Bucket */ public function updateBucket(array $options) { @@ -86,15 +90,15 @@ public function updateBucket(array $options) $options['BucketId'] = $this->getBucketIdFromName($options['BucketName']); } - $response = $this->client->request('POST', $this->apiUrl . '/b2_update_bucket', [ + $response = $this->client->request('POST', $this->apiUrl.'/b2_update_bucket', [ 'headers' => [ 'Authorization' => $this->authToken, ], 'json' => [ - 'accountId' => $this->accountId, - 'bucketId' => $options['BucketId'], - 'bucketType' => $options['BucketType'] - ] + 'accountId' => $this->accountId, + 'bucketId' => $options['BucketId'], + 'bucketType' => $options['BucketType'], + ], ]); return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']); @@ -109,13 +113,13 @@ public function listBuckets() { $buckets = []; - $response = $this->client->request('POST', $this->apiUrl . '/b2_list_buckets', [ + $response = $this->client->request('POST', $this->apiUrl.'/b2_list_buckets', [ 'headers' => [ 'Authorization' => $this->authToken, ], 'json' => [ - 'accountId' => $this->accountId - ] + 'accountId' => $this->accountId, + ], ]); foreach ($response['buckets'] as $bucket) { @@ -129,6 +133,7 @@ public function listBuckets() * Deletes the bucket identified by its ID. * * @param array $options + * * @return bool */ public function deleteBucket(array $options) @@ -137,14 +142,14 @@ public function deleteBucket(array $options) $options['BucketId'] = $this->getBucketIdFromName($options['BucketName']); } - $this->client->request('POST', $this->apiUrl . '/b2_delete_bucket', [ + $this->client->request('POST', $this->apiUrl.'/b2_delete_bucket', [ 'headers' => [ - 'Authorization' => $this->authToken + 'Authorization' => $this->authToken, ], 'json' => [ 'accountId' => $this->accountId, - 'bucketId' => $options['BucketId'] - ] + 'bucketId' => $options['BucketId'], + ], ]); return true; @@ -154,6 +159,7 @@ public function deleteBucket(array $options) * Uploads a file to a bucket and returns a File object. * * @param array $options + * * @return File */ public function upload(array $options) @@ -168,13 +174,13 @@ public function upload(array $options) } // Retrieve the URL that we should be uploading to. - $response = $this->client->request('POST', $this->apiUrl . '/b2_get_upload_url', [ + $response = $this->client->request('POST', $this->apiUrl.'/b2_get_upload_url', [ 'headers' => [ - 'Authorization' => $this->authToken + 'Authorization' => $this->authToken, ], 'json' => [ - 'bucketId' => $options['BucketId'] - ] + 'bucketId' => $options['BucketId'], + ], ]); $uploadEndpoint = $response['uploadUrl']; @@ -207,14 +213,14 @@ public function upload(array $options) $response = $this->client->request('POST', $uploadEndpoint, [ 'headers' => [ - 'Authorization' => $uploadAuthToken, - 'Content-Type' => $options['FileContentType'], - 'Content-Length' => $size, - 'X-Bz-File-Name' => $options['FileName'], - 'X-Bz-Content-Sha1' => $hash, - 'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified'] + 'Authorization' => $uploadAuthToken, + 'Content-Type' => $options['FileContentType'], + 'Content-Length' => $size, + 'X-Bz-File-Name' => $options['FileName'], + 'X-Bz-Content-Sha1' => $hash, + 'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified'], ], - 'body' => $options['Body'] + 'body' => $options['Body'], ]); return new File( @@ -231,6 +237,7 @@ public function upload(array $options) * Download a file from a B2 bucket. * * @param array $options + * * @return bool|mixed|string */ public function download(array $options) @@ -238,14 +245,14 @@ public function download(array $options) $requestUrl = null; $requestOptions = [ 'headers' => [ - 'Authorization' => $this->authToken + 'Authorization' => $this->authToken, ], - 'sink' => isset($options['SaveAs']) ? $options['SaveAs'] : null + 'sink' => isset($options['SaveAs']) ? $options['SaveAs'] : null, ]; if (isset($options['FileId'])) { $requestOptions['query'] = ['fileId' => $options['FileId']]; - $requestUrl = $this->downloadUrl . '/b2api/v1/b2_download_file_by_id'; + $requestUrl = $this->downloadUrl.'/b2api/v1/b2_download_file_by_id'; } else { if (!isset($options['BucketName']) && isset($options['BucketId'])) { $options['BucketName'] = $this->getBucketNameFromId($options['BucketId']); @@ -263,6 +270,7 @@ public function download(array $options) * Retrieve a collection of File objects representing the files stored inside a bucket. * * @param array $options + * * @return array */ public function listFiles(array $options) @@ -285,15 +293,15 @@ public function listFiles(array $options) // B2 returns, at most, 1000 files per "page". Loop through the pages and compile an array of File objects. while (true) { - $response = $this->client->request('POST', $this->apiUrl . '/b2_list_file_names', [ + $response = $this->client->request('POST', $this->apiUrl.'/b2_list_file_names', [ 'headers' => [ - 'Authorization' => $this->authToken + 'Authorization' => $this->authToken, ], 'json' => [ - 'bucketId' => $options['BucketId'], + 'bucketId' => $options['BucketId'], 'startFileName' => $nextFileName, - 'maxFileCount' => $maxFileCount, - ] + 'maxFileCount' => $maxFileCount, + ], ]); foreach ($response['files'] as $file) { @@ -318,7 +326,8 @@ public function listFiles(array $options) * Test whether a file exists in B2 for the given bucket. * * @param array $options - * @return boolean + * + * @return bool */ public function fileExists(array $options) { @@ -327,12 +336,13 @@ public function fileExists(array $options) return !empty($files); } - /** * Returns a single File object representing a file stored on B2. * * @param array $options + * * @throws NotFoundException If no file id was provided and BucketName + FileName does not resolve to a file, a NotFoundException is thrown. + * * @return File */ public function getFile(array $options) @@ -345,13 +355,13 @@ public function getFile(array $options) } } - $response = $this->client->request('POST', $this->apiUrl . '/b2_get_file_info', [ + $response = $this->client->request('POST', $this->apiUrl.'/b2_get_file_info', [ 'headers' => [ - 'Authorization' => $this->authToken + 'Authorization' => $this->authToken, ], 'json' => [ - 'fileId' => $options['FileId'] - ] + 'fileId' => $options['FileId'], + ], ]); return new File( @@ -371,6 +381,7 @@ public function getFile(array $options) * Deletes the file identified by ID from Backblaze B2. * * @param array $options + * * @return bool */ public function deleteFile(array $options) @@ -387,14 +398,14 @@ public function deleteFile(array $options) $options['FileId'] = $file->getId(); } - $this->client->request('POST', $this->apiUrl . '/b2_delete_file_version', [ + $this->client->request('POST', $this->apiUrl.'/b2_delete_file_version', [ 'headers' => [ - 'Authorization' => $this->authToken + 'Authorization' => $this->authToken, ], 'json' => [ 'fileName' => $options['FileName'], - 'fileId' => $options['FileId'] - ] + 'fileId' => $options['FileId'], + ], ]); return true; @@ -408,11 +419,11 @@ public function deleteFile(array $options) protected function authorizeAccount() { $response = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [ - 'auth' => [$this->accountId, $this->applicationKey] + 'auth' => [$this->accountId, $this->applicationKey], ]); $this->authToken = $response['authorizationToken']; - $this->apiUrl = $response['apiUrl'] . '/b2api/v1'; + $this->apiUrl = $response['apiUrl'].'/b2api/v1'; $this->downloadUrl = $response['downloadUrl']; } @@ -420,6 +431,7 @@ protected function authorizeAccount() * Maps the provided bucket name to the appropriate bucket ID. * * @param $name + * * @return null */ protected function getBucketIdFromName($name) @@ -431,14 +443,13 @@ protected function getBucketIdFromName($name) return $bucket->getId(); } } - - return null; } /** * Maps the provided bucket ID to the appropriate bucket name. * * @param $id + * * @return null */ protected function getBucketNameFromId($id) @@ -450,15 +461,13 @@ protected function getBucketNameFromId($id) return $bucket->getName(); } } - - return null; } protected function getFileIdFromBucketAndFileName($bucketName, $fileName) { $files = $this->listFiles([ 'BucketName' => $bucketName, - 'FileName' => $fileName, + 'FileName' => $fileName, ]); foreach ($files as $file) { @@ -466,7 +475,5 @@ protected function getFileIdFromBucketAndFileName($bucketName, $fileName) return $file->getId(); } } - - return null; } } diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index c023887..2ce0b76 100755 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -14,12 +14,12 @@ class ErrorHandler { protected static $mappings = [ - 'bad_json' => BadJsonException::class, - 'bad_value' => BadValueException::class, - 'duplicate_bucket_name' => BucketAlreadyExistsException::class, - 'not_found' => NotFoundException::class, - 'file_not_present' => FileNotPresentException::class, - 'cannot_delete_non_empty_bucket' => BucketNotEmptyException::class + 'bad_json' => BadJsonException::class, + 'bad_value' => BadValueException::class, + 'duplicate_bucket_name' => BucketAlreadyExistsException::class, + 'not_found' => NotFoundException::class, + 'file_not_present' => FileNotPresentException::class, + 'cannot_delete_non_empty_bucket' => BucketNotEmptyException::class, ]; public static function handleErrorResponse(Response $response) @@ -33,6 +33,6 @@ public static function handleErrorResponse(Response $response) $exceptionClass = B2Exception::class; } - throw new $exceptionClass('Received error from B2: ' . $responseJson['message']); + throw new $exceptionClass('Received error from B2: '.$responseJson['message']); } } diff --git a/src/Http/Client.php b/src/Http/Client.php index 4b0cd89..7a30aae 100755 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -7,8 +7,6 @@ /** * Client wrapper around Guzzle. - * - * @package BackblazeB2\Http */ class Client extends GuzzleClient { @@ -16,9 +14,10 @@ class Client extends GuzzleClient * Sends a response to the B2 API, automatically handling decoding JSON and errors. * * @param string $method - * @param null $uri - * @param array $options - * @param bool $asJson + * @param null $uri + * @param array $options + * @param bool $asJson + * * @return mixed|string */ public function request($method, $uri = null, array $options = [], $asJson = true) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 86c2646..e0499cb 100755 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -22,7 +22,7 @@ public function testCreatePublicBucket() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'create_bucket_public.json') + $this->buildResponseFromStub(200, [], 'create_bucket_public.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -30,7 +30,7 @@ public function testCreatePublicBucket() // Test that we get a public bucket back after creation $bucket = $client->createBucket([ 'BucketName' => 'Test bucket', - 'BucketType' => Bucket::TYPE_PUBLIC + 'BucketType' => Bucket::TYPE_PUBLIC, ]); $this->assertInstanceOf(Bucket::class, $bucket); $this->assertEquals('Test bucket', $bucket->getName()); @@ -41,7 +41,7 @@ public function testCreatePrivateBucket() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'create_bucket_private.json') + $this->buildResponseFromStub(200, [], 'create_bucket_private.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -49,7 +49,7 @@ public function testCreatePrivateBucket() // Test that we get a private bucket back after creation $bucket = $client->createBucket([ 'BucketName' => 'Test bucket', - 'BucketType' => Bucket::TYPE_PRIVATE + 'BucketType' => Bucket::TYPE_PRIVATE, ]); $this->assertInstanceOf(Bucket::class, $bucket); $this->assertEquals('Test bucket', $bucket->getName()); @@ -62,13 +62,13 @@ public function testBucketAlreadyExistsExceptionThrown() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'create_bucket_exists.json') + $this->buildResponseFromStub(400, [], 'create_bucket_exists.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->createBucket([ 'BucketName' => 'I already exist', - 'BucketType' => Bucket::TYPE_PRIVATE + 'BucketType' => Bucket::TYPE_PRIVATE, ]); } @@ -77,13 +77,13 @@ public function testInvalidBucketTypeThrowsException() $this->setExpectedException(ValidationException::class); $guzzle = $this->buildGuzzleFromResponses([ - $this->buildResponseFromStub(200, [], 'authorize_account.json') + $this->buildResponseFromStub(200, [], 'authorize_account.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->createBucket([ 'BucketName' => 'Test bucket', - 'BucketType' => 'i am not valid' + 'BucketType' => 'i am not valid', ]); } @@ -91,14 +91,14 @@ public function testUpdateBucketToPrivate() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'update_bucket_to_private.json') + $this->buildResponseFromStub(200, [], 'update_bucket_to_private.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $bucket = $client->updateBucket([ - 'BucketId' => 'bucketId', - 'BucketType' => Bucket::TYPE_PRIVATE + 'BucketId' => 'bucketId', + 'BucketType' => Bucket::TYPE_PRIVATE, ]); $this->assertInstanceOf(Bucket::class, $bucket); @@ -110,14 +110,14 @@ public function testUpdateBucketToPublic() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'update_bucket_to_public.json') + $this->buildResponseFromStub(200, [], 'update_bucket_to_public.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $bucket = $client->updateBucket([ - 'BucketId' => 'bucketId', - 'BucketType' => Bucket::TYPE_PUBLIC + 'BucketId' => 'bucketId', + 'BucketType' => Bucket::TYPE_PUBLIC, ]); $this->assertInstanceOf(Bucket::class, $bucket); @@ -129,7 +129,7 @@ public function testList3Buckets() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'list_buckets_3.json') + $this->buildResponseFromStub(200, [], 'list_buckets_3.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -144,7 +144,7 @@ public function testEmptyArrayWithNoBuckets() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'list_buckets_0.json') + $this->buildResponseFromStub(200, [], 'list_buckets_0.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -158,13 +158,13 @@ public function testDeleteBucket() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'delete_bucket.json') + $this->buildResponseFromStub(200, [], 'delete_bucket.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $this->assertTrue($client->deleteBucket([ - 'BucketId' => 'bucketId' + 'BucketId' => 'bucketId', ])); } @@ -174,13 +174,13 @@ public function testBadJsonThrownDeletingNonExistentBucket() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'delete_bucket_non_existent.json') + $this->buildResponseFromStub(400, [], 'delete_bucket_non_existent.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->deleteBucket([ - 'BucketId' => 'bucketId' + 'BucketId' => 'bucketId', ]); } @@ -190,13 +190,13 @@ public function testBucketNotEmptyThrownDeletingNonEmptyBucket() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'bucket_not_empty.json') + $this->buildResponseFromStub(400, [], 'bucket_not_empty.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->deleteBucket([ - 'BucketId' => 'bucketId' + 'BucketId' => 'bucketId', ]); } @@ -207,7 +207,7 @@ public function testUploadingResource() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), $this->buildResponseFromStub(200, [], 'get_upload_url.json'), - $this->buildResponseFromStub(200, [], 'upload.json') + $this->buildResponseFromStub(200, [], 'upload.json'), ], $history); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -221,7 +221,7 @@ public function testUploadingResource() $file = $client->upload([ 'BucketId' => 'bucketId', 'FileName' => 'test.txt', - 'Body' => $resource + 'Body' => $resource, ]); $this->assertInstanceOf(File::class, $file); @@ -245,7 +245,7 @@ public function testUploadingString() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), $this->buildResponseFromStub(200, [], 'get_upload_url.json'), - $this->buildResponseFromStub(200, [], 'upload.json') + $this->buildResponseFromStub(200, [], 'upload.json'), ], $history); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -255,7 +255,7 @@ public function testUploadingString() $file = $client->upload([ 'BucketId' => 'bucketId', 'FileName' => 'test.txt', - 'Body' => $content + 'Body' => $content, ]); $this->assertInstanceOf(File::class, $file); @@ -279,7 +279,7 @@ public function testUploadingWithCustomContentTypeAndLastModified() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), $this->buildResponseFromStub(200, [], 'get_upload_url.json'), - $this->buildResponseFromStub(200, [], 'upload.json') + $this->buildResponseFromStub(200, [], 'upload.json'), ], $history); $client = new Client('testId', 'testKey', ['client' => $guzzle]); @@ -289,11 +289,11 @@ public function testUploadingWithCustomContentTypeAndLastModified() $contentType = 'text/plain'; $file = $client->upload([ - 'BucketId' => 'bucketId', - 'FileName' => 'test.txt', - 'Body' => 'Test file content', - 'FileContentType' => $contentType, - 'FileLastModified' => $lastModified + 'BucketId' => 'bucketId', + 'FileName' => 'test.txt', + 'Body' => 'Test file content', + 'FileContentType' => $contentType, + 'FileLastModified' => $lastModified, ]); $this->assertInstanceOf(File::class, $file); @@ -309,13 +309,13 @@ public function testDownloadByIdWithoutSavePath() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'download_content') + $this->buildResponseFromStub(200, [], 'download_content'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $fileContent = $client->download([ - 'FileId' => 'fileId' + 'FileId' => 'fileId', ]); $this->assertEquals($fileContent, 'The quick brown fox jumps over the lazy dog'); @@ -325,20 +325,20 @@ public function testDownloadByIdWithSavePath() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'download_content') + $this->buildResponseFromStub(200, [], 'download_content'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->download([ 'FileId' => 'fileId', - 'SaveAs' => __DIR__ . '/test.txt' + 'SaveAs' => __DIR__.'/test.txt', ]); - $this->assertFileExists(__DIR__ . '/test.txt'); - $this->assertEquals('The quick brown fox jumps over the lazy dog', file_get_contents(__DIR__ . '/test.txt')); + $this->assertFileExists(__DIR__.'/test.txt'); + $this->assertEquals('The quick brown fox jumps over the lazy dog', file_get_contents(__DIR__.'/test.txt')); - unlink(__DIR__ . '/test.txt'); + unlink(__DIR__.'/test.txt'); } public function testDownloadingByIncorrectIdThrowsException() @@ -347,13 +347,13 @@ public function testDownloadingByIncorrectIdThrowsException() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'download_by_incorrect_id.json') + $this->buildResponseFromStub(400, [], 'download_by_incorrect_id.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->download([ - 'FileId' => 'incorrect' + 'FileId' => 'incorrect', ]); } @@ -361,14 +361,14 @@ public function testDownloadByPathWithoutSavePath() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'download_content') + $this->buildResponseFromStub(200, [], 'download_content'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $fileContent = $client->download([ 'BucketName' => 'test-bucket', - 'FileName' => 'test.txt' + 'FileName' => 'test.txt', ]); $this->assertEquals($fileContent, 'The quick brown fox jumps over the lazy dog'); @@ -378,21 +378,21 @@ public function testDownloadByPathWithSavePath() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'download_content') + $this->buildResponseFromStub(200, [], 'download_content'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->download([ 'BucketName' => 'test-bucket', - 'FileName' => 'test.txt', - 'SaveAs' => __DIR__ . '/test.txt' + 'FileName' => 'test.txt', + 'SaveAs' => __DIR__.'/test.txt', ]); - $this->assertFileExists(__DIR__ . '/test.txt'); - $this->assertEquals('The quick brown fox jumps over the lazy dog', file_get_contents(__DIR__ . '/test.txt')); + $this->assertFileExists(__DIR__.'/test.txt'); + $this->assertEquals('The quick brown fox jumps over the lazy dog', file_get_contents(__DIR__.'/test.txt')); - unlink(__DIR__ . '/test.txt'); + unlink(__DIR__.'/test.txt'); } public function testDownloadingByIncorrectPathThrowsException() @@ -401,14 +401,14 @@ public function testDownloadingByIncorrectPathThrowsException() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'download_by_incorrect_path.json') + $this->buildResponseFromStub(400, [], 'download_by_incorrect_path.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->download([ 'BucketName' => 'test-bucket', - 'FileName' => 'path/to/incorrect/file.txt' + 'FileName' => 'path/to/incorrect/file.txt', ]); } @@ -417,13 +417,13 @@ public function testListFilesHandlesMultiplePages() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), $this->buildResponseFromStub(200, [], 'list_files_page1.json'), - $this->buildResponseFromStub(200, [], 'list_files_page2.json') + $this->buildResponseFromStub(200, [], 'list_files_page2.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $files = $client->listFiles([ - 'BucketId' => 'bucketId' + 'BucketId' => 'bucketId', ]); $this->assertInternalType('array', $files); @@ -435,13 +435,13 @@ public function testListFilesReturnsEmptyArrayWithNoFiles() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'list_files_empty.json') + $this->buildResponseFromStub(200, [], 'list_files_empty.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $files = $client->listFiles([ - 'BucketId' => 'bucketId' + 'BucketId' => 'bucketId', ]); $this->assertInternalType('array', $files); @@ -452,13 +452,13 @@ public function testGetFile() { $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(200, [], 'get_file.json') + $this->buildResponseFromStub(200, [], 'get_file.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $file = $client->getFile([ - 'FileId' => 'fileId' + 'FileId' => 'fileId', ]); $this->assertInstanceOf(File::class, $file); @@ -470,13 +470,13 @@ public function testGettingNonExistentFileThrowsException() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'get_file_non_existent.json') + $this->buildResponseFromStub(400, [], 'get_file_non_existent.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $client->getFile([ - 'FileId' => 'fileId' + 'FileId' => 'fileId', ]); } @@ -485,13 +485,13 @@ public function testDeleteFile() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), $this->buildResponseFromStub(200, [], 'get_file.json'), - $this->buildResponseFromStub(200, [], 'delete_file.json') + $this->buildResponseFromStub(200, [], 'delete_file.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $this->assertTrue($client->deleteFile([ - 'FileId' => 'fileId' + 'FileId' => 'fileId', ])); } @@ -500,13 +500,13 @@ public function testDeleteFileRetrievesFileNameWhenNotProvided() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), $this->buildResponseFromStub(200, [], 'get_file.json'), - $this->buildResponseFromStub(200, [], 'delete_file.json') + $this->buildResponseFromStub(200, [], 'delete_file.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $this->assertTrue($client->deleteFile([ - 'FileId' => 'fileId' + 'FileId' => 'fileId', ])); } @@ -516,14 +516,14 @@ public function testDeletingNonExistentFileThrowsException() $guzzle = $this->buildGuzzleFromResponses([ $this->buildResponseFromStub(200, [], 'authorize_account.json'), - $this->buildResponseFromStub(400, [], 'delete_file_non_existent.json') + $this->buildResponseFromStub(400, [], 'delete_file_non_existent.json'), ]); $client = new Client('testId', 'testKey', ['client' => $guzzle]); $this->assertTrue($client->deleteFile([ - 'FileId' => 'fileId', - 'FileName' => 'fileName' + 'FileId' => 'fileId', + 'FileName' => 'fileName', ])); } } diff --git a/tests/TestHelper.php b/tests/TestHelper.php index 3e31837..f578ded 100755 --- a/tests/TestHelper.php +++ b/tests/TestHelper.php @@ -7,7 +7,6 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; - trait TestHelper { protected function buildGuzzleFromResponses(array $responses, $history = null) @@ -22,9 +21,9 @@ protected function buildGuzzleFromResponses(array $responses, $history = null) return new HttpClient(['handler' => $handler]); } - protected function buildResponseFromStub($statusCode, array $headers = [], $responseFile) + protected function buildResponseFromStub($statusCode, array $headers, $responseFile) { - $response = file_get_contents(dirname(__FILE__) . '/responses/' . $responseFile); + $response = file_get_contents(dirname(__FILE__).'/responses/'.$responseFile); return new Response($statusCode, $headers, $response); } From 4cd1b89a5c1b6a0906f8decb7d6f5dd5e9c93a94 Mon Sep 17 00:00:00 2001 From: Ramesh Mhetre <3095445+mhetreramesh@users.noreply.github.com> Date: Fri, 24 Aug 2018 23:22:13 +0200 Subject: [PATCH 2/2] ApplicationKey notice added --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f0a61ab..480319b 100755 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ use BackblazeB2\Bucket; $client = new Client('accountId', 'applicationKey'); ``` +## *ApplicationKey is not supported yet, please use MasterKey only* + #### Returns a bucket details ``` php $bucket = $client->createBucket([ @@ -138,4 +140,4 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio [link-code-quality]: https://scrutinizer-ci.com/g/gliterd/backblaze-b2 [link-downloads]: https://packagist.org/packages/gliterd/backblaze-b2 [link-author]: https://github.com/gliterd -[link-contributors]: ../../contributors \ No newline at end of file +[link-contributors]: ../../contributors