testFileOnDiskIsDeletedWhenRecordInDbIsDeleted()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 22
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Repository;
6
7
use Application\Enum\ProductType;
8
use Application\Model\File;
9
use Application\Model\Product;
10
use Application\Repository\FileRepository;
11
use ApplicationTest\Traits\LimitedAccessSubQuery;
12
use Cake\Chronos\ChronosDate;
13
14
class FileRepositoryTest extends AbstractRepositoryTest
15
{
16
    use LimitedAccessSubQuery;
17
18
    private FileRepository $repository;
19
20
    protected function setUp(): void
21
    {
22
        parent::setUp();
23
        $this->repository = _em()->getRepository(File::class);
24
    }
25
26
    public function providerGetAccessibleSubQuery(): iterable
27
    {
28
        $all = [9000, 9001, 9002, 9003];
29
        $articlesAndReviewsViaSubscriptions = [9000, 9001, 9002, 9003];
30
        $directPurchases = [9001, 9003];
31
        yield ['anonymous', [9003]];
32
        yield ['member', $directPurchases];
33
        yield ['othermember', $articlesAndReviewsViaSubscriptions];
34
        yield ['facilitator', $all];
35
        yield ['administrator', $all];
36
    }
37
38
    public function testFileOnDiskIsDeletedWhenRecordInDbIsDeleted(): void
39
    {
40
        $product = new Product('p1');
41
        $product->setReleaseDate(new ChronosDate());
42
        $product->setReviewNumber(1);
43
        $product->setType(ProductType::Both);
44
        $file = new File();
45
46
        $file->setFilename('test document.pdf');
47
        $product->setFile($file);
48
49
        $this->getEntityManager()->persist($product);
50
        $this->getEntityManager()->persist($file);
51
        $this->getEntityManager()->flush();
52
53
        $path = $file->getPath();
54
        touch($path);
55
        self::assertFileExists($path, 'test file must exist, because we just touch()ed it');
56
57
        $this->getEntityManager()->remove($file);
58
        $this->getEntityManager()->flush();
59
        self::assertFileDoesNotExist($path, 'test file must have been deleted when record was deleted');
60
    }
61
}
62