Completed
Push — staging ( b71873...14487f )
by
unknown
10:05 queued 09:56
created

DBALMocker::resetInserted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mautic\CoreBundle\Test\Doctrine;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
8
use Doctrine\DBAL\Query\QueryBuilder;
9
use Doctrine\ORM\EntityManager;
10
use Mautic\LeadBundle\Entity\Lead;
11
12
class DBALMocker
13
{
14
    protected $testCase;
15
    protected $mockEm;
16
    protected $mockConnection;
17
    protected $mockQueryBuilder;
18
    protected $queryResponse;
19
    protected $connectionUpdated;
20
    protected $connectionInserted;
21
22
    protected $queryParts = [
23
        'select'     => [],
24
        'from'       => [],
25
        'where'      => [],
26
        'parameters' => [],
27
    ];
28
29
    public function __construct(\PHPUnit_Framework_TestCase $testCase)
30
    {
31
        $this->testCase = $testCase;
32
    }
33
34
    public function setQueryResponse($queryResponse)
35
    {
36
        $this->queryResponse = $queryResponse;
37
    }
38
39
    public function getQueryParts()
40
    {
41
        return $this->queryParts;
42
    }
43
44
    public function getQueryPart($part)
45
    {
46
        if (array_key_exists($part, $this->queryParts)) {
47
            return $this->queryParts[$part];
48
        }
49
50
        throw new \UnexpectedValueException(sprintf(
51
            'The requested query part (%s) does not exist. It must be one of %s.',
52
            $part,
53
            implode(', ', array_keys($this->queryParts))
54
        ));
55
    }
56
57
    public function resetQueryParts()
58
    {
59
        $this->queryParts = [
60
            'select'     => [],
61
            'from'       => [],
62
            'where'      => [],
63
            'parameters' => [],
64
        ];
65
    }
66
67
    public function resetUpdated()
68
    {
69
        $this->connectionUpdated = [];
70
    }
71
72
    public function resetInserted()
73
    {
74
        $this->connectionInserted = [];
75
    }
76
77
    public function reset()
78
    {
79
        $this->resetQueryParts();
80
        $this->resetUpdated();
81
        $this->resetInserted();
82
    }
83
84
    public function getMockEm()
85
    {
86
        if ($this->mockEm === null) {
87
            $mock = $this->testCase->getMockBuilder(EntityManager::class)
88
                ->disableOriginalConstructor()
89
                ->setMethods(
90
                    [
91
                        'getConnection',
92
                        'getReference',
93
                    ]
94
                )
95
                ->getMock();
96
97
            $mock->expects($this->testCase->any())
98
                ->method('getConnection')
99
                ->willReturn($this->getMockConnection());
100
101
            $mock->expects($this->testCase->any())
102
                ->method('getReference')
103
                ->willReturnCallback(function () {
104
                    switch (func_get_arg(0)) {
105
                        case 'MauticLeadBundle:Lead':
106
                            $entity = new Lead();
107
                            break;
108
                    }
109
110
                    $entity->setId(func_get_arg(1));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $entity does not seem to be defined for all execution paths leading up to this point.
Loading history...
111
112
                    return $entity;
113
                });
114
115
            $this->mockEm = $mock;
116
        }
117
118
        return $this->mockEm;
119
    }
120
121
    public function getMockConnection()
122
    {
123
        if ($this->mockConnection === null) {
124
            $mock = $this->testCase->getMockBuilder(Connection::class)
125
                ->disableOriginalConstructor()
126
                ->setMethods([
127
                    'createQueryBuilder',
128
                    'quote',
129
                    'update',
130
                    'insert',
131
                ])
132
                ->getMock();
133
134
            $mock->expects($this->testCase->any())
135
                ->method('createQueryBuilder')
136
                ->willReturn($this->getMockQueryBuilder());
137
138
            $mock->expects($this->testCase->any())
139
                ->method('quote')
140
                ->willReturnArgument(0);
141
142
            $mock->expects($this->testCase->any())
143
                ->method('update')
144
                ->willReturnCallback(function () {
145
                    $this->connectionUpdated[] = func_get_args();
146
                });
147
148
            $mock->expects($this->testCase->any())
149
                ->method('insert')
150
                ->willReturnCallback(function () {
151
                    $this->connectionInserted[] = func_get_args();
152
                });
153
154
            $this->mockConnection = $mock;
155
        }
156
157
        return $this->mockConnection;
158
    }
159
160
    public function getMockQueryBuilder()
161
    {
162
        if ($this->mockQueryBuilder === null) {
163
            $mock = $this->testCase->getMockBuilder(QueryBuilder::class)
164
                ->disableOriginalConstructor()
165
                ->setMethods(
166
                    [
167
                        'select',
168
                        'from',
169
                        'expr',
170
                        'where',
171
                        'andWhere',
172
                        'setParameter',
173
                        'execute',
174
                    ]
175
                )
176
                ->getMock();
177
178
            $mock->expects($this->testCase->any())
179
                ->method('select')
180
                ->willReturnCallback(
181
                    function () use ($mock) {
182
                        $this->queryParts['select'][] = func_get_args();
183
184
                        return $mock;
185
                    }
186
                );
187
188
            $mock->expects($this->testCase->any())
189
                ->method('from')
190
                ->willReturnCallback(
191
                    function () use ($mock) {
192
                        $this->queryParts['from'][] = func_get_args();
193
194
                        return $mock;
195
                    }
196
                );
197
198
            $mock->expects($this->testCase->any())
199
                ->method('expr')
200
                ->willReturnCallback(
201
                    function () {
202
                        return new ExpressionBuilder($this->getMockConnection());
203
                    }
204
                );
205
206
            $mock->expects($this->testCase->any())
207
                ->method('where')
208
                ->willReturnCallback(
209
                    function () use ($mock) {
210
                        $this->queryParts['where'][] = func_get_args();
211
212
                        return $mock;
213
                    }
214
                );
215
216
            $mock->expects($this->testCase->any())
217
                ->method('andWhere')
218
                ->willReturnCallback(
219
                    function () use ($mock) {
220
                        $this->queryParts['where'][] = func_get_args();
221
222
                        return $mock;
223
                    }
224
                );
225
226
            $mock->expects($this->testCase->any())
227
                ->method('setParameter')
228
                ->willReturnCallback(
229
                    function () use ($mock) {
230
                        $this->queryParts['parameters'][] = func_get_args();
231
232
                        return $mock;
233
                    }
234
                );
235
236
            $mock->expects($this->testCase->any())
237
                ->method('execute')
238
                ->willReturnCallback([$this, 'getMockResultStatement']);
239
240
            $this->mockQueryBuilder = $mock;
241
        }
242
243
        return $this->mockQueryBuilder;
244
    }
245
246
    public function getMockResultStatement()
247
    {
248
        $mock = $this->testCase->getMockBuilder(ResultStatement::class)
249
            ->disableOriginalConstructor()
250
            ->setMethods([
251
                'closeCursor',
252
                'columnCount',
253
                'setFetchMode',
254
                'fetch',
255
                'fetchAll',
256
                'fetchColumn',
257
            ])
258
            ->getMock();
259
260
        $mock->method('closeCursor')
261
            ->willReturn(true);
262
263
        $mock->method('setFetchMode')
264
            ->willReturn(true);
265
266
        $mock->method('columnCount')
267
            ->willReturnCallback(function () {
268
                if (isset($this->queryResponse[0]) && is_array($this->queryResponse[0])) {
269
                    return count($this->queryResponse[0]);
270
                } else {
271
                    return count($this->queryResponse);
272
                }
273
            });
274
275
        $mock->expects($this->testCase->any())
276
            ->method('fetchAll')
277
            ->willReturn($this->queryResponse);
278
279
        $mock->expects($this->testCase->any())
280
            ->method('fetch')
281
            ->willReturn($this->queryResponse);
282
283
        return $mock;
284
    }
285
}
286