WorkerClassTest.php 8.6 KB

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