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