SafeTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once __DIR__.'/../../../bootstrap.php';
  11. use Symfony\Components\OutputEscaper\Safe;
  12. $t = new LimeTest(13);
  13. // ->getValue()
  14. $t->diag('->getValue()');
  15. $safe = new Safe('foo');
  16. $t->is($safe->getValue(), 'foo', '->getValue() returns the embedded value');
  17. // ->__set() ->__get()
  18. $t->diag('->__set() ->__get()');
  19. class TestClass1
  20. {
  21. public $foo = 'bar';
  22. }
  23. $safe = new Safe(new TestClass1());
  24. $t->is($safe->foo, 'bar', '->__get() returns the object parameter');
  25. $safe->foo = 'baz';
  26. $t->is($safe->foo, 'baz', '->__set() sets the object parameter');
  27. // ->__call()
  28. $t->diag('->__call()');
  29. class TestClass2
  30. {
  31. public function doSomething()
  32. {
  33. return 'ok';
  34. }
  35. }
  36. $safe = new Safe(new TestClass2());
  37. $t->is($safe->doSomething(), 'ok', '->__call() invokes the embedded method');
  38. // ->__isset() ->__unset()
  39. $t->diag('->__isset() ->__unset()');
  40. class TestClass3
  41. {
  42. public
  43. $boolValue = true,
  44. $nullValue = null;
  45. }
  46. $safe = new Safe(new TestClass3());
  47. $t->is(isset($safe->boolValue), true, '->__isset() returns true if the property is not null');
  48. $t->is(isset($safe->nullValue), false, '->__isset() returns false if the property is null');
  49. $t->is(isset($safe->undefinedValue), false, '->__isset() returns false if the property does not exist');
  50. unset($safe->boolValue);
  51. $t->is(isset($safe->boolValue), false, '->__unset() unsets the embedded property');
  52. // Iterator
  53. $t->diag('Iterator');
  54. $input = array('one' => 1, 'two' => 2, 'three' => 3, 'children' => array(1, 2, 3));
  55. $output = array();
  56. $safe = new Safe($input);
  57. foreach ($safe as $key => $value)
  58. {
  59. $output[$key] = $value;
  60. }
  61. $t->same($output, $input, '"Iterator" implementation imitates an array');
  62. // ArrayAccess
  63. $t->diag('ArrayAccess');
  64. $safe = new Safe(array('foo' => 'bar'));
  65. $t->is($safe['foo'], 'bar', '"ArrayAccess" implementation returns a value from the embedded array');
  66. $safe['foo'] = 'baz';
  67. $t->is($safe['foo'], 'baz', '"ArrayAccess" implementation sets a value on the embedded array');
  68. $t->is(isset($safe['foo']), true, '"ArrayAccess" checks if a value is set on the embedded array');
  69. unset($safe['foo']);
  70. $t->is(isset($safe['foo']), false, '"ArrayAccess" unsets a value on the embedded array');