-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMediaTest.php
More file actions
189 lines (150 loc) · 6.33 KB
/
Copy pathMediaTest.php
File metadata and controls
189 lines (150 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace JoliCode\MediaBundle\Tests\Model;
use JoliCode\MediaBundle\Binary\Binary;
use JoliCode\MediaBundle\Model\Format;
use JoliCode\MediaBundle\Model\Media;
use JoliCode\MediaBundle\Resolver\Resolver;
use JoliCode\MediaBundle\Tests\BaseTestCase;
class MediaTest extends BaseTestCase
{
private Media $media;
private Binary $binary;
protected function setUp(): void
{
parent::setUp();
$this->binary = new Binary('image/jpeg', Format::JPEG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::JPEG_FIXTURE_PATH));
$this->media = new Media('test.jpg', $this->originalStorage, $this->binary);
$this->media->store();
}
public function testGetPath(): void
{
self::assertEquals('test.jpg', $this->media->getPath());
}
public function testGetFilename(): void
{
self::assertEquals('test.jpg', $this->media->getFilename());
}
public function testGetBinary(): void
{
self::assertSame($this->binary, $this->media->getBinary());
}
public function testGetBinaryWhenNotStored(): void
{
$media = new Media('not-stored.jpg', $this->originalStorage);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('This media is not stored and its binary is not set');
$media->getBinary();
}
public function testGetFormat(): void
{
self::assertEquals('jpeg', $this->media->getFormat());
}
public function testGetMimeType(): void
{
self::assertEquals('image/jpeg', $this->media->getMimeType());
}
public function testGetFileType(): void
{
self::assertEquals('image', $this->media->getFileType());
}
public function testGetFileSize(): void
{
$this->originalFilesystem->write('test.jpg', 'test content');
self::assertEquals(12, $this->media->getFileSize());
}
public function testGetLastModified(): void
{
$this->originalFilesystem->write('test.jpg', 'test content');
$lastModified = $this->media->getLastModified();
self::assertInstanceOf(\DateTime::class, $lastModified);
}
public function testGetPixelDimensions(): void
{
$pixelDimensions = $this->media->getPixelDimensions();
self::assertIsArray($pixelDimensions);
self::assertArrayHasKey('width', $pixelDimensions);
self::assertArrayHasKey('height', $pixelDimensions);
self::assertEquals(2560, $pixelDimensions['width']);
self::assertEquals(1920, $pixelDimensions['height']);
}
public function testGetPixelDimensionsForNonImageContent(): void
{
// replace the binary with a non-image content
$this->originalFilesystem->write('test.jpg', 'test content');
self::assertFalse($this->media->getPixelDimensions());
}
public function testGetStorage(): void
{
self::assertSame($this->originalStorage, $this->media->getStorage());
}
public function testGetUrl(): void
{
self::assertEquals('/media/test.jpg', $this->media->getUrl());
}
public function testVariations(): void
{
$variation = $this->library->getVariationContainer()->get('thumbnail');
$mediaVariation = $this->media->createVariation($variation);
$this->media->addVariation($mediaVariation);
self::assertTrue($this->media->hasVariation('thumbnail'));
self::assertSame($mediaVariation, $this->media->getVariation('thumbnail'));
self::assertEquals(['thumbnail' => $mediaVariation], $this->media->getVariations());
}
public function testGetNonExistentVariation(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Variation "non-existent" not found');
$this->media->getVariation('non-existent');
}
public function testIsStored(): void
{
$binary = new Binary('image/png', Format::PNG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::PNG_FIXTURE_PATH));
$media = new Media('test.png', $this->originalStorage, $binary);
self::assertFalse($media->isStored());
$this->originalFilesystem->write('test.png', BaseTestCase::getFixtureBinaryContent(BaseTestCase::PNG_FIXTURE_PATH));
self::assertFalse($media->isStored());
}
public function testStore(): void
{
$binary = new Binary('image/png', Format::PNG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::PNG_FIXTURE_PATH));
$media = new Media('test.png', $this->originalStorage, $binary);
$media->store();
self::assertTrue($this->originalFilesystem->fileExists('test.png'));
self::assertTrue($media->isStored());
self::assertEquals(BaseTestCase::getFixtureBinaryContent(BaseTestCase::PNG_FIXTURE_PATH), $this->originalFilesystem->read('test.png'));
}
public function testStoreWithNewBinary(): void
{
$newBinary = new Binary('image/jpeg', Format::JPEG->value, 'new content');
$this->media->store($newBinary);
self::assertTrue($this->originalFilesystem->fileExists('test.jpg'));
self::assertEquals('new content', $this->originalFilesystem->read('test.jpg'));
}
public function testStoreWithNoBinary(): void
{
$media = new Media('test.jpg', $this->originalStorage);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('No binary set to store');
$media->store();
}
public function testSerializeUnserializeRestoresMedia(): void
{
Media::$resolverInitializer = fn (): Resolver => $this->resolver;
$restored = unserialize(serialize($this->media));
self::assertInstanceOf(Media::class, $restored);
self::assertSame('test.jpg', $restored->getPath());
self::assertSame('default', $restored->getLibrary()->getName());
}
public function testUnserializeSupportsLegacyIndexedPayload(): void
{
Media::$resolverInitializer = fn (): Resolver => $this->resolver;
$restored = (new \ReflectionClass(Media::class))->newInstanceWithoutConstructor();
$restored->__unserialize([
0 => 'test.jpg',
1 => 'default',
]);
self::assertSame('test.jpg', $restored->getPath());
self::assertSame('default', $restored->getLibrary()->getName());
}
}