/core/src/test/php/net/xp_framework/unittest/reflection/PrivateAccessibilityTest.class.php

https://github.com/ghiata/xp-framework · PHP · 377 lines · 150 code · 40 blank · 187 comment · 1 complexity · 44c0a83be068d55c0ac9aa9bf9f3c23b MD5 · raw file

  1. <?php namespace net\xp_framework\unittest\reflection;
  2. use unittest\TestCase;
  3. use lang\ClassLoader;
  4. /**
  5. * TestCase
  6. *
  7. * @see xp://lang.reflect.Constructor
  8. * @see xp://lang.reflect.Method
  9. * @see xp://lang.reflect.Field
  10. */
  11. class PrivateAccessibilityTest extends TestCase {
  12. private static
  13. $fixture = null,
  14. $fixtureChild = null,
  15. $fixtureCtorChild = null;
  16. /**
  17. * Initialize fixture, fixtureChild and fixtureCtorChild members
  18. *
  19. */
  20. #[@beforeClass]
  21. public static function initializeClasses() {
  22. if (version_compare(PHP_VERSION, '5.3.2', 'lt')) {
  23. throw new \unittest\PrerequisitesNotMetError('Private not supported', null, array('PHP 5.3.2'));
  24. }
  25. self::$fixture= \lang\XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixture');
  26. self::$fixtureChild= \lang\XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixtureChild');
  27. self::$fixtureCtorChild= \lang\XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixtureCtorChild');
  28. }
  29. /**
  30. * Invoke private constructor from here should not work
  31. *
  32. */
  33. #[@test, @expect('lang.IllegalAccessException')]
  34. public function invokingPrivateConstructor() {
  35. self::$fixture->getConstructor()->newInstance(array());
  36. }
  37. /**
  38. * Invoke private constructor from same class
  39. *
  40. */
  41. #[@test]
  42. public function invokingPrivateConstructorFromSameClass() {
  43. $this->assertInstanceOf(self::$fixture, PrivateAccessibilityFixture::construct(self::$fixture));
  44. }
  45. /**
  46. * Invoke private constructor from parent class
  47. *
  48. */
  49. #[@test, @expect('lang.IllegalAccessException')]
  50. public function invokingPrivateConstructorFromParentClass() {
  51. PrivateAccessibilityFixtureCtorChild::construct(self::$fixture);
  52. }
  53. /**
  54. * Invoke private constructor from child class
  55. *
  56. */
  57. #[@test, @expect('lang.IllegalAccessException')]
  58. public function invokingPrivateConstructorFromChildClass() {
  59. PrivateAccessibilityFixtureCtorChild::construct(self::$fixtureChild);
  60. }
  61. /**
  62. * Invoke private constructor from here should work if it's accessible
  63. *
  64. */
  65. #[@test]
  66. public function invokingPrivateConstructorMadeAccessible() {
  67. $this->assertInstanceOf(self::$fixture, self::$fixture
  68. ->getConstructor()
  69. ->setAccessible(true)
  70. ->newInstance(array())
  71. );
  72. }
  73. /**
  74. * Invoke private method from here should not work
  75. *
  76. */
  77. #[@test, @expect('lang.IllegalAccessException')]
  78. public function invokingPrivateMethod() {
  79. self::$fixture->getMethod('target')->invoke(PrivateAccessibilityFixture::construct(self::$fixture));
  80. }
  81. /**
  82. * Invoke private method from same class
  83. *
  84. */
  85. #[@test]
  86. public function invokingPrivateMethodFromSameClass() {
  87. $this->assertEquals('Invoked', PrivateAccessibilityFixture::invoke(self::$fixture));
  88. }
  89. /**
  90. * Invoke private method from parent class
  91. *
  92. */
  93. #[@test, @expect('lang.IllegalAccessException')]
  94. public function invokingPrivateMethodFromParentClass() {
  95. PrivateAccessibilityFixtureChild::invoke(self::$fixture);
  96. }
  97. /**
  98. * Invoke private method from child class
  99. *
  100. */
  101. #[@test, @expect('lang.IllegalAccessException')]
  102. public function invokingPrivateMethodFromChildClass() {
  103. PrivateAccessibilityFixtureChild::invoke(self::$fixtureChild);
  104. }
  105. /**
  106. * Invoke private method from here should work if it's accessible
  107. *
  108. */
  109. #[@test]
  110. public function invokingPrivateMethodMadeAccessible() {
  111. $this->assertEquals('Invoked', self::$fixture
  112. ->getMethod('target')
  113. ->setAccessible(true)
  114. ->invoke(PrivateAccessibilityFixture::construct(self::$fixture))
  115. );
  116. }
  117. /**
  118. * Invoke private method from here should not work
  119. *
  120. */
  121. #[@test, @expect('lang.IllegalAccessException')]
  122. public function invokingPrivateStaticMethod() {
  123. self::$fixture->getMethod('staticTarget')->invoke(null);
  124. }
  125. /**
  126. * Invoke private method from same class
  127. *
  128. */
  129. #[@test]
  130. public function invokingPrivateStaticMethodFromSameClass() {
  131. $this->assertEquals('Invoked', PrivateAccessibilityFixture::invokeStatic(self::$fixture));
  132. }
  133. /**
  134. * Invoke private method from same class
  135. *
  136. */
  137. #[@test, @expect('lang.IllegalAccessException')]
  138. public function invokingPrivateStaticMethodFromParentClass() {
  139. PrivateAccessibilityFixtureChild::invokeStatic(self::$fixture);
  140. }
  141. /**
  142. * Invoke private method from same class
  143. *
  144. */
  145. #[@test, @expect('lang.IllegalAccessException')]
  146. public function invokingPrivateStaticMethodFromChildClass() {
  147. PrivateAccessibilityFixtureChild::invokeStatic(self::$fixtureChild);
  148. }
  149. /**
  150. * Invoke private method from here should work if it's accessible
  151. *
  152. */
  153. #[@test]
  154. public function invokingPrivateStaticMethodMadeAccessible() {
  155. $this->assertEquals('Invoked', self::$fixture
  156. ->getMethod('staticTarget')
  157. ->setAccessible(true)
  158. ->invoke(null)
  159. );
  160. }
  161. /**
  162. * Read private member from here should not work
  163. *
  164. */
  165. #[@test, @expect('lang.IllegalAccessException')]
  166. public function readingPrivateMember() {
  167. self::$fixture->getField('target')->get(PrivateAccessibilityFixture::construct(self::$fixture));
  168. }
  169. /**
  170. * Read private member from same class
  171. *
  172. */
  173. #[@test]
  174. public function readingPrivateMemberFromSameClass() {
  175. $this->assertEquals('Target', PrivateAccessibilityFixture::read(self::$fixture));
  176. }
  177. /**
  178. * Read private member from same class
  179. *
  180. */
  181. #[@test, @expect('lang.IllegalAccessException')]
  182. public function readingPrivateMemberFromParentClass() {
  183. PrivateAccessibilityFixtureChild::read(self::$fixture);
  184. }
  185. /**
  186. * Read private member from same class
  187. *
  188. */
  189. #[@test, @ignore('$this->getClass()->getField($field) does not yield private field declared in parent class')]
  190. public function readingPrivateMemberFromChildClass() {
  191. PrivateAccessibilityFixtureChild::read(self::$fixtureChild);
  192. }
  193. /**
  194. * Read private member from here should work if it's accessible
  195. *
  196. */
  197. #[@test]
  198. public function readingPrivateMemberMadeAccessible() {
  199. $this->assertEquals('Target', self::$fixture
  200. ->getField('target')
  201. ->setAccessible(true)
  202. ->get(PrivateAccessibilityFixture::construct(self::$fixture))
  203. );
  204. }
  205. /**
  206. * Read private member from here should not work
  207. *
  208. */
  209. #[@test, @expect('lang.IllegalAccessException')]
  210. public function readingPrivateStaticMember() {
  211. self::$fixture->getField('staticTarget')->get(null);
  212. }
  213. /**
  214. * Read private static member from same class
  215. *
  216. */
  217. #[@test]
  218. public function readingPrivateStaticMemberFromSameClass() {
  219. $this->assertEquals('Target', PrivateAccessibilityFixture::readStatic(self::$fixture));
  220. }
  221. /**
  222. * Read private static member from same class
  223. *
  224. */
  225. #[@test, @expect('lang.IllegalAccessException')]
  226. public function readingPrivateStaticMemberFromParentClass() {
  227. PrivateAccessibilityFixtureChild::readStatic(self::$fixture);
  228. }
  229. /**
  230. * Read private static member from same class
  231. *
  232. */
  233. #[@test, @ignore('$this->getClass()->getField($field) does not yield private field declared in parent class')]
  234. public function readingPrivateStaticMemberFromChildClass() {
  235. PrivateAccessibilityFixtureChild::readStatic(self::$fixtureChild);
  236. }
  237. /**
  238. * Read private member from here should work if it's accessible
  239. *
  240. */
  241. #[@test]
  242. public function readingPrivateStaticMemberMadeAccessible() {
  243. $this->assertEquals('Target', self::$fixture
  244. ->getField('staticTarget')
  245. ->setAccessible(true)
  246. ->get(null)
  247. );
  248. }
  249. /**
  250. * Write private member from here should not work
  251. *
  252. */
  253. #[@test, @expect('lang.IllegalAccessException')]
  254. public function writingPrivateMember() {
  255. self::$fixture->getField('target')->set(PrivateAccessibilityFixture::construct(self::$fixture), null);
  256. }
  257. /**
  258. * Write private member from same class
  259. *
  260. */
  261. #[@test]
  262. public function writingPrivateMemberFromSameClass() {
  263. $this->assertEquals('Modified', PrivateAccessibilityFixture::write(self::$fixture));
  264. }
  265. /**
  266. * Write private member from same class
  267. *
  268. */
  269. #[@test, @expect('lang.IllegalAccessException')]
  270. public function writingPrivateMemberFromParentClass() {
  271. PrivateAccessibilityFixtureChild::write(self::$fixture);
  272. }
  273. /**
  274. * Write private member from same class
  275. *
  276. */
  277. #[@test, @ignore('$this->getClass()->getField($field) does not yield private field declared in parent class')]
  278. public function writingPrivateMemberFromChildClass() {
  279. PrivateAccessibilityFixtureChild::write(self::$fixtureChild);
  280. }
  281. /**
  282. * Write private member from here should work if it's accessible
  283. *
  284. */
  285. #[@test]
  286. public function writingPrivateMemberMadeAccessible() {
  287. with ($f= self::$fixture->getField('target'), $i= PrivateAccessibilityFixture::construct(self::$fixture)); {
  288. $f->setAccessible(true);
  289. $f->set($i, 'Modified');
  290. $this->assertEquals('Modified', $f->get($i));
  291. }
  292. }
  293. /**
  294. * Write private static member from same class
  295. *
  296. */
  297. #[@test, @expect('lang.IllegalAccessException')]
  298. public function writingPrivateStaticMember() {
  299. self::$fixture->getField('staticTarget')->set(null, 'Modified');
  300. }
  301. /**
  302. * Write private static member from same class
  303. *
  304. */
  305. #[@test]
  306. public function writingPrivateStaticMemberFromSameClass() {
  307. $this->assertEquals('Modified', PrivateAccessibilityFixture::writeStatic(self::$fixture));
  308. }
  309. /**
  310. * Write private static member from same class
  311. *
  312. */
  313. #[@test, @expect('lang.IllegalAccessException')]
  314. public function writingPrivateStaticMemberFromParentClass() {
  315. PrivateAccessibilityFixtureChild::writeStatic(self::$fixture);
  316. }
  317. /**
  318. * Write private static member from same class
  319. *
  320. */
  321. #[@test, @ignore('$this->getClass()->getField($field) does not yield private field declared in parent class')]
  322. public function writingPrivateStaticMemberFromChildClass() {
  323. PrivateAccessibilityFixtureChild::writeStatic(self::$fixtureChild);
  324. }
  325. /**
  326. * Write private member from here should work if it's accessible
  327. *
  328. */
  329. #[@test]
  330. public function writingPrivateStaticMemberMadeAccessible() {
  331. with ($f= self::$fixture->getField('staticTarget')); {
  332. $f->setAccessible(true);
  333. $f->set(null, 'Modified');
  334. $this->assertEquals('Modified', $f->get(null));
  335. }
  336. }
  337. }