CompositeTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. class HTMLPurifier_AttrDef_CSS_Composite_Testable extends
  3. HTMLPurifier_AttrDef_CSS_Composite
  4. {
  5. // we need to pass by ref to get the mocks in
  6. function HTMLPurifier_AttrDef_CSS_Composite_Testable(&$defs) {
  7. $this->defs =& $defs;
  8. }
  9. }
  10. class HTMLPurifier_AttrDef_CSS_CompositeTest extends HTMLPurifier_AttrDefHarness
  11. {
  12. protected $def1, $def2;
  13. function test() {
  14. generate_mock_once('HTMLPurifier_AttrDef');
  15. $config = HTMLPurifier_Config::createDefault();
  16. $context = new HTMLPurifier_Context();
  17. // first test: value properly validates on first definition
  18. // so second def is never called
  19. $def1 = new HTMLPurifier_AttrDefMock();
  20. $def2 = new HTMLPurifier_AttrDefMock();
  21. $defs = array(&$def1, &$def2);
  22. $def = new HTMLPurifier_AttrDef_CSS_Composite_Testable($defs);
  23. $input = 'FOOBAR';
  24. $output = 'foobar';
  25. $def1_params = array($input, $config, $context);
  26. $def1->expectOnce('validate', $def1_params);
  27. $def1->setReturnValue('validate', $output, $def1_params);
  28. $def2->expectNever('validate');
  29. $result = $def->validate($input, $config, $context);
  30. $this->assertIdentical($output, $result);
  31. // second test, first def fails, second def works
  32. $def1 = new HTMLPurifier_AttrDefMock();
  33. $def2 = new HTMLPurifier_AttrDefMock();
  34. $defs = array(&$def1, &$def2);
  35. $def = new HTMLPurifier_AttrDef_CSS_Composite_Testable($defs);
  36. $input = 'BOOMA';
  37. $output = 'booma';
  38. $def_params = array($input, $config, $context);
  39. $def1->expectOnce('validate', $def_params);
  40. $def1->setReturnValue('validate', false, $def_params);
  41. $def2->expectOnce('validate', $def_params);
  42. $def2->setReturnValue('validate', $output, $def_params);
  43. $result = $def->validate($input, $config, $context);
  44. $this->assertIdentical($output, $result);
  45. // third test, all fail, so composite faiils
  46. $def1 = new HTMLPurifier_AttrDefMock();
  47. $def2 = new HTMLPurifier_AttrDefMock();
  48. $defs = array(&$def1, &$def2);
  49. $def = new HTMLPurifier_AttrDef_CSS_Composite_Testable($defs);
  50. $input = 'BOOMA';
  51. $output = false;
  52. $def_params = array($input, $config, $context);
  53. $def1->expectOnce('validate', $def_params);
  54. $def1->setReturnValue('validate', false, $def_params);
  55. $def2->expectOnce('validate', $def_params);
  56. $def2->setReturnValue('validate', false, $def_params);
  57. $result = $def->validate($input, $config, $context);
  58. $this->assertIdentical($output, $result);
  59. }
  60. }
  61. // vim: et sw=4 sts=4