Passed
Push — master ( 71ac2a...8d79cc )
by Dmitriy
02:35
created

HttpClientCollectorTest::checkCollectedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 26
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 32
rs 9.504
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Unit\Collector;
6
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
10
use Yiisoft\Yii\Debug\Collector\HttpClientCollector;
11
use Yiisoft\Yii\Debug\Collector\TimelineCollector;
12
use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase;
13
14
final class HttpClientCollectorTest extends AbstractCollectorTestCase
15
{
16
    /**
17
     * @param CollectorInterface|HttpClientCollector $collector
18
     */
19
    protected function collectTestData(CollectorInterface $collector): void
20
    {
21
        $collector->collect(
0 ignored issues
show
Bug introduced by
The method collect() does not exist on Yiisoft\Yii\Debug\Collector\CollectorInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Yii\Debug\Collec...mmaryCollectorInterface or Yiisoft\Yii\Debug\Tests\...\Support\DummyCollector. Are you sure you never get one of those? ( Ignorable by Annotation )

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

21
        $collector->/** @scrutinizer ignore-call */ 
22
                    collect(
Loading history...
22
            new Request('GET', 'http://example.com'),
23
            startTime: 10.10,
24
            line: 'file1:123',
25
            uniqueId: 'test1',
26
        );
27
        $collector->collect(
28
            new Request('POST', 'http://yiiframework.com'),
29
            startTime: 12.10,
30
            line: 'file2:555',
31
            uniqueId: 'test2'
32
        );
33
        $collector->collect(
34
            new Request('GET', 'http://yiiframework.com'),
35
            startTime: 15.00,
36
            line: 'file2:666',
37
            uniqueId: 'test3'
38
        );
39
40
        $collector->collectTotalTime(
0 ignored issues
show
Bug introduced by
The method collectTotalTime() does not exist on Yiisoft\Yii\Debug\Collector\CollectorInterface. It seems like you code against a sub-type of Yiisoft\Yii\Debug\Collector\CollectorInterface such as Yiisoft\Yii\Debug\Collector\HttpClientCollector. ( Ignorable by Annotation )

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

40
        $collector->/** @scrutinizer ignore-call */ 
41
                    collectTotalTime(
Loading history...
41
            new Response(200, [], 'test'),
42
            endTime: 13.10,
43
            uniqueId: 'test1'
44
        );
45
        $collector->collectTotalTime(
46
            new Response(200, [], 'test'),
47
            endTime: 12.20,
48
            uniqueId: 'test2'
49
        );
50
        $collector->collectTotalTime(
51
            new Response(200, [], 'test'),
52
            endTime: 20.00,
53
            uniqueId: 'test4'
54
        );
55
    }
56
57
    protected function getCollector(): CollectorInterface
58
    {
59
        return new HttpClientCollector(new TimelineCollector());
60
    }
61
62
    protected function checkCollectedData(array $data): void
63
    {
64
        parent::checkCollectedData($data);
65
66
        $this->assertCount(3, $data);
67
68
        $entry = $data[0];
69
        $this->assertEquals(10.10, $entry['startTime']);
70
        $this->assertEquals(13.10, $entry['endTime']);
71
        $this->assertEquals(3.0, $entry['totalTime']);
72
        $this->assertEquals('GET', $entry['method']);
73
        $this->assertEquals('http://example.com', $entry['uri']);
74
        $this->assertEquals(['Host' => ['example.com']], $entry['headers']);
75
        $this->assertEquals('file1:123', $entry['line']);
76
77
        $entry = $data[1];
78
        $this->assertEquals(12.10, $entry['startTime']);
79
        $this->assertEquals(12.20, $entry['endTime']);
80
        $this->assertEquals(0.1, round($entry['totalTime'], 1));
81
        $this->assertEquals('POST', $entry['method']);
82
        $this->assertEquals('http://yiiframework.com', $entry['uri']);
83
        $this->assertEquals(['Host' => ['yiiframework.com']], $entry['headers']);
84
        $this->assertEquals('file2:555', $entry['line']);
85
86
        $entry = $data[2];
87
        $this->assertEquals(15.0, $entry['startTime']);
88
        $this->assertEquals(15.0, $entry['endTime']);
89
        $this->assertEquals(0.0, round($entry['totalTime'], 1));
90
        $this->assertEquals('GET', $entry['method']);
91
        $this->assertEquals('http://yiiframework.com', $entry['uri']);
92
        $this->assertEquals(['Host' => ['yiiframework.com']], $entry['headers']);
93
        $this->assertEquals('file2:666', $entry['line']);
94
    }
95
96
    protected function checkSummaryData(array $data): void
97
    {
98
        parent::checkSummaryData($data);
99
        $this->assertCount(1, $data);
100
        $this->assertArrayHasKey('http', $data);
101
        $this->assertCount(2, $data['http']);
102
        $this->assertArrayHasKey('count', $data['http']);
103
        $this->assertArrayHasKey('totalTime', $data['http']);
104
105
        $this->assertEquals(3, $data['http']['count']);
106
        $this->assertEquals(3.1, round($data['http']['totalTime'], 1));
107
    }
108
}
109