NoRecordExistsTest.php 4.6 KB

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