SudoTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test;
  11. use Psy\Sudo;
  12. class SudoTest extends TestCase
  13. {
  14. /**
  15. * @before
  16. */
  17. public function getReady()
  18. {
  19. if (\version_compare(\PHP_VERSION, '7.1.0', '<')) {
  20. $this->markTestSkipped('YOLO');
  21. }
  22. }
  23. public function testFetchProperty()
  24. {
  25. $obj = new ClassWithSecrets();
  26. $this->assertSame('private and prop', Sudo::fetchProperty($obj, 'privateProp'));
  27. }
  28. public function testAssignProperty()
  29. {
  30. $obj = new ClassWithSecrets();
  31. $this->assertSame('private and prop', Sudo::fetchProperty($obj, 'privateProp'));
  32. $this->assertSame('not so private now', Sudo::assignProperty($obj, 'privateProp', 'not so private now'));
  33. $this->assertSame('not so private now', Sudo::fetchProperty($obj, 'privateProp'));
  34. }
  35. public function testCallMethod()
  36. {
  37. $obj = new ClassWithSecrets();
  38. $this->assertSame('private and method', Sudo::callMethod($obj, 'privateMethod'));
  39. $this->assertSame('private and method with 1', Sudo::callMethod($obj, 'privateMethod', 1));
  40. $this->assertSame(
  41. 'private and method with ["foo",2]',
  42. Sudo::callMethod($obj, 'privateMethod', ['foo', 2]
  43. ));
  44. }
  45. public function testStaticProperty()
  46. {
  47. $obj = new ClassWithSecrets();
  48. // Unfortunately, since this is global mutable state, we can't assert the initial value.
  49. // Running tests out of order will blow things up :(
  50. $this->assertSame('not so private now', Sudo::assignStaticProperty($obj, 'privateStaticProp', 'not so private now'));
  51. $this->assertSame('not so private now', Sudo::fetchStaticProperty($obj, 'privateStaticProp'));
  52. $this->assertSame('wheee', Sudo::assignStaticProperty($obj, 'privateStaticProp', 'wheee'));
  53. $this->assertSame('wheee', Sudo::fetchStaticProperty($obj, 'privateStaticProp'));
  54. }
  55. public function testCallStatic()
  56. {
  57. $obj = new ClassWithSecrets();
  58. $this->assertSame('private and static and method', Sudo::callStatic($obj, 'privateStaticMethod'));
  59. $this->assertSame('private and static and method with 1', Sudo::callStatic($obj, 'privateStaticMethod', 1));
  60. $this->assertSame(
  61. 'private and static and method with ["foo",2]',
  62. Sudo::callStatic($obj, 'privateStaticMethod', ['foo', 2]
  63. ));
  64. }
  65. public function testFetchClassConst()
  66. {
  67. $obj = new ClassWithSecrets();
  68. $this->assertSame('private and const', Sudo::fetchClassConst($obj, 'PRIVATE_CONST'));
  69. }
  70. public function testParentProperties()
  71. {
  72. $obj = new ClassWithSecretiveParent();
  73. $this->assertSame('private and prop', Sudo::fetchProperty($obj, 'privateProp'));
  74. $this->assertSame('not so private now', Sudo::assignProperty($obj, 'privateProp', 'not so private now'));
  75. $this->assertSame('not so private now', Sudo::fetchProperty($obj, 'privateProp'));
  76. }
  77. public function testParentMethods()
  78. {
  79. $obj = new ClassWithSecretiveParent();
  80. $this->assertSame('private and method', Sudo::callMethod($obj, 'privateMethod'));
  81. $this->assertSame('private and method with 1', Sudo::callMethod($obj, 'privateMethod', 1));
  82. $this->assertSame(
  83. 'private and method with ["foo",2]',
  84. Sudo::callMethod($obj, 'privateMethod', ['foo', 2]
  85. ));
  86. }
  87. public function testParentStaticProps()
  88. {
  89. $obj = new ClassWithSecretiveParent();
  90. // Unfortunately, since this is global mutable state, we can't assert the initial value.
  91. // Running tests out of order will blow things up :(
  92. $this->assertSame('not so private now', Sudo::assignStaticProperty($obj, 'privateStaticProp', 'not so private now'));
  93. $this->assertSame('not so private now', Sudo::fetchStaticProperty($obj, 'privateStaticProp'));
  94. $this->assertSame('wheee', Sudo::assignStaticProperty($obj, 'privateStaticProp', 'wheee'));
  95. $this->assertSame('wheee', Sudo::fetchStaticProperty($obj, 'privateStaticProp'));
  96. }
  97. public function testParentStaticMethods()
  98. {
  99. $obj = new ClassWithSecretiveParent();
  100. $this->assertSame('private and static and method', Sudo::callStatic($obj, 'privateStaticMethod'));
  101. $this->assertSame('private and static and method with 1', Sudo::callStatic($obj, 'privateStaticMethod', 1));
  102. $this->assertSame(
  103. 'private and static and method with ["foo",2]',
  104. Sudo::callStatic($obj, 'privateStaticMethod', ['foo', 2]
  105. ));
  106. }
  107. public function testParentConsts()
  108. {
  109. $obj = new ClassWithSecretiveParent();
  110. $this->assertSame('private and const', Sudo::fetchClassConst($obj, 'PRIVATE_CONST'));
  111. }
  112. public function testNewInstance()
  113. {
  114. $obj = Sudo::newInstance(ClassWithSecretConstructor::class);
  115. $this->assertSame('private prop', Sudo::fetchProperty($obj, 'privateProp'));
  116. }
  117. public function testParentNewInstance()
  118. {
  119. $obj = Sudo::newInstance(ClassWithSecretParentConstructor::class, ['foo', 2]);
  120. $this->assertSame('private prop ["foo",2]', Sudo::fetchProperty($obj, 'privateProp'));
  121. }
  122. }