Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dam-bal committed Apr 19, 2024
1 parent c8c1cc2 commit a0e7a97
Showing 1 changed file with 225 additions and 0 deletions.
225 changes: 225 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\VercelBlobPhp;

use DateTime;
use Generator;
use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -10,6 +11,8 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use VercelBlobPhp\Client;
use VercelBlobPhp\CommonCreateBlobOptions;
use VercelBlobPhp\CopyBlobResult;
use VercelBlobPhp\Exception\BlobAccessException;
use VercelBlobPhp\Exception\BlobException;
use VercelBlobPhp\Exception\BlobNotFoundException;
Expand All @@ -18,9 +21,16 @@
use VercelBlobPhp\Exception\BlobStoreNotFoundException;
use VercelBlobPhp\Exception\BlobStoreSuspendedException;
use VercelBlobPhp\Exception\BlobUnknownException;
use VercelBlobPhp\HeadBlobResult;
use VercelBlobPhp\PutBlobResult;

class ClientTest extends TestCase
{
protected function setUp(): void
{
putenv("VERCEL_BLOB_API_URL=blob");
}

public static function requestThrowsCorrectExceptionBasedOnErrorCode(): Generator
{
yield [
Expand Down Expand Up @@ -124,4 +134,219 @@ public function testRequest(): void

$this->assertEquals($responseMock, $response);
}

public static function putDataProvider(): Generator
{
yield [
null,
[]
];

yield [
new CommonCreateBlobOptions(addRandomSuffix: true),
[
'x-random-suffix' => true
]
];

yield [
new CommonCreateBlobOptions(contentType: 'application/json'),
[
'x-content-type' => 'application/json'
]
];

yield [
new CommonCreateBlobOptions(cacheControlMaxAge: 123),
[
'x-cache-control-max-age' => 123
]
];
}

#[DataProvider('putDataProvider')]
public function testPut(?CommonCreateBlobOptions $options, array $expectedHeaders): void
{
$sut = new Client('my-token');

$sut->setClient(
$this->mockClient(
'blob/hello-world.txt',
'PUT',
[
'body' => 'hello world',
'headers' => $expectedHeaders,
],
[
'url' => 'url',
'downloadUrl' => 'downloadUrl',
'pathname' => 'pathname',
'contentType' => 'contentType',
'contentDisposition' => 'contentDisposition',
]
)
);

$this->assertEquals(
new PutBlobResult(
'url',
'downloadUrl',
'pathname',
'contentType',
'contentDisposition'
),
$sut->put('hello-world.txt', 'hello world', $options)
);
}

public static function copyDataProvider(): Generator
{
yield [
null,
[]
];

yield [
new CommonCreateBlobOptions(addRandomSuffix: true),
[
'x-random-suffix' => true
]
];

yield [
new CommonCreateBlobOptions(contentType: 'application/json'),
[
'x-content-type' => 'application/json'
]
];

yield [
new CommonCreateBlobOptions(cacheControlMaxAge: 123),
[
'x-cache-control-max-age' => 123
]
];
}

#[DataProvider('copyDataProvider')]
public function testCopy(?CommonCreateBlobOptions $options, array $expectedHeaders): void
{
$sut = new Client('my-token');

$sut->setClient(
$this->mockClient(
'blob/hello-world.txt?fromUrl=test-url',
'PUT',
[
'headers' => $expectedHeaders,
],
[
'url' => 'url',
'downloadUrl' => 'downloadUrl',
'pathname' => 'pathname',
'contentType' => 'contentType',
'contentDisposition' => 'contentDisposition',
]
)
);

$this->assertEquals(
new CopyBlobResult(
'url',
'downloadUrl',
'pathname',
'contentType',
'contentDisposition'
),
$sut->copy('test-url', 'hello-world.txt', $options)
);
}

public function testDel(): void
{
$sut = new Client('my-token');

$sut->setClient(
$this->mockClient(
'blob/delete',
'POST',
[
'json' => [
'urls' => [
'url1',
'url2',
]
]
],
[]
)
);

$sut->del(['url1', 'url2']);
}

public function testHead(): void
{
$sut = new Client('my-token');

$sut->setClient(
$this->mockClient(
'blob?url=test-url',
'GET',
[],
[
'url' => 'url',
'downloadUrl' => 'downloadUrl',
'size' => 1,
'uploadedAt' => '2024-01-01 10:00:00',
'pathname' => 'pathname',
'contentType' => 'contentType',
'contentDisposition' => 'contentDisposition',
'cacheControl' => 'cacheControl'
]
)
);

$this->assertEquals(
new HeadBlobResult(
'url',
'downloadUrl',
1,
new DateTime('2024-01-01 10:00:00'),
'pathname',
'contentType',
'contentDisposition',
'cacheControl'
),
$sut->head('test-url')
);
}

private function mockClient(
string $url,
string $method,
array $options,
array $response
) {
$clientMock = $this->createMock(\GuzzleHttp\Client::class);

$responseMock = $this->createMock(ResponseInterface::class);

$bodyMock = $this->createMock(StreamInterface::class);
$bodyMock
->method('getContents')
->willReturn(json_encode($response));

$responseMock
->method('getBody')
->willReturn($bodyMock);

$clientMock
->expects(self::once())
->method('request')
->with($method, $url, $options)
->willReturn($responseMock);

return $clientMock;
}
}

0 comments on commit a0e7a97

Please sign in to comment.