Failed Conditions
Push — master ( 77e3e5...46b695 )
by Michael
26s queued 19s
created

tests/Doctrine/Tests/Mocks/ConnectionMock.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Mocks;
6
7
use Doctrine\Common\EventManager;
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\DBAL\Driver;
11
use Doctrine\DBAL\Driver\ResultStatement;
12
use Doctrine\DBAL\Driver\Statement;
13
use Doctrine\DBAL\Platforms\AbstractPlatform;
14
use function is_string;
15
16
/**
17
 * Mock class for Connection.
18
 */
19
class ConnectionMock extends Connection
20
{
21
    /** @var mixed */
22
    private $fetchOneResult;
23
24
    /** @var \Exception|null */
25
    private $fetchOneException;
26
27
    /** @var Statement|null */
28
    private $queryResult;
29
30
    /** @var DatabasePlatformMock */
31
    private $platformMock;
32
33
    /** @var int */
34
    private $lastInsertId = 0;
35
36
    /** @var array */
37
    private $inserts = [];
38
39
    /** @var array */
40
    private $executeUpdates = [];
41
42
    /**
43
     * @param array              $params
44
     * @param Driver             $driver
45
     * @param Configuration|null $config
46
     * @param EventManager|null  $eventManager
47
     */
48
    public function __construct(array $params, $driver, $config = null, $eventManager = null)
49
    {
50
        $this->platformMock = new DatabasePlatformMock();
51
52
        parent::__construct($params, $driver, $config, $eventManager);
53
54
        // Override possible assignment of platform to database platform mock
55
        $this->platform = $this->platformMock;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getDatabasePlatform()
62
    {
63
        return $this->platformMock;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function insert($tableName, array $data, array $types = [])
70
    {
71
        $this->inserts[$tableName][] = $data;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function executeUpdate(string $query, array $params = [], array $types = []) : int
78
    {
79
        $this->executeUpdates[] = ['query' => $query, 'params' => $params, 'types' => $types];
80
81
        return 42;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function lastInsertId($seqName = null)
88
    {
89
        return $this->lastInsertId;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function fetchColumn($statement, array $params = [], $colnum = 0, array $types = [])
96
    {
97
        if ($this->fetchOneException !== null) {
98
            throw $this->fetchOneException;
99
        }
100
101
        return $this->fetchOneResult;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function query(string $sql) : ResultStatement
108
    {
109
        return $this->queryResult;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryResult could return the type null which is incompatible with the type-hinted return Doctrine\DBAL\Driver\ResultStatement. Consider adding an additional type-check to rule them out.
Loading history...
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function quote($input, $type = null)
116
    {
117
        if (is_string($input)) {
118
            return "'" . $input . "'";
119
        }
120
        return $input;
121
    }
122
123
    /* Mock API */
124
125
    /**
126
     * @param mixed $fetchOneResult
127
     *
128
     * @return void
129
     */
130
    public function setFetchOneResult($fetchOneResult)
131
    {
132
        $this->fetchOneResult = $fetchOneResult;
133
    }
134
135
    /**
136
     * @return void
137
     */
138
    public function setFetchOneException(?\Exception $exception = null)
139
    {
140
        $this->fetchOneException = $exception;
141
    }
142
143
    /**
144
     * @param AbstractPlatform $platform
145
     *
146
     * @return void
147
     */
148
    public function setDatabasePlatform($platform)
149
    {
150
        $this->platformMock = $platform;
151
    }
152
153
    /**
154
     * @param int $id
155
     *
156
     * @return void
157
     */
158
    public function setLastInsertId($id)
159
    {
160
        $this->lastInsertId = $id;
161
    }
162
163
    /**
164
     * @return void
165
     */
166
    public function setQueryResult(Statement $result)
167
    {
168
        $this->queryResult = $result;
169
    }
170
171
    /**
172
     * @return array
173
     */
174
    public function getInserts()
175
    {
176
        return $this->inserts;
177
    }
178
179
    /**
180
     * @return array
181
     */
182
    public function getExecuteUpdates()
183
    {
184
        return $this->executeUpdates;
185
    }
186
187
    /**
188
     * @return void
189
     */
190
    public function reset()
191
    {
192
        $this->inserts      = [];
193
        $this->lastInsertId = 0;
194
    }
195
}
196