ZippyTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Alchemy\Zippy\Tests;
  3. use Alchemy\Zippy\Zippy;
  4. use Alchemy\Zippy\Exception\NoAdapterOnPlatformException;
  5. use Alchemy\Zippy\Exception\FormatNotSupportedException;
  6. use Alchemy\Zippy\Exception\RuntimeException;
  7. class ZippyTest extends TestCase
  8. {
  9. /** @test */
  10. public function itShouldCreateAnArchive()
  11. {
  12. $filename = 'file.zippo';
  13. $fileToAdd = 'file1';
  14. $recursive = true;
  15. $adapter = $this->getSupportedAdapter();
  16. $adapter->expects($this->once())
  17. ->method('create')
  18. ->with($this->equalTo($filename), $this->equalTo($fileToAdd), $this->equalTo($recursive));
  19. $adapters = array($adapter);
  20. $strategy = $this->getStrategy('zippo', $adapters);
  21. $zippy = new Zippy($this->getContainer());
  22. $zippy->addStrategy($strategy);
  23. $zippy->create($filename, $fileToAdd, $recursive);
  24. }
  25. /** @test */
  26. public function itShouldCreateAnArchiveByForcingType()
  27. {
  28. $filename = 'file';
  29. $fileToAdd = 'file1';
  30. $recursive = true;
  31. $adapter = $this->getSupportedAdapter();
  32. $adapter->expects($this->once())
  33. ->method('create')
  34. ->with($this->equalTo($filename), $this->equalTo($fileToAdd), $this->equalTo($recursive));
  35. $adapters = array($adapter);
  36. $strategy = $this->getStrategy('zippo', $adapters);
  37. $zippy = new Zippy($this->getContainer());
  38. $zippy->addStrategy($strategy);
  39. $zippy->create($filename, $fileToAdd, $recursive, 'zippo');
  40. }
  41. /** @test */
  42. public function itShouldNotCreateAndThrowAnException()
  43. {
  44. $filename = 'file';
  45. $fileToAdd = 'file1';
  46. $recursive = true;
  47. $adapter = $this->getSupportedAdapter();
  48. $adapter->expects($this->never())->method('create');
  49. $adapters = array($adapter);
  50. $strategy = $this->getStrategy('zippo', $adapters);
  51. $zippy = new Zippy($this->getContainer());
  52. $zippy->addStrategy($strategy);
  53. try {
  54. $zippy->create($filename, $fileToAdd, $recursive, 'zippotte');
  55. $this->fail('Should have raised an exception');
  56. } catch (RuntimeException $e) {
  57. }
  58. }
  59. /** @test */
  60. public function itShouldOpenAnArchive()
  61. {
  62. $filename = 'file.zippo';
  63. $adapter = $this->getSupportedAdapter();
  64. $adapter->expects($this->once())
  65. ->method('open')
  66. ->with($this->equalTo($filename));
  67. $adapters = array($adapter);
  68. $strategy = $this->getStrategy('zippo', $adapters);
  69. $zippy = new Zippy($this->getContainer());
  70. $zippy->addStrategy($strategy);
  71. $zippy->open($filename);
  72. }
  73. /** @test */
  74. public function itShouldExposeContainerPassedOnConstructor()
  75. {
  76. $container = $this->getContainer();
  77. $zippy = new Zippy($container);
  78. $this->assertEquals($container, $zippy->adapters);
  79. }
  80. /** @test */
  81. public function itShouldRegisterStrategies()
  82. {
  83. $adapters = array($this->getSupportedAdapter());
  84. $strategy = $this->getStrategy('zippo', $adapters);
  85. $zippy = new Zippy($this->getContainer());
  86. $zippy->addStrategy($strategy);
  87. $this->assertEquals(array('zippo' => array($strategy)), $zippy->getStrategies());
  88. }
  89. /** @test */
  90. public function registerTwoStrategiesWithSameExtensionShouldBeinRightOrder()
  91. {
  92. $adapters1 = array($this->getSupportedAdapter());
  93. $strategy1 = $this->getStrategy('zippo', $adapters1);
  94. $adapters2 = array($this->getSupportedAdapter());
  95. $strategy2 = $this->getStrategy('zippo', $adapters2);
  96. $zippy = new Zippy($this->getContainer());
  97. $zippy->addStrategy($strategy1);
  98. $zippy->addStrategy($strategy2);
  99. $this->assertEquals(array('zippo' => array($strategy2, $strategy1)), $zippy->getStrategies());
  100. }
  101. /** @test */
  102. public function registerAStrategyTwiceShouldMoveItToLastAdded()
  103. {
  104. $adapters1 = array($this->getSupportedAdapter());
  105. $strategy1 = $this->getStrategy('zippo', $adapters1);
  106. $adapters2 = array($this->getSupportedAdapter());
  107. $strategy2 = $this->getStrategy('zippo', $adapters2);
  108. $zippy = new Zippy($this->getContainer());
  109. $zippy->addStrategy($strategy1);
  110. $zippy->addStrategy($strategy2);
  111. $zippy->addStrategy($strategy1);
  112. $this->assertEquals(array('zippo' => array($strategy1, $strategy2)), $zippy->getStrategies());
  113. }
  114. /** @test */
  115. public function itShouldReturnAnAdapterCorrespondingToTheRightStrategy()
  116. {
  117. $adapters = array($this->getSupportedAdapter());
  118. $strategy = $this->getStrategy('zippo', $adapters);
  119. $zippy = new Zippy($this->getContainer());
  120. $zippy->addStrategy($strategy);
  121. $this->assertEquals($adapters[0], $zippy->getAdapterFor('zippo'));
  122. $this->assertEquals($adapters[0], $zippy->getAdapterFor('.zippo'));
  123. $this->assertEquals($adapters[0], $zippy->getAdapterFor('ziPPo'));
  124. $this->assertEquals($adapters[0], $zippy->getAdapterFor('.ZIPPO'));
  125. }
  126. /** @test */
  127. public function itShouldThrowAnExceptionIfNoAdapterSupported()
  128. {
  129. $adapters = array($this->getNotSupportedAdapter());
  130. $strategy = $this->getStrategy('zippo', $adapters);
  131. $zippy = new Zippy($this->getContainer());
  132. $zippy->addStrategy($strategy);
  133. try {
  134. $zippy->getAdapterFor('zippo');
  135. $this->fail('Should have raised an exception');
  136. } catch (NoAdapterOnPlatformException $e) {
  137. }
  138. }
  139. /** @test */
  140. public function itShouldThrowAnExceptionIfFormatNotSupported()
  141. {
  142. $adapters = array($this->getSupportedAdapter());
  143. $strategy = $this->getStrategy('zippotte', $adapters);
  144. $zippy = new Zippy($this->getContainer());
  145. $zippy->addStrategy($strategy);
  146. try {
  147. $zippy->getAdapterFor('zippo');
  148. $this->fail('Should have raised an exception');
  149. } catch (FormatNotSupportedException $e) {
  150. }
  151. }
  152. /** @test */
  153. public function loadShouldRegisterStrategies()
  154. {
  155. $zippy = Zippy::load();
  156. $this->assertCount(7, $zippy->getStrategies());
  157. $this->assertArrayHasKey('zip', $zippy->getStrategies());
  158. $this->assertArrayHasKey('tar', $zippy->getStrategies());
  159. $this->assertArrayHasKey('tar.gz', $zippy->getStrategies());
  160. $this->assertArrayHasKey('tar.bz2', $zippy->getStrategies());
  161. $this->assertArrayHasKey('tbz2', $zippy->getStrategies());
  162. $this->assertArrayHasKey('tb2', $zippy->getStrategies());
  163. $this->assertArrayHasKey('tgz', $zippy->getStrategies());
  164. }
  165. private function getStrategy($extension, $adapters)
  166. {
  167. $strategy = $this->getMock('Alchemy\Zippy\FileStrategy\FileStrategyInterface');
  168. $strategy->expects($this->any())
  169. ->method('getFileExtension')
  170. ->will($this->returnValue($extension));
  171. $strategy->expects($this->any())
  172. ->method('getAdapters')
  173. ->will($this->returnValue($adapters));
  174. return $strategy;
  175. }
  176. private function getSupportedAdapter()
  177. {
  178. $adapter = $this->getMock('Alchemy\Zippy\Adapter\AdapterInterface');
  179. $adapter->expects($this->any())
  180. ->method('isSupported')
  181. ->will($this->returnValue(true));
  182. return $adapter;
  183. }
  184. private function getNotSupportedAdapter()
  185. {
  186. $adapter = $this->getMock('Alchemy\Zippy\Adapter\AdapterInterface');
  187. $adapter->expects($this->any())
  188. ->method('isSupported')
  189. ->will($this->returnValue(false));
  190. return $adapter;
  191. }
  192. private function getContainer()
  193. {
  194. return $this->getMock('Alchemy\Zippy\Adapter\AdapterContainer');
  195. }
  196. }