GearmanParserTest.php 8.1 KB

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