RecordExistsTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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-2012 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. /**
  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/RecordExists.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. * @category Zend
  51. * @package Zend_Validate
  52. * @subpackage UnitTests
  53. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  54. * @license http://framework.zend.com/license/new-bsd New BSD License
  55. * @group Zend_Validate
  56. */
  57. class Zend_Validate_Db_RecordExistsTest extends PHPUnit_Framework_TestCase
  58. {
  59. /**
  60. * @var Zend_Db_Adapter_Abstract
  61. */
  62. protected $_adapterHasResult;
  63. /**
  64. * @var Zend_Db_Adapter_Abstract
  65. */
  66. protected $_adapterNoResult;
  67. /**
  68. * Set up test configuration
  69. *
  70. * @return void
  71. */
  72. public function setUp()
  73. {
  74. $this->_adapterHasResult = new Db_MockHasResult();
  75. $this->_adapterNoResult = new Db_MockNoResult();
  76. }
  77. /**
  78. * Test basic function of RecordExists (no exclusion)
  79. *
  80. * @return void
  81. */
  82. public function testBasicFindsRecord()
  83. {
  84. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  85. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1'));
  86. $this->assertTrue($validator->isValid('value1'));
  87. }
  88. /**
  89. * Test basic function of RecordExists (no exclusion)
  90. *
  91. * @return void
  92. */
  93. public function testBasicFindsNoRecord()
  94. {
  95. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  96. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1'));
  97. $this->assertFalse($validator->isValid('nosuchvalue'));
  98. }
  99. /**
  100. * Test the exclusion function
  101. *
  102. * @return void
  103. */
  104. public function testExcludeWithArray()
  105. {
  106. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  107. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1', 'exclude' => array('field' => 'id', 'value' => 1)));
  108. $this->assertTrue($validator->isValid('value3'));
  109. }
  110. /**
  111. * Test the exclusion function
  112. * with an array
  113. *
  114. * @return void
  115. */
  116. public function testExcludeWithArrayNoRecord()
  117. {
  118. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  119. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1', 'exclude' => array('field' => 'id', 'value' => 1)));
  120. $this->assertFalse($validator->isValid('nosuchvalue'));
  121. }
  122. /**
  123. * Test the exclusion function
  124. * with a string
  125. *
  126. * @return void
  127. */
  128. public function testExcludeWithString()
  129. {
  130. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  131. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users', 'field' => 'field1', 'exclude' => 'id != 1'));
  132. $this->assertTrue($validator->isValid('value3'));
  133. }
  134. /**
  135. * Test the exclusion function
  136. * with a string
  137. *
  138. * @return void
  139. */
  140. public function testExcludeWithStringNoRecord()
  141. {
  142. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  143. $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
  144. $this->assertFalse($validator->isValid('nosuchvalue'));
  145. }
  146. /**
  147. * Test that the class throws an exception if no adapter is provided
  148. * and no default is set.
  149. *
  150. * @return void
  151. */
  152. public function testThrowsExceptionWithNoAdapter()
  153. {
  154. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  155. try {
  156. $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
  157. $valid = $validator->isValid('nosuchvalue');
  158. $this->markTestFailed('Did not throw exception');
  159. } catch (Exception $e) {
  160. }
  161. }
  162. /**
  163. * Test that schemas are supported and run without error
  164. *
  165. * @return void
  166. */
  167. public function testWithSchema()
  168. {
  169. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  170. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users',
  171. 'schema' => 'my',
  172. 'field' => 'field1'));
  173. $this->assertTrue($validator->isValid('value1'));
  174. }
  175. /**
  176. * Test that schemas are supported and run without error
  177. *
  178. * @return void
  179. */
  180. public function testWithSchemaNoResult()
  181. {
  182. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterNoResult);
  183. $validator = new Zend_Validate_Db_RecordExists(array('table' => 'users',
  184. 'schema' => 'my',
  185. 'field' => 'field1'));
  186. $this->assertFalse($validator->isValid('value1'));
  187. }
  188. /**
  189. * Test when adapter is provided
  190. *
  191. * @return void
  192. */
  193. public function testAdapterProvided()
  194. {
  195. //clear the default adapter to ensure provided one is used
  196. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  197. try {
  198. $validator = new Zend_Validate_Db_RecordExists('users', 'field1', null, $this->_adapterHasResult);
  199. $this->assertTrue($validator->isValid('value1'));
  200. } catch (Zend_Exception $e) {
  201. $this->markTestSkipped('No database available');
  202. }
  203. }
  204. /**
  205. * Test when adapter is provided
  206. *
  207. * @return void
  208. */
  209. public function testAdapterProvidedNoResult()
  210. {
  211. //clear the default adapter to ensure provided one is used
  212. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  213. try {
  214. $validator = new Zend_Validate_Db_RecordExists('users', 'field1', null, $this->_adapterNoResult);
  215. $this->assertFalse($validator->isValid('value1'));
  216. } catch (Exception $e) {
  217. $this->markTestSkipped('No database available');
  218. }
  219. }
  220. /**
  221. * @return ZF-8863
  222. */
  223. public function testExcludeConstructor()
  224. {
  225. Zend_Db_Table_Abstract::setDefaultAdapter($this->_adapterHasResult);
  226. $validator = new Zend_Validate_Db_RecordExists('users', 'field1', 'id != 1');
  227. $this->assertTrue($validator->isValid('value3'));
  228. }
  229. }