You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

384 lines
9.9 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiDI_Test
  4. *
  5. * 针对 ../PhalApi/DI.php PhalApi_DI 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20141004
  8. */
  9. require_once dirname(__FILE__) . '/test_env.php';
  10. if (!class_exists('PhalApi_DI')) {
  11. require dirname(__FILE__) . '/../PhalApi/DI.php';
  12. }
  13. class PhpUnderControl_PhalApiDI_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $coreDI;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $this->coreDI = new PhalApi_DI();
  20. }
  21. protected function tearDown()
  22. {
  23. }
  24. /**
  25. * @group testOne
  26. */
  27. public function testOne()
  28. {
  29. $rs = DI();
  30. }
  31. public function testSetterAndGetter()
  32. {
  33. $this->coreDI->set('name', 'dogstar');
  34. $this->assertEquals($this->coreDI->get('name'), 'dogstar');
  35. $arr = array(1, 5, 7);
  36. $this->coreDI->set('nameArr', $arr);
  37. $this->assertEquals($this->coreDI->get('nameArr'), $arr);
  38. }
  39. public function testMagicFunction()
  40. {
  41. $this->coreDI->setName('dogstar');
  42. $this->assertEquals($this->coreDI->getName(), 'dogstar');
  43. $this->assertEquals($this->coreDI->getNameDefault('2013'), '2013');
  44. $this->assertEquals($this->coreDI->getNameNull(), null);
  45. $this->coreDI->setNameSetNull();
  46. $this->assertEquals($this->coreDI->getNameSetNull(), null);
  47. }
  48. public function testClassSettterAndGetter()
  49. {
  50. $this->coreDI->name2 = 'dogstar';
  51. $this->assertEquals($this->coreDI->name2, 'dogstar');
  52. $this->coreDI->nameAgain = 'dogstarAgain';
  53. $this->assertEquals($this->coreDI->nameAgain, 'dogstarAgain');
  54. $this->assertNull($this->coreDI->nameNull);
  55. }
  56. public function testMixed()
  57. {
  58. $this->coreDI->name1 = 'dogstar1';
  59. $this->assertEquals($this->coreDI->name1, 'dogstar1');
  60. $this->assertEquals($this->coreDI->getName1('2013'), 'dogstar1');
  61. $this->assertEquals($this->coreDI->name1, 'dogstar1');
  62. $this->coreDI->setName1('dogstar2');
  63. $this->assertEquals($this->coreDI->name1, 'dogstar2');
  64. $this->assertEquals($this->coreDI->getName1('2013'), 'dogstar2');
  65. $this->assertEquals($this->coreDI->name1, 'dogstar2');
  66. $this->coreDI->set('name1', 'dogstar3');
  67. $this->assertEquals($this->coreDI->name1, 'dogstar3');
  68. $this->assertEquals($this->coreDI->getName1('2013'), 'dogstar3');
  69. $this->assertEquals($this->coreDI->name1, 'dogstar3');
  70. }
  71. public function testAnonymousFunction()
  72. {
  73. $this->coreDI->set('name', function(){
  74. return new Demo(2014);
  75. });
  76. $this->assertEquals($this->coreDI->name->mark, 2014);
  77. $mark = 2015;
  78. $this->coreDI->set('name1', function() use ($mark){
  79. return new Demo($mark);
  80. });
  81. $this->assertEquals($this->coreDI->name1->mark, $mark);
  82. $this->coreDI->name3 = function(){
  83. return new Demo(2015);
  84. };
  85. $this->assertEquals($this->coreDI->getName3()->mark, 2015);
  86. }
  87. public function testLazyLoadClass()
  88. {
  89. $this->coreDI->setName('Demo2');
  90. $this->assertEquals($this->coreDI->name instanceof Demo2, true);
  91. $this->assertEquals($this->coreDI->name->number, 3);
  92. $this->assertEquals($this->coreDI->name->number, 3);
  93. $this->coreDI->name->number = 9;
  94. $this->assertEquals($this->coreDI->name->number, 9);
  95. $this->assertEquals($this->coreDI->getName()->number, 9);
  96. }
  97. public function testArrayIndex()
  98. {
  99. $this->coreDI['name'] = 'dogstar';
  100. $this->assertEquals($this->coreDI->name, 'dogstar');
  101. $this->coreDI[2014] = 'horse';
  102. $this->assertEquals($this->coreDI->get2014(), 'horse');
  103. $this->assertEquals($this->coreDI[2014], 'horse');
  104. $this->assertEquals($this->coreDI->get(2014), 'horse');
  105. }
  106. /**
  107. * hope nobody use DI as bellow
  108. */
  109. public function testException()
  110. {
  111. try {
  112. $this->coreDI[array(1)] = 1;
  113. $this->fail('no way');
  114. } catch (Exception $ex) {
  115. }
  116. try {
  117. $this->coreDI->set(array(1), array(1));
  118. $this->fail('no way');
  119. } catch (Exception $ex) {
  120. }
  121. try {
  122. $this->coreDI->get(array(1), array(1));
  123. $this->fail('no way');
  124. } catch (Exception $ex) {
  125. }
  126. }
  127. public function testSetAgainAndAgain()
  128. {
  129. $this->coreDI['name'] = function () {
  130. return 'dogstar';
  131. };
  132. $this->assertEquals('dogstar', $this->coreDI['name']);
  133. $this->coreDI['name'] = 'dogstar2';
  134. $this->assertEquals('dogstar2', $this->coreDI['name']);
  135. $this->coreDI['name'] = function () {
  136. return 'dogstar3';
  137. };
  138. $this->assertEquals('dogstar3', $this->coreDI['name']);
  139. $this->coreDI['name'] = 'dogstar4';
  140. $this->assertEquals('dogstar4', $this->coreDI['name']);
  141. $this->coreDI['name'] = 'dogstar5';
  142. $this->assertEquals('dogstar5', $this->coreDI['name']);
  143. }
  144. public function testSetAgainAndAgainByProperty()
  145. {
  146. $this->coreDI->name = 'name';
  147. $this->assertSame('name', $this->coreDI->name);
  148. $this->coreDI->name = 'name2';
  149. $this->assertSame('name2', $this->coreDI->name);
  150. $this->coreDI->name = array('name3');
  151. $this->assertSame(array('name3'), $this->coreDI->name);
  152. }
  153. public function testSetAgainAndAgainBySetter()
  154. {
  155. $this->coreDI->set('name', 'value');
  156. $this->assertSame('value', $this->coreDI->name);
  157. $this->coreDI->set('name', 'value2');
  158. $this->assertSame('value2', $this->coreDI->name);
  159. }
  160. public function testSetAgainAndAgainByMagic()
  161. {
  162. $this->coreDI->setName('value');
  163. $this->assertSame('value', $this->coreDI->name);
  164. $this->coreDI->setName('value2');
  165. $this->assertSame('value2', $this->coreDI->name);
  166. }
  167. public function testGetAgainAndAgain()
  168. {
  169. $times = 0;
  170. $this->coreDI['name'] = function () use (&$times) {
  171. $times ++;
  172. return 'dogstar';
  173. };
  174. $this->assertEquals('dogstar', $this->coreDI['name']);
  175. $this->assertEquals('dogstar', $this->coreDI['name']);
  176. $this->assertEquals('dogstar', $this->coreDI['name']);
  177. $this->assertEquals('dogstar', $this->coreDI['name']);
  178. $this->assertEquals(1, $times);
  179. }
  180. public function testNumericKey()
  181. {
  182. $this->coreDI[0] = 0;
  183. $this->coreDI[1] = 10;
  184. $this->coreDI[2] = 20;
  185. $this->assertSame(0, $this->coreDI[0]);
  186. $this->assertSame(0, $this->coreDI->get(0));
  187. $this->assertSame(0, $this->coreDI->get0());
  188. $this->assertSame(10, $this->coreDI[1]);
  189. $this->assertSame(20, $this->coreDI->get(2));
  190. $this->assertSame(20, $this->coreDI->get2());
  191. }
  192. public function testMultiLevel()
  193. {
  194. $this->coreDI->dogstar = new PhalApi_DI();
  195. $this->coreDI->dogstar->name = "dogstar";
  196. $this->coreDI->dogstar->age = "?";
  197. $this->coreDI->dogstar->id = 1;
  198. $this->assertSame('dogstar', $this->coreDI->dogstar->name);
  199. $this->assertSame('?', $this->coreDI->dogstar->age);
  200. $this->assertSame(1, $this->coreDI->dogstar->id = 1);
  201. }
  202. public function testWithClassName()
  203. {
  204. $this->coreDI->key = 'PhalApi_DI';
  205. $this->assertInstanceOf('PhalApi_DI', $this->coreDI->key);
  206. }
  207. public function testArrayUsage()
  208. {
  209. $this->coreDI[0] = 0;
  210. $this->coreDI[1] = 1;
  211. $this->coreDI[2] = 2;
  212. foreach ($this->coreDI as $key => $value) {
  213. $this->assertSame($key, $value);
  214. }
  215. }
  216. /**
  217. * @expectedException Exception
  218. */
  219. public function testIllegalMethod()
  220. {
  221. $this->coreDI->doSomeThingWrong();
  222. }
  223. public function testMultiSet()
  224. {
  225. $this->coreDI->set('key1', 'value1')
  226. ->set('key2', 'value2')
  227. ->set('key2', 'value2')
  228. ->set('key3', 'value3');
  229. $this->coreDI->setKey4('value4')
  230. ->setKey5('value5')
  231. ->setkey6('value6');
  232. $this->assertSame('value2', $this->coreDI->key2);
  233. $this->assertSame('value6', $this->coreDI['key6']);
  234. }
  235. public function testOneWithInstanceNull()
  236. {
  237. $oldInstance = PhalApi_DI_Mock::getInstance();
  238. PhalApi_DI_Mock::setInstance(null);
  239. $newDI = PhalApi_DI::one();
  240. if (!isset($newDI['tmp'])) {
  241. $newDI['tmp'] = '2015';
  242. $this->assertEquals('2015', $newDI['tmp']);
  243. unset($newDI['tmp']);
  244. }
  245. $this->assertEquals(null, $newDI['tmp']);
  246. PhalApi_DI_Mock::setInstance($oldInstance);
  247. }
  248. }
  249. class Demo
  250. {
  251. public $hasConstruct = false;
  252. public $hasInitialized = false;
  253. public $mark = null;
  254. public function __construct($mark)
  255. {
  256. //echo "Demo::__construct()\n";
  257. $this->mark = $mark;
  258. }
  259. public function onConstruct()
  260. {
  261. $this->hasConstruct = true;
  262. //echo "Demo::onConstruct()\n";
  263. }
  264. public function onInitialize()
  265. {
  266. $this->hasInitialize = true;
  267. //echo "Demo:: onInitialize()\n";
  268. }
  269. }
  270. class Demo2 extends Demo
  271. {
  272. public $number = 1;
  273. public function __construct()
  274. {
  275. //echo "Demo2::__construct()\n";
  276. }
  277. public function onConstruct()
  278. {
  279. //echo "Demo2::onConstruct()\n";
  280. $this->number = 2;
  281. parent::onConstruct();
  282. }
  283. public function onInitialize()
  284. {
  285. //echo "Demo2::onInitialize()\n";
  286. $this->number = 3;
  287. parent::onInitialize();
  288. }
  289. public function onInit()
  290. {
  291. $this->onInitialize();
  292. }
  293. }
  294. class PhalApi_DI_Mock extends PhalApi_DI {
  295. public static function getInstance(){
  296. return PhalApi_DI::$instance;
  297. }
  298. public static function setInstance($instance) {
  299. PhalApi_DI::$instance = $instance;
  300. }
  301. }