WorkerClassTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\WorkerClass;
  10. use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
  11. /**
  12. * Tests JobClassTest class
  13. */
  14. class WorkerClassTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var WorkAnnotation
  18. *
  19. * Worker annotation driver
  20. */
  21. private $workAnnotation;
  22. /**
  23. * @var \ReflectionClass
  24. *
  25. * Reflection Class
  26. */
  27. private $reflectionClass;
  28. /**
  29. * @var Reader
  30. *
  31. * Reader
  32. */
  33. private $reader;
  34. /**
  35. * @var string
  36. *
  37. * Class namespace
  38. */
  39. private $classNamespace = 'MyClassNamespace';
  40. /**
  41. * @var string
  42. *
  43. * Class name
  44. */
  45. private $className = 'myClass';
  46. /**
  47. * @var string
  48. *
  49. * Filename
  50. */
  51. private $fileName = 'myClass.php';
  52. /**
  53. * @var array
  54. *
  55. * Servers list
  56. */
  57. private $servers = array(
  58. array(
  59. 'host' => '192.168.1.1',
  60. 'port' => '8080',
  61. ),
  62. );
  63. /**
  64. * @var array
  65. *
  66. * Default settings
  67. */
  68. private $defaultSettings = array(
  69. 'method' => 'doHigh',
  70. 'iterations' => 100,
  71. 'callbacks' => true,
  72. 'jobPrefix' => null,
  73. 'generate_unique_key' => true,
  74. 'workers_name_prepend_namespace' => true,
  75. );
  76. /**
  77. * Setup
  78. */
  79. public function setUp()
  80. {
  81. $this->reflectionClass = $this
  82. ->getMockBuilder('\ReflectionClass')
  83. ->disableOriginalConstructor()
  84. ->setMethods(array(
  85. 'getName',
  86. 'getNamespaceName',
  87. 'getFileName',
  88. 'getMethods',
  89. ))
  90. ->getMock();
  91. $this->workAnnotation = $this
  92. ->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Work')
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $this->reader = $this
  96. ->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->never())
  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->never())
  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->never())
  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. }