| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?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-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * PHPUnit_Framework_TestCase
- */
- /**
- * @see Zend_Db_Adapter_Pdo_Sqlite
- */
- require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
- /**
- * @see Zend_Db_Table_Abstract
- */
- require_once 'Zend/Db/Table/Abstract.php';
- /**
- * @see Zend_Validate_Db_Abstract.php
- */
- require_once 'Zend/Validate/Db/Abstract.php';
- /**
- * @see Zend_Validate_Db_RecordExists.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';
- /**
- * @category Zend
- * @package Zend_Validate
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Validate
- */
- 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
- *
- * @return void
- */
- public function setUp()
- {
- $this->_adapterHasResult = new Db_MockHasResult();
- $this->_adapterNoResult = new Db_MockNoResult();
- }
- /**
- * Test basic function of RecordExists (no exclusion)
- *
- * @return void
- */
- public function testBasicFindsRecord()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
- $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1'));
- $this->assertTrue($validator->isValid('value1'));
- }
- /**
- * Test basic function of RecordExists (no exclusion)
- *
- * @return void
- */
- public function testBasicFindsNoRecord()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
- $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1'));
- $this->assertFalse($validator->isValid('nosuchvalue'));
- }
- /**
- * Test the exclusion function
- *
- * @return void
- */
- public function testExcludeWithArray()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
- $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1', 'exclude' => array('field' => 'id', 'value' => 1)));
- $this->assertTrue($validator->isValid('value3'));
- }
- /**
- * Test the exclusion function
- * with an array
- *
- * @return void
- */
- public function testExcludeWithArrayNoRecord()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
- $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1', 'exclude' => array('field' => 'id', 'value' => 1)));
- $this->assertFalse($validator->isValid('nosuchvalue'));
- }
- /**
- * Test the exclusion function
- * with a string
- *
- * @return void
- */
- public function testExcludeWithString()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
- $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1', 'exclude' => 'id != 1'));
- $this->assertTrue($validator->isValid('value3'));
- }
- /**
- * Test the exclusion function
- * with a string
- *
- * @return void
- */
- 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'));
- }
- /**
- * Test that the class throws an exception if no adapter is provided
- * and no default is set.
- *
- * @return void
- */
- public function testThrowsExceptionWithNoAdapter()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter(null);
- try {
- $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
- $valid = $validator->isValid('nosuchvalue');
- $this->markTestFailed('Did not throw exception');
- } 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_RecordExists(array('table' => 'users',
- 'schema' => 'my',
- 'field' => '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',
- 'field' => '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 (Zend_Exception $e) {
- $this->markTestSkipped('No database available');
- }
- }
- /**
- * 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->markTestSkipped('No database available');
- }
- }
- /**
- * @return ZF-8863
- */
- public function testExcludeConstructor()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
- $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
- $this->assertTrue($validator->isValid('value3'));
- }
- }
|