Completed
Push — master ( 1475ae...f0640d )
by Timothy
16:29 queued 12:04
created

PsrSqlParamsLoggerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 163
Duplicated Lines 36.2 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 59
loc 163
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A getRecordByIndex() 0 8 1
A testLogsQuery() 0 26 1
A testLogsDuration() 28 28 1
A testSharedQueryId() 31 31 1
A testQueryIdChanges() 0 39 1
A testInvalidLogLevel() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AbacaphiliacTest\Doctrine;
4
5
use Abacaphiliac\Doctrine\PsrSqlParamsLogger;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Log\LogLevel;
8
use Psr\Log\NullLogger;
9
use Psr\Log\Test\TestLogger;
10
11
/**
12
 * @covers \Abacaphiliac\Doctrine\PsrSqlParamsLogger
13
 */
14
class PsrSqlParamsLoggerTest extends TestCase
15
{
16
    /** @var PsrSqlParamsLogger */
17
    private $sut;
18
19
    /** @var TestLogger */
20
    private $logger;
21
22
    /** @var string */
23
    private $sql = 'SELECT * FROM users WHERE id = :id';
24
25
    protected function setUp()
26
    {
27
        $this->logger = new TestLogger();
28
29
        $this->sut = new PsrSqlParamsLogger($this->logger);
30
    }
31
32
    private function getRecordByIndex(int $index): \stdClass
33
    {
34
        $record = $this->logger->records[$index];
35
36
        self::assertInternalType('array', $record);
37
38
        return (object) $record;
39
    }
40
41
    public function testLogsQuery()
42
    {
43
        self::assertCount(0, $this->logger->records);
44
45
        $this->sut->startQuery(
46
            $this->sql,
47
            [
48
                ':id' => 1234,
49
            ],
50
            [
51
                ':id' => \PDO::PARAM_INT,
52
            ]
53
        );
54
55
        self::assertCount(1, $this->logger->records);
56
57
        $log = $this->getRecordByIndex(0);
58
59
        self::assertInstanceOf(\stdClass::class, $log);
60
        self::assertSame(LogLevel::INFO, (string) $log->level);
61
        self::assertSame('Query started', (string) $log->message);
62
        self::assertNotEmpty($log->context['query_id']);
63
        self::assertSame($this->sql, $log->context['sql']);
64
        self::assertSame([':id' => 1234], $log->context['params']);
65
        self::assertSame([':id' => \PDO::PARAM_INT], $log->context['types']);
66
    }
67
68 View Code Duplication
    public function testLogsDuration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        self::assertCount(0, $this->logger->records);
71
72
        $this->sut->startQuery(
73
            $this->sql,
74
            [
75
                ':id' => 1234,
76
            ],
77
            [
78
                ':id' => \PDO::PARAM_INT,
79
            ]
80
        );
81
82
        $this->sut->stopQuery();
83
84
        self::assertCount(2, $this->logger->records);
85
86
        $log = $this->getRecordByIndex(1);
87
88
        self::assertInstanceOf(\stdClass::class, $log);
89
        self::assertSame(LogLevel::INFO, (string) $log->level);
90
        self::assertSame('Query finished', (string) $log->message);
91
        self::assertNotEmpty($log->context['query_id']);
92
        self::assertInternalType('float', $log->context['start']);
93
        self::assertInternalType('float', $log->context['stop']);
94
        self::assertInternalType('float', $log->context['duration_s']);
95
    }
96
97 View Code Duplication
    public function testSharedQueryId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        self::assertCount(0, $this->logger->records);
100
101
        $this->sut->startQuery(
102
            $this->sql,
103
            [
104
                ':id' => 1234,
105
            ],
106
            [
107
                ':id' => \PDO::PARAM_INT,
108
            ]
109
        );
110
111
        $this->sut->stopQuery();
112
113
        self::assertCount(2, $this->logger->records);
114
115
        $startLog = $this->getRecordByIndex(0);
116
117
        self::assertInstanceOf(\stdClass::class, $startLog);
118
119
        $queryId = $startLog->context['query_id'];
120
        self::assertNotEmpty($queryId);
121
122
123
        $stopLog = $this->getRecordByIndex(1);
124
125
        self::assertInstanceOf(\stdClass::class, $stopLog);
126
        self::assertSame($queryId, $stopLog->context['query_id']);
127
    }
128
129
    public function testQueryIdChanges()
130
    {
131
        self::assertCount(0, $this->logger->records);
132
133
        $this->sut->startQuery(
134
            $this->sql,
135
            [
136
                ':id' => 1234,
137
            ],
138
            [
139
                ':id' => \PDO::PARAM_INT,
140
            ]
141
        );
142
143
        $this->sut->startQuery(
144
            $this->sql,
145
            [
146
                ':id' => 2345,
147
            ],
148
            [
149
                ':id' => \PDO::PARAM_INT,
150
            ]
151
        );
152
153
        self::assertCount(2, $this->logger->records);
154
155
        $firstLog = $this->getRecordByIndex(0);
156
157
        self::assertInstanceOf(\stdClass::class, $firstLog);
158
159
        $queryId = $firstLog->context['query_id'];
160
        self::assertNotEmpty($queryId);
161
162
        $secondLog = $this->getRecordByIndex(1);
163
164
        self::assertInstanceOf(\stdClass::class, $secondLog);
165
        self::assertNotEmpty($secondLog->context['query_id']);
166
        self::assertNotEquals($queryId, $secondLog->context['query_id']);
167
    }
168
169
    /**
170
     * @expectedException \InvalidArgumentException
171
     */
172
    public function testInvalidLogLevel()
173
    {
174
        new PsrSqlParamsLogger(new NullLogger(), 'InvalidLevel');
175
    }
176
}
177