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