FilterTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2008 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-2008 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. * Ensures expected return value from empty filter chain
  57. *
  58. * @return void
  59. */
  60. public function testEmpty()
  61. {
  62. $value = 'something';
  63. $this->assertEquals($value, $this->_filter->filter($value));
  64. }
  65. /**
  66. * Ensures that filters are executed in the expected order (FIFO)
  67. *
  68. * @return void
  69. */
  70. public function testFilterOrder()
  71. {
  72. $this->_filter->addFilter(new Zend_FilterTest_LowerCase())
  73. ->addFilter(new Zend_FilterTest_StripUpperCase());
  74. $value = 'AbC';
  75. $valueExpected = 'abc';
  76. $this->assertEquals($valueExpected, $this->_filter->filter($value));
  77. }
  78. /**
  79. * Ensures that we can call the static method get()
  80. * to instantiate a named validator by its class basename
  81. * and it returns the result of filter() with the input.
  82. */
  83. public function testStaticFactory()
  84. {
  85. $filteredValue = Zend_Filter::get('1a2b3c4d', 'Digits');
  86. $this->assertEquals('1234', $filteredValue);
  87. }
  88. /**
  89. * Ensures that a validator with constructor arguments can be called
  90. * with the static method get().
  91. */
  92. public function testStaticFactoryWithConstructorArguments()
  93. {
  94. // Test HtmlEntities with one ctor argument.
  95. $filteredValue = Zend_Filter::get('"O\'Reilly"', 'HtmlEntities', array(ENT_COMPAT));
  96. $this->assertEquals('&quot;O\'Reilly&quot;', $filteredValue);
  97. // Test HtmlEntities with a different ctor argument,
  98. // and make sure it gives the correct response
  99. // so we know it passed the arg to the ctor.
  100. $filteredValue = Zend_Filter::get('"O\'Reilly"', 'HtmlEntities', array(ENT_QUOTES));
  101. $this->assertEquals('&quot;O&#039;Reilly&quot;', $filteredValue);
  102. }
  103. /**
  104. * Ensures that if we specify a validator class basename that doesn't
  105. * exist in the namespace, get() throws an exception.
  106. *
  107. * Refactored to conform with ZF-2724.
  108. *
  109. * @group ZF-2724
  110. * @return void
  111. */
  112. public function testStaticFactoryClassNotFound()
  113. {
  114. set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
  115. try {
  116. Zend_Filter::get('1234', 'UnknownFilter');
  117. } catch (Zend_Filter_Exception $e) {
  118. }
  119. restore_error_handler();
  120. $this->assertTrue($this->error);
  121. $this->assertTrue(isset($e));
  122. $this->assertContains('Filter class not found', $e->getMessage());
  123. }
  124. /**
  125. * Handle file not found errors
  126. *
  127. * @group ZF-2724
  128. * @param int $errnum
  129. * @param string $errstr
  130. * @return void
  131. */
  132. public function handleNotFoundError($errnum, $errstr)
  133. {
  134. if (strstr($errstr, 'No such file')) {
  135. $this->error = true;
  136. }
  137. }
  138. }
  139. class Zend_FilterTest_LowerCase implements Zend_Filter_Interface
  140. {
  141. public function filter($value)
  142. {
  143. return strtolower($value);
  144. }
  145. }
  146. class Zend_FilterTest_StripUpperCase implements Zend_Filter_Interface
  147. {
  148. public function filter($value)
  149. {
  150. return preg_replace('/[A-Z]/', '', $value);
  151. }
  152. }