summaryrefslogtreecommitdiffstats
path: root/tests/Utils/TableTest.php
diff options
context:
space:
mode:
authorDan Ungureanu <udan1107@gmail.com>2015-06-30 01:05:38 +0300
committerDan Ungureanu <udan1107@gmail.com>2015-06-30 01:05:38 +0300
commita2354cf85acd032efa374d1f9f611f02fdbe75f6 (patch)
tree3094a3144239860cb72c2f0871660a3f7c862e53 /tests/Utils/TableTest.php
parent5f15789bd325c4fcd6642ecfeea18a5b639acc99 (diff)
downloadsql-parser-a2354cf85acd032efa374d1f9f611f02fdbe75f6.zip
sql-parser-a2354cf85acd032efa374d1f9f611f02fdbe75f6.tar.gz
sql-parser-a2354cf85acd032efa374d1f9f611f02fdbe75f6.tar.bz2
Added statement builder (converts statement trees into executable query strings).
Implemented support for UNIONs in parser. Code coverage is now over 99%. Fixed CHECKSUM statement (typos). Refactored code. Removed pieces of duplicated code. Improved documentation and fixed coding style. Improved PHPUnit's configuration (more test suites for fine-grained testing).
Diffstat (limited to 'tests/Utils/TableTest.php')
-rw-r--r--tests/Utils/TableTest.php181
1 files changed, 181 insertions, 0 deletions
diff --git a/tests/Utils/TableTest.php b/tests/Utils/TableTest.php
new file mode 100644
index 0000000..bda6e78
--- /dev/null
+++ b/tests/Utils/TableTest.php
@@ -0,0 +1,181 @@
+<?php
+
+namespace SqlParser\Tests\Utils;
+
+use SqlParser\Parser;
+use SqlParser\Utils\Table;
+
+use SqlParser\Tests\TestCase;
+
+class TableTest extends TestCase
+{
+
+ /**
+ * @dataProvider getForeignKeysProvider
+ */
+ public function testGetForeignKeys($query, array $expected)
+ {
+ $parser = new Parser($query);
+ $this->assertEquals($expected, Table::getForeignKeys($parser->statements[0]));
+ }
+
+ public function getForeignKeysProvider()
+ {
+ return array(
+ array(
+ 'CREATE USER test',
+ array(),
+ ),
+ array(
+ 'CREATE TABLE `payment` (
+ `payment_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
+ `customer_id` smallint(5) unsigned NOT NULL,
+ `staff_id` tinyint(3) unsigned NOT NULL,
+ `rental_id` int(11) DEFAULT NULL,
+ `amount` decimal(5,2) NOT NULL,
+ `payment_date` datetime NOT NULL,
+ `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ PRIMARY KEY (`payment_id`),
+ KEY `idx_fk_staff_id` (`staff_id`),
+ KEY `idx_fk_customer_id` (`customer_id`),
+ KEY `fk_payment_rental` (`rental_id`),
+ CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE,
+ CONSTRAINT `fk_payment_rental` FOREIGN KEY (`rental_id`) REFERENCES `rental` (`rental_id`) ON DELETE SET NULL ON UPDATE CASCADE,
+ CONSTRAINT `fk_payment_staff` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`staff_id`) ON UPDATE CASCADE
+ ) ENGINE=InnoDB AUTO_INCREMENT=16050 DEFAULT CHARSET=utf8',
+ array(
+ array(
+ 'constraint' => 'fk_payment_customer',
+ 'index_list' => array('customer_id'),
+ 'ref_table_name' => 'customer',
+ 'ref_index_list' => array('customer_id'),
+ 'on_update' => 'CASCADE',
+ ),
+ array(
+ 'constraint' => 'fk_payment_rental',
+ 'index_list' => array('rental_id'),
+ 'ref_table_name' => 'rental',
+ 'ref_index_list' => array('rental_id'),
+ 'on_delete' => 'SET_NULL',
+ 'on_update' => 'CASCADE',
+ ),
+ array(
+ 'constraint' => 'fk_payment_staff',
+ 'index_list' => array('staff_id'),
+ 'ref_table_name' => 'staff',
+ 'ref_index_list' => array('staff_id'),
+ 'on_update' => 'CASCADE',
+ ),
+ ),
+ ),
+ array(
+ 'CREATE TABLE `actor` (
+ `actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
+ `first_name` varchar(45) NOT NULL,
+ `last_name` varchar(45) NOT NULL,
+ `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ PRIMARY KEY (`actor_id`),
+ KEY `idx_actor_last_name` (`last_name`)
+ ) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8',
+ array(),
+ ),
+ array(
+ 'CREATE TABLE `address` (
+ `address_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
+ `address` varchar(50) NOT NULL,
+ `address2` varchar(50) DEFAULT NULL,
+ `district` varchar(20) NOT NULL,
+ `city_id` smallint(5) unsigned NOT NULL,
+ `postal_code` varchar(10) DEFAULT NULL,
+ `phone` varchar(20) NOT NULL,
+ `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ PRIMARY KEY (`address_id`),
+ KEY `idx_fk_city_id` (`city_id`),
+ CONSTRAINT `fk_address_city` FOREIGN KEY (`city_id`) REFERENCES `city` (`city_id`) ON UPDATE CASCADE
+ ) ENGINE=InnoDB AUTO_INCREMENT=606 DEFAULT CHARSET=utf8',
+ array(
+ array(
+ 'constraint' => 'fk_address_city',
+ 'index_list' => array('city_id'),
+ 'ref_table_name' => 'city',
+ 'ref_index_list' => array('city_id'),
+ 'on_update' => 'CASCADE',
+ ),
+ ),
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider getFieldsProvider
+ */
+ public function testGetFields($query, array $expected)
+ {
+ $parser = new Parser($query);
+ $this->assertEquals($expected, Table::getFields($parser->statements[0]));
+ }
+
+ public function getFieldsProvider()
+ {
+ return array(
+ array(
+ 'CREATE USER test',
+ array(),
+ ),
+ array(
+ 'CREATE TABLE `address` (
+ `address_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
+ `address` varchar(50) NOT NULL,
+ `address2` varchar(50) DEFAULT NULL,
+ `district` varchar(20) NOT NULL,
+ `city_id` smallint(5) unsigned NOT NULL,
+ `postal_code` varchar(10) DEFAULT NULL,
+ `phone` varchar(20) NOT NULL,
+ `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ PRIMARY KEY (`address_id`),
+ KEY `idx_fk_city_id` (`city_id`),
+ CONSTRAINT `fk_address_city` FOREIGN KEY (`city_id`) REFERENCES `city` (`city_id`) ON UPDATE CASCADE
+ ) ENGINE=InnoDB AUTO_INCREMENT=606 DEFAULT CHARSET=utf8',
+ array(
+ 'address_id' => array(
+ 'type' => 'SMALLINT',
+ 'timestamp_not_null' => null,
+ ),
+ 'address' => array(
+ 'type' => 'VARCHAR',
+ 'timestamp_not_null' => null,
+ ),
+ 'address2' => array(
+ 'type' => 'VARCHAR',
+ 'timestamp_not_null' => null,
+ 'default_value' => 'NULL',
+ ),
+ 'district' => array(
+ 'type' => 'VARCHAR',
+ 'timestamp_not_null' => null,
+ ),
+ 'city_id' => array(
+ 'type' => 'SMALLINT',
+ 'timestamp_not_null' => null,
+ ),
+ 'postal_code' => array(
+ 'type' => 'VARCHAR',
+ 'timestamp_not_null' => null,
+ 'default_value' => 'NULL',
+ ),
+ 'phone' => array(
+ 'type' => 'VARCHAR',
+ 'timestamp_not_null' => null,
+ ),
+ 'last_update' => array(
+ 'type' => 'TIMESTAMP',
+ 'timestamp_not_null' => true,
+ 'default_value' => 'CURRENT_TIMESTAMP',
+ 'default_current_timestamp' => true,
+ 'on_update_current_timestamp' => true
+ )
+ )
+ ),
+ );
+ }
+}