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