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