Passed
Push — master ( 6aabbe...b4650c )
by Peter
02:54
created

MockStatementFactory::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AbterPhp\Framework\TestDouble\Database;
4
5
use InvalidArgumentException;
6
use Opulence\Databases\Adapters\Pdo\Statement;
7
use Opulence\Databases\IStatement;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
10
use PHPUnit\Framework\TestCase;
11
12
class MockStatementFactory
13
{
14
    public const EXPECTATION_ONCE  = -1;
15
    public const EXPECTATION_ANY   = -2;
16
    public const EXPECTATION_NEVER = -4;
17
18
    /**
19
     * @param TestCase $testCase
20
     * @param array    $valuesToBind
21
     * @param array    $rows
22
     * @param int      $atBindValues
23
     * @param int      $atExecute
24
     * @param int      $atRowCount
25
     * @param int      $atFetchAll
26
     *
27
     * @return IStatement|MockObject
28
     * @throws InvalidArgumentException
29
     */
30
    public static function createReadStatement(
31
        TestCase $testCase,
32
        array $valuesToBind,
33
        array $rows,
34
        int $atBindValues = self::EXPECTATION_ONCE,
35
        int $atExecute = self::EXPECTATION_ONCE,
36
        int $atRowCount = self::EXPECTATION_ANY,
37
        int $atFetchAll = self::EXPECTATION_ONCE
38
    ) {
39
        $statement = self::createStatement($testCase);
40
        $statement
41
            ->expects(static::getExpectation($testCase, $atBindValues))
42
            ->method('bindValues')
43
            ->with($valuesToBind);
44
        $statement
45
            ->expects(static::getExpectation($testCase, $atExecute))
46
            ->method('execute')
47
            ->willReturn(true);
48
        $statement
49
            ->expects(static::getExpectation($testCase, $atRowCount))
50
            ->method('rowCount')
51
            ->willReturn(count($rows));
52
        $statement
53
            ->expects(static::getExpectation($testCase, $atFetchAll))
54
            ->method('fetchAll')
55
            ->willReturn($rows);
56
57
        return $statement;
58
    }
59
60
    /**
61
     * @param TestCase $testCase
62
     * @param array    $valuesToBind
63
     * @param mixed    $returnValue
64
     * @param int      $atBindValues
65
     * @param int      $atExecute
66
     * @param int      $atFetch
67
     * @param bool     $executeResult
68
     *
69
     * @return IStatement|MockObject
70
     * @throws InvalidArgumentException
71
     */
72
    public static function createReadRowStatement(
73
        TestCase $testCase,
74
        array $valuesToBind,
75
        $returnValue,
76
        int $atBindValues = self::EXPECTATION_ONCE,
77
        int $atExecute = self::EXPECTATION_ONCE,
78
        int $atFetch = self::EXPECTATION_ONCE,
79
        bool $executeResult = true
80
    ) {
81
        $statement = static::createStatement($testCase);
82
        $statement
83
            ->expects(static::getExpectation($testCase, $atBindValues))
84
            ->method('bindValues')
85
            ->with($valuesToBind);
86
        $statement
87
            ->expects(static::getExpectation($testCase, $atExecute))
88
            ->method('execute')
89
            ->willReturn($executeResult);
90
        $statement
91
            ->expects(static::getExpectation($testCase, $atFetch))
92
            ->method('fetch')
93
            ->willReturn($returnValue);
94
95
        return $statement;
96
    }
97
98
    /**
99
     * @param TestCase $testCase
100
     * @param array    $valuesToBind
101
     * @param mixed    $returnValue
102
     * @param int      $atBindValues
103
     * @param int      $atExecute
104
     * @param int      $atFetchColumn
105
     * @param bool     $executeResult
106
     *
107
     * @return IStatement|MockObject
108
     * @throws InvalidArgumentException
109
     */
110
    public static function createReadColumnStatement(
111
        TestCase $testCase,
112
        array $valuesToBind,
113
        $returnValue,
114
        int $atBindValues = self::EXPECTATION_ONCE,
115
        int $atExecute = self::EXPECTATION_ONCE,
116
        int $atFetchColumn = self::EXPECTATION_ONCE,
117
        bool $executeResult = true
118
    ) {
119
        $statement = static::createStatement($testCase);
120
        $statement
121
            ->expects(static::getExpectation($testCase, $atBindValues))
122
            ->method('bindValues')
123
            ->with($valuesToBind);
124
        $statement
125
            ->expects(static::getExpectation($testCase, $atExecute))
126
            ->method('execute')
127
            ->willReturn($executeResult);
128
        $statement
129
            ->expects(static::getExpectation($testCase, $atFetchColumn))
130
            ->method('fetchColumn')
131
            ->willReturn($returnValue);
132
133
        return $statement;
134
    }
135
136
    /**
137
     * @param TestCase $testCase
138
     * @param array    $values
139
     * @param int      $atBindValues
140
     * @param int      $atExecute
141
     *
142
     * @return IStatement|MockObject
143
     * @throws InvalidArgumentException
144
     */
145
    public static function createWriteStatement(
146
        TestCase $testCase,
147
        array $values,
148
        int $atBindValues = self::EXPECTATION_ONCE,
149
        int $atExecute = self::EXPECTATION_ONCE
150
    ) {
151
        $statement = static::createStatement($testCase);
152
        $statement
153
            ->expects(static::getExpectation($testCase, $atBindValues))
154
            ->method('bindValues')
155
            ->with($values);
156
        $statement
157
            ->expects(static::getExpectation($testCase, $atExecute))
158
            ->method('execute')
159
            ->willReturn(true);
160
161
        return $statement;
162
    }
163
164
    /**
165
     * @param TestCase $testCase
166
     * @param array    $values
167
     * @param array    $errorInfo
168
     * @param int      $atBindValues
169
     * @param int      $atExecute
170
     * @param int      $atErrorInfo
171
     *
172
     * @return IStatement|MockObject
173
     * @throws InvalidArgumentException
174
     */
175
    public static function createErrorStatement(
176
        TestCase $testCase,
177
        array $values,
178
        array $errorInfo,
179
        int $atBindValues = self::EXPECTATION_ONCE,
180
        int $atExecute = self::EXPECTATION_ONCE,
181
        int $atErrorInfo = self::EXPECTATION_ONCE
182
    ) {
183
        $statement = static::createStatement($testCase);
184
        $statement
185
            ->expects(static::getExpectation($testCase, $atBindValues))
186
            ->method('bindValues')
187
            ->with($values);
188
        $statement
189
            ->expects(static::getExpectation($testCase, $atExecute))
190
            ->method('execute')
191
            ->willReturn(false);
192
        $statement
193
            ->expects(static::getExpectation($testCase, $atErrorInfo))
194
            ->method('errorInfo')
195
            ->willReturn($errorInfo);
196
197
        return $statement;
198
    }
199
200
    /**
201
     * @param TestCase $testCase
202
     * @param int      $atBindValues
203
     * @param int      $atExecute
204
     *
205
     * @return IStatement|MockObject
206
     * @throws InvalidArgumentException
207
     */
208
    public static function createWriteStatementWithAny(
209
        TestCase $testCase,
210
        int $atBindValues = self::EXPECTATION_ONCE,
211
        int $atExecute = self::EXPECTATION_ONCE
212
    ) {
213
        $statement = static::createStatement($testCase);
214
        $statement
215
            ->expects(static::getExpectation($testCase, $atBindValues))
216
            ->method('bindValues')
217
            ->withAnyParameters();
218
        $statement
219
            ->expects(static::getExpectation($testCase, $atExecute))
220
            ->method('execute');
221
222
        return $statement;
223
    }
224
225
    /**
226
     * @param TestCase $testCase
227
     *
228
     * @return IStatement|MockObject
229
     */
230
    public static function createStatement(TestCase $testCase)
231
    {
232
        /** @var IStatement|MockObject $mock */
233
        $statement = $testCase->getMockBuilder(Statement::class)
234
            ->disableOriginalConstructor()
235
            ->getMock();
236
237
        return $statement;
238
    }
239
240
    /**
241
     * @param TestCase $testCase
242
     * @param int      $at
243
     *
244
     * @return InvocationOrder
245
     */
246
    public static function getExpectation(TestCase $testCase, int $at): InvocationOrder
247
    {
248
        switch ($at) {
249
            case static::EXPECTATION_NEVER:
250
                return $testCase->never();
251
            case static::EXPECTATION_ONCE:
252
                return $testCase->once();
253
            case static::EXPECTATION_ANY:
254
                return $testCase->any();
255
        }
256
257
        throw new InvalidArgumentException(sprintf("getExpectation does not support %d", $at));
258
    }
259
}
260