Skip to content

Commit d72b30e

Browse files
committed
♻️ Added missing tests
1 parent 558ccea commit d72b30e

File tree

2 files changed

+99
-18
lines changed

2 files changed

+99
-18
lines changed

src/FlysystemCloudinaryAdapter.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,23 +379,7 @@ public function getMimetype($path): array | false
379379
{
380380
ray('adapter getMimetype');
381381

382-
$contents = $this->read($path);
383-
384-
$temp = tmpfile();
385-
386-
fwrite($temp, $contents['contents']);
387-
388-
$mime = mime_content_type($temp);
389-
390-
fclose($temp);
391-
392-
if ($mime === false) {
393-
return false;
394-
}
395-
396-
return [
397-
'mimetype' => $mime,
398-
];
382+
return $this->getMetadata($path);
399383
}
400384

401385
/**
@@ -405,7 +389,7 @@ public function getTimestamp($path): array | false
405389
{
406390
ray('adapter getTimestamp');
407391

408-
return $this->getMimetype($path);
392+
return $this->getMetadata($path);
409393
}
410394

411395
public function getUrl(string $path): string

tests/Feature/FlysystemCloudinaryAdapterTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,91 @@ public function it_does_get_metadata()
294294

295295
$meta = $this->adapter->getMetadata($publicId);
296296

297+
$this->assertMetadataResponse($meta, $publicId);
298+
$this->adapter->delete($publicId); // cleanup
299+
}
300+
301+
/** @test */
302+
public function it_does_not_get_metadata_if_file_is_not_found()
303+
{
304+
$publicId = 'file-does-not-exist';
305+
306+
$bool = $this->adapter->getMetadata($publicId);
307+
308+
$this->assertFalse($bool);
309+
}
310+
311+
/** @test */
312+
public function it_does_get_size()
313+
{
314+
$publicId = 'file-get-size-' . rand();
315+
$fakeImage = File::image('black.jpg')->getContent();
316+
$this->adapter->write($publicId, $fakeImage, new Config());
317+
318+
$meta = $this->adapter->getSize($publicId);
319+
320+
$this->assertMetadataResponse($meta, $publicId);
321+
$this->adapter->delete($publicId); // cleanup
322+
}
323+
324+
/** @test */
325+
public function it_does_not_get_size_if_file_is_not_found()
326+
{
327+
$publicId = 'file-does-not-exist';
328+
329+
$bool = $this->adapter->getSize($publicId);
330+
331+
$this->assertFalse($bool);
332+
}
333+
334+
/** @test */
335+
public function it_does_get_mimetype()
336+
{
337+
$publicId = 'file-get-mimetype-' . rand();
338+
$fakeImage = File::image('black.jpg')->getContent();
339+
$this->adapter->write($publicId, $fakeImage, new Config());
340+
341+
$meta = $this->adapter->getMimetype($publicId);
342+
343+
$this->assertMetadataResponse($meta, $publicId);
344+
$this->adapter->delete($publicId); // cleanup
345+
}
346+
347+
/** @test */
348+
public function it_does_not_get_mimetype_if_file_is_not_found()
349+
{
350+
$publicId = 'file-does-not-exist';
351+
352+
$bool = $this->adapter->getMimetype($publicId);
353+
354+
$this->assertFalse($bool);
355+
}
356+
357+
/** @test */
358+
public function it_does_get_timestamp()
359+
{
360+
$publicId = 'file-get-mimetype-' . rand();
361+
$fakeImage = File::image('black.jpg')->getContent();
362+
$this->adapter->write($publicId, $fakeImage, new Config());
363+
364+
$meta = $this->adapter->getTimestamp($publicId);
365+
366+
$this->assertMetadataResponse($meta, $publicId);
367+
$this->adapter->delete($publicId); // cleanup
368+
}
369+
370+
/** @test */
371+
public function it_does_not_get_timestamp_if_file_is_not_found()
372+
{
373+
$publicId = 'file-does-not-exist';
374+
375+
$bool = $this->adapter->getTimestamp($publicId);
376+
377+
$this->assertFalse($bool);
378+
}
379+
380+
protected function assertMetadataResponse(array $meta, string $publicId): void
381+
{
297382
$this->assertIsString($meta['contents']);
298383
$this->assertNull($meta['etag']);
299384
$this->assertSame('image/jpeg', $meta['mimetype']);
@@ -304,6 +389,18 @@ public function it_does_get_metadata()
304389
$this->assertIsInt($meta['version']);
305390
$this->assertIsString($meta['versionid']);
306391
$this->assertSame('public', $meta['visibility']);
392+
}
393+
394+
/** @test */
395+
public function it_does_get_url()
396+
{
397+
$publicId = 'file-get-url-' . rand();
398+
$fakeImage = File::image('black.jpg')->getContent();
399+
$this->adapter->write($publicId, $fakeImage, new Config());
400+
401+
$url = $this->adapter->getUrl($publicId);
402+
403+
$this->assertStringContainsString($publicId, $url);
307404
$this->adapter->delete($publicId); // cleanup
308405
}
309406
}

0 commit comments

Comments
 (0)