WorkerClassTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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\Module\WorkerClass;
  11. use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
  12. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JonAnnotation;
  13. /**
  14. * Tests JobClassTest class
  15. */
  16. class WorkerClassTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var WorkAnnotation
  20. *
  21. * Worker annotation driver
  22. */
  23. private $workAnnotation;
  24. /**
  25. * @var \ReflectionClass
  26. *
  27. * Reflection Class
  28. */
  29. private $reflectionClass;
  30. /**
  31. * @var Reader
  32. *
  33. * Reader
  34. */
  35. private $reader;
  36. /**
  37. * @var string
  38. *
  39. * Class namespace
  40. */
  41. private $classNamespace = 'MyClassNamespace';
  42. /**
  43. * @var string
  44. *
  45. * Class name
  46. */
  47. private $className = 'myClass';
  48. /**
  49. * @var string
  50. *
  51. * Filename
  52. */
  53. private $fileName = 'myClass.php';
  54. /**
  55. * @var array
  56. *
  57. * Servers list
  58. */
  59. private $servers = array(
  60. array(
  61. 'host' => '192.168.1.1',
  62. 'port' => '8080',
  63. ),
  64. );
  65. /**
  66. * @var array
  67. *
  68. * Default settings
  69. */
  70. private $defaultSettings = array(
  71. 'method' => 'doHigh',
  72. 'iterations' => 100,
  73. 'callbacks' => true,
  74. 'jobPrefix' => null,
  75. 'generate_unique_key' => true,
  76. 'workers_name_prepend_namespace' => true,
  77. );
  78. /**
  79. * Setup
  80. */
  81. public function setUp()
  82. {
  83. $this->reflectionClass = $this
  84. ->getMockBuilder('\ReflectionClass')
  85. ->disableOriginalConstructor()
  86. ->setMethods(array(
  87. 'getName',
  88. 'getNamespaceName',
  89. 'getFileName',
  90. 'getMethods',
  91. ))
  92. ->getMock();
  93. $this->workAnnotation = $this
  94. ->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Work')
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->reader = $this
  98. ->getMockBuilder('Doctrine\Common\Annotations\SimpleAnnotationReader')
  99. ->disableOriginalConstructor()
  100. ->setMethods(array(
  101. 'getMethodAnnotations'
  102. ))
  103. ->getMock();
  104. }
  105. /**
  106. * Testing scenario with all Job annotations filled
  107. *
  108. * All settings given in annotations should be considered to configure Job
  109. *
  110. * Also testing server definition in JobAnnotation as an array of arrays ( multi server )
  111. */
  112. public function testWorkerAnnotationsDefined()
  113. {
  114. $this
  115. ->reflectionClass
  116. ->expects($this->once())
  117. ->method('getNamespaceName')
  118. ->will($this->returnValue($this->classNamespace));
  119. $this
  120. ->reflectionClass
  121. ->expects($this->once())
  122. ->method('getName')
  123. ->will($this->returnValue($this->className));
  124. $this
  125. ->reflectionClass
  126. ->expects($this->once())
  127. ->method('getFileName')
  128. ->will($this->returnValue($this->fileName));
  129. $this
  130. ->reflectionClass
  131. ->expects($this->once())
  132. ->method('getMethods')
  133. ->will($this->returnValue(array()));
  134. $this
  135. ->reader
  136. ->expects($this->never())
  137. ->method('getMethodAnnotations');
  138. $this->workAnnotation->name = 'myOtherWorkerName';
  139. $this->workAnnotation->description = 'This is my own description';
  140. $this->workAnnotation->iterations = 200;
  141. $this->workAnnotation->defaultMethod = 'doHighBackground';
  142. $this->workAnnotation->service = 'my.service';
  143. $this->workAnnotation->servers = array(
  144. array(
  145. 'host' => '10.0.0.2',
  146. 'port' => '80',
  147. ),
  148. );
  149. $workerClass = new WorkerClass($this->workAnnotation, $this->reflectionClass, $this->reader, $this->servers, $this->defaultSettings);
  150. $this->assertEquals($workerClass->toArray(), array(
  151. 'namespace' => $this->classNamespace,
  152. 'className' => $this->className,
  153. 'fileName' => $this->fileName,
  154. 'callableName' => $this->classNamespace . $this->workAnnotation->name,
  155. 'description' => $this->workAnnotation->description,
  156. 'service' => $this->workAnnotation->service,
  157. 'servers' => $this->workAnnotation->servers,
  158. 'iterations' => $this->workAnnotation->iterations,
  159. 'jobs' => array(),
  160. ));
  161. }
  162. /**
  163. * Testing scenario with any Job annotation filled
  164. *
  165. * All settings set as default should be considered to configure Job
  166. *
  167. * Also testing empty server definition in JobAnnotation
  168. */
  169. public function testWorkerAnnotationsEmpty()
  170. {
  171. $this
  172. ->reflectionClass
  173. ->expects($this->once())
  174. ->method('getNamespaceName')
  175. ->will($this->returnValue($this->classNamespace));
  176. $this
  177. ->reflectionClass
  178. ->expects($this->exactly(2))
  179. ->method('getName')
  180. ->will($this->returnValue($this->className));
  181. $this
  182. ->reflectionClass
  183. ->expects($this->once())
  184. ->method('getFileName')
  185. ->will($this->returnValue($this->fileName));
  186. $this
  187. ->reflectionClass
  188. ->expects($this->once())
  189. ->method('getMethods')
  190. ->will($this->returnValue(array()));
  191. $this
  192. ->reader
  193. ->expects($this->never())
  194. ->method('getMethodAnnotations');
  195. $workerClass = new WorkerClass($this->workAnnotation, $this->reflectionClass, $this->reader, $this->servers, $this->defaultSettings);
  196. $this->assertEquals($workerClass->toArray(), array(
  197. 'namespace' => $this->classNamespace,
  198. 'className' => $this->className,
  199. 'fileName' => $this->fileName,
  200. 'callableName' => $this->className,
  201. 'description' => $workerClass::DEFAULT_DESCRIPTION,
  202. 'service' => null,
  203. 'servers' => $this->servers,
  204. 'iterations' => $this->defaultSettings['iterations'],
  205. 'jobs' => array(),
  206. ));
  207. }
  208. /**
  209. * Testing specific server scenario configured in Job annotations as a simple server
  210. */
  211. public function testCombinationServers()
  212. {
  213. $this
  214. ->reflectionClass
  215. ->expects($this->once())
  216. ->method('getNamespaceName')
  217. ->will($this->returnValue($this->classNamespace));
  218. $this
  219. ->reflectionClass
  220. ->expects($this->exactly(2))
  221. ->method('getName')
  222. ->will($this->returnValue($this->className));
  223. $this
  224. ->reflectionClass
  225. ->expects($this->once())
  226. ->method('getFileName')
  227. ->will($this->returnValue($this->fileName));
  228. $this
  229. ->reflectionClass
  230. ->expects($this->once())
  231. ->method('getMethods')
  232. ->will($this->returnValue(array()));
  233. $this
  234. ->reader
  235. ->expects($this->never())
  236. ->method('getMethodAnnotations');
  237. $this->workAnnotation->servers = array(
  238. 'host' => '10.0.0.2',
  239. 'port' => '80',
  240. );
  241. $workerClass = new WorkerClass($this->workAnnotation, $this->reflectionClass, $this->reader, $this->servers, $this->defaultSettings);
  242. $this->assertEquals($workerClass->toArray(), array(
  243. 'namespace' => $this->classNamespace,
  244. 'className' => $this->className,
  245. 'fileName' => $this->fileName,
  246. 'callableName' => $this->className,
  247. 'description' => $workerClass::DEFAULT_DESCRIPTION,
  248. 'service' => null,
  249. 'servers' => array($this->workAnnotation->servers),
  250. 'iterations' => $this->defaultSettings['iterations'],
  251. 'jobs' => array(),
  252. ));
  253. }
  254. }