WorkerClassTest.php 8.7 KB

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