WorkerClassTest.php 8.7 KB

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