You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5KB

  1. <?php
  2. // mocks
  3. class HTMLPurifier_ContextTest extends HTMLPurifier_Harness
  4. {
  5. protected $context;
  6. public function setUp()
  7. {
  8. $this->context = new HTMLPurifier_Context();
  9. }
  10. public function testStandardUsage()
  11. {
  12. generate_mock_once('HTMLPurifier_IDAccumulator');
  13. $this->assertFalse($this->context->exists('IDAccumulator'));
  14. $accumulator = new HTMLPurifier_IDAccumulatorMock();
  15. $this->context->register('IDAccumulator', $accumulator);
  16. $this->assertTrue($this->context->exists('IDAccumulator'));
  17. $accumulator_2 =& $this->context->get('IDAccumulator');
  18. $this->assertReference($accumulator, $accumulator_2);
  19. $this->context->destroy('IDAccumulator');
  20. $this->assertFalse($this->context->exists('IDAccumulator'));
  21. $this->expectError('Attempted to retrieve non-existent variable IDAccumulator');
  22. $accumulator_3 =& $this->context->get('IDAccumulator');
  23. $this->assertNull($accumulator_3);
  24. $this->expectError('Attempted to destroy non-existent variable IDAccumulator');
  25. $this->context->destroy('IDAccumulator');
  26. }
  27. public function testReRegister()
  28. {
  29. $var = true;
  30. $this->context->register('OnceOnly', $var);
  31. $this->expectError('Name OnceOnly produces collision, cannot re-register');
  32. $this->context->register('OnceOnly', $var);
  33. // destroy it, now registration is okay
  34. $this->context->destroy('OnceOnly');
  35. $this->context->register('OnceOnly', $var);
  36. }
  37. public function test_loadArray()
  38. {
  39. // references can be *really* wonky!
  40. $context_manual = new HTMLPurifier_Context();
  41. $context_load = new HTMLPurifier_Context();
  42. $var1 = 1;
  43. $var2 = 2;
  44. $context_manual->register('var1', $var1);
  45. $context_manual->register('var2', $var2);
  46. // you MUST set up the references when constructing the array,
  47. // otherwise the registered version will be a copy
  48. $array = array(
  49. 'var1' => &$var1,
  50. 'var2' => &$var2
  51. );
  52. $context_load->loadArray($array);
  53. $this->assertIdentical($context_manual, $context_load);
  54. $var1 = 10;
  55. $var2 = 20;
  56. $this->assertIdentical($context_manual, $context_load);
  57. }
  58. public function testNull() {
  59. $context = new HTMLPurifier_Context();
  60. $var = NULL;
  61. $context->register('var', $var);
  62. $this->assertNull($context->get('var'));
  63. $context->destroy('var');
  64. }
  65. }
  66. // vim: et sw=4 sts=4