ResumableTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Dilab\Test;
  3. use Dilab\Network\SimpleRequest;
  4. use Dilab\Resumable;
  5. use Cake\Filesystem\File;
  6. /**
  7. * Class ResumbableTest
  8. * @package Dilab\Test
  9. * @property $resumbable Resumable
  10. * @property $request Request
  11. * @property $response Response
  12. */
  13. class ResumbableTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public $resumbable;
  16. protected $provider;
  17. protected function setUp()
  18. {
  19. $this->request = $this->getMockBuilder('Dilab\Network\SimpleRequest')
  20. ->getMock();
  21. $this->response = $this->getMockBuilder('Dilab\Network\SimpleResponse')
  22. ->getMock();
  23. }
  24. public function tearDown()
  25. {
  26. unset($this->request);
  27. unset($this->response);
  28. parent::tearDown();
  29. }
  30. public function testProcessHandleChunk()
  31. {
  32. $resumableParams = array(
  33. 'resumableChunkNumber'=> 3,
  34. 'resumableTotalChunks'=> 600,
  35. 'resumableChunkSize'=> 200,
  36. 'resumableIdentifier'=> 'identifier',
  37. 'resumableFilename'=> 'mock.png',
  38. 'resumableRelativePath'=> 'upload',
  39. );
  40. $this->request->method('is')->will($this->returnValue(true));
  41. $this->request->method('file')
  42. ->will($this->returnValue(array(
  43. 'name'=> 'mock.png',
  44. 'tmp_name'=> 'test/files/mock.png.part3',
  45. 'error'=> 0,
  46. 'size'=> 27000,
  47. )));
  48. $this->request->method('data')->willReturn($resumableParams);
  49. $this->resumbable = $this->getMockBuilder('Dilab\Resumable')
  50. ->setConstructorArgs(array($this->request,$this->response))
  51. ->setMethods(array('handleChunk'))
  52. ->getMock();
  53. $this->resumbable->expects($this->once())
  54. ->method('handleChunk')
  55. ->willReturn(true);
  56. $this->resumbable->process();
  57. }
  58. public function testProcessHandleTestChunk()
  59. {
  60. $resumableParams = array(
  61. 'resumableChunkNumber'=> 3,
  62. 'resumableTotalChunks'=> 600,
  63. 'resumableChunkSize'=> 200,
  64. 'resumableIdentifier'=> 'identifier',
  65. 'resumableFilename'=> 'mock.png',
  66. 'resumableRelativePath'=> 'upload',
  67. );
  68. $this->request->method('is')->will($this->returnValue(true));
  69. $this->request->method('file')->will($this->returnValue(array()));
  70. $this->request->method('data')->willReturn($resumableParams);
  71. $this->resumbable = $this->getMockBuilder('Dilab\Resumable')
  72. ->setConstructorArgs(array($this->request,$this->response))
  73. ->setMethods(array('handleTestChunk'))
  74. ->getMock();
  75. $this->resumbable->expects($this->once())
  76. ->method('handleTestChunk')
  77. ->willReturn(true);
  78. $this->resumbable->process();
  79. }
  80. public function testHandleTestChunk()
  81. {
  82. $this->request->method('is')
  83. ->will($this->returnValue(true));
  84. $this->request->method('data')
  85. ->willReturn(array(
  86. 'resumableChunkNumber'=> 1,
  87. 'resumableTotalChunks'=> 600,
  88. 'resumableChunkSize'=> 200,
  89. 'resumableIdentifier'=> 'identifier',
  90. 'resumableFilename'=> 'mock.png',
  91. 'resumableRelativePath'=> 'upload',
  92. ));
  93. $this->response->expects($this->once())
  94. ->method('header')
  95. ->with($this->equalTo(200));
  96. $this->resumbable = new Resumable($this->request,$this->response);
  97. $this->resumbable->tempFolder = 'test/tmp';
  98. $this->resumbable->handleTestChunk();
  99. }
  100. public function testHandleChunk() {
  101. $resumableParams = array(
  102. 'resumableChunkNumber'=> 3,
  103. 'resumableTotalChunks'=> 600,
  104. 'resumableChunkSize'=> 200,
  105. 'resumableIdentifier'=> 'identifier',
  106. 'resumableFilename'=> 'mock.png',
  107. 'resumableRelativePath'=> 'upload',
  108. );
  109. $this->request->method('is')
  110. ->will($this->returnValue(true));
  111. $this->request->method('data')
  112. ->willReturn($resumableParams);
  113. $this->request->method('file')
  114. ->willReturn(array(
  115. 'name'=> 'mock.png',
  116. 'tmp_name'=> 'test/files/mock.png.part3',
  117. 'error'=> 0,
  118. 'size'=> 27000,
  119. ));
  120. $this->resumbable = new Resumable($this->request, $this->response);
  121. $this->resumbable->tempFolder = 'test/tmp';
  122. $this->resumbable->uploadFolder = 'test/uploads';
  123. $this->resumbable->deleteTmpFolder = false;
  124. $this->resumbable->handleChunk();
  125. $this->assertFileExists('test/uploads/mock.png');
  126. unlink('test/tmp/identifier/mock.png.part3');
  127. unlink('test/uploads/mock.png');
  128. }
  129. public function testResumableParamsGetRequest()
  130. {
  131. $resumableParams = array(
  132. 'resumableChunkNumber'=> 1,
  133. 'resumableTotalChunks'=> 100,
  134. 'resumableChunkSize'=> 1000,
  135. 'resumableIdentifier'=> 100,
  136. 'resumableFilename'=> 'mock_file_name',
  137. 'resumableRelativePath'=> 'upload',
  138. );
  139. $this->request = $this->getMockBuilder('Dilab\Network\SimpleRequest')
  140. ->getMock();
  141. $this->request->method('is')
  142. ->will($this->returnValue(true));
  143. $this->request->method('data')->willReturn($resumableParams);
  144. $this->resumbable = new Resumable($this->request,$this->response);
  145. $this->assertEquals($resumableParams, $this->resumbable->resumableParams());
  146. }
  147. public function isFileUploadCompleteProvider()
  148. {
  149. return array(
  150. array('mock.png', 'files', 20, 60, true),
  151. array('mock.png','files', 25, 60, true),
  152. array('mock.png','files', 10, 60, false),
  153. );
  154. }
  155. /**
  156. *
  157. * @dataProvider isFileUploadCompleteProvider
  158. */
  159. public function testIsFileUploadComplete($filename,$identifier, $chunkSize, $totalSize, $expected)
  160. {
  161. $this->resumbable = new Resumable($this->request,$this->response);
  162. $this->resumbable->tempFolder ='test';
  163. $this->assertEquals($expected, $this->resumbable->isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize));
  164. }
  165. public function testIsChunkUploaded()
  166. {
  167. $this->resumbable = new Resumable($this->request,$this->response);
  168. $this->resumbable->tempFolder ='test';
  169. $identifier = 'files';
  170. $filename = 'mock.png';
  171. $this->assertTrue($this->resumbable->isChunkUploaded($identifier,$filename,1));
  172. $this->assertFalse($this->resumbable->isChunkUploaded($identifier,$filename,10));
  173. }
  174. public function testTmpChunkDir()
  175. {
  176. $this->resumbable = new Resumable($this->request,$this->response);
  177. $this->resumbable->tempFolder ='test';
  178. $identifier = 'mock-identifier';
  179. $expected = $this->resumbable->tempFolder.DIRECTORY_SEPARATOR.$identifier;
  180. $this->assertEquals($expected, $this->resumbable->tmpChunkDir($identifier));
  181. $this->assertFileExists($expected);
  182. rmdir($expected);
  183. }
  184. public function testTmpChunkFile()
  185. {
  186. $this->resumbable = new Resumable($this->request,$this->response);
  187. $filename = 'mock-file.png';
  188. $chunkNumber = 1;
  189. $expected = $filename.'.part'.$chunkNumber;
  190. $this->assertEquals($expected, $this->resumbable->tmpChunkFilename($filename,$chunkNumber));
  191. }
  192. public function testCreateFileFromChunks()
  193. {
  194. $files = array(
  195. 'test/files/mock.png.part1',
  196. 'test/files/mock.png.part2',
  197. 'test/files/mock.png.part3',
  198. );
  199. $totalFileSize = array_sum(array(
  200. filesize('test/files/mock.png.part1'),
  201. filesize('test/files/mock.png.part2'),
  202. filesize('test/files/mock.png.part3')
  203. ));
  204. $destFile = 'test/files/5.png';
  205. $this->resumbable = new Resumable($this->request,$this->response);
  206. $this->resumbable->createFileFromChunks($files, $destFile);
  207. $this->assertFileExists($destFile);
  208. $this->assertEquals($totalFileSize, filesize($destFile));
  209. unlink('test/files/5.png');
  210. }
  211. public function testMoveUploadedFile()
  212. {
  213. $destFile = 'test/files/4.png';
  214. $this->resumbable = new Resumable($this->request,$this->response);
  215. $this->resumbable->moveUploadedFile('test/files/mock.png.part1', $destFile);
  216. $this->assertFileExists($destFile);
  217. unlink($destFile);
  218. }
  219. }