Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #320 from mhujer/db-tests
Browse files Browse the repository at this point in the history
DB tests are enabled for Travis and fixed to be passing
  • Loading branch information
froschdesign committed Apr 3, 2014
2 parents 4afa824 + 4f7338e commit 6fb5c4b
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 44 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ install:
- sh ./tests/install_dependencies.sh
- phpenv rehash

services:
- memcached

before_script:
- mysql -e 'create database zftest;'
- psql -c 'create database zftest;' -U postgres
- echo "extension = memcache.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- cp ./tests/TestConfiguration.travis.php ./tests/TestConfiguration.php

script:
- cd tests/
- php runalltests.php
Expand Down
40 changes: 20 additions & 20 deletions library/Zend/Db/Table/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1041,16 +1041,6 @@ public function insert(array $data)
$primary = (array) $this->_primary;
$pkIdentity = $primary[(int)$this->_identity];

/**
* If this table uses a database sequence object and the data does not
* specify a value, then get the next ID from the sequence and add it
* to the row. We assume that only the first column in a compound
* primary key takes a value from a sequence.
*/
if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
$data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
$pkSuppliedBySequence = true;
}

/**
* If the primary key can be generated automatically, and no value was
Expand All @@ -1060,7 +1050,7 @@ public function insert(array $data)
* position of the data. The following values are considered empty:
* null, false, true, '', array()
*/
if (!isset($pkSuppliedBySequence) && array_key_exists($pkIdentity, $data)) {
if (array_key_exists($pkIdentity, $data)) {
if ($data[$pkIdentity] === null // null
|| $data[$pkIdentity] === '' // empty string
|| is_bool($data[$pkIdentity]) // boolean
Expand All @@ -1069,6 +1059,16 @@ public function insert(array $data)
}
}

/**
* If this table uses a database sequence object and the data does not
* specify a value, then get the next ID from the sequence and add it
* to the row. We assume that only the first column in a compound
* primary key takes a value from a sequence.
*/
if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
$data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
}

/**
* INSERT the new row.
*/
Expand Down Expand Up @@ -1212,20 +1212,20 @@ public function _cascadeDelete($parentTableClassname, array $primaryKey)
{
// setup metadata
$this->_setupMetadata();

// get this class name
$thisClass = get_class($this);
if ($thisClass === 'Zend_Db_Table') {
$thisClass = $this->_definitionConfigName;
}

$rowsAffected = 0;

foreach ($this->_getReferenceMapNormalized() as $map) {
if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {

$where = array();

// CASCADE or CASCADE_RECURSE
if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
Expand All @@ -1237,10 +1237,10 @@ public function _cascadeDelete($parentTableClassname, array $primaryKey)
$primaryKey[$refCol], $type);
}
}

// CASCADE_RECURSE
if ($map[self::ON_DELETE] == self::CASCADE_RECURSE) {

/**
* Execute cascading deletes against dependent tables
*/
Expand All @@ -1259,7 +1259,7 @@ public function _cascadeDelete($parentTableClassname, array $primaryKey)
if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
$rowsAffected += $this->delete($where);
}

}
}
return $rowsAffected;
Expand Down Expand Up @@ -1610,5 +1610,5 @@ public static function getTableFromString($tableName, Zend_Db_Table_Abstract $re

return new $tableName($options);
}

}
85 changes: 85 additions & 0 deletions tests/TestConfiguration.travis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package UnitTests
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/

/**
* This file defines configuration for running the unit tests for the Zend
* Framework. Some tests have dependencies to PHP extensions or databases
* which may not necessary installed on the target system. For these cases,
* the ability to disable or configure testing is provided below. Tests for
* components which should run universally are always run by the master
* suite and cannot be disabled.
*
* Do not edit this file. Instead, copy this file to TestConfiguration.php,
* and edit the new file. Never commit plaintext passwords to the source
* code repository.
*/

/**
* Zend_Auth_Adapter_DbTable tests
*/
defined('TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_ENABLED') || define('TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_ENABLED', true);

/**
* Zend_Cache
*
* TESTS_ZEND_CACHE_SQLITE_ENABLED => sqlite extension has to be enabled
* TESTS_ZEND_CACHE_APC_ENABLED => apc extension has to be enabled
* TESTS_ZEND_CACHE_WINCACHE_ENABLED => wincache extension has to be enabled
* TESTS_ZEND_CACHE_MEMCACHED_ENABLED => memcache extension has to be enabled and
* a memcached server has to be available
* TESTS_ZEND_CACHE_LIBMEMCACHED_ENABLED => memcached extension has to be enabled and
* a memcached server has to be available
* TESTS_ZEND_CACHE_XCACHE_ENABLED => xcache extension has to be enabled
*/
defined('TESTS_ZEND_CACHE_SQLITE_ENABLED') || define('TESTS_ZEND_CACHE_SQLITE_ENABLED', true);
defined('TESTS_ZEND_CACHE_MEMCACHED_ENABLED') || define('TESTS_ZEND_CACHE_MEMCACHED_ENABLED', true);
defined('TESTS_ZEND_CACHE_LIBMEMCACHED_ENABLED') || define('TESTS_ZEND_CACHE_LIBMEMCACHED_ENABLED', true);

/**
* Zend_Db_Adapter_Pdo_Mysql and Zend_Db_Adapter_Mysqli
*
* There are separate properties to enable tests for the PDO_MYSQL adapter and
* the native Mysqli adapters, but the other properties are shared between the
* two MySQL-related Zend_Db adapters.
*/
defined('TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_ENABLED') || define('TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_ENABLED', true);
defined('TESTS_ZEND_DB_ADAPTER_MYSQLI_ENABLED') || define('TESTS_ZEND_DB_ADAPTER_MYSQLI_ENABLED', true);
defined('TESTS_ZEND_DB_ADAPTER_MYSQL_USERNAME') || define('TESTS_ZEND_DB_ADAPTER_MYSQL_USERNAME', 'travis');
defined('TESTS_ZEND_DB_ADAPTER_MYSQL_PASSWORD') || define('TESTS_ZEND_DB_ADAPTER_MYSQL_PASSWORD', '');
defined('TESTS_ZEND_DB_ADAPTER_MYSQL_DATABASE') || define('TESTS_ZEND_DB_ADAPTER_MYSQL_DATABASE', 'zftest');

/**
* Zend_Db_Adapter_Pdo_Sqlite
*
* Username and password are irrelevant for SQLite.
*/
defined('TESTS_ZEND_DB_ADAPTER_PDO_SQLITE_ENABLED') || define('TESTS_ZEND_DB_ADAPTER_PDO_SQLITE_ENABLED', true);

/**
* Zend_Db_Adapter_Pdo_Pgsql
*/
defined('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_ENABLED') || define('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_ENABLED', true);
defined('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_USERNAME') || define('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_USERNAME', 'postgres');
defined('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_PASSWORD') || define('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_PASSWORD', '');
defined('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_DATABASE') || define('TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_DATABASE', 'zftest');


require_once dirname(__FILE__) . '/TestConfiguration.php.dist';
47 changes: 47 additions & 0 deletions tests/Zend/Db/Adapter/Pdo/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,51 @@ public function getDriver()
return 'Pdo_Sqlite';
}

public function testAdapterOptionFetchMode()
{
$params = $this->_util->getParams();

$params['options'] = array(
Zend_Db::FETCH_MODE => 'obj'
);
$db = Zend_Db::factory($this->getDriver(), $params);

//two extra lines to make SQLite work
$db->query('CREATE TABLE zfproducts (id)');
$db->insert('zfproducts', array('id' => 1));

$select = $db->select()->from('zfproducts');
$row = $db->fetchRow($select);
$this->assertTrue($row instanceof stdClass);
}

protected function _testAdapterAlternateStatement($stmtClass)
{
$ip = get_include_path();
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files';
$newIp = $dir . PATH_SEPARATOR . $ip;
set_include_path($newIp);

$params = $this->_util->getParams();

$params['options'] = array(
Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
);
$db = Zend_Db::factory($this->getDriver(), $params);
$db->getConnection();
$db->setStatementClass($stmtClass);

$currentStmtClass = $db->getStatementClass();
$this->assertEquals($stmtClass, $currentStmtClass);

//extra fix for SQLite
$db->query('CREATE TABLE zfbugs (id)');

$bugs = $this->_db->quoteIdentifier('zfbugs');

$stmt = $db->prepare("SELECT COUNT(*) FROM $bugs");

$this->assertTrue($stmt instanceof $stmtClass,
'Expecting object of type ' . $stmtClass . ', got ' . get_class($stmt));
}
}
24 changes: 24 additions & 0 deletions tests/Zend/Db/Select/Pdo/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function testSelectJoinRight()

public function testSelectGroupBy()
{
//SQLite doesn't need different test from 5.3
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
return parent::testSelectGroupBy();
}
$select = $this->_selectGroupBy();
$stmt = $this->_db->query($select);
$result = $stmt->fetchAll();
Expand All @@ -74,6 +78,10 @@ public function testSelectGroupBy()

public function testSelectGroupByQualified()
{
//SQLite doesn't need different test from 5.3
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
return parent::testSelectGroupByQualified();
}
$select = $this->_selectGroupByQualified();
$stmt = $this->_db->query($select);
$result = $stmt->fetchAll();
Expand All @@ -91,6 +99,10 @@ public function testSelectGroupByQualified()

public function testSelectHaving()
{
//SQLite doesn't need different test from 5.3
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
return parent::testSelectHaving();
}
$select = $this->_selectHaving();
$stmt = $this->_db->query($select);
$result = $stmt->fetchAll();
Expand All @@ -104,6 +116,10 @@ public function testSelectHaving()

public function testSelectHavingWithParameter()
{
//SQLite doesn't need different test from 5.3
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
return parent::testSelectHavingWithParameter();
}
$select = $this->_selectHavingWithParameter();
$stmt = $this->_db->query($select);
$result = $stmt->fetchAll();
Expand All @@ -117,6 +133,10 @@ public function testSelectHavingWithParameter()

public function testSelectHavingOr()
{
//SQLite doesn't need different test from 5.3
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
return parent::testSelectHavingOr();
}
$select = $this->_selectHavingOr();
$stmt = $this->_db->query($select);
$result = $stmt->fetchAll();
Expand All @@ -132,6 +152,10 @@ public function testSelectHavingOr()

public function testSelectHavingOrWithParameter()
{
//SQLite doesn't need different test from 5.3
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
return parent::testSelectHavingOrWithParameter();
}
$select = $this->_selectHavingOrWithParameter();
$stmt = $this->_db->query($select);
$result = $stmt->fetchAll();
Expand Down
18 changes: 9 additions & 9 deletions tests/Zend/Db/Select/TestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ protected function _selectWhereWithTypeFloat()

public function testSelectWhereWithTypeFloat()
{
$locale = setlocale(LC_ALL, null);
$locale = setlocale(LC_ALL, 0);

$select = $this->_selectWhereWithTypeFloat();
$stmt = $this->_db->query($select);
Expand Down Expand Up @@ -1697,7 +1697,7 @@ public function testSerializeSelect()
$serialize = serialize($this->_select());
$this->assertTrue(is_string($serialize));
}

/**
* @group ZF-3792
*/
Expand All @@ -1706,7 +1706,7 @@ public function testJoinUsingActuallyGeneratesAnInnerJoinOnForCompatibilityReaso
$table_A = $this->_db->quoteTableAs('A');
$table_B = $this->_db->quoteTableAs('B');
$colname = $this->_db->quoteIdentifier('colname');

$s = $this->_db->select()->from('A')->joinUsing('B', $colname);
$this->assertContains("JOIN {$table_B} ON {$table_B}.{$colname} = {$table_A}.{$colname}", $s->assemble());
}
Expand All @@ -1720,7 +1720,7 @@ public function testJoinUsingAllowsSpecifyingMultipleColumnsViaAnArray()
$table_B = $this->_db->quoteTableAs('B');
$colOne = $this->_db->quoteIdentifier('colOne');
$colTwo = $this->_db->quoteIdentifier('colTwo');

$s = $this->_db->select()->from('A')->joinUsing('B', array($colOne,$colTwo));
$this->assertContains(
"JOIN {$table_B} ON {$table_B}.{$colOne} = {$table_A}.{$colOne}"
Expand All @@ -1737,12 +1737,12 @@ public function testJoinUsingUsesTableNameOfTableBeingJoinedWhenAliasNotDefined(
$table1 = $this->_db->quoteTableAs('table1');
$table2 = $this->_db->quoteTableAs('table2');
$colname = $this->_db->quoteIdentifier('column1');

$select = $this->_db->select();
$select->from('table1')->joinUsing('table2', $colname);
$this->assertRegexp("/ON {$table2}.{$colname}/s", $select->assemble());
}

/**
* @group ZF-3309
*/
Expand All @@ -1751,10 +1751,10 @@ public function testJoinUsingUsesAliasOfTableBeingJoinedWhenAliasIsDefined()
$table1 = $this->_db->quoteTableAs('table1');
$table2_alias = $this->_db->quoteTableAs('t2');
$colname = $this->_db->quoteIdentifier('column1');

$select = $this->_db->select();
$select->from('table1')->joinUsing(array('t2'=>'table2'), $colname);
$this->assertRegexp("/ON {$table2_alias}.{$colname}/s", $select->assemble());
}
}

}
Loading

0 comments on commit 6fb5c4b

Please sign in to comment.