Skip to content

Commit 3ac804e

Browse files
author
brecht.vermeersch
committed
add tests
1 parent 88bd139 commit 3ac804e

File tree

7 files changed

+143
-14
lines changed

7 files changed

+143
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
composer.lock
3-
.idea
3+
.idea
4+
.phpunit.cache

composer.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"AzureOss\\LaravelAzureStorageBlob\\": "src/"
99
}
1010
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"AzureOss\\LaravelAzureStorageBlob\\Tests": "tests/"
14+
}
15+
},
1116
"authors": [
1217
{
1318
"name": "Brecht Vermeersch",
@@ -21,7 +26,11 @@
2126
"azure-oss/storage-blob-flysystem": "^1.1"
2227
},
2328
"require-dev": {
24-
"laravel/pint": "^1.17"
29+
"laravel/pint": "^1.17",
30+
"orchestra/testbench": "^9.2",
31+
"phpstan/phpstan": "^1.11",
32+
"phpstan/phpstan-deprecation-rules": "^1.2",
33+
"phpstan/phpstan-strict-rules": "^1.6"
2534
},
2635
"extra": {
2736
"laravel": {

phpstan.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
includes:
6+
- vendor/phpstan/phpstan-strict-rules/rules.neon
7+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon

phpunit.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
shortenArraysForExportThreshold="10"
8+
beStrictAboutOutputDuringTests="true"
9+
failOnRisky="true"
10+
failOnWarning="true">
11+
<php>
12+
<env name="AZURE_STORAGE_CONNECTION_STRING" value="UseDevelopmentStorage=true"/>
13+
<env name="AZURE_STORAGE_CONTAINER" value="testing"/>
14+
</php>
15+
16+
<testsuites>
17+
<testsuite name="default">
18+
<directory>tests</directory>
19+
</testsuite>
20+
</testsuites>
21+
22+
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
23+
<include>
24+
<directory>src</directory>
25+
</include>
26+
</source>
27+
</phpunit>

src/AzureStorageBlobAdapter.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010

1111
/**
1212
* @internal
13+
*
14+
* @property AzureBlobStorageAdapter $adapter
1315
*/
1416
final class AzureStorageBlobAdapter extends FilesystemAdapter
1517
{
18+
/**
19+
* @param array{connection_string: string, container: string, prefix?: string} $config
20+
*/
1621
public function __construct(array $config)
1722
{
1823
$serviceClient = BlobServiceClient::fromConnectionString($config['connection_string']);
1924
$containerClient = $serviceClient->getContainerClient($config['container']);
20-
$adapter = new AzureBlobStorageAdapter($containerClient, $config['prefix'] ?? "");
25+
$adapter = new AzureBlobStorageAdapter($containerClient, $config['prefix'] ?? '');
2126

2227
parent::__construct(
2328
new Filesystem($adapter, $config),
@@ -26,17 +31,13 @@ public function __construct(array $config)
2631
);
2732
}
2833

34+
/** @phpstan-ignore-next-line */
2935
public function temporaryUrl($path, $expiration, array $options = [])
3036
{
31-
$options["permissions"] = $options["permissions"] ?? "r";
32-
33-
return $this->adapter->temporaryUrl($path, $expiration, new Config($options));
34-
}
35-
36-
public function temporaryUploadUrl($path, $expiration, array $options = [])
37-
{
38-
$options["permissions"] = $options["permissions"] ?? "w";
39-
40-
return $this->adapter->temporaryUrl($path, $expiration, new Config($options));
37+
return $this->adapter->temporaryUrl(
38+
$path,
39+
$expiration,
40+
new Config(['permissions' => 'r'])
41+
);
4142
}
42-
}
43+
}

src/AzureStorageBlobServiceProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ final class AzureStorageBlobServiceProvider extends ServiceProvider
1515
public function boot(): void
1616
{
1717
Storage::extend('azure-storage-blob', function (Application $app, array $config): FilesystemAdapter {
18+
if (! isset($config['connection_string']) || ! is_string($config['connection_string'])) {
19+
throw new \InvalidArgumentException('The [connection_string] must be a string in the disk configuration.');
20+
}
21+
22+
if (! isset($config['container']) && ! is_string($config['container'])) {
23+
throw new \InvalidArgumentException('The [container] must be a string in the disk configuration.');
24+
}
25+
26+
if (isset($config['prefix']) && ! is_string($config['prefix'])) {
27+
throw new \InvalidArgumentException('The [prefix] must be a string in the disk configuration.');
28+
}
29+
1830
return new AzureStorageBlobAdapter($config);
1931
});
2032
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace AzureOss\LaravelAzureStorageBlob\Tests;
4+
5+
use AzureOss\LaravelAzureStorageBlob\AzureStorageBlobAdapter;
6+
use AzureOss\LaravelAzureStorageBlob\AzureStorageBlobServiceProvider;
7+
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Facades\Storage;
9+
use Orchestra\Testbench\TestCase;
10+
use PHPUnit\Framework\Attributes\Test;
11+
12+
class AzureStorageBlobAdapterTest extends TestCase
13+
{
14+
protected function getPackageProviders($app): array
15+
{
16+
return [AzureStorageBlobServiceProvider::class];
17+
}
18+
19+
protected function getEnvironmentSetUp($app): void
20+
{
21+
$app['config']->set('filesystems.disks.azure', [
22+
'driver' => 'azure-storage-blob',
23+
'connection_string' => env('AZURE_STORAGE_CONNECTION_STRING'),
24+
'container' => env('AZURE_STORAGE_CONTAINER'),
25+
]);
26+
}
27+
28+
#[Test]
29+
public function it_resolves_from_manager(): void
30+
{
31+
self::assertInstanceOf(AzureStorageBlobAdapter::class, Storage::disk('azure'));
32+
}
33+
34+
#[Test]
35+
public function driver_works(): void
36+
{
37+
$driver = Storage::disk('azure');
38+
39+
// cleanup from previous test runs
40+
$driver->deleteDirectory('');
41+
42+
self::assertFalse($driver->exists('file.text'));
43+
44+
$driver->put('file.txt', 'content');
45+
46+
self::assertTrue($driver->exists('file.txt'));
47+
48+
self::assertEquals(
49+
'content',
50+
$driver->get('file.txt')
51+
);
52+
self::assertEquals(
53+
'content',
54+
Http::get($driver->temporaryUrl('file.txt', now()->addMinute()))->body()
55+
);
56+
57+
$driver->copy('file.txt', 'file2.txt');
58+
59+
self::assertTrue($driver->exists('file2.txt'));
60+
61+
$driver->move('file2.txt', 'file3.txt');
62+
63+
self::assertFalse($driver->exists('file2.txt'));
64+
self::assertTrue($driver->exists('file3.txt'));
65+
66+
self::assertCount(2, $driver->allFiles());
67+
68+
$driver->deleteDirectory('');
69+
70+
self::assertCount(0, $driver->allFiles());
71+
}
72+
}

0 commit comments

Comments
 (0)