NoRecordExistsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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-2008 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. *
  43. */
  44. class Zend_Validate_Db_NoRecordExistsTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Set up test configuration
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->_db = new Zend_Db_Adapter_Pdo_Sqlite(
  54. array('dbname' => 'test')
  55. );
  56. Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
  57. $createTable = 'CREATE TABLE [users] ( '
  58. . '[id] INTEGER NOT NULL PRIMARY KEY, '
  59. . '[field1] VARCHAR(20),'
  60. . '[field2] VARCHAR(20) )';
  61. $this->_db->query($createTable);
  62. $insert1 = 'INSERT INTO users (id, field1, field2) '
  63. . 'VALUES (1, "value1", "value2")';
  64. $insert2 = 'INSERT INTO users (id, field1, field2) '
  65. . 'VALUES (2, "value3", "value4")';
  66. $this->_db->query($insert1);
  67. $this->_db->query($insert2);
  68. }
  69. /**
  70. * Test basic function of RecordExists (no exclusion)
  71. *
  72. * @return void
  73. */
  74. public function testBasicFindsRecord()
  75. {
  76. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
  77. $this->assertFalse($validator->isValid('value1'));
  78. }
  79. /**
  80. * Test basic function of RecordExists (no exclusion)
  81. *
  82. * @return void
  83. */
  84. public function testBasicFindsNoRecord()
  85. {
  86. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1');
  87. $this->assertTrue($validator->isValid('nosuchvalue'));
  88. }
  89. /**
  90. * Test the exclusion function
  91. *
  92. * @return void
  93. */
  94. public function testExcludeWithArray()
  95. {
  96. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
  97. $this->assertFalse($validator->isValid('value3'));
  98. }
  99. /**
  100. * Test the exclusion function
  101. * with an array
  102. *
  103. * @return void
  104. */
  105. public function testExcludeWithArrayNoRecord()
  106. {
  107. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', array('field' => 'id', 'value' => 1));
  108. $this->assertTrue($validator->isValid('nosuchvalue'));
  109. }
  110. /**
  111. * Test the exclusion function
  112. * with a string
  113. *
  114. * @return void
  115. */
  116. public function testExcludeWithString()
  117. {
  118. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  119. $this->assertFalse($validator->isValid('value3'));
  120. }
  121. /**
  122. * Test the exclusion function
  123. * with a string
  124. *
  125. * @return void
  126. */
  127. public function testExcludeWithStringNoRecord()
  128. {
  129. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  130. $this->assertTrue($validator->isValid('nosuchvalue'));
  131. }
  132. /**
  133. * Test that the class throws an exception if no adapter is provided
  134. * and no default is set.
  135. *
  136. * @return void
  137. */
  138. public function testThrowsExceptionWithNoAdapter()
  139. {
  140. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  141. try {
  142. $validator = new Zend_Validate_Db_NoRecordExists('users', 'field1', 'id != 1');
  143. $valid = $validator->isValid('nosuchvalue');
  144. $this->markTestFailed('Did not throw exception');
  145. } catch (Exception $e) {
  146. }
  147. }
  148. public function tearDown()
  149. {
  150. $dropTable = 'DROP TABLE [users]';
  151. $this->_db->query($dropTable);
  152. }
  153. }