ResourceManagerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Alchemy\Zippy\Tests\Resource;
  3. use Alchemy\Zippy\Tests\TestCase;
  4. use Alchemy\Zippy\Resource\ResourceManager;
  5. class ResourceManagerTest extends TestCase
  6. {
  7. /**
  8. * @covers Alchemy\Zippy\Resource\ResourceManager::handle
  9. */
  10. public function testHandle()
  11. {
  12. $mapper = $this->getRequestMapperMock();
  13. $manager = new ResourceManager(
  14. $mapper,
  15. $this->getResourceTeleporterMock(),
  16. $this->getFilesystemMock()
  17. );
  18. $context = '/path/to/current/directory';
  19. $request = array('/path/to/a/file1', '/path/to/a/file2');
  20. $expectedCollection = $this->getMockBuilder('Alchemy\Zippy\Resource\ResourceCollection')
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $expectedCollection->expects($this->any())
  24. ->method('getIterator')
  25. ->will($this->returnValue(new \ArrayIterator(array())));
  26. $mapper->expects($this->once())
  27. ->method('map')
  28. ->with($this->equalTo($context), $this->equalTo($request))
  29. ->will($this->returnValue($expectedCollection));
  30. $collection = $manager->handle($context, $request);
  31. $this->assertEquals($expectedCollection, $collection);
  32. }
  33. /**
  34. * @covers Alchemy\Zippy\Resource\ResourceManager::cleanup
  35. */
  36. public function testCleanup()
  37. {
  38. $fs = $this->getFilesystemMock();
  39. $manager = new ResourceManager(
  40. $this->getRequestMapperMock(),
  41. $this->getResourceTeleporterMock(),
  42. $fs
  43. );
  44. $context = 'context' . mt_rand();
  45. $fs->expects($this->once())
  46. ->method('remove')
  47. ->with($this->equalTo($context));
  48. $collection = $this->getMockBuilder('Alchemy\Zippy\Resource\ResourceCollection')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $collection->expects($this->once())
  52. ->method('isTemporary')
  53. ->will($this->returnValue(true));
  54. $collection->expects($this->once())
  55. ->method('getContext')
  56. ->will($this->returnValue($context));
  57. $manager->cleanup($collection);
  58. }
  59. /**
  60. * @covers Alchemy\Zippy\Resource\ResourceManager::cleanup
  61. */
  62. public function testCleanupWhenCollectionIsNotTemporary()
  63. {
  64. $fs = $this->getFilesystemMock();
  65. $manager = new ResourceManager(
  66. $this->getRequestMapperMock(),
  67. $this->getResourceTeleporterMock(),
  68. $fs
  69. );
  70. $fs->expects($this->never())
  71. ->method('remove');
  72. $collection = $this->getMockBuilder('Alchemy\Zippy\Resource\ResourceCollection')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $collection->expects($this->once())
  76. ->method('isTemporary')
  77. ->will($this->returnValue(false));
  78. $collection->expects($this->never())
  79. ->method('getContext');
  80. $manager->cleanup($collection);
  81. }
  82. /**
  83. * @covers Alchemy\Zippy\Resource\ResourceManager::handle
  84. */
  85. public function testFunctionnal()
  86. {
  87. $wd = __DIR__;
  88. $tmpdir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
  89. $filesystem = new \Symfony\Component\Filesystem\Filesystem();
  90. $filesystem->mkdir($tmpdir . '/path/to/local/');
  91. $filesystem->mkdir($tmpdir . '/to/');
  92. $filesystem->mkdir($tmpdir . '/path/to/a');
  93. touch($tmpdir . '/path/to/local/file.ext');
  94. touch($tmpdir . '/path/to/local/file2.ext');
  95. touch($tmpdir . '/to/file3.ext');
  96. $request = array(
  97. $wd . '/input/path/to/local/file.ext',
  98. $wd . '/input/path/to/a/../local/file2.ext',
  99. $tmpdir . '/path/to/local/file.ext',
  100. $tmpdir . '/path/to/a/../local/file2.ext',
  101. 'http://www.google.com/+/business/images/plus-badge.png',
  102. 'http://www.google.com/+/business/images/plusone-button.png',
  103. 'file://' . $tmpdir . '/to/file3.ext',
  104. 'file://' . $wd . '/input/path/to/a/../local/file3.ext',
  105. '/I/want/this/file/to/go/there' => 'file://' . $wd . '/input/path/to/local/file2.ext',
  106. '/I/want/this/file/to/go/here' => 'file://' . $wd . '/input/path/to/local/file3.ext'
  107. );
  108. $expected = array(
  109. 'input/path/to/local/file.ext',
  110. 'input/path/to/local/file2.ext',
  111. 'file.ext',
  112. 'file2.ext',
  113. 'plus-badge.png',
  114. 'plusone-button.png',
  115. 'file3.ext',
  116. 'input/path/to/local/file3.ext',
  117. 'I/want/this/file/to/go/there',
  118. 'I/want/this/file/to/go/here',
  119. );
  120. $expectedSource = array(
  121. $request[0],
  122. $request[1],
  123. $request[2],
  124. $request[3],
  125. $request[4],
  126. $request[5],
  127. $request[6],
  128. $request[7],
  129. $request['/I/want/this/file/to/go/there'],
  130. $request['/I/want/this/file/to/go/here'],
  131. );
  132. $resourceManger = ResourceManager::create();
  133. $collection = $resourceManger->handle($wd, $request);
  134. $this->assertCount(10, $collection);
  135. $n = 0;
  136. foreach ($collection as $resource) {
  137. $this->assertEquals($expected[$n], $resource->getTarget());
  138. $this->assertEquals($expectedSource[$n], $resource->getOriginal());
  139. $n++;
  140. }
  141. }
  142. protected function getRequestMapperMock()
  143. {
  144. return $this->getMockBuilder('Alchemy\Zippy\Resource\RequestMapper')
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. }
  148. protected function getResourceTeleporterMock()
  149. {
  150. return $this->getMockBuilder('Alchemy\Zippy\Resource\ResourceTeleporter')
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. }
  154. protected function getFilesystemMock()
  155. {
  156. return $this->getMockBuilder('Symfony\Component\Filesystem\Filesystem')
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. }
  160. }