123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace Dilab\Test;
- use Dilab\Network\SimpleRequest;
- use Dilab\Resumable;
- use Cake\Filesystem\File;
- /**
- * Class ResumbableTest
- * @package Dilab\Test
- * @property $resumbable Resumable
- * @property $request Request
- * @property $response Response
- */
- class ResumbableTest extends \PHPUnit_Framework_TestCase
- {
- public $resumbable;
- protected $provider;
- protected function setUp()
- {
- $this->request = $this->getMockBuilder('Dilab\Network\SimpleRequest')
- ->getMock();
- $this->response = $this->getMockBuilder('Dilab\Network\SimpleResponse')
- ->getMock();
- }
- public function tearDown()
- {
- unset($this->request);
- unset($this->response);
- parent::tearDown();
- }
- public function testProcessHandleChunk()
- {
- $resumableParams = array(
- 'resumableChunkNumber'=> 3,
- 'resumableTotalChunks'=> 600,
- 'resumableChunkSize'=> 200,
- 'resumableIdentifier'=> 'identifier',
- 'resumableFilename'=> 'mock.png',
- 'resumableRelativePath'=> 'upload',
- );
- $this->request->method('is')->will($this->returnValue(true));
- $this->request->method('file')
- ->will($this->returnValue(array(
- 'name'=> 'mock.png',
- 'tmp_name'=> 'test/files/mock.png.part3',
- 'error'=> 0,
- 'size'=> 27000,
- )));
- $this->request->method('data')->willReturn($resumableParams);
- $this->resumbable = $this->getMockBuilder('Dilab\Resumable')
- ->setConstructorArgs(array($this->request,$this->response))
- ->setMethods(array('handleChunk'))
- ->getMock();
- $this->resumbable->expects($this->once())
- ->method('handleChunk')
- ->willReturn(true);
- $this->resumbable->process();
- }
- public function testProcessHandleTestChunk()
- {
- $resumableParams = array(
- 'resumableChunkNumber'=> 3,
- 'resumableTotalChunks'=> 600,
- 'resumableChunkSize'=> 200,
- 'resumableIdentifier'=> 'identifier',
- 'resumableFilename'=> 'mock.png',
- 'resumableRelativePath'=> 'upload',
- );
- $this->request->method('is')->will($this->returnValue(true));
- $this->request->method('file')->will($this->returnValue(array()));
- $this->request->method('data')->willReturn($resumableParams);
- $this->resumbable = $this->getMockBuilder('Dilab\Resumable')
- ->setConstructorArgs(array($this->request,$this->response))
- ->setMethods(array('handleTestChunk'))
- ->getMock();
- $this->resumbable->expects($this->once())
- ->method('handleTestChunk')
- ->willReturn(true);
- $this->resumbable->process();
- }
- public function testHandleTestChunk()
- {
- $this->request->method('is')
- ->will($this->returnValue(true));
- $this->request->method('data')
- ->willReturn(array(
- 'resumableChunkNumber'=> 1,
- 'resumableTotalChunks'=> 600,
- 'resumableChunkSize'=> 200,
- 'resumableIdentifier'=> 'identifier',
- 'resumableFilename'=> 'mock.png',
- 'resumableRelativePath'=> 'upload',
- ));
- $this->response->expects($this->once())
- ->method('header')
- ->with($this->equalTo(200));
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->resumbable->tempFolder = 'test/tmp';
- $this->resumbable->handleTestChunk();
- }
- public function testHandleChunk() {
- $resumableParams = array(
- 'resumableChunkNumber'=> 3,
- 'resumableTotalChunks'=> 600,
- 'resumableChunkSize'=> 200,
- 'resumableIdentifier'=> 'identifier',
- 'resumableFilename'=> 'mock.png',
- 'resumableRelativePath'=> 'upload',
- );
- $this->request->method('is')
- ->will($this->returnValue(true));
- $this->request->method('data')
- ->willReturn($resumableParams);
- $this->request->method('file')
- ->willReturn(array(
- 'name'=> 'mock.png',
- 'tmp_name'=> 'test/files/mock.png.part3',
- 'error'=> 0,
- 'size'=> 27000,
- ));
- $this->resumbable = new Resumable($this->request, $this->response);
- $this->resumbable->tempFolder = 'test/tmp';
- $this->resumbable->uploadFolder = 'test/uploads';
- $this->resumbable->deleteTmpFolder = false;
- $this->resumbable->handleChunk();
- $this->assertFileExists('test/uploads/mock.png');
- unlink('test/tmp/identifier/mock.png.part3');
- unlink('test/uploads/mock.png');
- }
- public function testResumableParamsGetRequest()
- {
- $resumableParams = array(
- 'resumableChunkNumber'=> 1,
- 'resumableTotalChunks'=> 100,
- 'resumableChunkSize'=> 1000,
- 'resumableIdentifier'=> 100,
- 'resumableFilename'=> 'mock_file_name',
- 'resumableRelativePath'=> 'upload',
- );
- $this->request = $this->getMockBuilder('Dilab\Network\SimpleRequest')
- ->getMock();
- $this->request->method('is')
- ->will($this->returnValue(true));
- $this->request->method('data')->willReturn($resumableParams);
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->assertEquals($resumableParams, $this->resumbable->resumableParams());
- }
- public function isFileUploadCompleteProvider()
- {
- return array(
- array('mock.png', 'files', 20, 60, true),
- array('mock.png','files', 25, 60, true),
- array('mock.png','files', 10, 60, false),
- );
- }
- /**
- *
- * @dataProvider isFileUploadCompleteProvider
- */
- public function testIsFileUploadComplete($filename,$identifier, $chunkSize, $totalSize, $expected)
- {
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->resumbable->tempFolder ='test';
- $this->assertEquals($expected, $this->resumbable->isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize));
- }
- public function testIsChunkUploaded()
- {
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->resumbable->tempFolder ='test';
- $identifier = 'files';
- $filename = 'mock.png';
- $this->assertTrue($this->resumbable->isChunkUploaded($identifier,$filename,1));
- $this->assertFalse($this->resumbable->isChunkUploaded($identifier,$filename,10));
- }
- public function testTmpChunkDir()
- {
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->resumbable->tempFolder ='test';
- $identifier = 'mock-identifier';
- $expected = $this->resumbable->tempFolder.DIRECTORY_SEPARATOR.$identifier;
- $this->assertEquals($expected, $this->resumbable->tmpChunkDir($identifier));
- $this->assertFileExists($expected);
- rmdir($expected);
- }
- public function testTmpChunkFile()
- {
- $this->resumbable = new Resumable($this->request,$this->response);
- $filename = 'mock-file.png';
- $chunkNumber = 1;
- $expected = $filename.'.part'.$chunkNumber;
- $this->assertEquals($expected, $this->resumbable->tmpChunkFilename($filename,$chunkNumber));
- }
- public function testCreateFileFromChunks()
- {
- $files = array(
- 'test/files/mock.png.part1',
- 'test/files/mock.png.part2',
- 'test/files/mock.png.part3',
- );
- $totalFileSize = array_sum(array(
- filesize('test/files/mock.png.part1'),
- filesize('test/files/mock.png.part2'),
- filesize('test/files/mock.png.part3')
- ));
- $destFile = 'test/files/5.png';
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->resumbable->createFileFromChunks($files, $destFile);
- $this->assertFileExists($destFile);
- $this->assertEquals($totalFileSize, filesize($destFile));
- unlink('test/files/5.png');
- }
- public function testMoveUploadedFile()
- {
- $destFile = 'test/files/4.png';
- $this->resumbable = new Resumable($this->request,$this->response);
- $this->resumbable->moveUploadedFile('test/files/mock.png.part1', $destFile);
- $this->assertFileExists($destFile);
- unlink($destFile);
- }
- }
|