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

https://github.com/treuter/xp-framework · PHP · 386 lines · 156 code · 39 blank · 191 comment · 1 complexity · 55443a547600047d24417901857f03f7 MD5 · raw file

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