Passed
Pull Request — master (#156)
by Wilmer
02:29
created

FileStorageTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 26
dl 0
loc 63
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testFlushWithGC() 0 10 1
A testClear() 0 10 1
A tearDown() 0 4 1
A getStorage() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Storage;
6
7
use League\Flysystem\Local\LocalFilesystemAdapter;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
10
use Yiisoft\Yii\Debug\Storage\FileStorage;
11
use Yiisoft\Yii\Debug\Storage\StorageInterface;
12
use Yiisoft\Yii\Filesystem\Filesystem;
13
14
final class FileStorageTest extends AbstractStorageTest
15
{
16
    private string $path = 'runtime';
17
    private Filesystem $fileSystem;
18
19
    protected function setUp(): void
20
    {
21
        parent::setUp();
22
        $this->fileSystem = new Filesystem(new LocalFilesystemAdapter('tests'));
23
    }
24
25
    protected function tearDown(): void
26
    {
27
        parent::tearDown();
28
        $this->fileSystem->deleteDirectory($this->path);
29
    }
30
31
    /**
32
     * @param array $data
33
     *
34
     * @dataProvider dataProvider()
35
     */
36
    public function testFlushWithGC(array $data): void
37
    {
38
        $idGenerator = new DebuggerIdGenerator();
39
        $storage = $this->getStorage($idGenerator);
40
        $storage->setHistorySize(5);
41
        $collector = $this->createFakeCollector($data);
42
43
        $storage->addCollector($collector);
44
        $storage->flush();
45
        $this->assertLessThanOrEqual(5, count($storage->read()));
46
    }
47
48
    /**
49
     * @dataProvider dataProvider()
50
     *
51
     * @param array $data
52
     */
53
    public function testClear(array $data): void
54
    {
55
        $idGenerator = new DebuggerIdGenerator();
56
        $storage = $this->getStorage($idGenerator);
57
        $collector = $this->createFakeCollector($data);
58
59
        $storage->addCollector($collector);
60
        $storage->flush();
61
        $storage->clear();
62
        $this->assertDirectoryDoesNotExist($this->path);
63
    }
64
65
    /**
66
     * @param DebuggerIdGenerator $idGenerator
67
     *
68
     * @return FileStorage|StorageInterface
69
     */
70
    public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface
71
    {
72
        return new FileStorage(
73
            $this->path,
74
            $this->fileSystem,
75
            $idGenerator,
76
            new Aliases()
77
        );
78
    }
79
}
80