FilterTest.php 6.8 KB

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