Skip to content

Commit

Permalink
clean up and minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
petrparolek committed Jan 27, 2021
1 parent f19b1c6 commit 1e74633
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 139 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"mpdf/mpdf": "^8.0"
},
"require-dev": {
"latte/latte": "~2.7",
"latte/latte": "~2.10.0",
"nette/application": "~3.0.0",
"nette/di": "^3.0",
"nette/di": "^3.0.0",
"ninjify/nunjuck": "^0.3.0",
"ninjify/qa": "^0.12",
"phpstan/phpstan": "^0.12",
Expand Down
20 changes: 9 additions & 11 deletions tests/PdfResponse/PdfResponse.full.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test(
function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
test(function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);

ob_start();
$fileResponse->send(new Request(new UrlScript()), new Response());
$actualData = ob_get_clean();
ob_start();
$fileResponse->send(new Request(new UrlScript()), new Response());
$actualData = ob_get_clean();

Assert::match('#^%PDF-#i', $actualData);
}
);
Assert::match('#^%PDF-#i', $actualData);
});
96 changes: 44 additions & 52 deletions tests/PdfResponse/PdfResponse.page.format.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,55 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');

test(
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
$fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE;
$fileResponse->pageFormat = 'A4-L';
$fileResponse->pageMargins = $fileResponse->getPageMargins();
test(function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
$fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE;
$fileResponse->pageFormat = 'A4-L';
$fileResponse->pageMargins = $fileResponse->getPageMargins();

ob_start();
$fileResponse->send(new Request(new UrlScript()), new Response());
$actualData = ob_get_clean();
ob_start();
$fileResponse->send(new Request(new UrlScript()), new Response());
$actualData = ob_get_clean();

Assert::match('#^%PDF-#i', $actualData);
}
);
Assert::match('#^%PDF-#i', $actualData);
});

test(
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();
test(function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();

Assert::exception(
function () use ($fileResponse): void {
$fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE;
},
InvalidStateException::class,
'mPDF instance already created. Set page orientation before calling getMPDF'
);
}
);
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE;
},
InvalidStateException::class,
'mPDF instance already created. Set page orientation before calling getMPDF'
);
});

test(
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();
test(function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();

Assert::exception(
function () use ($fileResponse): void {
$fileResponse->pageFormat = 'A4-L';
},
InvalidStateException::class,
'mPDF instance already created. Set page format before calling getMPDF'
);
}
);
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->pageFormat = 'A4-L';
},
InvalidStateException::class,
'mPDF instance already created. Set page format before calling getMPDF'
);
});

test(
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();
test(function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();

Assert::exception(
function () use ($fileResponse): void {
$fileResponse->pageMargins = $fileResponse->getPageMargins();
},
InvalidStateException::class,
'mPDF instance already created. Set page margins before calling getMPDF'
);
}
);
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->pageMargins = $fileResponse->getPageMargins();
},
InvalidStateException::class,
'mPDF instance already created. Set page margins before calling getMPDF'
);
});
16 changes: 7 additions & 9 deletions tests/PdfResponse/PdfResponse.save.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test(
function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::DOWNLOAD);
$fileResponse->save(TEMP_DIR, 'under_scored.pdf');
test(function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::DOWNLOAD);
$fileResponse->save(TEMP_DIR, 'under_scored.pdf');

Assert::true(file_exists(TEMP_DIR . '/under_scored.pdf'));
}
);
Assert::true(file_exists(TEMP_DIR . '/under_scored.pdf'));
});
44 changes: 21 additions & 23 deletions tests/PdfResponse/PdfResponse.setters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test(
function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
test(function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);

// zoom
$fileResponse->displayZoom = PdfResponse::ZOOM_REAL;
$fileResponse->displayZoom = 90;
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->displayZoom = 'invalid';
},
InvalidArgumentException::class
);
// zoom
$fileResponse->displayZoom = PdfResponse::ZOOM_REAL;
$fileResponse->displayZoom = 90;
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->displayZoom = 'invalid';
},
InvalidArgumentException::class
);

// layout
$fileResponse->displayLayout = PdfResponse::LAYOUT_TWO;
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->displayLayout = 'invalid';
},
InvalidArgumentException::class
);
}
);
// layout
$fileResponse->displayLayout = PdfResponse::LAYOUT_TWO;
Assert::exception(
function () use ($fileResponse): void {
$fileResponse->displayLayout = 'invalid';
},
InvalidArgumentException::class
);
});
22 changes: 10 additions & 12 deletions tests/PdfResponse/PdfResponse.symfony.crawler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test(
function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example2.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
$fileResponse->ignoreStylesInHTMLDocument = true;
test(function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example2.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
$fileResponse->ignoreStylesInHTMLDocument = true;

ob_start();
$fileResponse->send(new Request(new UrlScript()), new Response());
$actualData = ob_get_clean();
ob_start();
$fileResponse->send(new Request(new UrlScript()), new Response());
$actualData = ob_get_clean();

Assert::match('#^%PDF-#i', $actualData);
}
);
Assert::match('#^%PDF-#i', $actualData);
});
38 changes: 18 additions & 20 deletions tests/PdfResponse/PdfResponseFactory.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test(
function (): void {
$author = 'Jan Novák';
$mpdfTmpDir = 'tmp/mpdf';
test(function (): void {
$author = 'Jan Novák';
$mpdfTmpDir = 'tmp/mpdf';

$config = sprintf('
parameters:
mpdfConfig:
tempDir: %s
author: %s', $mpdfTmpDir, $author);
$config = sprintf('
parameters:
mpdfConfig:
tempDir: %s
author: %s', $mpdfTmpDir, $author);

$config .= '
services:
- Contributte\PdfResponse\PdfResponseFactory(%mpdfConfig%)
';
$config .= '
services:
- Contributte\PdfResponse\PdfResponseFactory(%mpdfConfig%)
';

$container = createContainer(new Compiler(), $config);
$container = createContainer(new Compiler(), $config);

/** @var PdfResponseFactory $factoryInstance */
$factoryInstance = $container->getByType(PdfResponseFactory::class);
Assert::same($author, $factoryInstance->mpdfConfig['author']);
Assert::same($mpdfTmpDir, $factoryInstance->mpdfConfig['tempDir']);
}
);
/** @var PdfResponseFactory $factoryInstance */
$factoryInstance = $container->getByType(PdfResponseFactory::class);
Assert::same($author, $factoryInstance->mpdfConfig['author']);
Assert::same($mpdfTmpDir, $factoryInstance->mpdfConfig['tempDir']);
});
4 changes: 0 additions & 4 deletions tests/php-unix.ini

This file was deleted.

6 changes: 0 additions & 6 deletions tests/php-win.ini

This file was deleted.

0 comments on commit 1e74633

Please sign in to comment.