Permalink
Browse files

Tests back to 100% and fucktorings.

  • Loading branch information...
pekkis committed Jun 13, 2012
1 parent 4c51c55 commit cf151914b0341c7062b8f6bc332f6e3d9495d10c
@@ -48,31 +48,46 @@ public function __construct(FileOperator $fileOperator, $upload, Folder $folder,
}
- public function getResource(FileUpload $upload)
+ public function getResource(File $file, FileUpload $upload)
{
+ $file = clone $file;
+
$hash = sha1_file($upload->getRealPath());
$profileObj = $this->fileOperator->getProfile($this->profile);
- if ($profileObj->isSharedResourceAllowed()) {
- $resources = $this->fileOperator->getBackend()->findResourcesByHash($hash);
- if ($resources) {
- foreach ($resources as $resource) {
- if (!$resource->isExclusive()) {
- return $resource;
- }
+
+ $resources = $this->fileOperator->getBackend()->findResourcesByHash($hash);
+ if ($resources) {
+ foreach ($resources as $resource) {
+ if (!$resource->isExclusive()) {
+ $file->setResource($resource);
+ break;
}
}
+
+ if (!$profileObj->isSharedResourceAllowed($file)) {
+ $file->unsetResource();
+ }
+
}
- $resource = new Resource();
- $resource->setDateCreated(new DateTime());
- $resource->setHash($hash);
- $resource->setSize($upload->getSize());
- $resource->setMimetype($upload->getMimeType());
- $this->fileOperator->getBackend()->createResource($resource);
- return $resource;
- }
+ if (!$file->getResource()) {
+ $resource = new Resource();
+ $resource->setDateCreated(new DateTime());
+ $resource->setHash($hash);
+ $resource->setSize($upload->getSize());
+ $resource->setMimetype($upload->getMimeType());
+ if (!$profileObj->isSharedResourceAllowed($file)) {
+ $resource->setExclusive(true);
+ }
+
+ $this->fileOperator->getBackend()->createResource($resource);
+ $file->setResource($resource);
+ }
+
+ return $file->getResource();
+ }
public function execute()
{
@@ -101,10 +116,9 @@ public function execute()
// @todo: actual statuses
$file->setStatus(File::STATUS_RAW);
- $resource = $this->getResource($upload);
+ $resource = $this->getResource($file, $upload);
$file->setResource($resource);
-
$this->fileOperator->getBackend()->upload($file, $folder);
$this->fileOperator->getStorage()->store($resource, $upload->getRealPath());
@@ -287,6 +287,10 @@ public function getResource()
return $this->resource;
}
+ public function unsetResource()
+ {
+ $this->resource = null;
+ }
/**
* Returns the file as standardized file array
@@ -326,15 +326,17 @@ private function ensureFileVersionArrayExists($fileType)
}
/**
- * Returns whether profile allows shared resources
+ * Returns whether profile allows shared resources for a file
*
+ * @param File $file
* @return boolean
*/
- public function isSharedResourceAllowed()
+ public function isSharedResourceAllowed(File $file)
{
foreach ($this->getPlugins() as $plugin) {
+
if ($plugin instanceof VersionProvider) {
- if (!$plugin->isSharedResourceAllowed()) {
+ if ($plugin->providesFor($file) && !$plugin->isSharedResourceAllowed()) {
return false;
}
}
@@ -52,7 +52,12 @@ public function publishVersion(File $file, $version, VersionProvider $versionPro
mkdir($path, $this->getDirectoryPermission(), true);
}
- $tmp = $this->getFilelib()->getStorage()->retrieveVersion($file->getResource(), $version);
+ if ($versionProvider->areSharedVersionsAllowed()) {
+ $tmp = $this->getFilelib()->getStorage()->retrieveVersion($file->getResource(), $version, null);
+ } else {
+ $tmp = $this->getFilelib()->getStorage()->retrieveVersion($file->getResource(), $version, $file);
+ }
+
copy($tmp, $link);
chmod($link, $this->getFilePermission());
}
@@ -4,6 +4,7 @@
use Xi\Filelib\FileLibrary;
use Xi\Filelib\Command;
+use Xi\Filelib\AbstractOperator;
class AbstractOperatorTest extends TestCase
{
@@ -268,5 +269,61 @@ public function executeOrQueueShouldExecuteWithSynchronousStrategy()
}
+ public function provideCallbackStrategies()
+ {
+ return array(
+ array('asynchronous', Command::STRATEGY_ASYNCHRONOUS),
+ array('synchronous', Command::STRATEGY_SYNCHRONOUS),
+ );
+ }
+
+ /**
+ * @test
+ * @dataProvider provideCallbackStrategies
+ */
+ public function executeOrQueueShouldUtilizeCallbacks($expectedValue, $strategy)
+ {
+ $callbacks = array(
+ Command::STRATEGY_ASYNCHRONOUS => function(AbstractOperator $op, $ret) {
+ return 'asynchronous';
+ },
+ Command::STRATEGY_SYNCHRONOUS => function(AbstractOperator $op, $ret) {
+ return 'synchronous';
+ }
+ );
+
+ $op = $this->getMockBuilder('Xi\Filelib\AbstractOperator')
+ ->disableOriginalConstructor()
+ ->setMethods(array('getCommandStrategy', 'getQueue'))
+ ->getMock();
+
+ $command = $this->getMockBuilder('Xi\Filelib\Command')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $queue = $this->getMock('Xi\Filelib\Queue\Queue');
+
+ $op->expects($this->once())->method('getCommandStrategy')
+ ->with($this->equalTo('tussi'))
+ ->will($this->returnValue($strategy));
+
+ $op->expects($this->any())->method('getQueue')
+ ->will($this->returnValue($queue));
+
+ $command->expects($this->any())->method('execute')
+ ->will($this->returnValue('originalValue'));
+
+ $queue->expects($this->any())->method('enqueue')
+ ->will($this->returnValue('originalValue'));
+
+
+ $ret = $op->executeOrQueue($command, 'tussi', $callbacks);
+
+ $this->assertEquals($expectedValue, $ret);
+
+ }
+
+
+
}
@@ -143,7 +143,7 @@ public function commandShouldUploadAndDelegateCorrectly($expectedCallToPublish,
->getMock();
$command->expects($this->once())->method('getResource')
- ->with($this->isInstanceOf('Xi\Filelib\File\Upload\FileUpload'))
+ ->with($this->isInstanceOf('Xi\Filelib\File\File'), $this->isInstanceOf('Xi\Filelib\File\Upload\FileUpload'))
->will($this->returnValue(Resource::create()));
$ret = $command->execute();
@@ -187,6 +187,8 @@ public function commandShouldSerializeAndUnserializeProperly()
*/
public function getResourceShouldGenerateNewResourceIfProfileAllowsButNoResourceIsFound()
{
+ $file = File::create(array());
+
$filelib = $this->getMock('Xi\Filelib\FileLibrary');
$op = $this->getMockBuilder('Xi\Filelib\File\DefaultFileOperator')
@@ -205,6 +207,7 @@ public function getResourceShouldGenerateNewResourceIfProfileAllowsButNoResource
$profile->expects($this->once())
->method('isSharedResourceAllowed')
+ ->with($this->isInstanceOf('Xi\Filelib\File\File'))
->will($this->returnValue(true));
$path = ROOT_TESTS . '/data/self-lussing-manatee.jpg';
@@ -219,8 +222,7 @@ public function getResourceShouldGenerateNewResourceIfProfileAllowsButNoResource
$folder = $this->getMock('Xi\Filelib\Folder\Folder');
$command = new UploadFileCommand($op, $upload, $folder, $profile);
-
- $ret = $command->getResource($upload);
+ $ret = $command->getResource($file, $upload);
$this->assertInstanceOf('Xi\Filelib\File\Resource', $ret);
$this->assertSame($hash, $ret->getHash());
@@ -232,6 +234,8 @@ public function getResourceShouldGenerateNewResourceIfProfileAllowsButNoResource
*/
public function getResourceShouldGenerateNewResourceIfProfileRequires()
{
+ $file = File::create(array());
+
$filelib = $this->getMock('Xi\Filelib\FileLibrary');
$op = $this->getMockBuilder('Xi\Filelib\File\DefaultFileOperator')
@@ -248,7 +252,7 @@ public function getResourceShouldGenerateNewResourceIfProfileRequires()
->with($this->equalTo('lussenhof'))
->will($this->returnValue($profile));
- $profile->expects($this->once())
+ $profile->expects($this->atLeastOnce())
->method('isSharedResourceAllowed')
->will($this->returnValue(false));
@@ -257,13 +261,18 @@ public function getResourceShouldGenerateNewResourceIfProfileRequires()
$upload = new FileUpload($path);
$hash = sha1_file($upload->getRealPath());
- $backend->expects($this->never())->method('findResourcesByHash');
+ $backend->expects($this->once())->method('findResourcesByHash')
+ ->with($this->equalTo($hash))
+ ->will($this->returnValue(array(
+ Resource::create(array('id' => 'first-id')),
+ Resource::create(array('id' => 'second-id')),
+ )));
$folder = $this->getMock('Xi\Filelib\Folder\Folder');
$command = new UploadFileCommand($op, $upload, $folder, $profile);
- $ret = $command->getResource($upload);
+ $ret = $command->getResource($file, $upload);
$this->assertInstanceOf('Xi\Filelib\File\Resource', $ret);
$this->assertSame($hash, $ret->getHash());
@@ -276,6 +285,7 @@ public function getResourceShouldGenerateNewResourceIfProfileRequires()
*/
public function getResourceShouldReuseResourceIfProfileAllowsAndResourcesAreFound()
{
+ $file = File::create(array());
$filelib = $this->getMock('Xi\Filelib\FileLibrary');
$op = $this->getMockBuilder('Xi\Filelib\File\DefaultFileOperator')
@@ -312,7 +322,7 @@ public function getResourceShouldReuseResourceIfProfileAllowsAndResourcesAreFoun
$command = new UploadFileCommand($op, $upload, $folder, $profile);
- $ret = $command->getResource($upload);
+ $ret = $command->getResource($file, $upload);
$this->assertInstanceOf('Xi\Filelib\File\Resource', $ret);
$this->assertSame('first-id', $ret->getId());
@@ -340,27 +340,64 @@ private function createProfileWithMockedVersions()
{
$profile = new FileProfile();
- $imageProvider = $this->getMockForAbstractClass('Xi\Filelib\Plugin\VersionProvider\VersionProvider');
+ $imageProvider = $this->getMock('Xi\Filelib\Plugin\VersionProvider\VersionProvider');
$imageProvider->expects($this->any())->method('getIdentifier')->will($this->returnValue('imagenizer'));
$imageProvider->expects($this->any())->method('getVersions')->will($this->returnValue(array('imagenizer')));
+ $imageProvider->expects($this->any())->method('isSharedResourceAllowed')->will($this->returnValue(true));
+ $imageProvider->expects($this->any())->method('providesFor')->will($this->returnCallback(function(File $file) { return $file->getMimetype() == 'image/png'; }));
- $videoProvider = $this->getMockForAbstractClass('Xi\Filelib\Plugin\VersionProvider\VersionProvider');
+ $videoProvider = $this->getMock('Xi\Filelib\Plugin\VersionProvider\VersionProvider');
$videoProvider->expects($this->any())->method('getIdentifier')->will($this->returnValue('videonizer'));
$videoProvider->expects($this->any())->method('getVersions')->will($this->returnValue(array('videonizer')));
+ $videoProvider->expects($this->any())->method('isSharedResourceAllowed')->will($this->returnValue(false));
+ $videoProvider->expects($this->any())->method('providesFor')->will($this->returnCallback(function(File $file) { return $file->getMimetype() == 'video/lus'; }));
- $globalProvider = $this->getMockForAbstractClass('Xi\Filelib\Plugin\VersionProvider\VersionProvider');
+ $globalProvider = $this->getMock('Xi\Filelib\Plugin\VersionProvider\VersionProvider');
$globalProvider->expects($this->any())->method('getIdentifier')->will($this->returnValue('globalizer'));
$globalProvider->expects($this->any())->method('getVersions')->will($this->returnValue(array('globalizer')));
+ $globalProvider->expects($this->any())->method('isSharedResourceAllowed')->will($this->returnValue(true));
+ $globalProvider->expects($this->any())->method('providesFor')->will($this->returnCallback(function(File $file) { return true; }));
$profile->addFileVersion('image', 'imagenizer', $imageProvider);
$profile->addFileVersion('video', 'videonizer', $videoProvider);
$profile->addFileVersion('image', 'globalizer', $globalProvider);
$profile->addFileVersion('video', 'globalizer', $globalProvider);
+ $profile->addPlugin($imageProvider);
+ $profile->addPlugin($videoProvider);
+ $profile->addPlugin($globalProvider);
+
return $profile;
}
+ /**
+ * @return array
+ */
+ public function provideDataForisSharedResourceAllowed()
+ {
+ return array(
+ array(true, 'image/png'),
+ array(false, 'video/lus'),
+ array(true, 'lussen/tussen'),
+ );
+ }
+
+ /**
+ * @test
+ * @dataProvider provideDataForisSharedResourceAllowed
+ */
+ public function isSharedResourceAllowedShouldReturnCorrectResult($expected, $mimetype)
+ {
+ $profile = $this->createProfileWithMockedVersions();
+ $profile->setFilelib($this->createMockedFilelib());
+
+ $file = File::create(array('resource' => Resource::create(array('mimetype' => $mimetype))));
+
+ $this->assertEquals($expected, $profile->isSharedResourceAllowed($file));
+ }
+
+
}
@@ -217,8 +217,6 @@ public function pluginShouldProvideForVideo()
/**
* @test
- * @plugin
- * @group watussi
*/
public function createVersionsShouldCreateVersions()
{
Oops, something went wrong.

0 comments on commit cf15191

Please sign in to comment.