JobClassTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\Module;
  13. use PHPUnit_Framework_TestCase;
  14. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  15. use Mmoreram\GearmanBundle\Module\JobClass;
  16. /**
  17. * Tests JobClassTest class
  18. */
  19. class JobClassTest extends PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var JobAnnotation
  23. *
  24. * Job annotation driver
  25. */
  26. private $jobAnnotation;
  27. /**
  28. * @var \ReflectionMethod
  29. *
  30. * Reflection Method
  31. */
  32. private $reflectionMethod;
  33. /**
  34. * @var string
  35. *
  36. * Callable name
  37. */
  38. private $callableNameClass = 'MyClassCallablaName';
  39. /**
  40. * @var string
  41. *
  42. * Class name
  43. */
  44. private $methodName = 'myMethod';
  45. /**
  46. * @var array
  47. *
  48. * Servers list
  49. */
  50. private $servers = array(
  51. array(
  52. 'host' => '192.168.1.1',
  53. 'port' => '8080',
  54. ),
  55. );
  56. /**
  57. * @var array
  58. *
  59. * Default settings
  60. */
  61. private $defaultSettings = array(
  62. 'method' => 'doHigh',
  63. 'iterations' => 100,
  64. 'minimumExecutionTime' => null,
  65. 'timeout' => null,
  66. 'callbacks' => true,
  67. 'jobPrefix' => null,
  68. 'generate_unique_key' => true,
  69. 'workers_name_prepend_namespace' => true,
  70. );
  71. /**
  72. * Setup
  73. */
  74. public function setUp()
  75. {
  76. $this->reflectionMethod = $this
  77. ->getMockBuilder('\ReflectionMethod')
  78. ->setConstructorArgs(array('\Mmoreram\GearmanBundle\Tests\Service\Mocks\SingleCleanFile', 'myMethod'))
  79. ->setMethods(array(
  80. 'getName',
  81. ))
  82. ->getMock();
  83. $this->jobAnnotation = $this
  84. ->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Job')
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. }
  88. /**
  89. * Testing scenario with all Job annotations filled
  90. *
  91. * All settings given in annotations should be considered to configure Job
  92. *
  93. * Also testing server definition in JobAnnotation as an array of arrays ( multi server )
  94. */
  95. public function testJobAnnotationsDefined()
  96. {
  97. $this
  98. ->reflectionMethod
  99. ->expects($this->once())
  100. ->method('getName')
  101. ->will($this->returnValue($this->methodName));
  102. $this->jobAnnotation->name = 'myOtherMethodName';
  103. $this->jobAnnotation->description = 'This is my own description';
  104. $this->jobAnnotation->iterations = 200;
  105. $this->jobAnnotation->defaultMethod = 'doHighBackground';
  106. $this->jobAnnotation->servers = array(
  107. array(
  108. 'host' => '10.0.0.2',
  109. 'port' => '80',
  110. ),
  111. );
  112. $jobClass = new JobClass(
  113. $this->jobAnnotation,
  114. $this->reflectionMethod,
  115. $this->callableNameClass,
  116. $this->servers,
  117. $this->defaultSettings
  118. );
  119. $this->assertEquals($jobClass->toArray(), array(
  120. 'callableName' => $this->jobAnnotation->name,
  121. 'methodName' => $this->methodName,
  122. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
  123. 'jobPrefix' => null,
  124. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
  125. 'description' => $this->jobAnnotation->description,
  126. 'iterations' => $this->jobAnnotation->iterations,
  127. 'minimumExecutionTime' => $this->jobAnnotation->minimumExecutionTime,
  128. 'timeout' => $this->jobAnnotation->timeout,
  129. 'servers' => $this->jobAnnotation->servers,
  130. 'defaultMethod' => $this->jobAnnotation->defaultMethod,
  131. ));
  132. }
  133. /**
  134. * Testing scenario with any Job annotation filled
  135. *
  136. * All settings set as default should be considered to configure Job
  137. *
  138. * Also testing empty server definition in JobAnnotation
  139. */
  140. public function testJobAnnotationsEmpty()
  141. {
  142. $this
  143. ->reflectionMethod
  144. ->expects($this->exactly(2))
  145. ->method('getName')
  146. ->will($this->returnValue($this->methodName));
  147. $jobClass = new JobClass(
  148. $this->jobAnnotation,
  149. $this->reflectionMethod,
  150. $this->callableNameClass,
  151. $this->servers,
  152. $this->defaultSettings
  153. );
  154. $this->assertEquals($jobClass->toArray(), array(
  155. 'callableName' => $this->methodName,
  156. 'methodName' => $this->methodName,
  157. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  158. 'jobPrefix' => null,
  159. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  160. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  161. 'iterations' => $this->defaultSettings['iterations'],
  162. 'minimumExecutionTime' => $this->defaultSettings['minimumExecutionTime'],
  163. 'timeout' => $this->defaultSettings['timeout'],
  164. 'servers' => $this->servers,
  165. 'defaultMethod' => $this->defaultSettings['method'],
  166. ));
  167. }
  168. /**
  169. * Testing specific server scenario configured in Job annotations as a simple server
  170. */
  171. public function testCombinationServers()
  172. {
  173. $this
  174. ->reflectionMethod
  175. ->expects($this->exactly(2))
  176. ->method('getName')
  177. ->will($this->returnValue($this->methodName));
  178. $this->jobAnnotation->servers = array(
  179. 'host' => '10.0.0.2',
  180. 'port' => '80',
  181. );
  182. $jobClass = new JobClass(
  183. $this->jobAnnotation,
  184. $this->reflectionMethod,
  185. $this->callableNameClass,
  186. $this->servers,
  187. $this->defaultSettings
  188. );
  189. $this->assertEquals($jobClass->toArray(), array(
  190. 'callableName' => $this->methodName,
  191. 'methodName' => $this->methodName,
  192. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  193. 'jobPrefix' => null,
  194. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  195. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  196. 'iterations' => $this->defaultSettings['iterations'],
  197. 'minimumExecutionTime' => $this->defaultSettings['minimumExecutionTime'],
  198. 'timeout' => $this->defaultSettings['timeout'],
  199. 'servers' => array($this->jobAnnotation->servers),
  200. 'defaultMethod' => $this->defaultSettings['method'],
  201. ));
  202. }
  203. /**
  204. * Testing if giving a job a prefix that it uses it.
  205. */
  206. public function testJobPrefix()
  207. {
  208. $this
  209. ->reflectionMethod
  210. ->expects($this->exactly(2))
  211. ->method('getName')
  212. ->will($this->returnValue($this->methodName));
  213. $this->jobAnnotation->servers = array(
  214. 'host' => '10.0.0.2',
  215. 'port' => '80',
  216. );
  217. $settings = $this->defaultSettings;
  218. $settings['jobPrefix'] = 'test';
  219. $jobClass = new JobClass(
  220. $this->jobAnnotation,
  221. $this->reflectionMethod,
  222. $this->callableNameClass,
  223. $this->servers,
  224. $settings
  225. );
  226. $this->assertEquals($jobClass->toArray(), array(
  227. 'callableName' => $this->methodName,
  228. 'methodName' => $this->methodName,
  229. 'realCallableName' => 'test' . str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  230. 'jobPrefix' => 'test',
  231. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  232. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  233. 'iterations' => $this->defaultSettings['iterations'],
  234. 'minimumExecutionTime' => $this->defaultSettings['minimumExecutionTime'],
  235. 'timeout' => $this->defaultSettings['timeout'],
  236. 'servers' => array($this->jobAnnotation->servers),
  237. 'defaultMethod' => $this->defaultSettings['method'],
  238. ));
  239. }
  240. }