1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
<?php
namespace Dompdf\Tests\FrameReflower;
use Dompdf\Css\Style;
use Dompdf\Css\Stylesheet;
use Dompdf\Dompdf;
use Dompdf\FrameDecorator\Image as ImageFrameDecorator;
use Dompdf\Tests\TestCase;
use Mockery;
class ImageTest extends TestCase
{
public function testGetMinMaxContainerWidthAuto(): void
{
$frame = $this->getImageMock(['width' => 'auto', 'height' => 'auto']);
$image = new ImageTestReflower($frame);
$result = $image->get_min_max_width();
$image->resolve_dimensions();
$style = $frame->get_style();
$expectedWidth = 1966.08;
$expectedHeight = 1474.56;
$this->assertEquals($expectedWidth, $style->width);
$this->assertEquals($expectedHeight, $style->height);
$this->assertEquals([$expectedWidth, $expectedWidth, 'min' => $expectedWidth, 'max' => $expectedWidth], $result);
}
public function testGetMinMaxContainerWidthBasic(): void
{
$frame = $this->getImageMock(['width' => '100px', 'height' => '200px']);
$image = new ImageTestReflower($frame);
$result = $image->get_min_max_width();
$image->resolve_dimensions();
$style = $frame->get_style();
$expectedWidth = 75;
$expectedHeight = 150;
$this->assertEquals($expectedWidth, $style->width);
$this->assertEquals($expectedHeight, $style->height);
$this->assertEquals([$expectedWidth, $expectedWidth, 'min' => $expectedWidth, 'max' => $expectedWidth], $result);
}
public function testGetMinMaxWidthPercentageChain(): void
{
$rootFrame = $this->getImageMock(['width' => '400px', 'height' => '800px'], null, [0, 0, 300, 600]);
$parentFrame = $this->getImageMock(['width' => '50%', 'height' => '75%'], $rootFrame, [0, 0, 300, 600]);
$imageFrame = $this->getImageMock(['width' => '50%', 'height' => '75%'], $parentFrame, [0, 0, 150, 450]);
$image = new ImageTestReflower($imageFrame);
$result = $image->get_min_max_width();
$image->resolve_dimensions();
$style = $imageFrame->get_style();
// 400px * 0.75 (dpi) * 0.50 (imageFrame) * 0.50 (rootFrame)
$expectedWidth = 75;
// 800px * 0.75 (dpi) * 0.75 (imageFrame) * 0.75 (rootFrame)
$expectedHeight = 337.5;
$this->assertEquals($expectedWidth, $style->width);
$this->assertEquals($expectedHeight, $style->height);
$this->assertEquals([0.0, 1966.08, 'min' => 0.0, 'max' => 1966.08], $result);
}
public function testGetMinMaxWidthZeroWidthZeroHeight(): void
{
$frame = $this->getImageMock(['width' => '0', 'height' => '0']);
$image = new ImageTestReflower($frame);
$result = $image->get_min_max_width();
$image->resolve_dimensions();
$style = $frame->get_style();
$expectedWidth = 0;
$expectedHeight = 0;
$this->assertEquals($expectedWidth, $style->width);
$this->assertEquals($expectedHeight, $style->height);
$this->assertEquals([$expectedWidth, $expectedWidth, 'min' => $expectedWidth, 'max' => $expectedWidth], $result);
}
public function testGetMinMaxWidthMinMaxCaps(): void
{
$frame = $this->getImageMock(
[
'width' => '100px',
'height' => '1200px',
'min_width' => '400px',
'max_width' => '800px',
'min_height' => '300px',
'max_height' => '500px',
]
);
$image = new ImageTestReflower($frame);
$result = $image->get_min_max_width();
$image->resolve_dimensions();
$style = $frame->get_style();
$expectedWidth = 300;
$expectedHeight = 375;
$this->assertEquals($expectedWidth, $style->width);
$this->assertEquals($expectedHeight, $style->height);
$this->assertEquals([$expectedWidth, $expectedWidth, 'min' => $expectedWidth, 'max' => $expectedWidth], $result);
}
public function tearDown(): void
{
Mockery::close();
}
private function getImageMock(
array $styleProperties,
ImageFrameDecorator $parentFrame = null,
array $containingBlock = [0, 0, 400, 400]
): ImageFrameDecorator {
$style = new Style(new Stylesheet(new Dompdf()));
foreach ($styleProperties as $prop => $val) {
$style->set_prop($prop, $val);
}
$frame = Mockery::mock(
ImageFrameDecorator::class,
[
'get_dompdf->getOptions->getDebugPng' => false,
'get_style' => $style,
'get_parent' => $parentFrame,
'get_dompdf->getOptions->getDpi' => 75,
'get_image_url' => dirname(__DIR__) . '/_files/jamaica.jpg',
'get_dompdf->getHttpContext' => null
]
);
$imgWidth = 2048;
$imgHeight = 1536;
$frame->shouldReceive('resample')->with($imgWidth)->andReturn(($imgWidth * 72) / 75);
$frame->shouldReceive('resample')->with($imgHeight)->andReturn(($imgHeight * 72) / 75);
$frame->shouldReceive('get_intrinsic_dimensions')->andReturn([$imgWidth, $imgHeight]);
$frame->shouldReceive('get_containing_block')->andReturn($containingBlock);
return $frame;
}
}
|