testCardOnDiskIsDeletedWhenRecordInDbIsDeleted()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Repository;
6
7
use Application\Enum\Site;
8
use Application\Model\Card;
9
use Application\Model\Export;
10
use Application\Repository\CardRepository;
11
12
class CardRepositoryTest extends AbstractRepositoryTest
13
{
14
    private CardRepository $repository;
15
16
    protected function setUp(): void
17
    {
18
        parent::setUp();
19
        $this->repository = _em()->getRepository(Card::class);
20
    }
21
22
    public function testCardOnDiskIsDeletedWhenRecordInDbIsDeleted(): void
23
    {
24
        $card = new Card('test card');
25
26
        $card->setSite(Site::Dilps);
27
        $card->setFilename('test card.jpg');
28
        $this->getEntityManager()->persist($card);
29
        $this->getEntityManager()->flush();
30
31
        touch($card->getPath());
32
        self::assertFileExists($card->getPath(), 'test file must exist, because we just touch()ed it');
33
34
        $this->getEntityManager()->remove($card);
35
        $this->getEntityManager()->flush();
36
        self::assertFileDoesNotExist($card->getPath(), 'test file must have been deleted when record was deleted');
37
    }
38
39
    public function testGetExportCards(): void
40
    {
41
        $export1 = $this->getEntityManager()->getReference(Export::class, 14000);
42
        self::assertCount(1, $this->repository->getExportCards($export1, 0));
0 ignored issues
show
Bug introduced by
It seems like $export1 can also be of type null; however, parameter $export of Application\Repository\C...itory::getExportCards() does only seem to accept Application\Model\Export, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        self::assertCount(1, $this->repository->getExportCards(/** @scrutinizer ignore-type */ $export1, 0));
Loading history...
43
        self::assertCount(0, $this->repository->getExportCards($export1, 6000));
44
45
        $export2 = $this->getEntityManager()->getReference(Export::class, 14001);
46
        self::assertCount(2, $this->repository->getExportCards($export2, 0));
47
        self::assertCount(1, $this->repository->getExportCards($export2, 6001));
48
        self::assertCount(0, $this->repository->getExportCards($export2, 6002));
49
    }
50
51
    public function testCachedArtistNames(): void
52
    {
53
        $this->assertArtistNames('Test artist 3000
54
Test artist 3001');
55
56
        $connection = $this->getEntityManager()->getConnection();
57
        $connection->executeStatement("UPDATE artist SET name = 'foo' WHERE id = 3000");
58
        $this->assertArtistNames('foo
59
Test artist 3001');
60
61
        $connection->executeStatement('DELETE FROM card_artist WHERE artist_id = 3000');
62
        $this->assertArtistNames('Test artist 3001');
63
64
        $connection->executeStatement('INSERT INTO card_artist (card_id, artist_id) VALUES (6000, 3000)');
65
        $this->assertArtistNames('foo
66
Test artist 3001');
67
68
        $connection->executeStatement('DELETE FROM artist WHERE id = 3001');
69
        $this->assertArtistNames('foo');
70
    }
71
72
    private function assertArtistNames(string $name): void
73
    {
74
        self::assertSame($name, $this->getEntityManager()->getConnection()->fetchOne('SELECT cached_artist_names FROM card WHERE id = 6000'));
75
    }
76
}
77