Harness.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * All-use harness, use this rather than SimpleTest's
  4. */
  5. class HTMLPurifier_Harness extends UnitTestCase
  6. {
  7. public function __construct($name = null) {
  8. parent::__construct($name);
  9. }
  10. protected $config, $context, $purifier;
  11. /**
  12. * Generates easily accessible default config/context, as well as
  13. * a convenience purifier for integration testing.
  14. */
  15. public function setUp() {
  16. list($this->config, $this->context) = $this->createCommon();
  17. $this->config->set('Output.Newline', '
  18. ');
  19. $this->purifier = new HTMLPurifier();
  20. }
  21. /**
  22. * Asserts a purification. Good for integration testing.
  23. */
  24. function assertPurification($input, $expect = null) {
  25. if ($expect === null) $expect = $input;
  26. $result = $this->purifier->purify($input, $this->config);
  27. $this->assertIdentical($expect, $result);
  28. }
  29. /**
  30. * Accepts config and context and prepares them into a valid state
  31. * @param &$config Reference to config variable
  32. * @param &$context Reference to context variable
  33. */
  34. protected function prepareCommon(&$config, &$context) {
  35. $config = HTMLPurifier_Config::create($config);
  36. if (!$context) $context = new HTMLPurifier_Context();
  37. }
  38. /**
  39. * Generates default configuration and context objects
  40. * @return Defaults in form of array($config, $context)
  41. */
  42. protected function createCommon() {
  43. return array(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
  44. }
  45. /**
  46. * Normalizes a string to Unix (\n) endings
  47. */
  48. protected function normalize(&$string) {
  49. $string = str_replace(array("\r\n", "\r"), "\n", $string);
  50. }
  51. /**
  52. * If $expect is false, ignore $result and check if status failed.
  53. * Otherwise, check if $status if true and $result === $expect.
  54. * @param $status Boolean status
  55. * @param $result Mixed result from processing
  56. * @param $expect Mixed expectation for result
  57. */
  58. protected function assertEitherFailOrIdentical($status, $result, $expect) {
  59. if ($expect === false) {
  60. $this->assertFalse($status, 'Expected false result, got true');
  61. } else {
  62. $this->assertTrue($status, 'Expected true result, got false');
  63. $this->assertIdentical($result, $expect);
  64. }
  65. }
  66. public function getTests() {
  67. // __onlytest makes only one test get triggered
  68. foreach (get_class_methods(get_class($this)) as $method) {
  69. if (strtolower(substr($method, 0, 10)) == '__onlytest') {
  70. $this->reporter->paintSkip('All test methods besides ' . $method);
  71. return array($method);
  72. }
  73. }
  74. return parent::getTests();
  75. }
  76. }
  77. // vim: et sw=4 sts=4