1+ <?php
2+
3+ require_once 'shared/ez_sql_core.php ' ;
4+
5+ require 'vendor/autoload.php ' ;
6+ use PHPUnit \Framework \TestCase ;
7+
8+ /**
9+ * Test class for ezSQL_mysql.
10+ * Generated by PHPUnit
11+ *
12+ * Needs database tear up to run test, that creates database and a user with
13+ * appropriate rights.
14+ * Run database tear down after tests to get rid of the database and the user.
15+ *
16+ * @author Stefanie Janine Stoelting <[email protected] > 17+ * @name ezSQL_mysqlTest
18+ * @uses mysql_test_db_tear_up.sql
19+ * @uses mysql_test_db_tear_down.sql
20+ * @package ezSQL
21+ * @subpackage unitTests
22+ * @license FREE / Donation (LGPL - You may do what you like with ezSQL - no exceptions.)
23+ */
24+ class ezSQL_mysqlTest extends TestCase {
25+
26+ /**
27+ * constant string user name
28+ */
29+ const TEST_DB_USER = 'ez_test ' ;
30+
31+ /**
32+ * constant string password
33+ */
34+ const TEST_DB_PASSWORD = 'ezTest ' ;
35+
36+ /**
37+ * constant database name
38+ */
39+ const TEST_DB_NAME = 'ez_test ' ;
40+
41+ /**
42+ * constant database host
43+ */
44+ const TEST_DB_HOST = 'localhost ' ;
45+
46+ /**
47+ * constant database connection charset
48+ */
49+ const TEST_DB_CHARSET = 'utf8 ' ;
50+
51+ /**
52+ * @var ezSQL_mysql
53+ */
54+ protected $ object ;
55+
56+ /**
57+ * Sets up the fixture, for example, opens a network connection.
58+ * This method is called before a test is executed.
59+ */
60+ protected function setUp () {
61+ if (!extension_loaded ('mysql ' )) {
62+ $ this ->markTestSkipped (
63+ 'The MySQL Lib is not available. '
64+ );
65+ }
66+ require_once 'mysql/ez_sql_mysql.php ' ;
67+ $ this ->object = new ezSQL_mysql ;
68+ }
69+
70+ /**
71+ * Tears down the fixture, for example, closes a network connection.
72+ * This method is called after a test is executed.
73+ */
74+ protected function tearDown () {
75+ $ this ->object = null ;
76+ }
77+
78+ /**
79+ * @covers ezSQL_mysql::quick_connect
80+ */
81+ public function testQuick_connect () {
82+ $ result = $ this ->object ->quick_connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD , self ::TEST_DB_NAME );
83+
84+ $ this ->assertTrue ($ result );
85+ }
86+
87+ /**
88+ * @covers ezSQL_mysql::quick_connect
89+ */
90+ public function testQuick_connect2 () {
91+ $ result = $ this ->object ->quick_connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD , self ::TEST_DB_NAME , self ::TEST_DB_CHARSET );
92+
93+ $ this ->assertTrue ($ result );
94+ }
95+
96+ /**
97+ * @covers ezSQL_mysql::connect
98+ */
99+ public function testConnect () {
100+ $ result = $ this ->object ->connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD );
101+
102+ $ this ->assertTrue ($ result );
103+ } // testConnect
104+
105+ /**
106+ * @covers ezSQL_mysql::select
107+ */
108+ public function testSelect () {
109+ $ this ->object ->connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD );
110+ $ this ->assertTrue ($ this ->object ->isConnected ());
111+
112+ $ result = $ this ->object ->select (self ::TEST_DB_NAME );
113+
114+ $ this ->assertTrue ($ result );
115+ } // testSelect
116+
117+ /**
118+ * @covers ezSQL_mysql::escape
119+ */
120+ public function testEscape () {
121+ $ result = $ this ->object ->escape ("This is'nt escaped. " );
122+
123+ $ this ->assertEquals ("This is \\'nt escaped. " , $ result );
124+ } // testEscape
125+
126+ /**
127+ * @covers ezSQL_mysql::sysdate
128+ */
129+ public function testSysdate () {
130+ $ this ->assertEquals ('NOW() ' , $ this ->object ->sysdate ());
131+ } // testSysdate
132+
133+ /**
134+ * @covers ezSQL_mysql::query
135+ */
136+ public function testQueryInsert () {
137+ $ this ->object ->connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD );
138+
139+ $ this ->object ->select (self ::TEST_DB_NAME );
140+
141+ $ this ->assertEquals ($ this ->object ->query ('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID)) ' ), 0 );
142+ $ this ->assertEquals ($ this ->object ->query ('INSERT INTO unit_test(id, test_key) VALUES(1, \'test 1 \') ' ), 1 );
143+ $ this ->assertEquals ($ this ->object ->query ('DROP TABLE unit_test ' ), 0 );
144+ } // testQueryInsert
145+
146+ /**
147+ * @covers ezSQL_mysql::query
148+ */
149+ public function testQuerySelect () {
150+ $ this ->object ->connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD );
151+
152+ $ this ->object ->select (self ::TEST_DB_NAME );
153+
154+ $ this ->assertEquals ($ this ->object ->query ('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID)) ' ), 0 );
155+
156+ $ this ->assertEquals ($ this ->object ->query ('INSERT INTO unit_test(id, test_key) VALUES(1, \'test 1 \') ' ), 1 );
157+ $ this ->assertEquals ($ this ->object ->query ('INSERT INTO unit_test(id, test_key) VALUES(2, \'test 2 \') ' ), 1 );
158+ $ this ->assertEquals ($ this ->object ->query ('INSERT INTO unit_test(id, test_key) VALUES(3, \'test 3 \') ' ), 1 );
159+
160+ $ result = $ this ->object ->query ('SELECT * FROM unit_test ' );
161+
162+ $ i = 1 ;
163+ foreach ($ this ->object ->get_results () as $ row ) {
164+ $ this ->assertEquals ($ i , $ row ->id );
165+ $ this ->assertEquals ('test ' . $ i , $ row ->test_key );
166+ ++$ i ;
167+ }
168+
169+ $ this ->assertEquals ($ this ->object ->query ('DROP TABLE unit_test ' ), 0 );
170+ } // testQuerySelect
171+
172+ /**
173+ * @covers ezSQL_mysql::getDBHost
174+ */
175+ public function testGetDBHost () {
176+ $ this ->assertEquals (self ::TEST_DB_HOST , $ this ->object ->getDBHost ());
177+ } // testGetDBHost
178+
179+ /**
180+ * @covers ezSQL_mysql::getCharset
181+ */
182+ public function testGetCharset () {
183+ $ this ->assertEquals (self ::TEST_DB_CHARSET , $ this ->object ->getCharset ());
184+ } // testGetCharset
185+
186+ /**
187+ * @covers ezSQL_mysql::disconnect
188+ */
189+ public function testDisconnect () {
190+ $ this ->object ->disconnect ();
191+
192+ $ this ->assertTrue (true );
193+ } // testDisconnect
194+
195+ /**
196+ * @covers ezSQL_mysql::getInsertId
197+ */
198+ public function testGetInsertId () {
199+ $ this ->object ->connect (self ::TEST_DB_USER , self ::TEST_DB_PASSWORD );
200+
201+ $ this ->object ->select (self ::TEST_DB_NAME );
202+
203+ $ this ->assertEquals ($ this ->object ->query ('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID))ENGINE=MyISAM DEFAULT CHARSET=utf8 ' ), 0 );
204+ $ this ->assertEquals ($ this ->object ->query ('INSERT INTO unit_test(id, test_key) VALUES(1, \'test 1 \') ' ), 1 );
205+
206+ $ this ->assertEquals (1 , $ this ->object ->getInsertId ($ this ->object ->dbh ));
207+
208+ $ this ->assertEquals ($ this ->object ->query ('DROP TABLE unit_test ' ), 0 );
209+ } // testInsertId
210+
211+ } // ezSQL_mysqlTest
0 commit comments