PsrSqlLoggerTest::testSharedQueryId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
dl 29
loc 29
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AbacaphiliacTest\Doctrine;
4
5
use Abacaphiliac\Doctrine\PsrSqlLogger;
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\PsrSqlLogger
13
 */
14
class PsrSqlLoggerTest extends TestCase
15
{
16
    /** @var PsrSqlLogger */
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(): void
26
    {
27
        $this->logger = new TestLogger();
28
29
        $this->sut = new PsrSqlLogger($this->logger);
30
    }
31
32
    private function getRecordByIndex(int $index): \stdClass
33
    {
34
        $record = $this->logger->records[$index];
35
36
        self::assertIsArray($record);
37
38
        return (object) $record;
39
    }
40
41
    public function testLogsQuery()
42
    {
43
        self::assertCount(0, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
57
        $log = $this->getRecordByIndex(0);
58
59
        self::assertSame(LogLevel::INFO, (string) $log->level);
60
        self::assertSame('Query started', (string) $log->message);
61
        self::assertNotEmpty($log->context['query_id']);
62
        self::assertSame($this->sql, $log->context['sql']);
63
        self::assertSame([':id' => \PDO::PARAM_INT], $log->context['types']);
64
    }
65
66 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...
67
    {
68
        self::assertCount(0, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
70
        $this->sut->startQuery(
71
            $this->sql,
72
            [
73
                ':id' => 1234,
74
            ],
75
            [
76
                ':id' => \PDO::PARAM_INT,
77
            ]
78
        );
79
80
        $this->sut->stopQuery();
81
82
        self::assertCount(2, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
        $log = $this->getRecordByIndex(1);
85
86
        self::assertSame(LogLevel::INFO, (string) $log->level);
87
        self::assertSame('Query finished', (string) $log->message);
88
        self::assertNotEmpty($log->context['query_id']);
89
        self::assertIsFloat($log->context['start']);
90
        self::assertIsFloat($log->context['stop']);
91
        self::assertIsFloat($log->context['duration_s']);
92
    }
93
94 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...
95
    {
96
        self::assertCount(0, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
98
        $this->sut->startQuery(
99
            $this->sql,
100
            [
101
                ':id' => 1234,
102
            ],
103
            [
104
                ':id' => \PDO::PARAM_INT,
105
            ]
106
        );
107
108
        $this->sut->stopQuery();
109
110
        self::assertCount(2, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
112
        $startLog = $this->getRecordByIndex(0);
113
114
        self::assertInstanceOf(\stdClass::class, $startLog);
115
116
        $queryId = $startLog->context['query_id'];
117
        self::assertNotEmpty($queryId);
118
119
        $stopLog = $this->getRecordByIndex(1);
120
121
        self::assertSame($queryId, $stopLog->context['query_id']);
122
    }
123
124
    public function testQueryIdChanges()
125
    {
126
        self::assertCount(0, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
128
        $this->sut->startQuery(
129
            $this->sql,
130
            [
131
                ':id' => 1234,
132
            ],
133
            [
134
                ':id' => \PDO::PARAM_INT,
135
            ]
136
        );
137
138
        $this->sut->startQuery(
139
            $this->sql,
140
            [
141
                ':id' => 2345,
142
            ],
143
            [
144
                ':id' => \PDO::PARAM_INT,
145
            ]
146
        );
147
148
        self::assertCount(2, $this->logger->records);
0 ignored issues
show
Documentation introduced by
$this->logger->records is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
150
        $firstLog = $this->getRecordByIndex(0);
151
152
        $queryId = $firstLog->context['query_id'];
153
        self::assertNotEmpty($queryId);
154
155
        $secondLog = $this->getRecordByIndex(1);
156
157
        self::assertNotEmpty($secondLog->context['query_id']);
158
        self::assertNotEquals($queryId, $secondLog->context['query_id']);
159
    }
160
161
    public function testInvalidLogLevel()
162
    {
163
        self::expectException(\InvalidArgumentException::class);
164
165
        new PsrSqlLogger(new NullLogger(), 'InvalidLevel');
166
    }
167
}
168