JobClassTest.php 7.9 KB

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