NoRecordExistsTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. */
  21. /**
  22. * PHPUnit_Framework_TestCase
  23. */
  24. require_once 'PHPUnit/Framework/TestCase.php';
  25. /**
  26. * @see Zend_Db_Adapter_Pdo_Sqlite
  27. */
  28. require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
  29. /**
  30. * @see Zend_Db_Table_Abstract
  31. */
  32. require_once 'Zend/Db/Table/Abstract.php';
  33. /**
  34. * @see Zend_Validate_Db_Abstract.php
  35. */
  36. require_once 'Zend/Validate/Db/Abstract.php';
  37. /**
  38. * @see Zend_Validate_Db_RecordExists.php
  39. */
  40. require_once 'Zend/Validate/Db/NoRecordExists.php';
  41. /**
  42. * Mock No Result Adapter
  43. */
  44. require_once dirname(__FILE__) . '/_files/Db/MockNoResult.php';
  45. /**
  46. * Mock Result Adapter
  47. */
  48. require_once dirname(__FILE__) . '/_files/Db/MockHasResult.php';
  49. /**
  50. *
  51. */
  52. class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
  53. {
  54. /**
  55. * @var Zend_Db_Adapter_Abstract
  56. */
  57. protected $_adapterHasResult;
  58. /**
  59. * @var Zend_Db_Adapter_Abstract
  60. */
  61. protected $_adapterNoResult;
  62. /**
  63. * Set up test configuration
  64. *
  65. * @return void
  66. */
  67. public function setUp()
  68. {
  69. $this->_adapterHasResult = new Db_MockHasResult();
  70. $this->_adapterNoResult = new Db_MockNoResult();
  71. }
  72. /**
  73. * Test basic function of RecordExists (no exclusion)
  74. *
  75. * @return void
  76. */
  77. public function testBasicFindsRecord()
  78. {
  79. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  80. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
  81. $this->assertFalse($validator->isValid('value1'));
  82. }
  83. /**
  84. * Test basic function of RecordExists (no exclusion)
  85. *
  86. * @return void
  87. */
  88. public function testBasicFindsNoRecord()
  89. {
  90. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  91. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
  92. $this->assertTrue($validator->isValid('nosuchvalue'));
  93. }
  94. /**
  95. * Test the exclusion function
  96. *
  97. * @return void
  98. */
  99. public function testExcludeWithArray()
  100. {
  101. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  102. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
  103. $this->assertFalse($validator->isValid('value3'));
  104. }
  105. /**
  106. * Test the exclusion function
  107. * with an array
  108. *
  109. * @return void
  110. */
  111. public function testExcludeWithArrayNoRecord()
  112. {
  113. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  114. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
  115. $this->assertTrue($validator->isValid('nosuchvalue'));
  116. }
  117. /**
  118. * Test the exclusion function
  119. * with a string
  120. *
  121. * @return void
  122. */
  123. public function testExcludeWithString()
  124. {
  125. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  126. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  127. $this->assertFalse($validator->isValid('value3'));
  128. }
  129. /**
  130. * Test the exclusion function
  131. * with a string
  132. *
  133. * @return void
  134. */
  135. public function testExcludeWithStringNoRecord()
  136. {
  137. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  138. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  139. $this->assertTrue($validator->isValid('nosuchvalue'));
  140. }
  141. /**
  142. * Test that the class throws an exception if no adapter is provided
  143. * and no default is set.
  144. *
  145. * @return void
  146. */
  147. public function testThrowsExceptionWithNoAdapter()
  148. {
  149. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  150. try {
  151. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  152. $valid = $validator->isValid('nosuchvalue');
  153. $this->markTestFailed('Did not throw exception');
  154. } catch (Exception $e) {
  155. }
  156. }
  157. /**
  158. * Test that schemas are supported and run without error
  159. *
  160. * @return void
  161. */
  162. public function testWithSchema()
  163. {
  164. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  165. $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'users',
  166. 'schema' => 'my'),
  167. 'field1');
  168. $this->assertFalse($validator->isValid('value1'));
  169. }
  170. /**
  171. * Test that schemas are supported and run without error
  172. *
  173. * @return void
  174. */
  175. public function testWithSchemaNoResult()
  176. {
  177. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  178. $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'users',
  179. 'schema' => 'my'),
  180. 'field1');
  181. $this->assertTrue($validator->isValid('value1'));
  182. }
  183. /**
  184. * Test when adapter is provided
  185. *
  186. * @return void
  187. */
  188. public function testAdapterProvided()
  189. {
  190. //clear the default adapter to ensure provided one is used
  191. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  192. try {
  193. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', null, $this->_adapterHasResult);
  194. $this->assertFalse($validator->isValid('value1'));
  195. } catch (Exception $e) {
  196. $this->markTestFailed('Threw an exception when adapter was provided');
  197. }
  198. }
  199. /**
  200. * Test when adapter is provided
  201. *
  202. * @return void
  203. */
  204. public function testAdapterProvidedNoResult()
  205. {
  206. //clear the default adapter to ensure provided one is used
  207. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  208. try {
  209. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', null, $this->_adapterNoResult);
  210. $this->assertTrue($validator->isValid('value1'));
  211. } catch (Exception $e) {
  212. $this->markTestFailed('Threw an exception when adapter was provided');
  213. }
  214. }
  215. }