createFakeSummaryCollector()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
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
use function json_decode;
17
18
abstract class AbstractStorageTestCase extends TestCase
19
{
20
    /**
21
     * @dataProvider dataProvider()
22
     */
23
    public function testAddAndGet(array $data): void
24
    {
25
        $idGenerator = new DebuggerIdGenerator();
26
        $storage = $this->getStorage($idGenerator);
27
        $collector = $this->createFakeCollector($data);
28
29
        $this->assertEquals([], $storage->getData());
30
        $storage->addCollector($collector);
31
        $this->assertEquals([$collector->getName() => $data], $storage->getData());
32
    }
33
34
    /**
35
     * @dataProvider dataProvider()
36
     */
37
    public function testRead(array $data): void
38
    {
39
        $idGenerator = new DebuggerIdGenerator();
40
        $storage = $this->getStorage($idGenerator);
41
42
        $storage->addCollector($this->createFakeCollector($data));
43
        $storage->addCollector($this->createFakeSummaryCollector($data));
44
        $expectedData = $storage->getData();
45
        $encodedExpectedData = json_decode(Dumper::create($expectedData)->asJson(), true, 512, JSON_THROW_ON_ERROR);
46
47
        if (!$storage instanceof MemoryStorage) {
48
            $storage->flush();
49
        }
50
51
        $result = $storage->read(StorageInterface::TYPE_DATA);
0 ignored issues
show
Bug introduced by
The call to Yiisoft\Yii\Debug\Storage\StorageInterface::read() has too few arguments starting with id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $result = $storage->read(StorageInterface::TYPE_DATA);

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.

Loading history...
52
        $dumper = Dumper::create($result);
53
        $encodedResult = json_decode($dumper->asJson(), true, 512, JSON_THROW_ON_ERROR);
54
        $this->assertEquals([$idGenerator->getId() => $encodedExpectedData], $encodedResult);
55
    }
56
57
    /**
58
     * @dataProvider dataProvider()
59
     */
60
    public function testFlush(array $data): void
61
    {
62
        $idGenerator = new DebuggerIdGenerator();
63
        $storage = $this->getStorage($idGenerator);
64
        $collector = $this->createFakeCollector($data);
65
66
        $storage->addCollector($collector);
67
        $storage->flush();
68
        $this->assertEquals([], $storage->getData());
69
    }
70
71
    abstract public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface;
72
73
    public static function dataProvider(): iterable
74
    {
75
        yield 'integers' => [[1, 2, 3]];
76
        yield 'string' => [['string']];
77
        yield 'empty values' => [[[['', 0, false]]]];
78
        yield 'false' => [[false]];
79
        yield 'null' => [[null]];
80
        yield 'zero' => [[0]];
81
        yield 'stdClass' => [[new stdClass()]];
82
    }
83
84
    protected function createFakeCollector(array $data)
85
    {
86
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
87
        $collector
88
            ->method('getCollected')
89
            ->willReturn($data);
90
        $collector
91
            ->method('getName')
92
            ->willReturn('Mock_Collector');
93
94
        return $collector;
95
    }
96
97
    protected function createFakeSummaryCollector(array $data)
98
    {
99
        $collector = $this->getMockBuilder(SummaryCollectorInterface::class)->getMock();
100
        $collector
101
            ->method('getCollected')
102
            ->willReturn($data);
103
        $collector
104
            ->method('getName')
105
            ->willReturn('SummaryMock_Collector');
106
107
        $collector
108
            ->method('getSummary')
109
            ->willReturn(['summary' => 'summary data']);
110
111
        return $collector;
112
    }
113
}
114