WorkerClassTest.php 9.5 KB

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