LuckyBoxTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace LuckyBox\Tests;
  3. use LuckyBox\LuckyBox;
  4. use LuckyBox\Card\IdCard;
  5. class LuckyBoxTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testDraw_Functional()
  8. {
  9. $card1 = new IdCard();
  10. $card1->setId(1);
  11. $card1->setRate(55);
  12. $card2 = new IdCard();
  13. $card2->setId(2);
  14. $card2->setRate(45);
  15. $box = new LuckyBox();
  16. $box->add($card1);
  17. $box->add($card2);
  18. for ($i = 0 ; $i < 3; $i++) {
  19. $result = $box->draw();
  20. $this->assertTrue($result === $card1 || $result === $card2);
  21. }
  22. $this->assertFalse($box->isEmpty());
  23. $box->clear();
  24. $this->assertTrue($box->isEmpty());
  25. $box->add($card1);
  26. $box->add($card2);
  27. $box->setConsumable(true);
  28. while (!$box->isEmpty()) {
  29. $result = $box->draw();
  30. $this->assertTrue($result === $card1 || $result === $card2);
  31. }
  32. $box->clear();
  33. $this->assertTrue($box->isEmpty());
  34. $box->add($card1);
  35. $this->assertFalse($box->isEmpty());
  36. $box->remove($card1);
  37. $this->assertTrue($box->isEmpty());
  38. }
  39. public function testDraw_RandomPositionIsNull()
  40. {
  41. $box = $this->getMockBuilder('LuckyBox\LuckyBox')
  42. ->disableOriginalConstructor()
  43. ->setMethods(array('getRandomPosition'))
  44. ->getMock();
  45. $box->expects($this->once())
  46. ->method('getRandomPosition')
  47. ->will($this->returnValue(null));
  48. $this->assertNull($box->draw());
  49. }
  50. public function testDraw_CardIsNull()
  51. {
  52. $box = $this->getMockBuilder('LuckyBox\LuckyBox')
  53. ->disableOriginalConstructor()
  54. ->setMethods(array('getRandomPosition', 'find'))
  55. ->getMock();
  56. $box->expects($this->once())
  57. ->method('getRandomPosition')
  58. ->will($this->returnValue(1));
  59. $box->expects($this->once())
  60. ->method('find')
  61. ->will($this->returnValue(null));
  62. $this->assertNull($box->draw());
  63. }
  64. public function testDraw_ReturnsCard()
  65. {
  66. $card = new IdCard();
  67. $box = $this->getMockBuilder('LuckyBox\LuckyBox')
  68. ->disableOriginalConstructor()
  69. ->setMethods(array('getRandomPosition', 'find'))
  70. ->getMock();
  71. $box->expects($this->once())
  72. ->method('getRandomPosition')
  73. ->will($this->returnValue(1));
  74. $box->expects($this->once())
  75. ->method('find')
  76. ->will($this->returnValue($card));
  77. $this->assertEquals($card, $box->draw());
  78. }
  79. public function testDraw_ReturnsCardAndNotConsumable()
  80. {
  81. $card = new IdCard();
  82. $box = $this->getMockBuilder('LuckyBox\LuckyBox')
  83. ->disableOriginalConstructor()
  84. ->setMethods(array('getRandomPosition', 'find', 'remove'))
  85. ->getMock();
  86. $box->expects($this->once())
  87. ->method('getRandomPosition')
  88. ->will($this->returnValue(1));
  89. $box->expects($this->once())
  90. ->method('find')
  91. ->will($this->returnValue($card));
  92. $box->expects($this->once())
  93. ->method('remove')
  94. ->with($this->equalTo($card));
  95. $box->setConsumable(true);
  96. $this->assertEquals($card, $box->draw());
  97. }
  98. public function testAdd()
  99. {
  100. $card1 = new IdCard();
  101. $card1->setRate(10);
  102. $card2 = new IdCard();
  103. $card2->setRate(90);
  104. $box = new LuckyBox();
  105. $rp1 = new \ReflectionProperty($box, 'cards');
  106. $rp1->setAccessible(true);
  107. $rp2 = new \ReflectionProperty($box, 'totalRate');
  108. $rp2->setAccessible(true);
  109. $this->assertEquals(array(), $rp1->getValue($box));
  110. $this->assertEquals(0, $rp2->getValue($box));
  111. $box->add($card1);
  112. $this->assertEquals(array($card1), $rp1->getValue($box));
  113. $this->assertEquals(10, $rp2->getValue($box));
  114. $box->add($card2);
  115. $this->assertEquals(array($card1, $card2), $rp1->getValue($box));
  116. $this->assertEquals(100, $rp2->getValue($box));
  117. }
  118. public function testRemove()
  119. {
  120. $card1 = new IdCard();
  121. $card1->setRate(10);
  122. $card2 = new IdCard();
  123. $card2->setRate(90);
  124. $box = new LuckyBox();
  125. $box->add($card1);
  126. $box->add($card2);
  127. $box->remove($card1);
  128. $rp = new \ReflectionProperty($box, 'cards');
  129. $rp->setAccessible(true);
  130. $this->assertEquals(array($card2), $rp->getValue($box));
  131. $rp = new \ReflectionProperty($box, 'totalRate');
  132. $rp->setAccessible(true);
  133. $this->assertEquals(90, $rp->getValue($box));
  134. }
  135. public function testClear()
  136. {
  137. $card = new IdCard();
  138. $card->setRate(10);
  139. $box = new LuckyBox();
  140. $box->add($card);
  141. $rp1 = new \ReflectionProperty($box, 'cards');
  142. $rp1->setAccessible(true);
  143. $rp2 = new \ReflectionProperty($box, 'totalRate');
  144. $rp2->setAccessible(true);
  145. $this->assertCount(1, $rp1->getValue($box));
  146. $this->assertEquals(10, $rp2->getValue($box));
  147. $box->clear();
  148. $this->assertCount(0, $rp1->getValue($box));
  149. $this->assertEquals(0, $rp2->getValue($box));
  150. }
  151. public function testIsEmpty()
  152. {
  153. $box = new LuckyBox();
  154. $this->assertTrue($box->isEmpty());
  155. $box->add(new IdCard());
  156. $this->assertFalse($box->isEmpty());
  157. }
  158. public function testSetConsumable()
  159. {
  160. $box = new LuckyBox();
  161. $rp = new \ReflectionProperty($box, 'consumable');
  162. $rp->setAccessible(true);
  163. $box->setConsumable(false);
  164. $this->assertFalse($rp->getValue($box));
  165. $box->setConsumable(true);
  166. $this->assertTrue($rp->getValue($box));
  167. }
  168. public function testIsConsumable()
  169. {
  170. $box = new LuckyBox();
  171. $rp = new \ReflectionProperty($box, 'consumable');
  172. $rp->setAccessible(true);
  173. $rp->setValue($box, true);
  174. $this->assertTrue($box->isConsumable());
  175. $rp->setValue($box, false);
  176. $this->assertFalse($box->isConsumable());
  177. }
  178. public function testFind_CardIsFound()
  179. {
  180. $card = new IdCard();
  181. $card->setRate(1);
  182. $box = new LuckyBox();
  183. $box->add($card);
  184. $rm = new \ReflectionMethod($box, 'find');
  185. $rm->setAccessible(true);
  186. $this->assertEquals($card, $rm->invoke($box, 0));
  187. }
  188. public function testFind_CardIsFoundWithMultipleCards()
  189. {
  190. $card1 = new IdCard();
  191. $card1->setRate(1);
  192. $card2 = new IdCard();
  193. $card2->setRate(1);
  194. $box = new LuckyBox();
  195. $box->add($card1);
  196. $box->add($card2);
  197. $rm = new \ReflectionMethod($box, 'find');
  198. $rm->setAccessible(true);
  199. $this->assertEquals($card1, $rm->invoke($box, 0));
  200. $this->assertEquals($card2, $rm->invoke($box, 1));
  201. $this->assertNull($rm->invoke($box, 2));
  202. }
  203. public function testFind_CardIsNotFound()
  204. {
  205. $card = new IdCard();
  206. $card->setRate(0);
  207. $box = new LuckyBox();
  208. $box->add($card);
  209. $rm = new \ReflectionMethod($box, 'find');
  210. $rm->setAccessible(true);
  211. $this->assertNull($rm->invoke($box, 0));
  212. }
  213. public function testGetRandomPosition_TotalRateIsLessThan1()
  214. {
  215. $box = new LuckyBox();
  216. $rp = new \ReflectionProperty($box, 'totalRate');
  217. $rp->setAccessible(true);
  218. $rp->setValue($box, 0);
  219. $rm = new \ReflectionMethod($box, 'getRandomPosition');
  220. $rm->setAccessible(true);
  221. $this->assertNull($rm->invoke($box));
  222. }
  223. public function testGetRandomPosition_ReturnsRandomValueBetween0AndTotalRate()
  224. {
  225. $box = new LuckyBox();
  226. $rp = new \ReflectionProperty($box, 'totalRate');
  227. $rp->setAccessible(true);
  228. $rp->setValue($box, 1);
  229. $rm = new \ReflectionMethod($box, 'getRandomPosition');
  230. $rm->setAccessible(true);
  231. $this->assertEquals(0, $rm->invoke($box));
  232. }
  233. }