11<?php
22namespace BlitzPHP \Database \Spec \Mock ;
33
4- use BlitzPHP \Database \Connection \BaseConnection ;
4+ use BlitzPHP \Contracts \Database \ResultInterface ;
5+ use BlitzPHP \Database \Connection \SQLite ;
56
6- class MockConnection extends BaseConnection
7+ class MockConnection extends SQLite
78{
89 /**
910 * {@inheritDoc}
@@ -12,16 +13,6 @@ class MockConnection extends BaseConnection
1213
1314 protected array $ returnValues = [];
1415
15- /**
16- * Database schema for Postgre and SQLSRV
17- */
18- protected string $ schema ;
19-
20- /**
21- * {@inheritDoc}
22- */
23- public string $ database ;
24-
2516 /**
2617 * {@inheritDoc}
2718 */
@@ -34,99 +25,41 @@ public function shouldReturn(string $method, $return): self
3425 return $ this ;
3526 }
3627
37- /**
38- * Orchestrates a query against the database. Queries must use
39- * Database\Statement objects to store the query and build it.
40- * This method works with the cache.
41- *
42- * Should automatically handle different connections for read/write
43- * queries if needed.
44- *
45- * @param mixed ...$binds
46- *
47- * @return BaseResult|bool|Query
48- *
49- * @todo BC set $queryClass default as null in 4.1
50- */
51- public function query (string $ sql , $ binds = null , bool $ setEscapeFlags = true , string $ queryClass = '' )
28+ protected function afterConnect (): void
5229 {
53- $ queryClass = str_replace ('Connection ' , 'Query ' , static ::class);
54-
55- $ query = new $ queryClass ($ this );
56-
57- $ query ->setQuery ($ sql , $ binds , $ setEscapeFlags );
58-
59- if (! empty ($ this ->swapPre ) && ! empty ($ this ->prefix )) {
60- $ query ->swapPrefix ($ this ->prefix , $ this ->swapPre );
61- }
62-
63- $ startTime = microtime (true );
64-
65- $ this ->lastQuery = $ query ;
66-
67- // Run the query
68- if (false === ($ this ->result = $ this ->simpleQuery ($ query ->getQuery ()))) {
69- $ query ->setDuration ($ startTime , $ startTime );
70-
71- // @todo deal with errors
72-
73- return false ;
74- }
75-
76- $ query ->setDuration ($ startTime );
77-
78- // resultID is not false, so it must be successful
79- if ($ query ->isWriteType ($ sql )) {
80- return true ;
81- }
82-
83- // query is not write-type, so it must be read-type query; return QueryResult
84- $ resultClass = str_replace ('Connection ' , 'Result ' , static ::class);
85-
86- return new $ resultClass ($ this ->conn , $ this ->result );
30+
8731 }
8832
8933 /**
90- * Connect to the database.
91- *
92- * @return mixed
34+ * {@inheritDoc}
9335 */
94- public function connect ( bool $ persistent = false )
36+ protected function getDsn (): string
9537 {
96- $ return = $ this ->returnValues ['connect ' ] ?? true ;
97-
98- if (is_array ($ return )) {
99- // By removing the top item here, we can
100- // get a different value for, say, testing failover connections.
101- $ return = array_shift ($ this ->returnValues ['connect ' ]);
102- }
103-
104- return $ return ;
38+ return 'sqlite::memory: ' ;
10539 }
10640
10741 /**
108- * Keep or establish the connection if no queries have been sent for
109- * a length of time exceeding the server's idle timeout.
42+ * {@inheritDoc}
11043 */
111- public function reconnect ( ): bool
44+ public function setDatabase ( string $ databaseName ): bool
11245 {
46+ $ this ->config ['database ' ] = $ databaseName ;
47+
11348 return true ;
11449 }
11550
11651 /**
117- * Select a specific database table to use.
118- *
119- * @return mixed
52+ * {@inheritDoc}
12053 */
121- public function setDatabase ( string $ databaseName )
54+ public function getDriver (): string
12255 {
123- $ this ->database = $ databaseName ;
56+ $ this ->initialize () ;
12457
125- return $ this ;
58+ return ' mysql ' ;
12659 }
12760
12861 /**
129- * Returns a string containing the version of the database being used.
62+ * {@inheritDoc}
13063 */
13164 public function getVersion (): string
13265 {
@@ -160,11 +93,7 @@ public function numRows(): int
16093 }
16194
16295 /**
163- * Returns the last error code and message.
164- *
165- * Must return an array with keys 'code' and 'message':
166- *
167- * return ['code' => null, 'message' => null);
96+ * {@inheritDoc}
16897 */
16998 public function error (): array
17099 {
@@ -181,7 +110,7 @@ public function error(): array
181110 */
182111 public function insertID (?string $ table = null )
183112 {
184- return $ this -> conn -> insert_id ;
113+ return 1 ;
185114 }
186115
187116 /**
@@ -191,83 +120,4 @@ protected function _escapeString(string $str): string
191120 {
192121 return "' " . parent ::_escapeString ($ str ) . "' " ;
193122 }
194-
195- /**
196- * Generates the SQL for listing tables in a platform-dependent manner.
197- */
198- protected function _listTables (bool $ constrainByPrefix = false ): string
199- {
200- return '' ;
201- }
202-
203- /**
204- * Generates a platform-specific query string so that the column names can be fetched.
205- */
206- protected function _listColumns (string $ table = '' ): string
207- {
208- return '' ;
209- }
210-
211- protected function _fieldData (string $ table ): array
212- {
213- return [];
214- }
215-
216- protected function _indexData (string $ table ): array
217- {
218- return [];
219- }
220-
221- protected function _foreignKeyData (string $ table ): array
222- {
223- return [];
224- }
225-
226- /**
227- * Close the connection.
228- */
229- protected function _close ()
230- {
231- }
232-
233- /**
234- * Begin Transaction
235- */
236- protected function _transBegin (): bool
237- {
238- return true ;
239- }
240-
241- /**
242- * Commit Transaction
243- */
244- protected function _transCommit (): bool
245- {
246- return true ;
247- }
248-
249- /**
250- * Rollback Transaction
251- */
252- protected function _transRollback (): bool
253- {
254- return true ;
255- }
256-
257-
258- /**
259- * Returns platform-specific SQL to disable foreign key checks.
260- */
261- protected function _disableForeignKeyChecks (): string
262- {
263- return '' ;
264- }
265-
266- /**
267- * Returns platform-specific SQL to disable foreign key checks.
268- */
269- protected function _enableForeignKeyChecks (): string
270- {
271- return '' ;
272- }
273123}
0 commit comments