1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Tests\Unit\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\MemoryStorage; |
11
|
|
|
use Yiisoft\Yii\Debug\Storage\StorageInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractStorageTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @dataProvider dataProvider() |
17
|
|
|
*/ |
18
|
|
|
public function testAddAndGet(array $data): void |
19
|
|
|
{ |
20
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
21
|
|
|
$storage = $this->getStorage($idGenerator); |
22
|
|
|
$collector = $this->createFakeCollector($data); |
23
|
|
|
|
24
|
|
|
$this->assertEquals([], $storage->getData()); |
25
|
|
|
$storage->addCollector($collector); |
26
|
|
|
$this->assertEquals([$collector->getName() => $data], $storage->getData()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider dataProvider() |
31
|
|
|
*/ |
32
|
|
|
public function testRead(array $data): void |
33
|
|
|
{ |
34
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
35
|
|
|
$storage = $this->getStorage($idGenerator); |
36
|
|
|
$collector = $this->createFakeCollector($data); |
37
|
|
|
|
38
|
|
|
$storage->addCollector($collector); |
39
|
|
|
$expectedData = $storage->getData(); |
40
|
|
|
if (!$storage instanceof MemoryStorage) { |
41
|
|
|
$storage->flush(); |
42
|
|
|
} |
43
|
|
|
$data = $storage->read(StorageInterface::TYPE_DATA); |
|
|
|
|
44
|
|
|
$this->assertEquals([$idGenerator->getId() => $expectedData], $data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider dataProvider() |
49
|
|
|
*/ |
50
|
|
|
public function testFlush(array $data): void |
51
|
|
|
{ |
52
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
53
|
|
|
$storage = $this->getStorage($idGenerator); |
54
|
|
|
$collector = $this->createFakeCollector($data); |
55
|
|
|
|
56
|
|
|
$storage->addCollector($collector); |
57
|
|
|
$storage->flush(); |
58
|
|
|
$this->assertEquals([], $storage->getData()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
abstract public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface; |
62
|
|
|
|
63
|
|
|
public function dataProvider(): array |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
[[1, 2, 3]], |
67
|
|
|
[['string']], |
68
|
|
|
[[[['', 0, false]]]], |
69
|
|
|
[['test']], |
70
|
|
|
[[false]], |
71
|
|
|
[[null]], |
72
|
|
|
[[0]], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function createFakeCollector(array $data) |
77
|
|
|
{ |
78
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
79
|
|
|
$collector |
80
|
|
|
->method('getCollected') |
81
|
|
|
->willReturn($data); |
82
|
|
|
$collector |
83
|
|
|
->method('getName') |
84
|
|
|
->willReturn('Mock_Collector'); |
85
|
|
|
|
86
|
|
|
return $collector; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.