GearmanJobExecuteCommandTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * Feel free to edit as you please, and have fun.
  9. *
  10. * @author Marc Morera <yuhu@mmoreram.com>
  11. */
  12. namespace Mmoreram\GearmanBundle\Tests\Command;
  13. use PHPUnit_Framework_TestCase;
  14. use Symfony\Component\Console\Helper\DialogHelper;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\HttpKernel\KernelInterface;
  18. use Mmoreram\GearmanBundle\Command\GearmanJobExecuteCommand;
  19. use Mmoreram\GearmanBundle\Service\GearmanClient;
  20. use Mmoreram\GearmanBundle\Service\GearmanDescriber;
  21. use Mmoreram\GearmanBundle\Service\GearmanExecute;
  22. /**
  23. * Class GearmanJobExecuteCommandTest
  24. */
  25. class GearmanJobExecuteCommandTest extends PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @var GearmanJobExecuteCommand
  29. *
  30. * Command
  31. */
  32. protected $command;
  33. /**
  34. * @var InputInterface
  35. *
  36. * Input
  37. */
  38. protected $input;
  39. /**
  40. * @var OutputInterface
  41. *
  42. * Output
  43. */
  44. protected $output;
  45. /**
  46. * @var DialogHelper
  47. *
  48. * Dialog helper
  49. */
  50. protected $dialogHelper;
  51. /**
  52. * @var GearmanClient
  53. *
  54. * Gearman client
  55. */
  56. protected $gearmanClient;
  57. /**
  58. * @var GearmanDescriber
  59. *
  60. * Gearman describer
  61. */
  62. protected $gearmanDescriber;
  63. /**
  64. * @var GearmanExecute
  65. *
  66. * Gearman execute
  67. */
  68. protected $gearmanExecute;
  69. /**
  70. * @var KernelInterface
  71. *
  72. * Kernel
  73. */
  74. protected $kernel;
  75. /**
  76. * setup
  77. */
  78. public function setUp()
  79. {
  80. $this->command = $this
  81. ->getMockBuilder('Mmoreram\GearmanBundle\Command\GearmanJobExecuteCommand')
  82. ->setMethods(array(
  83. 'getHelperSet'
  84. ))
  85. ->getMock();
  86. $this->dialogHelper = $this
  87. ->getMockBuilder('Symfony\Component\Console\Helper\DialogHelper')
  88. ->setMethods(array('askConfirmation'))
  89. ->getMock();
  90. $helperSet = $this
  91. ->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')
  92. ->setMethods(array('get'))
  93. ->getMock();
  94. $helperSet
  95. ->expects($this->any())
  96. ->method('get')
  97. ->will($this->returnValue($this->dialogHelper));
  98. $this
  99. ->command
  100. ->expects($this->any())
  101. ->method('getHelperSet')
  102. ->will($this->returnValue($helperSet));
  103. $this->input = $this
  104. ->getMockBuilder('Symfony\Component\Console\Input\InputInterface')
  105. ->disableOriginalConstructor()
  106. ->setMethods(array())
  107. ->getMock();
  108. $this->output = $this
  109. ->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
  110. ->disableOriginalConstructor()
  111. ->setMethods(array())
  112. ->getMock();
  113. $this->kernel = $this
  114. ->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')
  115. ->disableOriginalConstructor()
  116. ->setMethods(array())
  117. ->getMock();
  118. $this->gearmanClient = $this
  119. ->getMockBuilder('Mmoreram\GearmanBundle\Service\GearmanClient')
  120. ->disableOriginalConstructor()
  121. ->setMethods(array(
  122. 'getJob'
  123. ))
  124. ->getMock();
  125. $this->gearmanDescriber = $this
  126. ->getMockBuilder('Mmoreram\GearmanBundle\Service\GearmanDescriber')
  127. ->disableOriginalConstructor()
  128. ->setMethods(array(
  129. 'describeJob'
  130. ))
  131. ->getMock();
  132. $this->gearmanExecute = $this
  133. ->getMockBuilder('Mmoreram\GearmanBundle\Service\GearmanExecute')
  134. ->disableOriginalConstructor()
  135. ->setMethods(array(
  136. 'executeJob'
  137. ))
  138. ->getMock();
  139. $this->kernel
  140. ->expects($this->any())
  141. ->method('getEnvironment')
  142. ->will($this->returnValue('dev'));
  143. }
  144. /**
  145. * Test quietness
  146. *
  147. * @dataProvider dataQuietness
  148. */
  149. public function testQuietness(
  150. $quiet,
  151. $noInteraction,
  152. $confirmation,
  153. $countWriteln,
  154. $countDescriber,
  155. $countClient,
  156. $countExecute
  157. )
  158. {
  159. $this
  160. ->input
  161. ->expects($this->any())
  162. ->method('getOption')
  163. ->will($this->returnValueMap(array(
  164. array('quiet', $quiet),
  165. array('no-interaction', $noInteraction)
  166. )));
  167. $this
  168. ->dialogHelper
  169. ->expects($this->any())
  170. ->method('askConfirmation')
  171. ->will($this->returnValue($confirmation));
  172. $this
  173. ->output
  174. ->expects($countWriteln)
  175. ->method('writeln');
  176. $this
  177. ->gearmanDescriber
  178. ->expects($countDescriber)
  179. ->method('describeJob')
  180. ->will($this->returnValue('olakase'));
  181. $this
  182. ->gearmanClient
  183. ->expects($countClient)
  184. ->method('getJob')
  185. ->will($this->returnValue(array()));
  186. $this
  187. ->gearmanExecute
  188. ->expects($countExecute)
  189. ->method('executeJob');
  190. $this->command
  191. ->setGearmanClient($this->gearmanClient)
  192. ->setGearmanDescriber($this->gearmanDescriber)
  193. ->setGearmanExecute($this->gearmanExecute)
  194. ->setKernel($this->kernel)
  195. ->run($this->input, $this->output);
  196. }
  197. /**
  198. * Data provider for testQuietness
  199. */
  200. public function dataQuietness()
  201. {
  202. return array(
  203. array(
  204. true,
  205. true,
  206. true,
  207. $this->never(),
  208. $this->never(),
  209. $this->atLeastOnce(),
  210. $this->atLeastOnce(),
  211. ),
  212. array(
  213. true,
  214. true,
  215. false,
  216. $this->never(),
  217. $this->never(),
  218. $this->atLeastOnce(),
  219. $this->atLeastOnce(),
  220. ),
  221. array(
  222. true,
  223. false,
  224. true,
  225. $this->never(),
  226. $this->never(),
  227. $this->atLeastOnce(),
  228. $this->atLeastOnce(),
  229. ),
  230. array(
  231. true,
  232. false,
  233. false,
  234. $this->never(),
  235. $this->never(),
  236. $this->never(),
  237. $this->never(),
  238. ),
  239. array(
  240. false,
  241. true,
  242. true,
  243. $this->atLeastOnce(),
  244. $this->atLeastOnce(),
  245. $this->atLeastOnce(),
  246. $this->atLeastOnce(),
  247. ),
  248. array(
  249. false,
  250. true,
  251. false,
  252. $this->atLeastOnce(),
  253. $this->atLeastOnce(),
  254. $this->atLeastOnce(),
  255. $this->atLeastOnce(),
  256. ),
  257. array(
  258. false,
  259. false,
  260. true,
  261. $this->atLeastOnce(),
  262. $this->atLeastOnce(),
  263. $this->atLeastOnce(),
  264. $this->atLeastOnce(),
  265. ),
  266. array(
  267. false,
  268. false,
  269. false,
  270. $this->any(),
  271. $this->any(),
  272. $this->never(),
  273. $this->never(),
  274. ),
  275. );
  276. }
  277. }