Test Failed
Push — master ( f9dc8e...64f7bc )
by Petr
08:10
created

ImageUploadTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 78
rs 10
c 1
b 0
f 0
wmc 5
1
<?php
2
3
namespace ContentTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\Access\Factory;
8
use kalanis\kw_files\Extended\Config;
9
use kalanis\kw_files\FilesException;
10
use kalanis\kw_images\Content\Images;
11
use kalanis\kw_images\Content\ImageSize;
12
use kalanis\kw_images\Content\ImageUpload;
13
use kalanis\kw_images\Graphics;
14
use kalanis\kw_images\ImagesException;
15
use kalanis\kw_images\Sources;
16
use kalanis\kw_mime\Check\CustomList;
17
use kalanis\kw_mime\MimeException;
18
use kalanis\kw_paths\PathsException;
19
use kalanis\kw_storage\Storage\Key;
20
use kalanis\kw_storage\Storage\Target;
21
use kalanis\kw_storage\StorageException;
22
23
24
class ImageUploadTest extends CommonTestClass
25
{
26
    protected function tearDown(): void
27
    {
28
        $tgt0 = $this->targetPath() . DIRECTORY_SEPARATOR . 'tstimg1.png';
29
        if (is_file($tgt0)) {
30
            unlink($tgt0);
31
        }
32
    }
33
34
    /**
35
     * @throws FilesException
36
     * @throws ImagesException
37
     * @throws MimeException
38
     * @throws PathsException
39
     */
40
    public function testUploadPass(): void
41
    {
42
        $src = $this->targetPath() . DIRECTORY_SEPARATOR . 'testimage.png';
43
        $file = $this->targetPath() . DIRECTORY_SEPARATOR . 'tstimg1.png';
44
        $tgt = ['testtree', 'tstimg1.png'];
45
        $lib = $this->getLib();
46
47
        copy($src, $file);
48
49
        $this->assertTrue($lib->process($tgt, $file, static::TEST_STRING, true, true));
50
    }
51
52
    /**
53
     * @throws FilesException
54
     * @throws ImagesException
55
     * @throws StorageException
56
     * @throws PathsException
57
     */
58
    public function testFreeName(): void
59
    {
60
        $memory = new Target\Memory();
61
        $memory->save(DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'testimage.png' , static::TEST_STRING);
62
        $memory->save(DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.png' , static::TEST_STRING);
63
        $memory->save(DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg_0.png' , static::TEST_STRING);
64
        $memory->save(DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg_1.png' , static::TEST_STRING);
65
66
        $lib = $this->getLib([], $memory);
67
        $this->assertEquals('solo.png', $lib->findFreeName(['testtree'], 'solo.png'));
68
        $this->assertEquals('testimage_0.png', $lib->findFreeName(['testtree'], 'testimage.png'));
69
        $this->assertEquals('tstimg_2.png', $lib->findFreeName(['testtree'], 'tstimg.png'));
70
        $this->assertEquals('tstimg.png', $lib->findFreeName(['testtree', 'testdir'], 'tstimg.png'));
71
    }
72
73
    /**
74
     * @param array<string, string|int> $params
75
     * @param Target\Memory|null $memory
76
     * @throws FilesException
77
     * @throws ImagesException
78
     * @throws PathsException
79
     * @return ImageUpload
80
     */
81
    protected function getLib(array $params = [], ?Target\Memory $memory = null): ImageUpload
82
    {
83
        $storage = new \kalanis\kw_storage\Storage\Storage(new Key\DefaultKey(), $memory ?? new Target\Memory());
84
        $config = (new Config())->setData($params);
85
        $composite = new Factory();
86
        $access = $composite->getClass($storage);
87
        $graphics = new Graphics(new Graphics\Processor(new Graphics\Format\Factory()), new CustomList());
88
        $image = new Sources\Image($access, $config);
89
        return new ImageUpload(  // process uploaded images
90
            $graphics,
91
            $image,
92
            (new Graphics\ImageConfig())->setData($params),
93
            new Images(
94
                new ImageSize(
95
                    $graphics,
96
                    (new Graphics\ThumbConfig())->setData($params),
97
                    $image
98
                ),
99
                new Sources\Image($access, $config),
100
                new Sources\Thumb($access, $config),
101
                new Sources\Desc($access, $config),
102
            )
103
        );
104
    }
105
}
106