|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace le0daniel\Tests\Laravel\ImageEngine\Image; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use le0daniel\Laravel\ImageEngine\Image\ImageRepresentation; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +final class ImageRepresentationTest extends TestCase |
| 10 | +{ |
| 11 | + /** @dataProvider fromSerializedDataProvider */ |
| 12 | + public function testFromSerialized($expectedSerialized, ImageRepresentation $image) |
| 13 | + { |
| 14 | + $serialized = $image->serialize(); |
| 15 | + $this->assertSame($expectedSerialized, $serialized); |
| 16 | + $unserializedImage = ImageRepresentation::fromSerialized($serialized); |
| 17 | + |
| 18 | + foreach (['filePath', 'size', 'expires', 'diskName'] as $attribute) { |
| 19 | + $this->assertEquals( |
| 20 | + $image->{$attribute}, |
| 21 | + $unserializedImage->{$attribute} |
| 22 | + ); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public function fromSerializedDataProvider() |
| 27 | + { |
| 28 | + return [ |
| 29 | + 'Simple image' => [ |
| 30 | + 'eyJwIjoiZmlsZSIsInMiOiJtZWRpdW0ifQ', |
| 31 | + ImageRepresentation::from('file', 'medium'), |
| 32 | + ], |
| 33 | + 'Image with expiry' => [ |
| 34 | + 'eyJwIjoiZmlsZSIsInMiOiJtZWRpdW0iLCJlIjoxMDB9', |
| 35 | + ImageRepresentation::from('file', 'medium', 100), |
| 36 | + ], |
| 37 | + 'Image with all attributes' => [ |
| 38 | + 'eyJkIjoiZGlzayIsInAiOiJmaWxlIiwicyI6Im1lZGl1bSIsImUiOjEwMH0', |
| 39 | + ImageRepresentation::from('file', 'medium', 100, 'disk'), |
| 40 | + ], |
| 41 | + 'Image with carbon time' => [ |
| 42 | + 'eyJkIjoiZGlzayIsInAiOiJmaWxlIiwicyI6Im1lZGl1bSIsImUiOjEwMH0', |
| 43 | + ImageRepresentation::from('file', 'medium', Carbon::createFromTimestamp(100), 'disk'), |
| 44 | + ], |
| 45 | + 'Image without expires' => [ |
| 46 | + 'eyJkIjoiZGlzayIsInAiOiJmaWxlIiwicyI6Im1lZGl1bSJ9', |
| 47 | + ImageRepresentation::from('file', 'medium', null, 'disk'), |
| 48 | + ], |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public function testIsConfidential() |
| 53 | + { |
| 54 | + $this->assertTrue(ImageRepresentation::from('file', 'medium', 100, 'disk')->isConfidential); |
| 55 | + $this->assertFalse(ImageRepresentation::from('file', 'medium')->isConfidential); |
| 56 | + } |
| 57 | + |
| 58 | + public function testIsExpired() |
| 59 | + { |
| 60 | + $this->assertFalse(ImageRepresentation::from('file', 'medium', now()->addMinutes(100), 'disk')->isExpired); |
| 61 | + $this->assertFalse(ImageRepresentation::from('file', 'medium')->isExpired); |
| 62 | + $this->assertTrue(ImageRepresentation::from('file', 'medium', now()->subMinutes(100), 'disk')->isExpired); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + public function testExpiresCarbon() |
| 67 | + { |
| 68 | + $this->assertInstanceOf( |
| 69 | + Carbon::class, |
| 70 | + ImageRepresentation::from('file', 'medium', 100, 'disk')->expiresCarbon |
| 71 | + ); |
| 72 | + $this->assertNull(ImageRepresentation::from('file', 'medium')->expiresCarbon); |
| 73 | + } |
| 74 | + |
| 75 | + public function testTimestamp() |
| 76 | + { |
| 77 | + $this->assertEquals(100,ImageRepresentation::from('file', 'medium', 100, 'disk')->timestamp); |
| 78 | + $this->assertNull(ImageRepresentation::from('file', 'medium')->timestamp); |
| 79 | + } |
| 80 | + |
| 81 | + public function testExtensions() |
| 82 | + { |
| 83 | + $extensions = ['png', 'jpg', 'tiff', 'some']; |
| 84 | + foreach ($extensions as $extension) { |
| 85 | + $image = ImageRepresentation::from("file.$extension", 'medium'); |
| 86 | + $this->assertEquals($extension, $image->extension); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // public function testCacheControlHeaders() |
| 91 | + // { |
| 92 | + // } |
| 93 | + |
| 94 | + public function testFrom() |
| 95 | + { |
| 96 | + $this->assertInstanceOf(ImageRepresentation::class, ImageRepresentation::from('file', 'medium')); |
| 97 | + } |
| 98 | + |
| 99 | + // public function testToArray() |
| 100 | + // { |
| 101 | + // } |
| 102 | +// |
| 103 | + // public function testSerialize() |
| 104 | + // { |
| 105 | + // } |
| 106 | +} |
0 commit comments