NoRecordExistsTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * PHPUnit_Framework_TestCase
  24. */
  25. require_once 'PHPUnit/Framework/TestCase.php';
  26. /**
  27. * @see Zend_Db_Adapter_Pdo_Sqlite
  28. */
  29. require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
  30. /**
  31. * @see Zend_Db_Table_Abstract
  32. */
  33. require_once 'Zend/Db/Table/Abstract.php';
  34. /**
  35. * @see Zend_Validate_Db_Abstract.php
  36. */
  37. require_once 'Zend/Validate/Db/Abstract.php';
  38. /**
  39. * @see Zend_Validate_Db_RecordExists.php
  40. */
  41. require_once 'Zend/Validate/Db/NoRecordExists.php';
  42. /**
  43. * Mock No Result Adapter
  44. */
  45. require_once dirname(__FILE__) . '/_files/Db/MockNoResult.php';
  46. /**
  47. * Mock Result Adapter
  48. */
  49. require_once dirname(__FILE__) . '/_files/Db/MockHasResult.php';
  50. /**
  51. * @category Zend
  52. * @package Zend_Validate
  53. * @subpackage UnitTests
  54. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  55. * @license http://framework.zend.com/license/new-bsd New BSD License
  56. * @group Zend_Validate
  57. */
  58. class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
  59. {
  60. /**
  61. * @var Zend_Db_Adapter_Abstract
  62. */
  63. protected $_adapterHasResult;
  64. /**
  65. * @var Zend_Db_Adapter_Abstract
  66. */
  67. protected $_adapterNoResult;
  68. /**
  69. * Set up test configuration
  70. *
  71. * @return void
  72. */
  73. public function setUp()
  74. {
  75. $this->_adapterHasResult = new Db_MockHasResult();
  76. $this->_adapterNoResult = new Db_MockNoResult();
  77. }
  78. /**
  79. * Test basic function of RecordExists (no exclusion)
  80. *
  81. * @return void
  82. */
  83. public function testBasicFindsRecord()
  84. {
  85. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  86. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
  87. $this->assertFalse($validator->isValid('value1'));
  88. }
  89. /**
  90. * Test basic function of RecordExists (no exclusion)
  91. *
  92. * @return void
  93. */
  94. public function testBasicFindsNoRecord()
  95. {
  96. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  97. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
  98. $this->assertTrue($validator->isValid('nosuchvalue'));
  99. }
  100. /**
  101. * Test the exclusion function
  102. *
  103. * @return void
  104. */
  105. public function testExcludeWithArray()
  106. {
  107. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  108. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
  109. $this->assertFalse($validator->isValid('value3'));
  110. }
  111. /**
  112. * Test the exclusion function
  113. * with an array
  114. *
  115. * @return void
  116. */
  117. public function testExcludeWithArrayNoRecord()
  118. {
  119. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  120. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
  121. $this->assertTrue($validator->isValid('nosuchvalue'));
  122. }
  123. /**
  124. * Test the exclusion function
  125. * with a string
  126. *
  127. * @return void
  128. */
  129. public function testExcludeWithString()
  130. {
  131. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  132. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  133. $this->assertFalse($validator->isValid('value3'));
  134. }
  135. /**
  136. * Test the exclusion function
  137. * with a string
  138. *
  139. * @return void
  140. */
  141. public function testExcludeWithStringNoRecord()
  142. {
  143. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  144. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  145. $this->assertTrue($validator->isValid('nosuchvalue'));
  146. }
  147. /**
  148. * Test that the class throws an exception if no adapter is provided
  149. * and no default is set.
  150. *
  151. * @return void
  152. */
  153. public function testThrowsExceptionWithNoAdapter()
  154. {
  155. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  156. try {
  157. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  158. $valid = $validator->isValid('nosuchvalue');
  159. $this->markTestFailed('Did not throw exception');
  160. } catch (Exception $e) {
  161. }
  162. }
  163. /**
  164. * Test that schemas are supported and run without error
  165. *
  166. * @return void
  167. */
  168. public function testWithSchema()
  169. {
  170. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  171. $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'users',
  172. 'schema' => 'my'),
  173. 'field1');
  174. $this->assertFalse($validator->isValid('value1'));
  175. }
  176. /**
  177. * Test that schemas are supported and run without error
  178. *
  179. * @return void
  180. */
  181. public function testWithSchemaNoResult()
  182. {
  183. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  184. $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'users',
  185. 'schema' => 'my'),
  186. 'field1');
  187. $this->assertTrue($validator->isValid('value1'));
  188. }
  189. /**
  190. * Test when adapter is provided
  191. *
  192. * @return void
  193. */
  194. public function testAdapterProvided()
  195. {
  196. //clear the default adapter to ensure provided one is used
  197. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  198. try {
  199. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', null, $this->_adapterHasResult);
  200. $this->assertFalse($validator->isValid('value1'));
  201. } catch (Exception $e) {
  202. $this->markTestSkipped('No database available');
  203. }
  204. }
  205. /**
  206. * Test when adapter is provided
  207. *
  208. * @return void
  209. */
  210. public function testAdapterProvidedNoResult()
  211. {
  212. //clear the default adapter to ensure provided one is used
  213. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  214. try {
  215. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', null, $this->_adapterNoResult);
  216. $this->assertTrue($validator->isValid('value1'));
  217. } catch (Exception $e) {
  218. $this->markTestSkipped('No database available');
  219. }
  220. }
  221. }