Skip to content

Commit

Permalink
Updated tests for refactored variant processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Coen Zimmerman committed Aug 9, 2017
1 parent 0734179 commit b84f0e5
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 48 deletions.
30 changes: 15 additions & 15 deletions tests/Helpers/Strategies/SpyVariantStrategy.php
@@ -1,8 +1,9 @@
<?php
namespace Czim\FileHandling\Test\Helpers\Strategies;

use Czim\FileHandling\Contracts\Storage\ProcessableFileInterface;
use Czim\FileHandling\Contracts\Variant\VariantStrategyInterface;
use SplFileInfo;
use Czim\FileHandling\Exceptions\VariantStrategyShouldNotBeAppliedException;

class SpyVariantStrategy implements VariantStrategyInterface
{
Expand All @@ -27,28 +28,27 @@ class SpyVariantStrategy implements VariantStrategyInterface
*/
public $optionsSet = false;

/**
* Returns whether this strategy can be applied to a file with a given mimeType.
*
* @param string $mimeType
* @return bool
*/
public function shouldApplyForMimeType($mimeType)
{
return $this->shouldApply;
}

/**
* Applies strategy to a file.
*
* @param SplFileInfo $file
* @return bool
* @param ProcessableFileInterface $file
* @return ProcessableFileInterface|false
* @throws VariantStrategyShouldNotBeAppliedException
*/
public function apply(SplFileInfo $file)
public function apply(ProcessableFileInterface $file)
{
if ( ! $this->shouldApply) {
throw new VariantStrategyShouldNotBeAppliedException;
}

$this->applied = true;

return $this->applySuccessfully;
if ( ! $this->applySuccessfully) {
return false;
}

return $file;
}

/**
Expand Down
90 changes: 90 additions & 0 deletions tests/Unit/Storage/File/ProcessableFileTest.php
@@ -0,0 +1,90 @@
<?php
namespace Czim\FileHandling\Test\Unit\Storage\File;

use Czim\FileHandling\Storage\File\ProcessableFile;
use Czim\FileHandling\Storage\File\SplFileInfoStorableFile;
use Czim\FileHandling\Test\TestCase;
use SplFileInfo;

class ProcessableFileTest extends TestCase
{
const XML_TEST_FILE = 'tests/resources/test.xml';

/**
* @test
*/
function it_can_set_and_retrieve_content_data()
{
$file = new ProcessableFile;

$fileInfo = new SplFileInfo($this->getExampleLocalPath());

static::assertSame($file, $file->setData($fileInfo));

static::assertEquals(file_get_contents($fileInfo->getRealPath()), $file->content());
}

/**
* @test
*/
function it_can_set_data_as_a_path_string()
{
$file = new ProcessableFile;

$path = $this->getExampleLocalPath();

static::assertSame($file, $file->setData($path));

static::assertEquals(file_get_contents($path), $file->content());
}

/**
* @test
* @expectedException \RuntimeException
*/
function it_throws_an_exception_if_the_referenced_path_is_not_found()
{
$file = new ProcessableFile;

$fileInfo = new SplFileInfo('/no/file/exists/here');

$file->setData($fileInfo);
}

/**
* @test
*/
function it_returns_content_size_when_set()
{
$file = new ProcessableFile;

$fileInfo = new SplFileInfo($this->getExampleLocalPath());

static::assertSame($file, $file->setData($fileInfo));

static::assertEquals($fileInfo->getSize(), $file->size());
}

/**
* @test
*/
function it_returns_the_path()
{
$file = new ProcessableFile;

$fileInfo = new SplFileInfo($this->getExampleLocalPath());

static::assertSame($file, $file->setData($fileInfo));

static::assertEquals($fileInfo->getRealPath(), $file->path());
}

/**
* @return string
*/
protected function getExampleLocalPath()
{
return realpath(dirname(__DIR__) . '/../../../' . static::XML_TEST_FILE);
}

}
33 changes: 20 additions & 13 deletions tests/Unit/Variant/Strategies/ImageAutoOrientStrategyTest.php
@@ -1,28 +1,31 @@
<?php
namespace Czim\FileHandling\Test\Unit\Variant\Strategies;

use Czim\FileHandling\Contracts\Storage\ProcessableFileInterface;
use Czim\FileHandling\Support\Image\OrientationFixer;
use Czim\FileHandling\Test\TestCase;
use Czim\FileHandling\Variant\Strategies\ImageAutoOrientStrategy;
use Mockery;
use SplFileInfo;

class ImageAutoOrientStrategyTest extends TestCase
{

/**
* @test
* @test]
* @expectedException \Czim\FileHandling\Exceptions\VariantStrategyShouldNotBeAppliedException
*/
function it_should_apply_only_to_images()
function it_should_throw_an_exception_if_it_is_applied_to_a_non_image()
{
/** @var Mockery\MockInterface|OrientationFixer $fixer */
$fixer = Mockery::mock(OrientationFixer::class);

$strategy = new ImageAutoOrientStrategy($fixer);

static::assertTrue($strategy->shouldApplyForMimeType('image/jpeg'));
static::assertFalse($strategy->shouldApplyForMimeType('video/mpeg'));
static::assertFalse($strategy->shouldApplyForMimeType('text/plain'));
/** @var Mockery\MockInterface|ProcessableFileInterface $file */
$file = Mockery::mock(ProcessableFileInterface::class);
$file->shouldReceive('mimeType')->andReturn('video/mpeg');

$strategy->apply($file);
}

/**
Expand All @@ -33,14 +36,16 @@ function it_auto_orients_an_image()
/** @var Mockery\MockInterface|OrientationFixer $fixer */
$fixer = Mockery::mock(OrientationFixer::class);

/** @var Mockery\MockInterface|SplFileInfo $file */
$file = Mockery::mock(SplFileInfo::class);
/** @var Mockery\MockInterface|ProcessableFileInterface $file */
$file = Mockery::mock(ProcessableFileInterface::class);
$file->shouldReceive('mimeType')->andReturn('image/jpeg');
$file->shouldReceive('path')->andReturn('tmp/test.jpg');

$fixer->shouldReceive('fixFile')->once()->andReturn(true);

$strategy = new ImageAutoOrientStrategy($fixer);

static::assertTrue($strategy->apply($file));
static::assertSame($file, $strategy->apply($file));
}

/**
Expand All @@ -51,16 +56,18 @@ function it_can_disable_quiet_mode_on_the_fixer()
/** @var Mockery\MockInterface|OrientationFixer $fixer */
$fixer = Mockery::mock(OrientationFixer::class);

/** @var Mockery\MockInterface|SplFileInfo $file */
$file = Mockery::mock(SplFileInfo::class);
/** @var Mockery\MockInterface|ProcessableFileInterface $file */
$file = Mockery::mock(ProcessableFileInterface::class);
$file->shouldReceive('mimeType')->andReturn('image/jpeg');
$file->shouldReceive('path')->andReturn('tmp/test.jpg');

$fixer->shouldReceive('fixFile')->once()->with($file)->andReturn(true);
$fixer->shouldReceive('fixFile')->once()->andReturn(true);
$fixer->shouldReceive('disableQuietMode')->once()->andReturnSelf();

$strategy = new ImageAutoOrientStrategy($fixer);
$strategy->setOptions(['quiet' => false]);

static::assertTrue($strategy->apply($file));
static::assertSame($file, $strategy->apply($file));
}

}
34 changes: 21 additions & 13 deletions tests/Unit/Variant/Strategies/ImageResizeStrategyTest.php
@@ -1,30 +1,33 @@
<?php
namespace Czim\FileHandling\Test\Unit\Variant\Strategies;

use Czim\FileHandling\Contracts\Storage\ProcessableFileInterface;
use Czim\FileHandling\Support\Image\OrientationFixer;
use Czim\FileHandling\Support\Image\Resizer;
use Czim\FileHandling\Test\TestCase;
use Czim\FileHandling\Variant\Strategies\ImageAutoOrientStrategy;
use Czim\FileHandling\Variant\Strategies\ImageResizeStrategy;
use Mockery;
use SplFileInfo;

class ImageResizeStrategyTest extends TestCase
{

/**
* @test
* @test]
* @expectedException \Czim\FileHandling\Exceptions\VariantStrategyShouldNotBeAppliedException
*/
function it_should_apply_only_to_images()
function it_should_throw_an_exception_if_it_is_applied_to_a_non_image()
{
/** @var Mockery\MockInterface|Resizer $resizer */
$resizer = Mockery::mock(Resizer::class);
/** @var Mockery\MockInterface|OrientationFixer $fixer */
$fixer = Mockery::mock(OrientationFixer::class);

$strategy = new ImageResizeStrategy($resizer);
$strategy = new ImageAutoOrientStrategy($fixer);

/** @var Mockery\MockInterface|ProcessableFileInterface $file */
$file = Mockery::mock(ProcessableFileInterface::class);
$file->shouldReceive('mimeType')->andReturn('text/plain');

static::assertTrue($strategy->shouldApplyForMimeType('image/jpeg'));
static::assertFalse($strategy->shouldApplyForMimeType('video/mpeg'));
static::assertFalse($strategy->shouldApplyForMimeType('text/plain'));
$strategy->apply($file);
}

/**
Expand All @@ -35,17 +38,22 @@ function it_rotates_an_image()
/** @var Mockery\MockInterface|Resizer $resizer */
$resizer = Mockery::mock(Resizer::class);

/** @var Mockery\MockInterface|SplFileInfo $file */
$file = Mockery::mock(SplFileInfo::class);
/** @var Mockery\MockInterface|ProcessableFileInterface $file */
$file = Mockery::mock(ProcessableFileInterface::class);
$file->shouldReceive('mimeType')->andReturn('image/jpeg');
$file->shouldReceive('path')->andReturn('tmp/test.jpg');

$options = ['test' => true];

$resizer->shouldReceive('resize')->once()->with($file, $options)->andReturn(true);
$resizer->shouldReceive('resize')
->once()
->with(Mockery::type(\SplFileInfo::class), $options)
->andReturn(true);

$strategy = new ImageResizeStrategy($resizer);
$strategy->setOptions($options);

static::assertTrue($strategy->apply($file));
static::assertSame($file, $strategy->apply($file));
}

}
31 changes: 24 additions & 7 deletions tests/Unit/Variant/VariantProcessorTest.php
@@ -1,16 +1,17 @@
<?php
namespace Czim\FileHandling\Test\Unit\Support\Content;

use Czim\FileHandling\Contracts\Storage\ProcessableFileInterface;
use Czim\FileHandling\Contracts\Storage\StorableFileFactoryInterface;
use Czim\FileHandling\Contracts\Storage\StorableFileInterface;
use Czim\FileHandling\Contracts\Variant\VariantStrategyFactoryInterface;
use Czim\FileHandling\Contracts\Variant\VariantStrategyInterface;
use Czim\FileHandling\Exceptions\VariantStrategyShouldNotBeAppliedException;
use Czim\FileHandling\Test\TestCase;
use Czim\FileHandling\Variant\VariantProcessor;
use Mockery;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use SplFileInfo;

class VariantProcessorTest extends TestCase
{
Expand Down Expand Up @@ -69,7 +70,8 @@ function it_processes_a_variant()

$variant = $processor->process($source, 'variant', [ 'test-strategy' => ['test' => 'a'] ]);

static::assertSame($target, $variant);
static::assertInstanceOf(ProcessableFileInterface::class, $variant);
static::assertEquals('text/plain', $variant->mimeType());
}

/**
Expand Down Expand Up @@ -155,7 +157,8 @@ function it_silently_ignores_a_strategy_if_it_should_not_be_applied_for_a_mimety

$variant = $processor->process($source, 'variant', [ 'test-strategy' => ['test' => 'a'] ]);

static::assertSame($target, $variant);
static::assertInstanceOf(ProcessableFileInterface::class, $variant);
static::assertEquals('text/plain', $variant->mimeType());
}


Expand Down Expand Up @@ -200,12 +203,26 @@ protected function makeMockSourceStorableFile($tmpPath, $mimeType = 'text/plain'
* @param array $options
* @return VariantStrategyInterface|Mockery\MockInterface
*/
protected function makeMockVariantStrategy($successful = true, $shouldApply = true, $options = ['test' => 'a'])
{
protected function makeMockVariantStrategy(
$successful = true,
$shouldApply = true,
$options = ['test' => 'a']
) {
$mock = Mockery::mock(VariantStrategyInterface::class);
$mock->shouldReceive('setOptions')->once()->with($options)->andReturnSelf();
$mock->shouldReceive('shouldApplyForMimeType')->andReturn($shouldApply);
$mock->shouldReceive('apply')->once()->with(Mockery::type(SplFileInfo::class))->andReturn($successful);

if ($shouldApply) {
$mock->shouldReceive('apply')->once()
->with(Mockery::type(ProcessableFileInterface::class))
->andReturnUsing(function ($file) use ($successful) {
return $successful ? $file : false;
});
} else {
$mock->shouldReceive('apply')->once()
->with(Mockery::type(ProcessableFileInterface::class))
->andThrow(new VariantStrategyShouldNotBeAppliedException);
}


return $mock;
}
Expand Down

0 comments on commit b84f0e5

Please sign in to comment.