File: MiddlewareTest.php

package info (click to toggle)
php-doctrine-dbal 3.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,500 kB
  • sloc: php: 54,704; xml: 485; makefile: 42; sh: 24
file content (140 lines) | stat: -rw-r--r-- 4,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Logging;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Logging\Middleware;
use Doctrine\DBAL\ParameterType;
use PHPUnit\Framework\TestCase;
use Psr\Log\Test\TestLogger;

class MiddlewareTest extends TestCase
{
    private Driver $driver;
    private TestLogger $logger;

    public function setUp(): void
    {
        $connection = $this->createMock(Connection::class);

        $driver = $this->createMock(Driver::class);
        $driver->method('connect')
            ->willReturn($connection);

        $this->logger = new TestLogger();

        $middleware   = new Middleware($this->logger);
        $this->driver = $middleware->wrap($driver);
    }

    public function testConnectAndDisconnect(): void
    {
        $this->driver->connect([
            'username' => 'admin',
            'password' => 'Passw0rd!',
            'url' => 'mysql://user:secret@localhost/mydb',
        ]);

        self::assertTrue($this->logger->hasInfo([
            'message' => 'Connecting with parameters {params}',
            'context' => [
                'params' => [
                    'username' => 'admin',
                    'password' => '<redacted>',
                    'url' => '<redacted>',
                ],
            ],
        ]));
    }

    public function testQuery(): void
    {
        $connection = $this->driver->connect([]);
        $connection->query('SELECT 1');

        self::assertTrue($this->logger->hasDebug([
            'message' => 'Executing query: {sql}',
            'context' => ['sql' => 'SELECT 1'],
        ]));
    }

    public function testExec(): void
    {
        $connection = $this->driver->connect([]);
        $connection->exec('DROP DATABASE doctrine');

        self::assertTrue($this->logger->hasDebug([
            'message' => 'Executing statement: {sql}',
            'context' => ['sql' => 'DROP DATABASE doctrine'],
        ]));
    }

    public function testBeginCommitRollback(): void
    {
        $connection = $this->driver->connect([]);
        $connection->beginTransaction();
        $connection->commit();
        $connection->rollBack();

        self::assertTrue($this->logger->hasDebug('Beginning transaction'));
        self::assertTrue($this->logger->hasDebug('Committing transaction'));
        self::assertTrue($this->logger->hasDebug('Rolling back transaction'));
    }

    public function testExecuteStatementWithUntypedParameters(): void
    {
        $connection = $this->driver->connect([]);
        $statement  = $connection->prepare('SELECT ?');
        $statement->execute([42]);

        self::assertTrue($this->logger->hasDebug([
            'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
            'context' => [
                'sql' => 'SELECT ?',
                'params' => [42],
                'types' => [],
            ],
        ]));
    }

    public function testExecuteStatementWithTypedParameters(): void
    {
        $connection = $this->driver->connect([]);
        $statement  = $connection->prepare('SELECT ?, ?');
        $statement->bindValue(1, 42, ParameterType::INTEGER);
        $statement->bindParam(2, $byRef, ParameterType::STRING);

        $byRef = 'Test';
        $statement->execute();

        self::assertTrue($this->logger->hasDebug([
            'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
            'context' => [
                'sql' => 'SELECT ?, ?',
                'params' => [1 => 42, 2 => 'Test'],
                'types' => [1 => ParameterType::INTEGER, 2 => ParameterType::STRING],
            ],
        ]));
    }

    public function testExecuteStatementWithNamedParameters(): void
    {
        $connection = $this->driver->connect([]);
        $statement  = $connection->prepare('SELECT :value');
        $statement->bindValue('value', 'Test');

        $statement->execute();

        self::assertTrue($this->logger->hasDebug([
            'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
            'context' => [
                'sql' => 'SELECT :value',
                'params' => ['value' => 'Test'],
                'types' => ['value' => ParameterType::STRING],
            ],
        ]));
    }
}