JobClassTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. );
  61. /**
  62. * Setup
  63. */
  64. public function setUp()
  65. {
  66. $this->reflectionMethod = $this->getMockBuilder('\ReflectionMethod')
  67. ->disableOriginalConstructor()
  68. ->setMethods(array(
  69. 'getName',
  70. ))
  71. ->getMock();
  72. $this->jobAnnotation = $this->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Job')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. }
  76. /**
  77. * Testing scenario with all Job annotations filled
  78. *
  79. * All settings given in annotations should be considered to configure Job
  80. *
  81. * Also testing server definition in JobAnnotation as an array of arrays ( multi server )
  82. */
  83. public function testJobAnnotationsDefined()
  84. {
  85. $this
  86. ->reflectionMethod
  87. ->expects($this->once())
  88. ->method('getName')
  89. ->will($this->returnValue($this->methodName));
  90. $this->jobAnnotation->name = 'myOtherMethodName';
  91. $this->jobAnnotation->description = 'This is my own description';
  92. $this->jobAnnotation->iterations = 200;
  93. $this->jobAnnotation->defaultMethod = 'doHighBackground';
  94. $this->jobAnnotation->servers = array(
  95. array(
  96. 'host' => '10.0.0.2',
  97. 'port' => '80',
  98. ),
  99. );
  100. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
  101. $this->assertEquals($jobClass->toArray(), array(
  102. 'callableName' => $this->jobAnnotation->name,
  103. 'methodName' => $this->methodName,
  104. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
  105. 'jobPrefix' => null,
  106. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
  107. 'description' => $this->jobAnnotation->description,
  108. 'iterations' => $this->jobAnnotation->iterations,
  109. 'servers' => $this->jobAnnotation->servers,
  110. 'defaultMethod' => $this->jobAnnotation->defaultMethod,
  111. ));
  112. }
  113. /**
  114. * Testing scenario with any Job annotation filled
  115. *
  116. * All settings set as default should be considered to configure Job
  117. *
  118. * Also testing empty server definition in JobAnnotation
  119. */
  120. public function testJobAnnotationsEmpty()
  121. {
  122. $this
  123. ->reflectionMethod
  124. ->expects($this->exactly(2))
  125. ->method('getName')
  126. ->will($this->returnValue($this->methodName));
  127. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
  128. $this->assertEquals($jobClass->toArray(), array(
  129. 'callableName' => $this->methodName,
  130. 'methodName' => $this->methodName,
  131. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  132. 'jobPrefix' => null,
  133. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  134. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  135. 'iterations' => $this->defaultSettings['iterations'],
  136. 'servers' => $this->servers,
  137. 'defaultMethod' => $this->defaultSettings['method'],
  138. ));
  139. }
  140. /**
  141. * Testing specific server scenario configured in Job annotations as a simple server
  142. */
  143. public function testCombinationServers()
  144. {
  145. $this
  146. ->reflectionMethod
  147. ->expects($this->exactly(2))
  148. ->method('getName')
  149. ->will($this->returnValue($this->methodName));
  150. $this->jobAnnotation->servers = array(
  151. 'host' => '10.0.0.2',
  152. 'port' => '80',
  153. );
  154. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
  155. $this->assertEquals($jobClass->toArray(), array(
  156. 'callableName' => $this->methodName,
  157. 'methodName' => $this->methodName,
  158. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  159. 'jobPrefix' => null,
  160. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  161. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  162. 'iterations' => $this->defaultSettings['iterations'],
  163. 'servers' => array($this->jobAnnotation->servers),
  164. 'defaultMethod' => $this->defaultSettings['method'],
  165. ));
  166. }
  167. /**
  168. * Testing if giving a job a prefix that it uses it.
  169. */
  170. public function testJobPrefix()
  171. {
  172. $this
  173. ->reflectionMethod
  174. ->expects($this->exactly(2))
  175. ->method('getName')
  176. ->will($this->returnValue($this->methodName));
  177. $this->jobAnnotation->servers = array(
  178. 'host' => '10.0.0.2',
  179. 'port' => '80',
  180. );
  181. $settings = $this->defaultSettings;
  182. $settings['jobPrefix'] = 'test';
  183. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $settings);
  184. $this->assertEquals($jobClass->toArray(), array(
  185. 'callableName' => $this->methodName,
  186. 'methodName' => $this->methodName,
  187. 'realCallableName' => 'test'.str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  188. 'jobPrefix' => 'test',
  189. 'realCallableNameNoPrefix' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  190. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  191. 'iterations' => $this->defaultSettings['iterations'],
  192. 'servers' => array($this->jobAnnotation->servers),
  193. 'defaultMethod' => $this->defaultSettings['method'],
  194. ));
  195. }
  196. }