WorkerClassTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. 'jobs' => array(),
  171. ));
  172. }
  173. /**
  174. * Testing scenario with any Job annotation filled
  175. *
  176. * All settings set as default should be considered to configure Job
  177. *
  178. * Also testing empty server definition in JobAnnotation
  179. */
  180. public function testWorkerAnnotationsEmpty()
  181. {
  182. $this
  183. ->reflectionClass
  184. ->expects($this->once())
  185. ->method('getNamespaceName')
  186. ->will($this->returnValue($this->classNamespace));
  187. $this
  188. ->reflectionClass
  189. ->expects($this->exactly(2))
  190. ->method('getName')
  191. ->will($this->returnValue($this->className));
  192. $this
  193. ->reflectionClass
  194. ->expects($this->once())
  195. ->method('getFileName')
  196. ->will($this->returnValue($this->fileName));
  197. $this
  198. ->reflectionClass
  199. ->expects($this->once())
  200. ->method('getMethods')
  201. ->will($this->returnValue(array()));
  202. $this
  203. ->reader
  204. ->expects($this->never())
  205. ->method('getMethodAnnotations');
  206. $workerClass = new WorkerClass(
  207. $this->workAnnotation,
  208. $this->reflectionClass,
  209. $this->reader,
  210. $this->servers,
  211. $this->defaultSettings
  212. );
  213. $this->assertEquals($workerClass->toArray(), array(
  214. 'namespace' => $this->classNamespace,
  215. 'className' => $this->className,
  216. 'fileName' => $this->fileName,
  217. 'callableName' => $this->className,
  218. 'description' => $workerClass::DEFAULT_DESCRIPTION,
  219. 'service' => null,
  220. 'servers' => $this->servers,
  221. 'iterations' => $this->defaultSettings['iterations'],
  222. 'jobs' => array(),
  223. ));
  224. }
  225. /**
  226. * Testing specific server scenario configured in Job annotations as a simple server
  227. */
  228. public function testCombinationServers()
  229. {
  230. $this
  231. ->reflectionClass
  232. ->expects($this->once())
  233. ->method('getNamespaceName')
  234. ->will($this->returnValue($this->classNamespace));
  235. $this
  236. ->reflectionClass
  237. ->expects($this->exactly(2))
  238. ->method('getName')
  239. ->will($this->returnValue($this->className));
  240. $this
  241. ->reflectionClass
  242. ->expects($this->once())
  243. ->method('getFileName')
  244. ->will($this->returnValue($this->fileName));
  245. $this
  246. ->reflectionClass
  247. ->expects($this->once())
  248. ->method('getMethods')
  249. ->will($this->returnValue(array()));
  250. $this
  251. ->reader
  252. ->expects($this->never())
  253. ->method('getMethodAnnotations');
  254. $this->workAnnotation->servers = array(
  255. 'host' => '10.0.0.2',
  256. 'port' => '80',
  257. );
  258. $workerClass = new WorkerClass(
  259. $this->workAnnotation,
  260. $this->reflectionClass,
  261. $this->reader,
  262. $this->servers,
  263. $this->defaultSettings
  264. );
  265. $this->assertEquals($workerClass->toArray(), array(
  266. 'namespace' => $this->classNamespace,
  267. 'className' => $this->className,
  268. 'fileName' => $this->fileName,
  269. 'callableName' => $this->className,
  270. 'description' => $workerClass::DEFAULT_DESCRIPTION,
  271. 'service' => null,
  272. 'servers' => array($this->workAnnotation->servers),
  273. 'iterations' => $this->defaultSettings['iterations'],
  274. 'jobs' => array(),
  275. ));
  276. }
  277. }