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 stdClass; |
9
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorInterface; |
10
|
|
|
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; |
11
|
|
|
use Yiisoft\Yii\Debug\DebuggerIdGenerator; |
12
|
|
|
use Yiisoft\Yii\Debug\Dumper; |
13
|
|
|
use Yiisoft\Yii\Debug\Storage\MemoryStorage; |
14
|
|
|
use Yiisoft\Yii\Debug\Storage\StorageInterface; |
15
|
|
|
|
16
|
|
|
abstract class AbstractStorageTestCase extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @dataProvider dataProvider() |
20
|
|
|
*/ |
21
|
|
|
public function testAddAndGet(array $data): void |
22
|
|
|
{ |
23
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
24
|
|
|
$storage = $this->getStorage($idGenerator); |
25
|
|
|
$collector = $this->createFakeCollector($data); |
26
|
|
|
|
27
|
|
|
$this->assertEquals([], $storage->getData()); |
28
|
|
|
$storage->addCollector($collector); |
29
|
|
|
$this->assertEquals([$collector->getName() => $data], $storage->getData()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider dataProvider() |
34
|
|
|
*/ |
35
|
|
|
public function testRead(array $data): void |
36
|
|
|
{ |
37
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
38
|
|
|
$storage = $this->getStorage($idGenerator); |
39
|
|
|
|
40
|
|
|
$storage->addCollector($this->createFakeCollector($data)); |
41
|
|
|
$storage->addCollector($this->createFakeSummaryCollector($data)); |
42
|
|
|
$expectedData = $storage->getData(); |
43
|
|
|
$encodedExpectedData = \json_decode(Dumper::create($expectedData)->asJson(), true, 512, JSON_THROW_ON_ERROR); |
44
|
|
|
|
45
|
|
|
if (!$storage instanceof MemoryStorage) { |
46
|
|
|
$storage->flush(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$result = $storage->read(StorageInterface::TYPE_DATA); |
|
|
|
|
50
|
|
|
$dumper = Dumper::create($result); |
51
|
|
|
$encodedResult = \json_decode($dumper->asJson(), true, 512, JSON_THROW_ON_ERROR); |
52
|
|
|
$this->assertEquals([$idGenerator->getId() => $encodedExpectedData], $encodedResult); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider dataProvider() |
57
|
|
|
*/ |
58
|
|
|
public function testFlush(array $data): void |
59
|
|
|
{ |
60
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
61
|
|
|
$storage = $this->getStorage($idGenerator); |
62
|
|
|
$collector = $this->createFakeCollector($data); |
63
|
|
|
|
64
|
|
|
$storage->addCollector($collector); |
65
|
|
|
$storage->flush(); |
66
|
|
|
$this->assertEquals([], $storage->getData()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
abstract public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface; |
70
|
|
|
|
71
|
|
|
public static function dataProvider(): iterable |
72
|
|
|
{ |
73
|
|
|
yield 'integers' => [[1, 2, 3]]; |
74
|
|
|
yield 'string' => [['string']]; |
75
|
|
|
yield 'empty values' => [[[['', 0, false]]]]; |
76
|
|
|
yield 'false' => [[false]]; |
77
|
|
|
yield 'null' => [[null]]; |
78
|
|
|
yield 'zero' => [[0]]; |
79
|
|
|
yield 'stdClass' => [[new stdClass()]]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function createFakeCollector(array $data) |
83
|
|
|
{ |
84
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
85
|
|
|
$collector |
86
|
|
|
->method('getCollected') |
87
|
|
|
->willReturn($data); |
88
|
|
|
$collector |
89
|
|
|
->method('getName') |
90
|
|
|
->willReturn('Mock_Collector'); |
91
|
|
|
|
92
|
|
|
return $collector; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function createFakeSummaryCollector(array $data) |
96
|
|
|
{ |
97
|
|
|
$collector = $this->getMockBuilder(SummaryCollectorInterface::class)->getMock(); |
98
|
|
|
$collector |
99
|
|
|
->method('getCollected') |
100
|
|
|
->willReturn($data); |
101
|
|
|
$collector |
102
|
|
|
->method('getName') |
103
|
|
|
->willReturn('SummaryMock_Collector'); |
104
|
|
|
|
105
|
|
|
$collector |
106
|
|
|
->method('getSummary') |
107
|
|
|
->willReturn(['summary' => 'summary data']); |
108
|
|
|
|
109
|
|
|
return $collector; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.