Passed
Push — master ( bafd2f...fc0646 )
by Alexander
02:28
created

AbstractStorageTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 35
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createFakeCollector() 0 11 1
A testAddAndGet() 0 9 1
A testFlush() 0 9 1
A dataProvider() 0 11 1
A testRead() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Storage;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
9
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
10
use Yiisoft\Yii\Debug\Storage\StorageInterface;
11
12
abstract class AbstractStorageTest extends TestCase
13
{
14
    /**
15
     * @dataProvider dataProvider()
16
     *
17
     * @param array $data
18
     */
19
    public function testAddAndGet(array $data): void
20
    {
21
        $idGenerator = new DebuggerIdGenerator();
22
        $storage = $this->getStorage($idGenerator);
23
        $collector = $this->createFakeCollector($data);
24
25
        $this->assertEquals([], $storage->getData());
26
        $storage->addCollector($collector);
27
        $this->assertEquals([$collector->getName() => $data], $storage->getData());
28
    }
29
30
    /**
31
     * @dataProvider dataProvider()
32
     *
33
     * @param array $data
34
     */
35
    public function testRead(array $data): void
36
    {
37
        $idGenerator = new DebuggerIdGenerator();
38
        $storage = $this->getStorage($idGenerator);
39
        $collector = $this->createFakeCollector($data);
40
41
        $storage->addCollector($collector);
42
        $storage->flush();
43
        $this->assertIsArray($storage->read());
44
        $this->assertEquals($storage->getData(), $storage->read(StorageInterface::TYPE_DATA));
45
    }
46
47
    /**
48
     * @dataProvider dataProvider()
49
     *
50
     * @param array $data
51
     */
52
    public function testFlush(array $data): void
53
    {
54
        $idGenerator = new DebuggerIdGenerator();
55
        $storage = $this->getStorage($idGenerator);
56
        $collector = $this->createFakeCollector($data);
57
58
        $storage->addCollector($collector);
59
        $storage->flush();
60
        $this->assertEquals([], $storage->getData());
61
    }
62
63
    abstract public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface;
64
65
    public function dataProvider(): array
66
    {
67
        return [
68
            [[1, 2, 3]],
69
            [['string']],
70
            [[[['', 0, false]]]],
71
            [['test']],
72
            [[false]],
73
            [[null]],
74
            [[0]],
75
            [[new \stdClass()]],
76
        ];
77
    }
78
79
    protected function createFakeCollector(array $data)
80
    {
81
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
82
        $collector
83
            ->method('getCollected')
84
            ->willReturn($data);
85
        $collector
86
            ->method('getName')
87
            ->willReturn('Mock_Collector');
88
89
        return $collector;
90
    }
91
}
92