ProcedureLoaderTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Tests\Unit\Procedure;
  9. use Symfony\Component\Config\FileLocatorInterface;
  10. use JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface;
  11. use JonnyW\PhantomJs\Procedure\ProcedureLoader;
  12. /**
  13. * PHP PhantomJs
  14. *
  15. * @author Jon Wenmoth <contact@jonnyw.me>
  16. */
  17. class ProcedureLoaderTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Test filename
  21. *
  22. * @var string
  23. * @access protected
  24. */
  25. protected $filename;
  26. /**
  27. * Test directory
  28. *
  29. * @var string
  30. * @access protected
  31. */
  32. protected $directory;
  33. /** +++++++++++++++++++++++++++++++++++ **/
  34. /** ++++++++++++++ TESTS ++++++++++++++ **/
  35. /** +++++++++++++++++++++++++++++++++++ **/
  36. /**
  37. * Test load throws invalid argument exception
  38. * if file is not a local stream.
  39. *
  40. * @access public
  41. * @return void
  42. */
  43. public function testLoadThrowsInvalidArgumentExceptionIfFileIsNotALocalStream()
  44. {
  45. $this->setExpectedException('\InvalidArgumentException');
  46. $procedureFactory = $this->getProcedureFactory();
  47. $fileLocator = $this->getFileLocator();
  48. $fileLocator->expects($this->any())
  49. ->method('locate')
  50. ->will($this->returnValue('http://example.com/index.html'));
  51. $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
  52. $procedureLoader->load('test');
  53. }
  54. /**
  55. * Test load throws not exists exception if
  56. * file does not exist.
  57. *
  58. * @access public
  59. * @return void
  60. */
  61. public function testLoadThrowsNotExistsExceptionIfFileDoesNotExist()
  62. {
  63. $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotExistsException');
  64. $procedureFactory = $this->getProcedureFactory();
  65. $fileLocator = $this->getFileLocator();
  66. $fileLocator->expects($this->any())
  67. ->method('locate')
  68. ->will($this->returnValue('/invalid/file.proc'));
  69. $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
  70. $procedureLoader->load('test');
  71. }
  72. /**
  73. * Test load returns procedure instance.
  74. *
  75. * @access public
  76. * @return void
  77. */
  78. public function testLoadReturnsProcedureInstance()
  79. {
  80. $body = 'PROCEDURE BODY';
  81. $file = $this->writeProcedure($body);
  82. $procedureFactory = $this->getProcedureFactory();
  83. $fileLocator = $this->getFileLocator();
  84. $procedure = $this->getProcedure();
  85. $fileLocator->expects($this->any())
  86. ->method('locate')
  87. ->will($this->returnValue($file));
  88. $procedureFactory->expects($this->any())
  89. ->method('createProcedure')
  90. ->will($this->returnValue($procedure));
  91. $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
  92. $this->assertInstanceOf('\JonnyW\PhantomJs\Procedure\ProcedureInterface', $procedureLoader->load('test'));
  93. }
  94. /**
  95. * Test load sets procedure body in
  96. * procedure instance.
  97. *
  98. * @access public
  99. * @return void
  100. */
  101. public function testLoadSetsProcedureBodyInProcedureInstance()
  102. {
  103. $body = 'PROCEDURE BODY';
  104. $file = $this->writeProcedure($body);
  105. $procedureFactory = $this->getProcedureFactory();
  106. $fileLocator = $this->getFileLocator();
  107. $procedure = $this->getProcedure();
  108. $fileLocator->expects($this->any())
  109. ->method('locate')
  110. ->will($this->returnValue($file));
  111. $procedureFactory->expects($this->any())
  112. ->method('createProcedure')
  113. ->will($this->returnValue($procedure));
  114. $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
  115. $this->assertSame($body, $procedureLoader->load('test')->getProcedure());
  116. }
  117. /** +++++++++++++++++++++++++++++++++++ **/
  118. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  119. /** +++++++++++++++++++++++++++++++++++ **/
  120. /**
  121. * Get procedure loader instance.
  122. *
  123. * @access public
  124. * @param \JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface $procedureFactory
  125. * @param \Symfony\Component\Config\FileLocatorInterface $locator
  126. * @return \JonnyW\PhantomJs\Procedure\ProcedureLoader
  127. */
  128. protected function getProcedureLoader(ProcedureFactoryInterface $procedureFactory, FileLocatorInterface $locator)
  129. {
  130. $procedureLoader = new ProcedureLoader($procedureFactory, $locator);
  131. return $procedureLoader;
  132. }
  133. /** +++++++++++++++++++++++++++++++++++ **/
  134. /** ++++++++++ MOCKS / STUBS ++++++++++ **/
  135. /** +++++++++++++++++++++++++++++++++++ **/
  136. /**
  137. * Get mock procedure factory instance.
  138. *
  139. * @access protected
  140. * @return \JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface
  141. */
  142. protected function getProcedureFactory()
  143. {
  144. $mockProcedureFactory = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface');
  145. return $mockProcedureFactory;
  146. }
  147. /**
  148. * Get mock file locator instance.
  149. *
  150. * @access protected
  151. * @return \Symfony\Component\Config\FileLocatorInterface
  152. */
  153. protected function getFileLocator()
  154. {
  155. $mockFileLocator = $this->getMock('\Symfony\Component\Config\FileLocatorInterface');
  156. return $mockFileLocator;
  157. }
  158. /**
  159. * Get mock procedure instance.
  160. *
  161. * @access protected
  162. * @return \JonnyW\PhantomJs\Procedure\Procedure
  163. */
  164. protected function getProcedure()
  165. {
  166. $mockProcedure = $this->getMockBuilder('\JonnyW\PhantomJs\Procedure\Procedure')
  167. ->setMethods(null)
  168. ->disableOriginalConstructor()
  169. ->getMock();
  170. return $mockProcedure;
  171. }
  172. /** +++++++++++++++++++++++++++++++++++ **/
  173. /** ++++++++++++ UTILITIES ++++++++++++ **/
  174. /** +++++++++++++++++++++++++++++++++++ **/
  175. /**
  176. * Set up test environment.
  177. *
  178. * @access public
  179. * @return void
  180. */
  181. public function setUp()
  182. {
  183. $this->filename = 'test.proc';
  184. $this->directory = sys_get_temp_dir();
  185. if (!is_writable($this->directory)) {
  186. throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
  187. }
  188. }
  189. /**
  190. * Tear down test environment.
  191. *
  192. * @access public
  193. * @return void
  194. */
  195. public function tearDown()
  196. {
  197. $filename = $this->getFilename();
  198. if (file_exists($filename)) {
  199. unlink($filename);
  200. }
  201. }
  202. /**
  203. * Get test filename.
  204. *
  205. * @access public
  206. * @return string
  207. */
  208. public function getFilename()
  209. {
  210. return sprintf('%1$s/%2$s', $this->directory, $this->filename);
  211. }
  212. /**
  213. * Write procedure body to file.
  214. *
  215. * @access public
  216. * @param string $data
  217. * @return string
  218. */
  219. public function writeProcedure($procedure)
  220. {
  221. $filename = $this->getFilename();
  222. file_put_contents($filename, $procedure);
  223. return $filename;
  224. }
  225. }