Passed
Pull Request — master (#85)
by
unknown
16:09 queued 01:02
created

ImageRepositoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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