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