Skip to content

Commit

Permalink
test: Check auto-sizing works
Browse files Browse the repository at this point in the history
  • Loading branch information
famoser committed Jan 1, 2024
1 parent cca1b16 commit 6983930
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/Integration/Frontend/LinearDocument/GridTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

namespace PdfGenerator\Tests\Integration\Frontend\LinearDocument;

use PdfGenerator\Frontend\Content\Paragraph;
use PdfGenerator\Frontend\Content\Rectangle;
use PdfGenerator\Frontend\Content\Style\DrawingStyle;
use PdfGenerator\Frontend\Content\Style\TextStyle;
use PdfGenerator\Frontend\Layout\AbstractBlock;
use PdfGenerator\Frontend\Layout\ContentBlock;
use PdfGenerator\Frontend\Layout\Grid;
use PdfGenerator\Frontend\Layout\Parts\Row;
use PdfGenerator\Frontend\Layout\Style\BlockStyle;
use PdfGenerator\Frontend\Layout\Style\ColumnSize;
use PdfGenerator\Frontend\LinearDocument;
use PdfGenerator\Frontend\Resource\Font;
use PdfGenerator\IR\Document\Content\Common\Color;

class GridTestCase extends LinearDocumentTestCase
Expand Down Expand Up @@ -119,6 +122,47 @@ public function testPrintAutoGrid()
$this->assertStringContainsString('1 0 0 1 100 0 cm 0 0 40 20 re b', $result);
}

public function testAutoSizingGrid()
{
// arrange
$document = new LinearDocument([210, 297], [5, 5, 5, 5]);

$grid = new Grid(10, 0, [ColumnSize::AUTO, ColumnSize::AUTO]);
$this->setBorderStyle($grid);
$this->printWidthRectangles($grid, [[10, 20], [20, 10]]);
$document->add($grid);

$grid = new Grid(10, 0, [ColumnSize::AUTO, ColumnSize::AUTO]);
$this->setBorderStyle($grid);
$this->printWidthRectangles($grid, [[10, 20], [10, 20]]);
$document->add($grid);

// assert
$result = $this->render($document);
$this->assertStringContainsString('1 0 0 1 -105 -40 cm 0 0 0 RG 0 0 0 rg 0 0 200 40 re', $result);
$this->assertStringContainsString('1 0 0 1 0 20 cm 0 1 1 RG 0 1 0 rg 0 0 10 20 re', $result);
}

public function testAutoSizingTextGrid()
{
// arrange
$document = new LinearDocument([210, 297], [5, 5, 5, 5]);

$grid = new Grid(1, 2, [ColumnSize::MINIMAL, ColumnSize::AUTO, ColumnSize::AUTO]);
$this->setBorderStyle($grid);
$text = [
['23', 'Bitte die grössten nicht.', 'hi'],
['231', 'Dies ist immer noch am längesten.', 'Aber hier viel mehr Gewicht; daher sollte dieser Teil insgesamt doch mehr Platz eingeräumt werden'],
];
$this->printText($grid, $text);
$document->add($grid);

// assert
$result = $this->render($document);
$this->assertStringContainsString('1 0 0 1 6.004 0 cm BT', $result);
$this->assertStringContainsString('1 0 0 1 71.361331 0 cm BT', $result);
}

public function testPrintUnitGrid()
{
// arrange
Expand Down Expand Up @@ -195,4 +239,24 @@ private function printWidthRectangles(Grid $grid, array $dimensions): void
$grid->add($row);
}
}

/**
* @param string[][] $text
*/
private function printText(Grid $grid, array $text): void
{
$font = Font::createFromDefault();
$normalText = new TextStyle($font, 3);

foreach ($text as $line) {
$row = new Row();
foreach ($line as $index => $cell) {
$paragraph = new Paragraph();
$paragraph->add($normalText, $cell);
$row->setContent($index, $paragraph);
}

$grid->add($row);
}
}
}

0 comments on commit 6983930

Please sign in to comment.