GearmanParserTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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\Service;
  13. use Doctrine\Common\Annotations\Reader;
  14. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  15. use Symfony\Component\Finder\Finder;
  16. use Symfony\Component\HttpKernel\Bundle\Bundle;
  17. use Mmoreram\GearmanBundle\Module\WorkerCollection;
  18. use Mmoreram\GearmanBundle\Service\GearmanParser;
  19. /**
  20. * Tests GearmanParser class
  21. */
  22. class GearmanParserTest extends WebTestCase
  23. {
  24. /**
  25. * @var GearmanParser
  26. *
  27. * GearmanParser mock
  28. */
  29. private $gearmanParser;
  30. /**
  31. * @var Bundle
  32. *
  33. * Bundle mock
  34. */
  35. private $bundleMock;
  36. /**
  37. * @var array
  38. *
  39. * KernelBundles
  40. */
  41. private $kernelBundles;
  42. /**
  43. * @var string
  44. *
  45. * Bundle path
  46. */
  47. private $bundlePath = '/my/bundle/path';
  48. /**
  49. * Setup
  50. */
  51. public function setUp()
  52. {
  53. $this->gearmanParser = $this
  54. ->getMockBuilder('\Mmoreram\GearmanBundle\Service\GearmanParser')
  55. ->disableOriginalConstructor()
  56. ->setMethods(null)
  57. ->getMock();
  58. $this->bundleMock = $this
  59. ->getMockBuilder('\Symfony\Component\HttpKernel\Bundle\Bundle')
  60. ->disableOriginalConstructor()
  61. ->setMethods(array(
  62. 'getPath'
  63. ))
  64. ->getMock();
  65. }
  66. /**
  67. * Test service can be instanced through container
  68. */
  69. public function testGearmanParserLoadFromContainer()
  70. {
  71. static::$kernel = static::createKernel();
  72. static::$kernel->boot();
  73. $this->assertInstanceOf(
  74. '\Mmoreram\GearmanBundle\Service\GearmanParser',
  75. static::$kernel
  76. ->getContainer()
  77. ->get('gearman.parser')
  78. );
  79. }
  80. /**
  81. * testing getFileClassNamespace
  82. */
  83. public function testGetFileClassNamespaceSingle()
  84. {
  85. $mockNamespace = dirname(__FILE__) . '/Mocks/SingleCleanFile.php';
  86. /**
  87. * @var GearmanParser $gearmanParser
  88. */
  89. $gearmanParser = $this
  90. ->getMockBuilder('\Mmoreram\GearmanBundle\Service\GearmanParser')
  91. ->disableOriginalConstructor()
  92. ->setMethods(null)
  93. ->getMock();
  94. $this->assertEquals(
  95. 'Mmoreram\GearmanBundle\Tests\Service\Mocks\SingleCleanFile',
  96. $gearmanParser->getFileClassNamespace($mockNamespace)
  97. );
  98. }
  99. /**
  100. * testing getFileClassNamespace
  101. */
  102. public function testGetFileClassNamespaceCommented()
  103. {
  104. $mockNamespace = dirname(__FILE__) . '/Mocks/SingleCommentedFile.php';
  105. /**
  106. * @var GearmanParser $gearmanParser
  107. */
  108. $gearmanParser = $this
  109. ->getMockBuilder('\Mmoreram\GearmanBundle\Service\GearmanParser')
  110. ->disableOriginalConstructor()
  111. ->setMethods(null)
  112. ->getMock();
  113. $this->assertEquals(
  114. 'Mmoreram\GearmanBundle\Tests\Service\Mocks\SingleCommentedFile',
  115. $gearmanParser->getFileClassNamespace($mockNamespace)
  116. );
  117. }
  118. /**
  119. * Testing parseNamespaceMap with empty paths
  120. */
  121. public function testParseNamespaceMapEmptyPaths()
  122. {
  123. $paths = array();
  124. $excludedPaths = array();
  125. /**
  126. * @var Reader $reader
  127. */
  128. $reader = $this
  129. ->getMockBuilder('\Doctrine\Common\Annotations\Reader')
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. /**
  133. * @var Finder $finder
  134. */
  135. $finder = $this
  136. ->getMockBuilder('\Symfony\Component\Finder\Finder')
  137. ->disableOriginalConstructor()
  138. ->setMethods(array(
  139. 'files',
  140. ))
  141. ->getMock();
  142. $finder
  143. ->expects($this->never())
  144. ->method('getPath');
  145. $workerCollection = $this
  146. ->gearmanParser
  147. ->parseNamespaceMap($finder, $reader, $paths, $excludedPaths);
  148. $this->assertEquals($workerCollection, new workerCollection());
  149. }
  150. /**
  151. * Testing parseNamespaceMap with some paths
  152. */
  153. public function testParseNamespaceMapSomePaths()
  154. {
  155. $this->gearmanParser = $this
  156. ->getMockBuilder('\Mmoreram\GearmanBundle\Service\GearmanParser')
  157. ->disableOriginalConstructor()
  158. ->setMethods(array(
  159. 'parseFiles',
  160. ))
  161. ->getMock();
  162. $paths = array(
  163. dirname(__FILE__) . '/Mocks/',
  164. );
  165. $excludedPaths = array();
  166. $reader = $this
  167. ->getMockBuilder('\Doctrine\Common\Annotations\SimpleAnnotationReader')
  168. ->setMethods(null)
  169. ->getMock();
  170. $finder = $this
  171. ->getMockBuilder('\Symfony\Component\Finder\Finder')
  172. ->setMethods(null)
  173. ->getMock();
  174. $this
  175. ->gearmanParser
  176. ->expects($this->once())
  177. ->method('parseFiles')
  178. ->with(
  179. $this->equalTo($finder),
  180. $this->equalTo($reader),
  181. $this->equalTo(new WorkerCollection)
  182. );
  183. $workerCollection = $this
  184. ->gearmanParser
  185. ->parseNamespaceMap($finder, $reader, $paths, $excludedPaths);
  186. $this->assertEquals($workerCollection, new workerCollection());
  187. }
  188. /**
  189. * Testing parseNamespaceMap with some paths
  190. *
  191. * @dataProvider loadNamespaceMapDataProvider
  192. */
  193. public function testLoadNamespaceMap($active, $include, $ignore, $expectedPaths, $expectedExcludedPaths)
  194. {
  195. $this
  196. ->bundleMock
  197. ->expects($this->any())
  198. ->method('getPath')
  199. ->will($this->returnValue($this->bundlePath));
  200. $this->kernelBundles = array(
  201. "FirstBundleName" => $this->bundleMock,
  202. );
  203. list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array(
  204. "FirstBundle" => array(
  205. "name" => "FirstBundleName",
  206. "active" => $active,
  207. "include" => $include,
  208. "ignore" => $ignore,
  209. ),
  210. ));
  211. $this->assertEquals($paths, $expectedPaths);
  212. $this->assertEquals($excludedPaths, $expectedExcludedPaths);
  213. }
  214. /**
  215. * Load namespace map Data Provider
  216. */
  217. public function loadNamespaceMapDataProvider()
  218. {
  219. return array(
  220. // Testing loadNamespaceMap with includes and exclude values
  221. array(
  222. true,
  223. array(
  224. 'Controllers',
  225. 'libs'
  226. ),
  227. array(
  228. 'Services',
  229. 'Workers',
  230. 'libs',
  231. ),
  232. array(
  233. $this->bundlePath . '/Controllers/',
  234. $this->bundlePath . '/libs/',
  235. ),
  236. array(
  237. 'Services',
  238. 'Workers',
  239. 'libs',
  240. )
  241. ),
  242. // Testing loadNamespaceMap without Include and Exclude values
  243. array(
  244. true,
  245. array(),
  246. array(),
  247. array(
  248. $this->bundlePath . '/',
  249. ),
  250. array(),
  251. ),
  252. // Testing loadNamespaceMap with just exclude values
  253. array(
  254. true,
  255. array(),
  256. array(
  257. 'Services',
  258. 'Workers',
  259. ),
  260. array(
  261. $this->bundlePath . '/',
  262. ),
  263. array(
  264. 'Services',
  265. 'Workers',
  266. ),
  267. ),
  268. // Testing loadNamespaceMap with just Include values
  269. array(
  270. true,
  271. array(
  272. 'Services',
  273. 'Workers',
  274. ),
  275. array(),
  276. array(
  277. $this->bundlePath . '/Services/',
  278. $this->bundlePath . '/Workers/',
  279. ),
  280. array(),
  281. ),
  282. // Testing loadNamespaceMap with invalid bundle
  283. array(
  284. false,
  285. array(
  286. 'Services',
  287. 'Workers',
  288. ),
  289. array(
  290. 'Ignore',
  291. 'Tests',
  292. ),
  293. array(),
  294. array(),
  295. )
  296. );
  297. }
  298. }