JobClassTest.php 8.0 KB

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