DeprecationWrapperTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Elgg;
  3. class DeprecationWrapperTest extends \PHPUnit_Framework_TestCase {
  4. public $last_stack_line = '';
  5. public function setUp() {
  6. $this->last_stack_line = '';
  7. }
  8. // to some extent this also tests the stack trace operation, but that's OK
  9. public function report($msg, $version, $backtrace_level) {
  10. $backtrace = debug_backtrace();
  11. // never show this call.
  12. array_shift($backtrace);
  13. $i = count($backtrace);
  14. foreach ($backtrace as $trace) {
  15. $this->last_stack_line = "{$trace['file']}:{$trace['line']}";
  16. $i--;
  17. if ($backtrace_level > 0) {
  18. if ($backtrace_level <= 1) {
  19. break;
  20. }
  21. $backtrace_level--;
  22. }
  23. }
  24. }
  25. function testWrapString() {
  26. $str = 'Hello';
  27. $str = new DeprecationWrapper($str, 'BAD!', 1.8, array($this, 'report'));
  28. $file = __FILE__;
  29. $new_str = "$str"; $line = __LINE__;
  30. $this->assertEquals('Hello', $new_str);
  31. $this->assertEquals("$file:$line", $this->last_stack_line);
  32. }
  33. function testWrapObject() {
  34. $obj = new DeprecationWrapperTestObj1();
  35. $obj = new DeprecationWrapper($obj, 'BAD!', 1.8, array($this, 'report'));
  36. $file = __FILE__;
  37. $foo = $obj->foo; $line = __LINE__;
  38. $this->assertEquals($foo, 'foo');
  39. $this->assertEquals("$file:$line", $this->last_stack_line);
  40. $foo = $obj->foo(); $line = __LINE__;
  41. $this->assertEquals($foo, 'foo');
  42. $this->assertEquals("$file:$line", $this->last_stack_line);
  43. $foo = "$obj"; $line = __LINE__;
  44. $this->assertEquals($foo, 'foo');
  45. $this->assertEquals("$file:$line", $this->last_stack_line);
  46. }
  47. function testArrayAccessProxiesObjectArrayAccessMethods() {
  48. $file = __FILE__;
  49. $obj = new DeprecationWrapperTestObj2();
  50. $obj->data['foo'] = 'test';
  51. $wrapper = new DeprecationWrapper($obj, 'FOO', 1.9, array($this, 'report'));
  52. $foo = $wrapper['foo']; $line = __LINE__;
  53. $this->assertEquals('test', $foo);
  54. $this->assertEquals("$file:$line", $this->last_stack_line);
  55. $wrapper[0] = 'value'; $line = __LINE__;
  56. $this->assertEquals('value', $obj->data[0]);
  57. $this->assertEquals("$file:$line", $this->last_stack_line);
  58. unset($wrapper[0]); $line = __LINE__;
  59. $this->assertFalse(isset($obj->data[0]));
  60. $this->assertEquals("$file:$line", $this->last_stack_line);
  61. $wrapper[] = 'hello'; $line = __LINE__;
  62. $key_created = array_search('hello', $obj->data);
  63. $this->assertFalse($key_created === false);
  64. $this->assertTrue($key_created !== '');
  65. $this->assertEquals("$file:$line", $this->last_stack_line);
  66. }
  67. function testArrayAccessProxiesProperties() {
  68. $file = __FILE__;
  69. $obj = new \stdClass();
  70. $obj->foo = 'test';
  71. $wrapper = new DeprecationWrapper($obj, 'FOO', 1.9, array($this, 'report'));
  72. $foo = $wrapper['foo']; $line = __LINE__;
  73. $this->assertEquals('test', $foo);
  74. $this->assertEquals("$file:$line", $this->last_stack_line);
  75. $wrapper[0] = 'value'; $line = __LINE__;
  76. $this->assertEquals('value', $obj->{'0'});
  77. $this->assertEquals("$file:$line", $this->last_stack_line);
  78. }
  79. }
  80. class DeprecationWrapperTestObj1 {
  81. public $foo = 'foo';
  82. public function foo() { return 'foo'; }
  83. public function __toString() { return 'foo'; }
  84. }
  85. class DeprecationWrapperTestObj2 extends \ArrayObject {
  86. public $data = array();
  87. public function offsetSet($offset, $value) {
  88. if (is_null($offset)) {
  89. $this->data[] = $value;
  90. } else {
  91. $this->data[$offset] = $value;
  92. }
  93. }
  94. public function offsetExists($offset) {
  95. return array_key_exists($offset, $this->data);
  96. }
  97. public function offsetUnset($offset) {
  98. unset($this->data[$offset]);
  99. }
  100. public function offsetGet($offset) {
  101. return isset($this->data[$offset]) ? $this->data[$offset] : null;
  102. }
  103. }