Skip to content

Commit b2c612e

Browse files
committed
implement tests
1 parent df50e8a commit b2c612e

File tree

6 files changed

+431
-42
lines changed

6 files changed

+431
-42
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"orchestra/testbench": "^9.9",
3333
"pestphp/pest": "^2.36",
3434
"sempro/phpunit-pretty-print": "^1.2",
35-
"laravel/pint": "^1.20"
35+
"laravel/pint": "^1.20",
36+
"phpspec/prophecy-phpunit": "^2.3"
3637
},
3738
"autoload": {
3839
"psr-4": {

composer.lock

Lines changed: 196 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CloudinaryServiceProvider.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ class CloudinaryServiceProvider extends ServiceProvider
1212
public function boot(): void
1313
{
1414
$this->app['filesystem']->extend('cloudinary', function ($app, $config) {
15-
$adapter = new CloudinaryStorageAdapter($config);
15+
if (isset($config['url'])) {
16+
$cloudinary = new Cloudinary($config['url']);
17+
} else {
18+
$cloudinary = new Cloudinary([
19+
'cloud' => [
20+
'cloud_name' => $config['cloud'],
21+
'api_key' => $config['key'],
22+
'api_secret' => $config['secret'],
23+
],
24+
'url' => [
25+
'secure' => $config['secure'] ?? false,
26+
],
27+
]);
28+
}
29+
30+
$adapter = new CloudinaryStorageAdapter($cloudinary, $config['prefix'] ?? '');
1631

1732
return new FilesystemAdapter(new Filesystem($adapter, $config), $adapter, $config);
1833
});

src/CloudinaryStorageAdapter.php

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,14 @@
1515

1616
class CloudinaryStorageAdapter implements ChecksumProvider, FilesystemAdapter
1717
{
18-
private Cloudinary $cloudinary;
19-
2018
private PathPrefixer $prefixer;
2119

2220
private MimeTypeDetector $mimeTypeDetector;
2321

24-
public function __construct(array $config, string $prefix = '', ?MimeTypeDetector $mimeTypeDetector = null)
22+
public function __construct(private Cloudinary $cloudinary, string $prefix = '', ?MimeTypeDetector $mimeTypeDetector = null)
2523
{
2624
$this->prefixer = new PathPrefixer($prefix);
2725
$this->mimeTypeDetector = $mimeTypeDetector ?: new FinfoMimeTypeDetector;
28-
29-
if (isset($config['url'])) {
30-
$this->cloudinary = new Cloudinary($config['url']);
31-
} else {
32-
$this->cloudinary = new Cloudinary([
33-
'cloud' => [
34-
'cloud_name' => $config['cloud'],
35-
'api_key' => $config['key'],
36-
'api_secret' => $config['secret'],
37-
],
38-
'url' => [
39-
'secure' => $config['secure'] ?? false,
40-
],
41-
]);
42-
}
4326
}
4427

4528
public function copy(string $source, string $destination, Config $config): void
@@ -79,46 +62,59 @@ public function directoryExists(string $path): bool
7962

8063
public function fileExists(string $path): bool
8164
{
65+
[$id, $type] = $this->prepareResource($path);
66+
8267
try {
83-
$this->cloudinary->adminApi()->asset($path);
68+
$this->cloudinary->adminApi()->asset($id, ['resource_type' => $type]);
69+
70+
return true;
8471
} catch (\Throwable $e) {
8572
return false;
8673
}
87-
88-
return true;
8974
}
9075

9176
public function fileSize(string $path): FileAttributes
9277
{
93-
$resource = $this->cloudinary->adminApi()->asset($path);
78+
[$id, $type] = $this->prepareResource($path);
79+
$resource = $this->cloudinary->adminApi()->asset($id, ['resource_type' => $type]);
9480

95-
return new FileAttributes($path, $resource['bytes']);
81+
return new FileAttributes($path, $resource->offsetGet('bytes'));
9682
}
9783

9884
public function lastModified(string $path): FileAttributes
9985
{
100-
$resource = $this->cloudinary->adminApi()->asset($path);
86+
[$id, $type] = $this->prepareResource($path);
87+
$resource = $this->cloudinary->adminApi()->asset($id, ['resource_type' => $type]);
10188

102-
return new FileAttributes($path, null, null, $resource['created_at']);
89+
$dateTime = new \DateTime($resource->offsetGet('created_at'));
90+
91+
return new FileAttributes($path, null, null, $dateTime->getTimestamp());
10392
}
10493

10594
public function listContents(string $path, bool $deep): array|\Traversable
10695
{
10796
$resources = [];
108-
10997
$response = null;
11098

11199
do {
112-
$response = (array) $this->cloudinary->adminApi()->assets([
100+
$response = $this->cloudinary->adminApi()->assets([
113101
'type' => 'upload',
114102
'prefix' => $path,
115103
'max_results' => 500,
116-
'next_cursor' => $response['next_cursor'] ?? null,
104+
'next_cursor' => isset($response) ? $response->offsetGet('next_cursor') : null,
117105
]);
118-
$resources = array_merge($resources, $response['resources']);
119-
} while (array_key_exists('next_cursor', $response));
120-
121-
return array_map(fn ($resource) => new FileAttributes($resource['public_id'], $resource['bytes'], null, $resource['created_at']), $resources);
106+
$resources = array_merge($resources, $response->offsetGet('resources'));
107+
} while ($response->offsetExists('next_cursor'));
108+
109+
return array_map(
110+
fn ($resource) => new FileAttributes(
111+
$resource['public_id'],
112+
$resource['bytes'],
113+
null,
114+
(new \DateTime($resource['created_at']))->getTimestamp()
115+
),
116+
$resources
117+
);
122118
}
123119

124120
public function mimeType(string $path): FileAttributes
@@ -141,19 +137,17 @@ public function move(string $source, string $destination, Config $config): void
141137
public function read(string $path): string
142138
{
143139
[$id, $type] = $this->prepareResource($path);
144-
145140
$resource = $this->cloudinary->adminApi()->asset($id, ['resource_type' => $type]);
146141

147-
return file_get_contents($resource['secure_url']);
142+
return file_get_contents($resource->offsetGet('secure_url'));
148143
}
149144

150145
public function readStream(string $path)
151146
{
152147
[$id, $type] = $this->prepareResource($path);
153-
154148
$resource = $this->cloudinary->adminApi()->asset($id, ['resource_type' => $type]);
155149

156-
return fopen($resource['secure_url'], 'rb');
150+
return fopen($resource->offsetGet('secure_url'), 'rb');
157151
}
158152

159153
public function setVisibility(string $path, string $visibility): void
@@ -181,7 +175,7 @@ public function checksum(string $path, Config $config): string
181175
return hash($algo, file_get_contents($resource['secure_url']));
182176
}
183177

184-
private function prepareResource(string $path): array
178+
public function prepareResource(string $path): array
185179
{
186180
$id = pathinfo($path, PATHINFO_FILENAME);
187181
$mimeType = $this->mimeTypeDetector->detectMimeTypeFromPath($path);

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ protected function getEnvironmentSetup($app)
2323
'url' => env('CLOUDINARY_URL'),
2424
'secure' => (bool) env('CLOUDINARY_SECURE', false),
2525
]);
26+
27+
$app['config']->set('filesystems.default', 'cloudinary');
2628
}
2729

2830
protected function getPackageProviders($app): array

0 commit comments

Comments
 (0)