|
1 | 1 | <?php |
| 2 | + |
2 | 3 | /** |
3 | 4 | * This file is part of the Cloudinary PHP package. |
4 | 5 | * |
|
10 | 11 |
|
11 | 12 | namespace Cloudinary\Test\Unit\Utils; |
12 | 13 |
|
| 14 | +use Cloudinary\Api\Exception\GeneralError; |
13 | 15 | use Cloudinary\FileUtils; |
14 | 16 | use Cloudinary\Tag\ImageTag; |
15 | 17 | use PHPUnit\Framework\TestCase; |
@@ -64,4 +66,39 @@ public function testSplitFilenameExtension() |
64 | 66 | self::assertEquals($file[1], FileUtils::splitPathFilenameExtension($file[0])); |
65 | 67 | } |
66 | 68 | } |
| 69 | + |
| 70 | + public function testSafeFileOpenWithExistingFile() |
| 71 | + { |
| 72 | + $existingFile = __DIR__ . '/../../assets/sample.png'; |
| 73 | + $resource = FileUtils::safeFileOpen($existingFile, 'rb'); |
| 74 | + |
| 75 | + self::assertIsResource($resource); |
| 76 | + self::assertEquals('stream', get_resource_type($resource)); |
| 77 | + |
| 78 | + fclose($resource); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSafeFileOpenWithNonExistingFile() |
| 82 | + { |
| 83 | + $nonExistingFile = __DIR__ . '/../../assets/non_existing_file.txt'; |
| 84 | + |
| 85 | + $this->expectException(GeneralError::class); |
| 86 | + FileUtils::safeFileOpen($nonExistingFile, 'rb'); |
| 87 | + } |
| 88 | + |
| 89 | + public function testSafeFileOpenWithNonExistingFolder() |
| 90 | + { |
| 91 | + $nonExistingFile = 'foo/bar'; |
| 92 | + |
| 93 | + $this->expectException(GeneralError::class); |
| 94 | + FileUtils::safeFileOpen($nonExistingFile, 'rb'); |
| 95 | + } |
| 96 | + |
| 97 | + public function testSafeFileOpenWithEmptyPath() |
| 98 | + { |
| 99 | + $this->expectException(GeneralError::class); |
| 100 | + $this->expectExceptionMessage('Path cannot be empty'); |
| 101 | + |
| 102 | + FileUtils::safeFileOpen('', 'rb'); |
| 103 | + } |
67 | 104 | } |
0 commit comments