JobClassTest.php 8.3 KB

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