Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pboivin committed Mar 2, 2023
1 parent 8496541 commit 616210b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/ImageRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,40 @@ public function test_can_override_noscript_attributes()
);
}

public function test_can_initialize_from_array()
{
$imageArray = [
"source" => [
"fileName" => "01.jpg",
"path" => "/path/to/images/source/01.jpg",
"url" => "/images/source/01.jpg",
"width" => 2932,
"height" => 2000,
"ratio" => 1.466,
],
"cached" => [
"fileName" => "01.jpg/test.gif",
"path" => "/path/to/images/cache/01.jpg/test.gif",
"url" => "/images/cache/01.jpg/test.gif",
"width" => 15,
"height" => 10,
"ratio" => 1.5,
],
];

$imageRender = ImageRender::fromArray($imageArray);

$output = $imageRender->img([
'class' => 'test',
'alt' => 'This is a test',
]);

$this->assertEquals(
'<img class="lazyload test" alt="This is a test" src="/images/cache/01.jpg/test.gif" data-src="/images/source/01.jpg" width="2932" height="2000">',
$output
);
}

protected function prepareImageRender($options = []): object
{
$prepared = $this->prepareImage();
Expand Down
66 changes: 66 additions & 0 deletions tests/ImageSetRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,64 @@ public function test_can_override_noscript_attributes()
);
}

public function test_can_initialize_from_array()
{
$imageSetArray = array(
'sources' => array(
0 => array(
'image' => '01.jpg',
'widths' => array(0 => 500, 1 => 900, 2 => 1300, 3 => 1700),
'srcset' => array(
0 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/one.jpg', 'path' => '/path/to/images/cache/01.jpg/one.jpg', 'url' => '/images/cache/01.jpg/one.jpg', 'width' => 500, 'height' => 341, 'ratio' => 1.466275659824047,),
),
'width' => 500,
),
1 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/two.jpg', 'path' => '/path/to/images/cache/01.jpg/two.jpg', 'url' => '/images/cache/01.jpg/two.jpg', 'width' => 900, 'height' => 614, 'ratio' => 1.4657980456026058,),
),
'width' => 900,
),
2 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/three.jpg', 'path' => '/path/to/images/cache/01.jpg/three.jpg', 'url' => '/images/cache/01.jpg/three.jpg', 'width' => 1300, 'height' => 887, 'ratio' => 1.4656144306651635,),
),
'width' => 1300,
),
3 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/four.jpg', 'path' => '/path/to/images/cache/01.jpg/four.jpg', 'url' => '/images/cache/01.jpg/four.jpg', 'width' => 1700, 'height' => 1160, 'ratio' => 1.4655172413793103,),
),
'width' => 1700,
),
),
),
),
'lqip' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/lqip.gif', 'path' => '/path/to/images/cache/01.jpg/lqip.gif', 'url' => '/images/cache/01.jpg/lqip.gif', 'width' => 15, 'height' => 10, 'ratio' => 1.5,),
),
);

$imageRender = ImageSetRender::fromArray($imageSetArray);

$output = $imageRender->img([
'class' => 'test',
'alt' => 'This is a test',
]);

$this->assertEquals(
'<img class="lazyload test" alt="This is a test" src="/images/cache/01.jpg/lqip.gif" width="1700" height="1160" data-src="/images/cache/01.jpg/four.jpg" data-srcset="/images/cache/01.jpg/one.jpg 500w, /images/cache/01.jpg/two.jpg 900w, /images/cache/01.jpg/three.jpg 1300w, /images/cache/01.jpg/four.jpg 1700w" data-sizes="100vw">',
$output
);
}

protected function prepareAllImages(): array
{
$image1 = $this->prepareImage();
Expand Down Expand Up @@ -500,3 +558,11 @@ protected function prepareForPictureFormats($options = []): object
];
}
}



/*
*/

0 comments on commit 616210b

Please sign in to comment.