ImageRepositoryTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 38
c 1
b 0
f 0
dl 0
loc 72
rs 10

3 Methods

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