1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Repository; |
6
|
|
|
|
7
|
|
|
use Application\Model\Image; |
8
|
|
|
use Application\Model\Product; |
9
|
|
|
use Application\Model\User; |
10
|
|
|
use Application\Repository\ImageRepository; |
11
|
|
|
use Ecodev\Felix\Service\AbstractDatabase; |
12
|
|
|
|
13
|
|
|
class ImageRepositoryTest extends AbstractRepositoryTest |
14
|
|
|
{ |
15
|
|
|
protected function tearDown(): void |
16
|
|
|
{ |
17
|
|
|
// Restore all images that might have been deleted |
18
|
|
|
AbstractDatabase::executeLocalCommand('git checkout -- data/images/'); |
19
|
|
|
parent::tearDown(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testImageOnDiskIsDeletedWhenRecordInDbIsDeleted(): void |
23
|
|
|
{ |
24
|
|
|
$image = new Image(); |
25
|
|
|
|
26
|
|
|
$image->setFilename('test image.jpg'); |
27
|
|
|
$this->getEntityManager()->persist($image); |
28
|
|
|
$this->getEntityManager()->flush(); |
29
|
|
|
|
30
|
|
|
$path = $image->getPath(); |
31
|
|
|
touch($path); |
32
|
|
|
self::assertFileExists($path, 'test file must exist, because we just touch()ed it'); |
33
|
|
|
|
34
|
|
|
$this->getEntityManager()->remove($image); |
35
|
|
|
$this->getEntityManager()->flush(); |
36
|
|
|
self::assertFileDoesNotExist($path, 'test file must have been deleted when record was deleted'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testDoctrineDoesNotFuckUpAndDeleteImageFromUnrelatedProduct(): void |
40
|
|
|
{ |
41
|
|
|
/** @var User $user */ |
42
|
|
|
$user = _em()->getRepository(User::class)->getOneByEmail('[email protected]'); |
43
|
|
|
User::setCurrent($user); |
44
|
|
|
|
45
|
|
|
// Make one image usable |
46
|
|
|
$this->getEntityManager()->getConnection()->update('product', ['image_id' => null], ['image_id' => 5007]); |
47
|
|
|
$this->getEntityManager()->getConnection()->executeStatement('REPLACE INTO image (id, filename, width, height) VALUES(5999, \'foo.svg\', 113, 86);'); |
48
|
|
|
|
49
|
|
|
$paths = [ |
50
|
|
|
'data/images/train.jpg', |
51
|
|
|
'data/images/revue61.jpg', |
52
|
|
|
'data/images/revue62.jpg', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
// All images must exist before testing |
56
|
|
|
foreach ($paths as $p) { |
57
|
|
|
self::assertFileExists($p); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Image that will be orphaned must exist in DB |
61
|
|
|
$imageToBeOrphanedQuery = 'SELECT COUNT(*) FROM image WHERE id = 5000'; |
62
|
|
|
self::assertSame('1', $this->getEntityManager()->getConnection()->fetchOne($imageToBeOrphanedQuery)); |
63
|
|
|
|
64
|
|
|
// Affect existing image to an existing product |
65
|
|
|
$product = $this->getEntityManager()->find(Product::class, 3000); |
66
|
|
|
$image = $this->getEntityManager()->find(Image::class, 5999); |
67
|
|
|
self::assertNotNull($image); |
68
|
|
|
$product->setImage($image); |
69
|
|
|
self::assertSame($image, $product->getImage(), 'should get image that was set'); |
70
|
|
|
$this->getEntityManager()->flush(); |
71
|
|
|
|
72
|
|
|
// Most images must still exist after affecting an existing image to an existing product. |
73
|
|
|
// Only the orphaned image should be deleted, but absolutely never an image related to **another** product |
74
|
|
|
$mustBeDeleted = 'data/images/revue61.jpg'; |
75
|
|
|
foreach ($paths as $p) { |
76
|
|
|
if ($p === $mustBeDeleted) { |
77
|
|
|
self::assertFileDoesNotExist($p); |
78
|
|
|
} else { |
79
|
|
|
self::assertFileExists($p); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Orphaned image was deleted from DB |
84
|
|
|
self::assertSame('0', $this->getEntityManager()->getConnection()->fetchOne($imageToBeOrphanedQuery)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|