Completed
Push — master ( adc2fd...9c1ef9 )
by Alex
02:11
created

PdoCrudUnitTest::testInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
class ResultMock
4
{
5
6
    public function rowCount(): int
7
    {
8
        return 0;
9
    }
10
}
11
12
class PdoCrudUnitTest extends \PHPUnit\Framework\TestCase
13
{
14
15
    /**
16
     * Test case setup
17
     */
18
    public static function setUpBeforeClass(): void
19
    {
20
        \Mezon\PdoCrud\Tests\Utils::$mockingMethods = [
21
            'query',
22
            'processQueryError',
23
            'lastInsertId'
24
        ];
25
    }
26
27
    /**
28
     * Method returns mock
29
     *
30
     * @return object PdoCrud mock
31
     */
32
    protected function getPdoMock(): object
33
    {
34
        $mock = \Mezon\PdoCrud\Tests\Utils::getMock($this);
35
36
        $mock->expects($this->once())
37
            ->method('query');
38
39
        $mock->expects($this->once())
40
            ->method('processQueryError');
41
42
        return $mock;
43
    }
44
45
    /**
46
     * Testing multiple insertion method
47
     */
48
    public function testInsertMultyple(): void
49
    {
50
        $mock = $this->getPdoMock();
51
52
        $mock->insertMultyple('records', [
53
            [
54
                'id' => 1
55
            ],
56
            [
57
                'id' => 2
58
            ]
59
        ]);
60
    }
61
62
    /**
63
     * Testing insertion method
64
     */
65
    public function testInsert(): void
66
    {
67
        // setup
68
        $mock = $this->getPdoMock();
69
70
        $mock->expects($this->once())
71
            ->method('lastInsertId')
72
            ->willReturn(1);
73
74
        // test body and assertions
75
        $mock->insert('records', [
76
            'id' => 1
77
        ]);
78
    }
79
80
    /**
81
     * Testing rollback method
82
     */
83
    public function testRollback(): void
84
    {
85
        // setup
86
        $mock = $this->getPdoMock();
87
88
        $mock->expects($this->once())
89
            ->method('query')
90
            ->willReturn(true);
91
92
        // test body and assertions
93
        $mock->rollback();
94
    }
95
96
    /**
97
     * Testing commit method
98
     */
99
    public function testCommit(): void
100
    {
101
        // setup
102
        $mock = \Mezon\PdoCrud\Tests\Utils::getMock($this);
103
104
        $mock->expects($this->exactly(2))
105
            ->method('query')
106
            ->willReturn(true);
107
108
        // test body and assertions
109
        $mock->commit();
110
    }
111
112
    /**
113
     * Testing startTransaction method
114
     */
115
    public function testStartTransaction(): void
116
    {
117
        // setup
118
        $mock = \Mezon\PdoCrud\Tests\Utils::getMock($this);
119
120
        $mock->expects($this->exactly(2))
121
            ->method('query')
122
            ->willReturn(true);
123
124
        // test body and assertions
125
        $mock->startTransaction();
126
    }
127
128
    /**
129
     * Testing unlock method
130
     */
131
    public function testUnlock(): void
132
    {
133
        // setup
134
        $mock = $this->getPdoMock();
135
136
        // test body and assertions
137
        $mock->unlock();
138
    }
139
140
    /**
141
     * Testing lock method
142
     */
143
    public function testLock(): void
144
    {
145
        // setup
146
        $mock = $this->getPdoMock();
147
148
        // test body and assertions
149
        $mock->lock([
150
            'records'
151
        ], [
152
            'WRITE'
153
        ]);
154
    }
155
156
    /**
157
     * Testing delete method
158
     */
159
    public function testDelete(): void
160
    {
161
        // setup
162
        $mock = \Mezon\PdoCrud\Tests\Utils::getMock($this);
163
164
        $mock->expects($this->exactly(1))
165
            ->method('query')
166
            ->willReturn(new ResultMock());
167
168
        // test body and assertions
169
        $mock->delete('records', 'id=1');
170
    }
171
172
    /**
173
     * Testing update method
174
     */
175
    public function testUpdate(): void
176
    {
177
        // setup
178
        $mock = \Mezon\PdoCrud\Tests\Utils::getMock($this);
179
        $mock->expects($this->exactly(1))
180
            ->method('query')
181
            ->willReturn(new ResultMock());
182
183
        // test body and assertions
184
        $mock->update('som-record', [], '1=1');
185
    }
186
187
    /**
188
     * Testing select method
189
     */
190
    public function testSelect(): void
191
    {
192
        // setup
193
        $queryResultMock = $this->getMockBuilder('QueryResult')
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

193
        $queryResultMock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('QueryResult')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
194
            ->setMethods([
195
            'fetchAll'
196
        ])
197
            ->disableOriginalConstructor()
198
            ->getMock();
199
        $queryResultMock->method('fetchAll')->willReturn([
200
            [],
201
            []
202
        ]);
203
204
        $mock = \Mezon\PdoCrud\Tests\Utils::getMock($this);
205
        $mock->expects($this->exactly(1))
206
            ->method('query')
207
            ->willReturn($queryResultMock);
208
209
        // test body
210
        $result = $mock->select('som-record', '', '1=1');
211
212
        // assertions
213
        $this->assertCount(2, $result);
214
    }
215
}
216