WorkerClassTest.php 8.9 KB

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