Skip to content

Commit

Permalink
Merge pull request #12 from pboivin/update-tests
Browse files Browse the repository at this point in the history
Housekeeping
  • Loading branch information
pboivin authored Sep 16, 2022
2 parents d1cb7b9 + 8414a3d commit d7efd71
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 344 deletions.
16 changes: 13 additions & 3 deletions src/ImgRenderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,13 @@ protected function renderImg(array $attributes = []): string
protected function handlePaddingTop(string $input): string
{
if ($this->paddingTop && $this->aspectRatio) {
$padding = (1 / $this->aspectRatio) * 100;

return $this->htmlWrap(
'div',
[
'class' => $this->paddingClass,
'style' => $this->collectStyles([
'position' => 'relative',
'padding-top' => "{$padding}%",
'padding-top' => $this->getPaddingTopValue((float) $this->aspectRatio),
]),
],
$input
Expand Down Expand Up @@ -219,4 +217,16 @@ protected function handleWrapper(string $input): string

return $input;
}

protected function getPaddingTopValue(float $aspectRatio): string
{
$padding = (1 / $aspectRatio) * 100;

$padding = number_format($padding, 2);

// remove decimals if number is not really a float
$padding = preg_replace('/\.0+$/', '', "$padding");

return "{$padding}%";
}
}
68 changes: 32 additions & 36 deletions tests/Concerns/Mocking.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

trait Mocking
{
public function getFactory($options = [])
public function prepareFactory($options = []): object
{
$factory = new ImageFactory(
$options ?: [
Expand All @@ -23,63 +23,59 @@ public function getFactory($options = [])
]
);

$factory->setGlideServer($this->mockServer());
$factory->setGlideServer($server = $this->mockServer());

$factory->setInspector($this->mockInspector());
$factory->setInspector($inspector = $this->mockInspector());

return $factory;
return (object) [
'factory' => $factory,
'serverMock' => $server,
'inspectorMock' => $inspector,
];
}

public function getImage()
public function prepareImage(): object
{
return new Image($this->mockImageFile(), $this->mockImageFile());
}
$source = $this->mockImageFile();

public function mockServer()
{
/** @var Server */
$server = Mockery::mock(Server::class);
$cached = $this->mockImageFile();

$image = new Image($source, $cached);

return $server;
return (object) [
'image' => $image,
'sourceMock' => $source,
'cachedMock' => $cached,
];
}

public function mockInspector()
public function mockServer(): mixed
{
/** @var ImageFileInspector */
$inspector = Mockery::mock(ImageFileInspector::class);

return $inspector;
return Mockery::mock(Server::class);
}

public function mockImageFile()
public function mockInspector(): mixed
{
/** @var ImageFile */
$file = Mockery::mock(ImageFile::class);

return $file;
return Mockery::mock(ImageFileInspector::class);
}

public function mockFactory()
public function mockImageFile(): mixed
{
/** @var ImageFactory */
$factory = Mockery::mock(ImageFactory::class);

return $factory;
return Mockery::mock(ImageFile::class);
}

public function mockImage()
public function mockFactory(): mixed
{
/** @var Image */
$image = Mockery::mock(Image::class);

return $image;
return Mockery::mock(ImageFactory::class);
}

public function mockImageSet()
public function mockImage(): mixed
{
/** @var ImageSet */
$set = Mockery::mock(ImageSet::class);
return Mockery::mock(Image::class);
}

return $set;
public function mockImageSet(): mixed
{
return Mockery::mock(ImageSet::class);
}
}
30 changes: 12 additions & 18 deletions tests/ImageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ public function test_static_constructor()

public function test_generates_image_with_default_params()
{
$flou = $this->getFactory();
$prepared = $this->prepareFactory();

$flou
->glideServer()
$prepared->serverMock
->shouldReceive('makeImage')
->with('source.jpg', ImageFactory::DEFAULT_GLIDE_PARAMS)
->andReturn('cached.jpg');

$image = $flou->image('source.jpg');
$image = $prepared->factory->image('source.jpg');

$this->assertEquals('/images/cache/cached.jpg', $image->cached()->url());
$this->assertEquals('/images/source/source.jpg', $image->source()->url());
Expand All @@ -109,30 +108,26 @@ public function test_generates_image_with_default_params()

public function test_generates_image_with_inline_glide_params()
{
$flou = $this->getFactory();
$prepared = $this->prepareFactory();

$flou
->glideServer()
$prepared->serverMock
->shouldReceive('makeImage')
->with('source.jpg', ['h' => 123])
->andReturn('cached.jpg');

$image = $flou->image('source.jpg', ['h' => 123]);
$image = $prepared->factory->image('source.jpg', ['h' => 123]);

$this->assertEquals('/images/cache/cached.jpg', $image->cached()->url());
$this->assertEquals('/images/source/source.jpg', $image->source()->url());
}

public function test_generates_imageset()
{
$flou = $this->getFactory();
$prepared = $this->prepareFactory();

$flou
->glideServer()
->shouldReceive('makeImage')
->andReturn('cached.jpg');
$prepared->serverMock->shouldReceive('makeImage')->andReturn('cached.jpg');

$set = $flou->imageSet([
$set = $prepared->factory->imageSet([
'image' => 'source.jpg',
'sources' => [
'sm' => ['width' => 400],
Expand All @@ -148,10 +143,9 @@ public function test_generates_imageset()

public function test_generates_imageset_with_custom_glide_params()
{
$flou = $this->getFactory();
$prepared = $this->prepareFactory();

$flou
->glideServer()
$prepared->serverMock
->shouldReceive('makeImage')
->with('source.jpg', ['filt' => 'greyscale', 'w' => 400])
->andReturn('cached.jpg')
Expand All @@ -162,7 +156,7 @@ public function test_generates_imageset_with_custom_glide_params()
->with('source.jpg', ['h' => 10, 'fm' => 'gif'])
->andReturn('cached.jpg');

$set = $flou->imageSet(
$set = $prepared->factory->imageSet(
[
'image' => 'source.jpg',
'widths' => [400, 800],
Expand Down
Loading

0 comments on commit d7efd71

Please sign in to comment.