11<?php namespace DustinGraham \ReactMysql \Tests ;
22
3- class DatabaseTest extends TestCase
3+ use DustinGraham \ReactMysql \Command ;
4+ use DustinGraham \ReactMysql \ConnectionFactory ;
5+ use DustinGraham \ReactMysql \Database ;
6+ use React \Promise \PromiseInterface ;
7+
8+ class DatabaseTest extends TestCaseDatabase
49{
5- public $ tableName = '`react`.`react` ' ;
6-
710 public function testCommandClass ()
811 {
9- $ database = new \ DustinGraham \ ReactMysql \ Database ();
12+ $ database = new Database ();
1013
1114 $ command = $ database ->createCommand ();
1215
13- $ this ->assertInstanceOf (\ DustinGraham \ ReactMysql \ Command::class, $ command );
16+ $ this ->assertInstanceOf (Command::class, $ command );
1417
1518 $ command ->bindValues ([]);
1619 }
1720
1821 public function testMysqliConnection ()
1922 {
20- $ c = \mysqli_connect ( ' localhost ' , ' react ' , ' react ' , ' react ' );
23+ $ c = $ this -> getMysqliConnection ( );
2124
2225 $ this ->assertInstanceOf (\mysqli::class, $ c );
2326
2427 $ this ->assertNull ($ c ->connect_error );
2528
2629 $ this ->assertEquals (0 , $ c ->connect_errno );
2730
31+ $ this ->assertTrue (in_array ($ c ->server_version , [
32+ 50505 ,
33+ 50547 ,
34+ ]));
35+
2836 // Don't know if we care about these.
2937 // This is what the development environment was.
3038 // We can remove these as we understand them better.
3139 $ this ->assertEquals (10 , $ c ->protocol_version );
3240 $ this ->assertEquals (50011 , $ c ->client_version );
33- $ this ->assertEquals (50505 , $ c ->server_version );
3441 $ this ->assertEquals (0 , $ c ->warning_count );
3542 $ this ->assertEquals ('00000 ' , $ c ->sqlstate );
36-
37- $ c ->close ();
3843 }
3944
4045 public function testMysqliSynchronous ()
4146 {
42- $ c = \mysqli_connect ( ' localhost ' , ' react ' , ' react ' , ' react ' );
47+ $ c = $ this -> getMysqliConnection ( );
4348
44- $ result = $ c ->query ('SELECT * FROM ' . $ this -> tableName );
45- $ this ->assertEquals (3 , $ result ->num_rows );
49+ $ result = $ c ->query ('SELECT * FROM simple_table; ' );
50+ $ this ->assertEquals (2 , $ result ->num_rows );
4651
4752 $ tempTableName = 'temptable123 ' ;
48- $ c ->query ('CREATE TEMPORARY TABLE ' . $ tempTableName . ' LIKE ' . $ this -> tableName );
53+ $ c ->query ('CREATE TEMPORARY TABLE ' . $ tempTableName . ' LIKE simple_table; ' );
4954 $ result = $ c ->query ('SELECT * FROM ' . $ tempTableName );
5055 $ this ->assertEquals (0 , $ result ->num_rows );
5156
52- $ stmt = $ c ->prepare ('INSERT INTO ' . $ tempTableName . ' (`id`, `created_by`, `created_for`, `created_at`) VALUES (?, ?, ?, ?) ' );
53-
54- $ stmt ->bind_param ('isid ' , $ id , $ created_by , $ created_for , $ created_at );
57+ $ stmt = $ c ->prepare ('INSERT INTO ' . $ tempTableName . ' (`id`, `name`) VALUES (?, ?) ' );
5558
5659 $ id = null ;
57- $ created_by = 'john ' ;
58- $ created_for = 3 ;
59- $ created_at = ' NOW() ' ;
60+ $ name = 'john ' ;
61+
62+ $ stmt -> bind_param ( ' is ' , $ id , $ name ) ;
6063
6164 $ stmt ->execute ();
6265 $ this ->assertEquals (1 , $ stmt ->affected_rows , 'Did not insert the row. ' );
6366 $ stmt ->close ();
64-
65- $ c ->close ();
6667 }
6768
6869 public function testMysqliAsynchronous ()
6970 {
70- $ c = \mysqli_connect ( ' localhost ' , ' react ' , ' react ' , ' react ' );
71+ $ c = $ this -> getMysqliConnection ( );
7172
72- $ c ->query ('SELECT * FROM ' . $ this -> tableName , MYSQLI_ASYNC );
73+ $ c ->query ('SELECT * FROM simple_table; ' , MYSQLI_ASYNC );
7374
7475 $ result = $ c ->reap_async_query ();
75- $ this ->assertEquals (3 , $ result ->num_rows );
76-
77- $ c ->close ();
76+ $ this ->assertEquals (2 , $ result ->num_rows );
7877 }
7978
8079 public function testCreateCommandGetPromise ()
8180 {
82- $ db = new \ DustinGraham \ ReactMysql \ Database ();
81+ $ db = new Database ();
8382
8483 $ cmd = $ db ->createCommand ();
85- $ cmd ->sql = 'SELECT * FROM ' . $ this ->tableName . ' WHERE id = :id ' ;
84+
85+ $ cmd ->sql = 'SELECT * FROM simple_table WHERE id = :id ' ;
8686 $ cmd ->bind (':id ' , 1 );
8787
8888 $ promise = $ cmd ->execute ();
89- $ this ->assertInstanceOf (\ React \ Promise \ PromiseInterface::class, $ promise );
89+ $ this ->assertInstanceOf (PromiseInterface::class, $ promise );
9090
9191 ////
9292
9393 $ promise = $ db ->createCommand (
94- 'SELECT * FROM ' . $ this ->tableName . ' WHERE id = :test ' ,
95- [':test ' , 1 ]
94+ 'SELECT * FROM simple_table WHERE id = :test ' ,
95+ [
96+ ':test ' ,
97+ 1 ,
98+ ]
9699 )->execute ();
97- $ this ->assertInstanceOf (\ React \ Promise \ PromiseInterface::class, $ promise );
100+ $ this ->assertInstanceOf (PromiseInterface::class, $ promise );
98101 }
99102
100103 public function testCommandResolvedResults ()
101104 {
102105 $ this ->markTestSkipped ('Still to do. ' );
103106
104107 $ didItWork = false ;
105- $ db = new \ DustinGraham \ ReactMysql \ Database ();
108+ $ db = new Database ();
106109 $ db ->createCommand (
107- 'SELECT * FROM ' . $ this ->tableName . ' WHERE id = :test ' ,
108- [':test ' , 1 ]
110+ 'SELECT * FROM simple_table WHERE id = :test ' ,
111+ [
112+ ':test ' ,
113+ 1 ,
114+ ]
109115 )->execute ()->then (
110116 function ($ results ) use (&$ didItWork )
111117 {
@@ -125,51 +131,45 @@ public function testAssertStrings()
125131
126132 public function testSimpleCommandParameterBinding ()
127133 {
128- $ db = new \ DustinGraham \ ReactMysql \ Database ();
134+ $ db = new Database ();
129135
130136 $ cmd = $ db ->createCommand ();
131- $ cmd ->sql = 'SELECT * FROM ' . $ this -> tableName . ' WHERE id = :id ' ;
137+ $ cmd ->sql = 'SELECT * FROM simple_table WHERE id = :id ' ;
132138 $ cmd ->bind (':id ' , 1 );
133139
134- $ connection = \ DustinGraham \ ReactMysql \ ConnectionFactory::createConnection ();
140+ $ connection = ConnectionFactory::createConnection ();
135141 $ query = $ cmd ->getPreparedQuery ($ connection );
136142
137- $ this ->assertEquals ('SELECT * FROM ' . $ this -> tableName . ' WHERE id = 1 ' , $ query );
143+ $ this ->assertEquals ('SELECT * FROM simple_table WHERE id = 1 ' , $ query );
138144 }
139145
140146 public function testComplexCommandParameterBinding ()
141147 {
142- $ this ->markTestSkipped ('TODO: Implement complex binding. ' );
148+ $ this ->markTestSkipped ('TODO: Implement binding null values . ' );
143149
144- $ db = new \ DustinGraham \ ReactMysql \ Database ();
150+ $ db = new Database ();
145151
146152 $ cmd = $ db ->createCommand ();
147153 $ cmd ->sql = "
148- INSERT INTO { $ this -> tableName } (
154+ INSERT INTO simple_table (
149155 `id`,
150- `created_by`,
151- `created_for`,
152- `created_at`
156+ `name`
153157 ) VALUES (
154158 :id,
155- :created_by,
156- :created_for,
157- :created_at
159+ :name
158160 );
159161 " ;
160162
161163 $ cmd ->bind ([
162164 ':id ' => null ,
163- ':created_by ' => 'neth ' ,
164- ':created_for ' => 3 ,
165- ':created_at ' => 'NOW() ' ,
165+ ':name ' => 'John Cash ' ,
166166 ]);
167167
168- $ connection = \ DustinGraham \ ReactMysql \ ConnectionFactory::createConnection ();
168+ $ connection = ConnectionFactory::createConnection ();
169169 $ query = $ cmd ->getPreparedQuery ($ connection );
170170
171171 $ this ->assertStringEqualsIgnoreSpacing (
172- "INSERT INTO `react`.`react` ( `id`, `created_by`, `created_for`, `created_at` ) VALUES ( NULL, 'test', '3', NOW() ); " ,
172+ "INSERT INTO simple_table ( `id`, `name` ) VALUES ( NULL, 'John Cash' ); " ,
173173 $ query
174174 );
175175 }
0 commit comments