JobClassTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. 'minimum_execution_time' => 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. 'servers' => $this->jobAnnotation->servers,
  128. 'defaultMethod' => $this->jobAnnotation->defaultMethod,
  129. ));
  130. }
  131. /**
  132. * Testing scenario with any Job annotation filled
  133. *
  134. * All settings set as default should be considered to configure Job
  135. *
  136. * Also testing empty server definition in JobAnnotation
  137. */
  138. public function testJobAnnotationsEmpty()
  139. {
  140. $this
  141. ->reflectionMethod
  142. ->expects($this->exactly(2))
  143. ->method('getName')
  144. ->will($this->returnValue($this->methodName));
  145. $jobClass = new JobClass(
  146. $this->jobAnnotation,
  147. $this->reflectionMethod,
  148. $this->callableNameClass,
  149. $this->servers,
  150. $this->defaultSettings
  151. );
  152. $this->assertEquals($jobClass->toArray(), array(
  153. 'callableName' => $this->methodName,
  154. 'methodName' => $this->methodName,
  155. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  156. 'jobPrefix' => null,
  157. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  158. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  159. 'iterations' => $this->defaultSettings['iterations'],
  160. 'servers' => $this->servers,
  161. 'defaultMethod' => $this->defaultSettings['method'],
  162. ));
  163. }
  164. /**
  165. * Testing specific server scenario configured in Job annotations as a simple server
  166. */
  167. public function testCombinationServers()
  168. {
  169. $this
  170. ->reflectionMethod
  171. ->expects($this->exactly(2))
  172. ->method('getName')
  173. ->will($this->returnValue($this->methodName));
  174. $this->jobAnnotation->servers = array(
  175. 'host' => '10.0.0.2',
  176. 'port' => '80',
  177. );
  178. $jobClass = new JobClass(
  179. $this->jobAnnotation,
  180. $this->reflectionMethod,
  181. $this->callableNameClass,
  182. $this->servers,
  183. $this->defaultSettings
  184. );
  185. $this->assertEquals($jobClass->toArray(), array(
  186. 'callableName' => $this->methodName,
  187. 'methodName' => $this->methodName,
  188. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  189. 'jobPrefix' => null,
  190. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  191. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  192. 'iterations' => $this->defaultSettings['iterations'],
  193. 'servers' => array($this->jobAnnotation->servers),
  194. 'defaultMethod' => $this->defaultSettings['method'],
  195. ));
  196. }
  197. /**
  198. * Testing if giving a job a prefix that it uses it.
  199. */
  200. public function testJobPrefix()
  201. {
  202. $this
  203. ->reflectionMethod
  204. ->expects($this->exactly(2))
  205. ->method('getName')
  206. ->will($this->returnValue($this->methodName));
  207. $this->jobAnnotation->servers = array(
  208. 'host' => '10.0.0.2',
  209. 'port' => '80',
  210. );
  211. $settings = $this->defaultSettings;
  212. $settings['jobPrefix'] = 'test';
  213. $jobClass = new JobClass(
  214. $this->jobAnnotation,
  215. $this->reflectionMethod,
  216. $this->callableNameClass,
  217. $this->servers,
  218. $settings
  219. );
  220. $this->assertEquals($jobClass->toArray(), array(
  221. 'callableName' => $this->methodName,
  222. 'methodName' => $this->methodName,
  223. 'realCallableName' => 'test' . str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  224. 'jobPrefix' => 'test',
  225. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  226. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  227. 'iterations' => $this->defaultSettings['iterations'],
  228. 'servers' => array($this->jobAnnotation->servers),
  229. 'defaultMethod' => $this->defaultSettings['method'],
  230. ));
  231. }
  232. }