testDoctrineDoesNotFuckUpAndDeleteImageFromUnrelatedBookable()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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