FilterTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_Filter
  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. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../TestHelper.php';
  26. /**
  27. * @see Zend_Filter
  28. */
  29. require_once 'Zend/Filter.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Filter
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_FilterTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Zend_Filter object
  41. *
  42. * @var Zend_Filter
  43. */
  44. protected $_filter;
  45. /**
  46. * Creates a new Zend_Filter object for each test method
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. $this->error = null;
  53. $this->_filter = new Zend_Filter();
  54. }
  55. /**
  56. * Resets the default namespaces
  57. *
  58. * @return void
  59. */
  60. public function tearDown()
  61. {
  62. Zend_Filter::setDefaultNamespaces(array());
  63. }
  64. /**
  65. * Ensures expected return value from empty filter chain
  66. *
  67. * @return void
  68. */
  69. public function testEmpty()
  70. {
  71. $value = 'something';
  72. $this->assertEquals($value, $this->_filter->filter($value));
  73. }
  74. /**
  75. * Ensures that filters are executed in the expected order (FIFO)
  76. *
  77. * @return void
  78. */
  79. public function testFilterOrder()
  80. {
  81. $this->_filter->addFilter(new Zend_FilterTest_LowerCase())
  82. ->addFilter(new Zend_FilterTest_StripUpperCase());
  83. $value = 'AbC';
  84. $valueExpected = 'abc';
  85. $this->assertEquals($valueExpected, $this->_filter->filter($value));
  86. }
  87. /**
  88. * Ensures that we can call the static method get()
  89. * to instantiate a named validator by its class basename
  90. * and it returns the result of filter() with the input.
  91. */
  92. public function testStaticFactory()
  93. {
  94. $filteredValue = Zend_Filter::get('1a2b3c4d', 'Digits');
  95. $this->assertEquals('1234', $filteredValue);
  96. }
  97. /**
  98. * Ensures that a validator with constructor arguments can be called
  99. * with the static method get().
  100. */
  101. public function testStaticFactoryWithConstructorArguments()
  102. {
  103. // Test HtmlEntities with one ctor argument.
  104. $filteredValue = Zend_Filter::get('"O\'Reilly"', 'HtmlEntities', array(array('quotestyle' => ENT_COMPAT)));
  105. $this->assertEquals('&quot;O\'Reilly&quot;', $filteredValue);
  106. // Test HtmlEntities with a different ctor argument,
  107. // and make sure it gives the correct response
  108. // so we know it passed the arg to the ctor.
  109. $filteredValue = Zend_Filter::get('"O\'Reilly"', 'HtmlEntities', array(array('quotestyle' => ENT_QUOTES)));
  110. $this->assertEquals('&quot;O&#039;Reilly&quot;', $filteredValue);
  111. }
  112. /**
  113. * Ensures that if we specify a validator class basename that doesn't
  114. * exist in the namespace, get() throws an exception.
  115. *
  116. * Refactored to conform with ZF-2724.
  117. *
  118. * @group ZF-2724
  119. * @return void
  120. */
  121. public function testStaticFactoryClassNotFound()
  122. {
  123. set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
  124. try {
  125. Zend_Filter::get('1234', 'UnknownFilter');
  126. } catch (Zend_Filter_Exception $e) {
  127. }
  128. restore_error_handler();
  129. $this->assertTrue($this->error);
  130. $this->assertTrue(isset($e));
  131. $this->assertContains('Filter class not found', $e->getMessage());
  132. }
  133. /**
  134. * Handle file not found errors
  135. *
  136. * @group ZF-2724
  137. * @param int $errnum
  138. * @param string $errstr
  139. * @return void
  140. */
  141. public function handleNotFoundError($errnum, $errstr)
  142. {
  143. if (strstr($errstr, 'No such file')) {
  144. $this->error = true;
  145. }
  146. }
  147. /**
  148. * Testing Namespaces
  149. *
  150. * @return void
  151. */
  152. public function testNamespaces()
  153. {
  154. $this->assertEquals(array(), Zend_Filter::getDefaultNamespaces());
  155. $this->assertFalse(Zend_Filter::hasDefaultNamespaces());
  156. Zend_Filter::setDefaultNamespaces('TestDir');
  157. $this->assertEquals(array('TestDir'), Zend_Filter::getDefaultNamespaces());
  158. Zend_Filter::setDefaultNamespaces('OtherTestDir');
  159. $this->assertEquals(array('OtherTestDir'), Zend_Filter::getDefaultNamespaces());
  160. $this->assertTrue(Zend_Filter::hasDefaultNamespaces());
  161. Zend_Filter::setDefaultNamespaces(array());
  162. $this->assertEquals(array(), Zend_Filter::getDefaultNamespaces());
  163. $this->assertFalse(Zend_Filter::hasDefaultNamespaces());
  164. Zend_Filter::addDefaultNamespaces(array('One', 'Two'));
  165. $this->assertEquals(array('One', 'Two'), Zend_Filter::getDefaultNamespaces());
  166. Zend_Filter::addDefaultNamespaces('Three');
  167. $this->assertEquals(array('One', 'Two', 'Three'), Zend_Filter::getDefaultNamespaces());
  168. Zend_Filter::setDefaultNamespaces(array());
  169. }
  170. }
  171. class Zend_FilterTest_LowerCase implements Zend_Filter_Interface
  172. {
  173. public function filter($value)
  174. {
  175. return strtolower($value);
  176. }
  177. }
  178. class Zend_FilterTest_StripUpperCase implements Zend_Filter_Interface
  179. {
  180. public function filter($value)
  181. {
  182. return preg_replace('/[A-Z]/', '', $value);
  183. }
  184. }