Skip to content

Commit

Permalink
Added UploadFileHelper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel Sint committed Aug 19, 2013
1 parent 9120d01 commit 130bb59
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 6 deletions.
4 changes: 2 additions & 2 deletions File/UploadFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ protected function getObjectManager()
/**
* Add an editor helper
*
* @param string $name
* @param EditorHelperInterface $helper
* @param string $name
* @param UploadEditorHelperInterface $helper
*/
public function addEditorHelper($name, UploadEditorHelperInterface $helper)
{
Expand Down
10 changes: 7 additions & 3 deletions Tests/Functional/Doctrine/Phpcr/MediaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ public function setDefaultsProvider()
public function testSetDefaults($expectedName = null, $name = null, $nameExists = false, $id = null)
{
$returnMediaExists = $nameExists ? new Media() : null;
$rootPath = '/test/media/file';
$managerName = 'anothermanager';

$this->registryMock->expects($this->once())
->method('getManager')
->with($this->equalTo('themanager'))
->with($this->equalTo($managerName))
->will($this->returnValue($this->dmMock))
;
$this->dmMock->expects($this->any())
->method('find')
->will($this->returnValueMap(array(
array('Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Media', '/test/media/'.$name, $returnMediaExists),
array(null, '/test/media', $this->testRoot),
array('Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Media', $rootPath.'/'.$name, $returnMediaExists),
array(null, $rootPath, $this->testRoot),
)))
;

Expand All @@ -100,6 +102,8 @@ public function testSetDefaults($expectedName = null, $name = null, $nameExists
$media->setName($name);

$mediaManager = $this->getMediaManager();
$mediaManager->setRootPath($rootPath);
$mediaManager->setManagerName($managerName);
$mediaManager->setDefaults($media);

$this->assertEquals($this->testRoot, $media->getParent());
Expand Down
207 changes: 207 additions & 0 deletions Tests/Unit/File/UploadFileHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

namespace Symfony\Cmf\Bundle\MediaBundle\Tests\Unit\File;

use org\bovigo\vfs\vfsStream;
use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelper;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class UploadFileHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $containerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $registryMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dmMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mediaManagerMock;
/**
* @var string
*/
private $class;
/**
* @var string
*/
private $rootPath;

public function setUp()
{
$this->containerMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')
->disableOriginalConstructor()
->getMock()
;
$this->registryMock = $this->getMockBuilder('Doctrine\Bundle\PHPCRBundle\ManagerRegistry')
->disableOriginalConstructor()
->getMock()
;
$this->dmMock = $this->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager')
->disableOriginalConstructor()
->getMock()
;
$this->mediaManagerMock = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\MediaManager')
->disableOriginalConstructor()
->getMock()
;
$this->class = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File';
$this->rootPath = '/test/media';
}

private function getUploadFileHelper()
{
return new UploadFileHelper($this->registryMock, 'themanager', $this->class, $this->rootPath, $this->mediaManagerMock);
}

public function testAddGetEditorHelper()
{
$uploadFileHelper = $this->getUploadFileHelper();

$this->assertNull($uploadFileHelper->getEditorHelper());
$this->assertNull($uploadFileHelper->getEditorHelper('unknown'));

$uploadDefaultHelper = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadDefaultHelper')
->disableOriginalConstructor()
->getMock()
;
$uploadCkeditorHelper = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadCkeditorHelper')
->disableOriginalConstructor()
->getMock()
;

$uploadFileHelper->addEditorHelper('default', $uploadDefaultHelper);
$uploadFileHelper->addEditorHelper('ckeditor', $uploadCkeditorHelper);

$this->assertEquals($uploadDefaultHelper, $uploadFileHelper->getEditorHelper('default'));
$this->assertEquals($uploadDefaultHelper, $uploadFileHelper->getEditorHelper('unknown'));
$this->assertEquals($uploadCkeditorHelper, $uploadFileHelper->getEditorHelper('ckeditor'));
}

public function testHandleUploadedFile()
{
vfsStream::setup('home');
$testFile = vfsStream::url('home/test.txt');
file_put_contents($testFile, "Test file content.");

$class = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File';
$uploadFileHelper = $this->getUploadFileHelper();
$uploadFileHelper->setClass($class);
$uploadFileHelper->setRootPath($this->rootPath.'/file');
$uploadedFile = new UploadedFile($testFile, 'test.txt');

$this->mediaManagerMock->expects($this->once())
->method('setDefaults')
->with(
$this->isInstanceOf($this->class),
$this->equalTo($this->rootPath.'/file')
)
;

$file = $uploadFileHelper->handleUploadedFile($uploadedFile);

$this->assertInstanceOf($class, $file);
$this->assertEquals('test.txt', $file->getName());
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testHandleUploadedFileException()
{
vfsStream::setup('home');
$testFile = vfsStream::url('home/test.txt');
file_put_contents($testFile, "Test file content.");

$uploadFileHelper = $this->getUploadFileHelper();
$uploadedFile = new UploadedFile($testFile, 'test.txt');

$this->mediaManagerMock->expects($this->once())
->method('setDefaults')
->will($this->throwException(new \RuntimeException()))
;

$uploadFileHelper->handleUploadedFile($uploadedFile);
}

public function testGetUploadedResponse()
{
vfsStream::setup('home');
$testFile = vfsStream::url('home/test.txt');
file_put_contents($testFile, "Test file content.");

$request = new Request();
$request->files->set('file', new UploadedFile($testFile, 'test.txt'));
$response = new Response('upload response');

$uploadFileHelper = $this->getUploadFileHelper();

$uploadDefaultHelper = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadDefaultHelper')
->disableOriginalConstructor()
->getMock()
;
$uploadFileHelper->addEditorHelper('default', $uploadDefaultHelper);

$uploadDefaultHelper->expects($this->once())
->method('setFileDefaults')
->with(
$this->equalTo($request),
$this->isInstanceOf($this->class)
)
;

$class = $this->class;
$uploadDefaultHelper->expects($this->once())
->method('getUploadResponse')
->with(
$this->equalTo($request),
$this->callback(function ($files) use ($class) {
return isset($files[0]) && $files[0] instanceof $class;
})
)
->will($this->returnValue($response))
;

$this->mediaManagerMock->expects($this->once())
->method('setDefaults')
->with(
$this->isInstanceOf($this->class),
$this->equalTo($this->rootPath)
)
;

$uploadFileHelper->setManagerName('anothermanager');
$this->registryMock->expects($this->any())
->method('getManager')
->with($this->equalTo('anothermanager'))
->will($this->returnValue($this->dmMock))
;
$this->dmMock->expects($this->once())
->method('persist')
->with($this->isInstanceOf($this->class))
;
$this->dmMock->expects($this->once())
->method('flush')
;

$this->assertEquals($response, $uploadFileHelper->getUploadResponse($request));
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testGetUploadResponseException()
{
$uploadFileHelper = $this->getUploadFileHelper();

$uploadFileHelper->getUploadResponse(new Request());
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require-dev": {
"symfony-cmf/testing": "1.0.*",
"jms/serializer-bundle": "0.12.*",
"liip/imagine-bundle": "dev-master"
"liip/imagine-bundle": "dev-master",
"mikey179/vfsStream": "~1.2"
},
"suggest": {
"knplabs/gaufrette": "When using the Gaufrette adapter",
Expand Down

0 comments on commit 130bb59

Please sign in to comment.