--- tests/TestConfiguration.php.dist (revision 855) +++ tests/TestConfiguration.php.dist (working copy) @@ -30,11 +30,21 @@ /** - * @todo Pgsql, Mssql, etc. + * Zend_Db_Adapter_Pdo_Mssql */ +define('TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_ENABLED', false); +define('TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_HOSTNAME', '127.0.0.1'); +define('TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_USERNAME', null); +define('TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_PASSWORD', null); +define('TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_DATABASE', 'test'); /** + * @todo Pgsql, etc. + */ + + +/** * Zend_HttpClient * * IMPORTANT: If this option is enabled, the Zend_HttpClient test suite Index: tests/Zend/AllTests.php =================================================================== --- tests/Zend/AllTests.php (revision 855) +++ tests/Zend/AllTests.php (working copy) @@ -20,6 +20,7 @@ require_once 'Zend/ConfigTest.php'; require_once 'Zend/Config/AllTests.php'; require_once 'Zend/Cache/AllTests.php'; +require_once 'Zend/Db/AllTests.php'; class Zend_AllTests @@ -32,7 +33,6 @@ public static function suite() { $suite = new PHPUnit2_Framework_TestSuite('Zend Framework - Zend'); - $suite->addTest(Zend_Feed_AllTests::suite()); $suite->addTest(Zend_Http_AllTests::suite()); $suite->addTestSuite('Zend_JsonTest'); @@ -46,6 +46,8 @@ $suite->addTestSuite('Zend_ConfigTest'); $suite->addTest(Zend_Config_AllTests::suite()); $suite->addTest(Zend_Cache_AllTests::suite()); + $suite->addTestSuite('Zend_DbTest'); + $suite->addTest(Zend_Db_AllTests::suite()); return $suite; } } Index: tests/Zend/Db/Adapter/Pdo/MssqlTest.php =================================================================== --- tests/Zend/Db/Adapter/Pdo/MssqlTest.php (revision 0) +++ tests/Zend/Db/Adapter/Pdo/MssqlTest.php (revision 0) @@ -0,0 +1,85 @@ + TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_HOSTNAME, + 'username' => TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_USERNAME, + 'password' => TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_PASSWORD, + 'dbname' => TESTS_ZEND_DB_ADAPTER_PDO_MSSQL_DATABASE); + + return $params; + } + + + public function testQuote() + { + // test double quotes are fine + $value = $this->_db->quote('St John"s Wort'); + $this->assertEquals("'St John\"s Wort'", $value); + + // test that single quotes are escaped with another single quote + $value = $this->_db->quote('St John\'s Wort'); + $this->assertEquals("'St John''s Wort'", $value); + + // quote an array + $value = $this->_db->quote(array("it's", 'all', 'right!')); + $this->assertEquals("'it''s', 'all', 'right!'", $value); + + // test numeric + $value = $this->_db->quote(1); + $this->assertEquals("'1'", $value); + + $value = $this->_db->quote(array('1')); + $this->assertEquals("'1'", $value); + } + + public function testQuoteInto() + { + // test double quotes are fine + $value = $this->_db->quoteInto('id=?', 'St John"s Wort'); + $this->assertEquals("id='St John\"s Wort'", $value); + + // test that single quotes are escaped with another single quote + $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort'); + $this->assertEquals("id = 'St John''s Wort'", $value); + } + + public function testQuoteIdentifier() + { + $value = $this->_db->quoteIdentifier('table_name'); + $this->assertEquals("[table_name]", $value); + + $value = $this->_db->quoteIdentifier('table_[]_name'); + $this->assertEquals("[table_[]]_name]", $value); + } + +} Index: tests/Zend/Db/Adapter/Pdo/MysqlTest.php =================================================================== --- tests/Zend/Db/Adapter/Pdo/MysqlTest.php (revision 0) +++ tests/Zend/Db/Adapter/Pdo/MysqlTest.php (revision 0) @@ -0,0 +1,87 @@ + TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_HOSTNAME, + 'username' => TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_USERNAME, + 'password' => TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_PASSWORD, + 'dbname' => TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_DATABASE); + + return $params; + } + + + public function testQuote() + { + // test double quotes are fine + $value = $this->_db->quote('St John"s Wort'); + $this->assertEquals("'St John\\\"s Wort'", $value); + + // test that single quotes are escaped with another single quote + $value = $this->_db->quote("St John's Wort"); + $this->assertEquals("'St John\\'s Wort'", $value); + + // quote an array + $value = $this->_db->quote(array("it's", 'all', 'right!')); + $this->assertEquals("'it\\'s', 'all', 'right!'", $value); + + // test numeric + $value = $this->_db->quote('1'); + $this->assertEquals("'1'", $value); + + $value = $this->_db->quote(1); + $this->assertEquals("'1'", $value); + + $value = $this->_db->quote(array(1,'2',3)); + $this->assertEquals("'1', '2', '3'", $value); + } + + public function testQuoteInto() + { + // test double quotes are fine + $value = $this->_db->quoteInto('id=?', 'St John"s Wort'); + $this->assertEquals("id='St John\\\"s Wort'", $value); + + // test that single quotes are escaped with another single quote + $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort'); + $this->assertEquals("id = 'St John\\'s Wort'", $value); + } + + public function testQuoteIdentifier() + { + $value = $this->_db->quoteIdentifier('table_name'); + $this->assertEquals("`table_name`", $value); + $value = $this->_db->quoteIdentifier('table_`_name'); + $this->assertEquals("`table_``_name`", $value); + } + +} Index: tests/Zend/Db/Adapter/Pdo/Common.php =================================================================== --- tests/Zend/Db/Adapter/Pdo/Common.php (revision 0) +++ tests/Zend/Db/Adapter/Pdo/Common.php (revision 0) @@ -0,0 +1,21 @@ +_db->query($this->getCreatTableSQL()); + + $sql = 'INSERT INTO ' . self::TableName . " (title, subTitle, body, date_created) + VALUES ('News Item 1', 'Sub title 1', 'This is body 1', '2006-05-01 11:11:11')"; + $this->_db->query($sql); + + $sql = 'INSERT INTO ' . self::TableName . " (title, subTitle, body, date_created) + VALUES ('News Item 2', 'Sub title 2', 'This is body 2', '2006-05-02 12:12:12')"; + $this->_db->query($sql); + } + + function setUp() + { + // open a new connection + $this->_db = Zend_Db::factory($this->getDriver(), $this->getParams()); + + // create a test table and populate it + $this->createTestTable(); + } + + function tearDown() + { + // drop test table + $this->_db->query($this->getDropTableSQL()); + + // close the PDO connection + $connection = $this->_db->getConnection(); + $connection = null; + $this->_db = null; + } + + + function testFetchAll() + { + $result = $this->_db->query('SELECT * FROM ' . self::TableName . ' WHERE date_created > :placeholder', + array('placeholder' => '2006-01-01') + ); + + $rows = $result->fetchAll(); + $this->assertEquals(2, count($rows)); + $this->assertEquals('1', $rows[0]['id']); + } + + function testFieldNamesAreLowercase() + { + $result = $this->_db->query('SELECT * FROM ' . self::TableName . ' WHERE date_created > :placeholder', + array('placeholder' => '2006-01-01') + ); + + // use the PDOStatement $result to fetch all rows as an array + $row = $result->fetch(); + + $this->assertEquals(5, count($row)); // correct number of fields + $this->assertEquals('1', $row['id']); // correct data + $this->assertTrue(array_key_exists('subtitle', $row)); // "subTitle" is now "subtitle" + $this->assertFalse(array_key_exists('subTitle', $row)); // "subTitle" is not a key + + } + + function testInsert() + { + $row = array ( + 'title' => 'News Item 3', + 'subTitle' => 'Sub title 3', + 'body' => 'This is body 1', + 'date_created' => '2006-05-03 13:13:13' + ); + $rows_affected = $this->_db->insert(self::TableName, $row); + $last_insert_id = $this->_db->lastInsertId(); + $this->assertEquals('3', (string)$last_insert_id); // correct id has been set + } + +} Index: tests/Zend/Db/AllTests.php =================================================================== --- tests/Zend/Db/AllTests.php (revision 0) +++ tests/Zend/Db/AllTests.php (revision 0) @@ -0,0 +1,40 @@ +addTestSuite('Zend_Db_Adapter_Pdo_MssqlTest'); + } + if(TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_ENABLED == true) { + $suite->addTestSuite('Zend_Db_Adapter_Pdo_MysqlTest'); + } + + return $suite; + } +} + +if (PHPUnit2_MAIN_METHOD == 'Zend_Db_AllTests::main') { + Zend_Db_AllTests::main(); +}