ContextTest.php 2.3 KB

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