Sfoglia il codice sorgente

Refactored Zend_Validate_Db tests to use a mock adapter, and added tests for schema support

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17156 44c647ce-9c0f-0410-b52a-842ac1e357ba
bittarman 16 anni fa
parent
commit
42f1043d0e

+ 0 - 1
tests/Zend/Validate/Db/AllTests.php

@@ -1,6 +1,5 @@
 <?php
 
-
 require_once dirname(__FILE__) . '/../../../TestHelper.php';
 
 if (!defined('PHPUnit_MAIN_METHOD')) {

+ 90 - 27
tests/Zend/Validate/Db/NoRecordExistsTest.php

@@ -48,10 +48,32 @@ require_once 'Zend/Validate/Db/Abstract.php';
 require_once 'Zend/Validate/Db/NoRecordExists.php';
 
 /**
+ * Mock No Result Adapter
+ */
+require_once dirname(__FILE__) . '/_files/Db/MockNoResult.php';
+
+/**
+ * Mock Result Adapter
+ */
+require_once dirname(__FILE__) . '/_files/Db/MockHasResult.php';
+
+
+/**
  *
  */
 class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
 {
+
+    /**
+     * @var Zend_Db_Adapter_Abstract
+     */
+    protected $_adapterHasResult;
+    
+    /**
+     * @var Zend_Db_Adapter_Abstract
+     */
+    protected $_adapterNoResult;
+    
     /**
      * Set up test configuration
      *
@@ -59,29 +81,8 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function setUp()
     {
-        if (!extension_loaded('pdo_sqlite')) {
-            $this->markTestSkipped('No sqlite available');
-        }
-
-        $this->_db = new Zend_Db_Adapter_Pdo_Sqlite(
-            array('dbname' => ':memory:')
-        );
-
-        Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
-
-        $createTable = 'CREATE TABLE [users] ( '
-                   . '[id] INTEGER  NOT NULL PRIMARY KEY, '
-                   . '[field1] VARCHAR(20),'
-                   . '[field2] VARCHAR(20) )';
-        $this->_db->query($createTable);
-
-        $insert1 = 'INSERT INTO users (id, field1, field2) '
-                            . 'VALUES (1, "value1", "value2")';
-        $insert2 = 'INSERT INTO users (id, field1, field2) '
-                            . 'VALUES (2, "value3", "value4")';
-
-        $this->_db->query($insert1);
-        $this->_db->query($insert2);
+        $this->_adapterHasResult = new Db_MockHasResult();
+        $this->_adapterNoResult = new Db_MockNoResult();        
 
     }
 
@@ -92,6 +93,7 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testBasicFindsRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
         $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
         $this->assertFalse($validator->isValid('value1'));
     }
@@ -103,6 +105,7 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testBasicFindsNoRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
         $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
         $this->assertTrue($validator->isValid('nosuchvalue'));
     }
@@ -114,6 +117,7 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithArray()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
         $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
         $this->assertFalse($validator->isValid('value3'));
     }
@@ -126,6 +130,7 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithArrayNoRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
         $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
         $this->assertTrue($validator->isValid('nosuchvalue'));
     }
@@ -138,6 +143,7 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithString()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
         $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
         $this->assertFalse($validator->isValid('value3'));
     }
@@ -149,7 +155,8 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
      * @return void
      */
     public function testExcludeWithStringNoRecord()
-    {
+    {        
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
         $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
         $this->assertTrue($validator->isValid('nosuchvalue'));
     }
@@ -170,10 +177,66 @@ class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
         } catch (Exception $e) {
         }
     }
+    
+    /**
+     * Test that schemas are supported and run without error
+     *
+     * @return void
+     */
+    public function testWithSchema()
+    {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
+        $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'users',
+                                                               'schema' => 'my'),
+                                                         'field1');
+        $this->assertFalse($validator->isValid('value1'));
+    }
+    
+    /**
+     * Test that schemas are supported and run without error
+     *
+     * @return void
+     */
+    public function testWithSchemaNoResult()
+    {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
+        $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'users',
+                                                               'schema' => 'my'),
+                                                         'field1');
+        $this->assertTrue($validator->isValid('value1'));
+    }
 
-    public function tearDown()
+    /**
+     * Test when adapter is provided
+     *
+     * @return void
+     */
+    public function testAdapterProvided()
     {
-        $dropTable = 'DROP TABLE [users]';
-        $this->_db->query($dropTable);
+        //clear the default adapter to ensure provided one is used
+        Zend_Db_Table_Abstract::setDefaultAdapter(null);
+        try {
+            $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', null, $this->_adapterHasResult);
+            $this->assertFalse($validator->isValid('value1'));
+        } catch (Exception $e) {
+            $this->markTestFailed('Threw an exception when adapter was provided');
+        }
+    }
+    
+    /**
+     * Test when adapter is provided
+     *
+     * @return void
+     */
+    public function testAdapterProvidedNoResult()
+    {
+        //clear the default adapter to ensure provided one is used
+        Zend_Db_Table_Abstract::setDefaultAdapter(null);
+        try {
+            $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', null, $this->_adapterNoResult);
+            $this->assertTrue($validator->isValid('value1'));
+        } catch (Exception $e) {
+            $this->markTestFailed('Threw an exception when adapter was provided');
+        }
     }
 }

+ 88 - 26
tests/Zend/Validate/Db/RecordExistsTest.php

@@ -48,10 +48,31 @@ require_once 'Zend/Validate/Db/Abstract.php';
 require_once 'Zend/Validate/Db/RecordExists.php';
 
 /**
+ * Mock No Result Adapter
+ */
+require_once dirname(__FILE__) . '/_files/Db/MockNoResult.php';
+
+/**
+ * Mock Result Adapter
+ */
+require_once dirname(__FILE__) . '/_files/Db/MockHasResult.php';
+
+/**
  *
  */
 class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
 {
+    
+    /**
+     * @var Zend_Db_Adapter_Abstract
+     */
+    protected $_adapterHasResult;
+    
+    /**
+     * @var Zend_Db_Adapter_Abstract
+     */
+    protected $_adapterNoResult;
+    
     /**
      * Set up test configuration
      *
@@ -59,29 +80,8 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function setUp()
     {
-        if (!extension_loaded('pdo_sqlite')) {
-            $this->markTestSkipped('No sqlite available');
-        }
-
-        $this->_db = new Zend_Db_Adapter_Pdo_Sqlite(
-            array('dbname' => ':memory:')
-        );
-
-        Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
-
-        $createTable = 'CREATE TABLE [users] ( '
-                   . '[id] INTEGER  NOT NULL PRIMARY KEY, '
-                   . '[field1] VARCHAR(20),'
-                   . '[field2] VARCHAR(20) )';
-        $this->_db->query($createTable);
-
-        $insert1 = 'INSERT INTO users (id, field1, field2) '
-                            . 'VALUES (1, "value1", "value2")';
-        $insert2 = 'INSERT INTO users (id, field1, field2) '
-                            . 'VALUES (2, "value3", "value4")';
-
-        $this->_db->query($insert1);
-        $this->_db->query($insert2);
+        $this->_adapterHasResult = new Db_MockHasResult();
+        $this->_adapterNoResult = new Db_MockNoResult();        
 
     }
 
@@ -92,6 +92,7 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testBasicFindsRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
         $validator = new Zend_Validate_Db_RecordExists('users', 'field1');
         $this->assertTrue($validator->isValid('value1'));
     }
@@ -103,6 +104,7 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testBasicFindsNoRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
         $validator = new Zend_Validate_Db_RecordExists('users', 'field1');
         $this->assertFalse($validator->isValid('nosuchvalue'));
     }
@@ -114,6 +116,7 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithArray()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
         $validator = new Zend_Validate_Db_RecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
         $this->assertTrue($validator->isValid('value3'));
     }
@@ -126,6 +129,7 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithArrayNoRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
         $validator = new Zend_Validate_Db_RecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
         $this->assertFalse($validator->isValid('nosuchvalue'));
     }
@@ -138,6 +142,7 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithString()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
         $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
         $this->assertTrue($validator->isValid('value3'));
     }
@@ -150,6 +155,7 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
      */
     public function testExcludeWithStringNoRecord()
     {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
         $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
         $this->assertFalse($validator->isValid('nosuchvalue'));
     }
@@ -171,9 +177,65 @@ class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
         }
     }
 
-    public function tearDown()
+    /**
+     * Test that schemas are supported and run without error
+     *
+     * @return void
+     */
+    public function testWithSchema()
     {
-        $dropTable = 'DROP TABLE [users]';
-        $this->_db->query($dropTable);
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
+        $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users',
+                                                               'schema' => 'my'),
+                                                         'field1');
+        $this->assertTrue($validator->isValid('value1'));
+    }
+    
+    /**
+     * Test that schemas are supported and run without error
+     *
+     * @return void
+     */
+    public function testWithSchemaNoResult()
+    {
+        Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
+        $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users',
+                                                               'schema' => 'my'),
+                                                         'field1');
+        $this->assertFalse($validator->isValid('value1'));
+    }
+    
+    /**
+     * Test when adapter is provided
+     *
+     * @return void
+     */
+    public function testAdapterProvided()
+    {
+        //clear the default adapter to ensure provided one is used
+        Zend_Db_Table_Abstract::setDefaultAdapter(null);
+        try {
+            $validator = new Zend_Validate_Db_RecordExists('users', 'field1', null, $this->_adapterHasResult);
+            $this->assertTrue($validator->isValid('value1'));
+        } catch (Exception $e) {
+            $this->markTestFailed('Threw an exception when adapter was provided');
+        }
+    }
+    
+    /**
+     * Test when adapter is provided
+     *
+     * @return void
+     */
+    public function testAdapterProvidedNoResult()
+    {
+        //clear the default adapter to ensure provided one is used
+        Zend_Db_Table_Abstract::setDefaultAdapter(null);
+        try {
+            $validator = new Zend_Validate_Db_RecordExists('users', 'field1', null, $this->_adapterNoResult);
+            $this->assertFalse($validator->isValid('value1'));
+        } catch (Exception $e) {
+            $this->markTestFailed('Threw an exception when adapter was provided');
+        }
     }
 }

+ 117 - 0
tests/Zend/Validate/Db/_files/Db/MockHasResult.php

@@ -0,0 +1,117 @@
+<?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 license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Validate
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * @see Zend_Db_Adapter_Abstract
+ */
+require_once 'Zend/Db/Adapter/Abstract.php';
+
+/**
+ * Mock Db adapter for Zend_Validate_Db tests
+ *
+ */
+class Db_MockHasResult extends Zend_Db_Adapter_Abstract
+{
+    /**
+     * Returns an array to emulate a result
+     *
+     * @param string|Zend_Db_Select $sql An SQL SELECT statement.
+     * @param mixed $bind Data to bind into SELECT placeholders.
+     * @param mixed                 $fetchMode Override current fetch mode.
+     * @return array
+     */
+    public function fetchRow($sql, $bind = array(), $fetchMode = null)
+    {
+        return array('one' => 'one');
+    }
+    
+    /**
+     * Override for the constructor
+     * @param array $config
+     */
+    public function __construct($config = null)
+    {
+        // Do Nothing.
+    }
+    
+    /**
+     * The below methods are un-needed, but need to be implemented for this to 
+     * be a concrete class
+     */
+    public function listTables()
+    {
+        return null;
+    }
+    public function describeTable($tableName, $schemaName = null)
+    {
+        return null;
+    }
+    protected function _connect()
+    {
+        return null;
+    }
+    public function isConnected()
+    {
+        return null;
+    }
+    public function closeConnection()
+    {
+        return null;
+    }
+    public function prepare($sql)
+    {
+        return null;
+    }
+    public function lastInsertId($tableName = null, $primaryKey = null)
+    {
+        return null;
+    }
+    protected function _beginTransaction()
+    {
+        return null;
+    }
+    protected function _commit()
+    {
+        return null;
+    }
+    protected function _rollBack()
+    {
+        return null;
+    }
+    public function setFetchMode($mode)
+    {
+        return null;
+    }
+    public function limit($sql, $count, $offset = 0)
+    {
+        return null;
+    }
+    public function supportsParameters($type)
+    {
+        return null;
+    }
+    public function getServerVersion()
+    {
+        return null;
+    }
+
+}

+ 118 - 0
tests/Zend/Validate/Db/_files/Db/MockNoResult.php

@@ -0,0 +1,118 @@
+<?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 license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Validate
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * @see Zend_Db_Adapter_Abstract
+ */
+require_once 'Zend/Db/Adapter/Abstract.php';
+
+/**
+ * Mock Db adapter for Zend_Validate_Db tests
+ * 
+ */
+class Db_MockNoResult extends Zend_Db_Adapter_Abstract
+{
+    
+    /**
+     * Returns a fixed result
+     *
+     * @param string|Zend_Db_Select $sql An SQL SELECT statement.
+     * @param mixed $bind Data to bind into SELECT placeholders.
+     * @param mixed                 $fetchMode Override current fetch mode.
+     * @return null
+     */
+    public function fetchRow($sql, $bind = array(), $fetchMode = null)
+    {
+        return null;
+    }
+    
+    /**
+     * Override for the constructor
+     * @param array $config
+     */
+    public function __construct($config = null)
+    {
+        // Do Nothing.
+    }
+    
+    /**
+     * The below methods are un-needed, but need to be implemented for this to 
+     * be a concrete class
+     */
+    public function listTables()
+    {
+        return null;
+    }
+    public function describeTable($tableName, $schemaName = null)
+    {
+        return null;
+    }
+    protected function _connect()
+    {
+        return null;
+    }
+    public function isConnected()
+    {
+        return null;
+    }
+    public function closeConnection()
+    {
+        return null;
+    }
+    public function prepare($sql)
+    {
+        return null;
+    }
+    public function lastInsertId($tableName = null, $primaryKey = null)
+    {
+        return null;
+    }
+    protected function _beginTransaction()
+    {
+        return null;
+    }
+    protected function _commit()
+    {
+        return null;
+    }
+    protected function _rollBack()
+    {
+        return null;
+    }
+    public function setFetchMode($mode)
+    {
+        return null;
+    }
+    public function limit($sql, $count, $offset = 0)
+    {
+        return null;
+    }
+    public function supportsParameters($type)
+    {
+        return null;
+    }
+    public function getServerVersion()
+    {
+        return null;
+    }
+
+}