diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99b7119e..eb7945cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,12 +17,9 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['8.1', '8.2', '8.3'] + php-version: ['8.2', '8.3', '8.4'] dependencies: ['highest'] include: - - php-version: '8.4' - dependencies: 'highest' - composer-options: '--ignore-platform-req=php' - php-version: '8.1' dependencies: 'lowest' @@ -37,12 +34,10 @@ jobs: coverage: pcov - name: Install packages - run: | - sudo apt install xfonts-base xfonts-75dpi - wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb - sudo dpkg -i wkhtmltox_0.12.6.1-2.jammy_amd64.deb - sudo apt install -f - wkhtmltopdf --version + uses: awalsh128/cache-apt-pkgs-action@v1 + with: + packages: xfonts-base xfonts-75dpi wkhtmltopdf + version: ubuntu-24.04 - name: Composer install uses: ramsey/composer-install@v3 @@ -56,14 +51,14 @@ jobs: - name: Run PHPUnit run: | if [[ ${{ matrix.php-version }} == '8.3' ]]; then - vendor/bin/phpunit --display-warnings --display-deprecations --display-incomplete --display-skipped --coverage-clover=coverage.xml + vendor/bin/phpunit --display-warnings --display-deprecations --display-phpunit-deprecations --display-incomplete --display-skipped --coverage-clover=coverage.xml else vendor/bin/phpunit --display-warnings --display-deprecations fi - name: Code Coverage Report if: matrix.php-version == '8.3' - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v5 env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/README.md b/README.md index 5f5d4281..ccd08c7a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF. Engines included in the plugin: -* DomPdf (^2.0) +* DomPdf (^3.0) * Mpdf (^8.0.4) * Tcpdf (^6.3) * WkHtmlToPdf **RECOMMENDED ENGINE** diff --git a/composer.json b/composer.json index 53d70c8c..0d3b83ec 100644 --- a/composer.json +++ b/composer.json @@ -9,8 +9,8 @@ "cakephp/cakephp": "^5.0.1" }, "require-dev": { - "phpunit/phpunit": "^10.3", - "dompdf/dompdf": "^2.0", + "phpunit/phpunit": "^10.5.5 || ^11.1.3", + "dompdf/dompdf": "^3.0", "mpdf/mpdf": "^8.1.6", "tecnickcom/tcpdf": "^6.3", "cakephp/cakephp-codesniffer": "^5.0" diff --git a/config/routes.php b/config/routes.php deleted file mode 100644 index c0795812..00000000 --- a/config/routes.php +++ /dev/null @@ -1,6 +0,0 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + html()]]> + _Pdf->orientation()]]> + _Pdf->pageSize()]]> + + + + + + _Pdf->html()]]> + + + + + + + + _Pdf->html()]]> + _Pdf->orientation()]]> + + + + + + + _Pdf->encoding()]]> + + + + + + + + _Pdf->html()]]> + + + + diff --git a/psalm.xml b/psalm.xml index bc10acf2..c90aaac8 100644 --- a/psalm.xml +++ b/psalm.xml @@ -7,6 +7,7 @@ xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" findUnusedBaselineEntry="true" findUnusedCode="false" + errorBaseline="psalm-baseline.xml" > diff --git a/src/CakePdfPlugin.php b/src/CakePdfPlugin.php index 6bcb90ee..59fa1a01 100644 --- a/src/CakePdfPlugin.php +++ b/src/CakePdfPlugin.php @@ -8,18 +8,18 @@ class CakePdfPlugin extends BasePlugin { /** - * Do bootstrap or not + * Do bootstrapping or not * * @var bool */ protected bool $bootstrapEnabled = false; /** - * Load routes or not + * Console middleware * * @var bool */ - protected bool $routesEnabled = false; + protected bool $consoleEnabled = false; /** * Enable middleware @@ -29,9 +29,23 @@ class CakePdfPlugin extends BasePlugin protected bool $middlewareEnabled = false; /** - * Console middleware + * Register container services * * @var bool */ - protected bool $consoleEnabled = false; + protected bool $servicesEnabled = false; + + /** + * Load routes or not + * + * @var bool + */ + protected bool $routesEnabled = false; + + /** + * Load events or not + * + * @var bool + */ + protected bool $eventsEnabled = false; } diff --git a/src/Pdf/CakePdf.php b/src/Pdf/CakePdf.php index 774a2e26..73ae0ea9 100644 --- a/src/Pdf/CakePdf.php +++ b/src/Pdf/CakePdf.php @@ -74,16 +74,16 @@ class CakePdf /** * Instance of PdfEngine class * - * @var \CakePdf\Pdf\Engine\AbstractPdfEngine + * @var \CakePdf\Pdf\Engine\AbstractPdfEngine|null */ - protected AbstractPdfEngine $_engineClass; + protected ?AbstractPdfEngine $_engine = null; /** * Instance of PdfCrypto class * * @var \CakePdf\Pdf\Crypto\AbstractPdfCrypto|null */ - protected ?AbstractPdfCrypto $_cryptoClass = null; + protected ?AbstractPdfCrypto $_crypto = null; /** * Html to be rendered @@ -211,9 +211,9 @@ class CakePdf * true: all * array: List of permissions that are allowed * - * @var mixed + * @var array|bool */ - protected mixed $_allow = false; + protected array|bool $_allow = false; /** * Available permissions @@ -280,15 +280,12 @@ public function __construct(array $config = []) */ public function output(?string $html = null): string { - $Engine = $this->engine(); - if ($Engine === null) { + $engine = $this->engine(); + if ($engine === null) { throw new CakeException('Engine is not loaded'); } - if ($html === null) { - $html = $this->_render(); - } - $this->html($html); + $this->html($html ?? $this->_render()); $cacheKey = ''; $cache = $this->cache(); @@ -300,7 +297,7 @@ public function output(?string $html = null): string } } - $output = $Engine->output(); + $output = $engine->output(); if ($this->protect()) { $output = $this->crypto()->encrypt($output); @@ -317,9 +314,10 @@ public function output(?string $html = null): string * Get/Set Html. * * @param string|null $html Html to set - * @return mixed + * @return $this|string + * @psalm-return ($html is null ? string : $this) */ - public function html(?string $html = null): mixed + public function html(?string $html = null): static|string { if ($html === null) { return $this->_html; @@ -362,14 +360,14 @@ public function write(string $destination, bool $create = true, ?string $html = /** * Load PdfEngine * - * @param array|string|null $name Classname of pdf engine without `Engine` suffix. For example `CakePdf.DomPdf` + * @param \CakePdf\Pdf\Engine\AbstractPdfEngine|array|string|null $name Classname of pdf engine without `Engine` suffix. For example `CakePdf.DomPdf` * @throws \Cake\Core\Exception\CakeException * @return \CakePdf\Pdf\Engine\AbstractPdfEngine|null */ - public function engine(array|string|null $name = null): ?AbstractPdfEngine + public function engine(AbstractPdfEngine|array|string|null $name = null): ?AbstractPdfEngine { if ($name === null) { - return $this->_engineClass; + return $this->_engine; } $config = []; if (is_array($name)) { @@ -377,17 +375,30 @@ public function engine(array|string|null $name = null): ?AbstractPdfEngine $name = $name['className']; } + if (is_object($name)) { + assert( + is_subclass_of($name, AbstractPdfEngine::class), + 'Pdf engines must extend "AbstractPdfEngine"' + ); + + $this->_engine = $name; + $this->_engine->setConfig($config); + + return $this->_engine; + } + $engineClassName = App::className($name, 'Pdf/Engine', 'Engine'); if ($engineClassName === null) { throw new CakeException(sprintf('Pdf engine "%s" not found', $name)); } - if (!is_subclass_of($engineClassName, AbstractPdfEngine::class)) { - throw new CakeException('Pdf engines must extend "AbstractPdfEngine"'); - } - $this->_engineClass = new $engineClassName($this); - $this->_engineClass->setConfig($config); + assert( + is_subclass_of($engineClassName, AbstractPdfEngine::class, true), + 'Pdf engines must extend "AbstractPdfEngine"' + ); + $this->_engine = new $engineClassName($this); + $this->_engine->setConfig($config); - return $this->_engineClass; + return $this->_engine; } /** @@ -400,11 +411,9 @@ public function engine(array|string|null $name = null): ?AbstractPdfEngine public function crypto(string|array|null $name = null): AbstractPdfCrypto { if ($name === null) { - if ($this->_cryptoClass !== null) { - return $this->_cryptoClass; - } - throw new CakeException('Crypto is not loaded'); + return $this->_crypto ?? throw new CakeException('Crypto is not loaded'); } + $config = []; if (is_array($name)) { $config = $name; @@ -415,22 +424,24 @@ public function crypto(string|array|null $name = null): AbstractPdfCrypto if ($engineClassName === null || !class_exists($engineClassName)) { throw new CakeException(sprintf('Pdf crypto `%s` not found', $name)); } - if (!is_subclass_of($engineClassName, AbstractPdfCrypto::class)) { - throw new CakeException('Crypto engine must extend `AbstractPdfCrypto`'); - } - $this->_cryptoClass = new $engineClassName($this); - $this->_cryptoClass->config($config); + assert( + is_subclass_of($engineClassName, AbstractPdfCrypto::class), + 'Crypto engine must extend `AbstractPdfCrypto`' + ); + $this->_crypto = new $engineClassName($this); + $this->_crypto->config($config); - return $this->_cryptoClass; + return $this->_crypto; } /** * Get/Set Page size. * * @param string|null $pageSize Page size to set - * @return mixed + * @return $this|string + * @psalm-return ($pageSize is null ? string : $this) */ - public function pageSize(?string $pageSize = null): mixed + public function pageSize(?string $pageSize = null): static|string { if ($pageSize === null) { return $this->_pageSize; @@ -444,9 +455,10 @@ public function pageSize(?string $pageSize = null): mixed * Get/Set Orientation. * * @param string|null $orientation orientation to set - * @return mixed + * @return $this|string + * @psalm-return ($orientation is null ? string : $this) */ - public function orientation(?string $orientation = null): mixed + public function orientation(?string $orientation = null): static|string { if ($orientation === null) { return $this->_orientation; @@ -460,9 +472,10 @@ public function orientation(?string $orientation = null): mixed * Get/Set Encoding. * * @param string|null $encoding encoding to set - * @return mixed + * @return $this|string + * @psalm-return ($encoding is null ? string : $this) */ - public function encoding(?string $encoding = null): mixed + public function encoding(?string $encoding = null): static|string { if ($encoding === null) { return $this->_encoding; @@ -478,9 +491,10 @@ public function encoding(?string $encoding = null): mixed * @param array|string|null $left left side footer * @param string|null $center center footer * @param string|null $right right side footer - * @return mixed + * @return $this|array + * @psalm-return ($left is null && $center is null && $right is null ? array : $this) */ - public function footer(string|array|null $left = null, ?string $center = null, ?string $right = null): mixed + public function footer(string|array|null $left = null, ?string $center = null, ?string $right = null): static|array { if ($left === null && $center === null && $right === null) { return $this->_footer; @@ -501,9 +515,10 @@ public function footer(string|array|null $left = null, ?string $center = null, ? * @param array|string|null $left left side header * @param string|null $center center header * @param string|null $right right side header - * @return mixed + * @return $this|array + * @psalm-return ($left is null && $center is null && $right is null ? array : $this) */ - public function header(string|array|null $left = null, ?string $center = null, ?string $right = null): mixed + public function header(string|array|null $left = null, ?string $center = null, ?string $right = null): static|array { if ($left === null && $center === null && $right === null) { return $this->_header; @@ -545,14 +560,15 @@ public function header(string|array|null $left = null, ?string $center = null, ? * @param string|null $left left margin * @param string|null $right right margin * @param string|null $top top margin - * @return mixed + * @return $this|array + * @psalm-return ($bottom is null ? array : $this) */ public function margin( string|int|array|null $bottom = null, string|int|null $left = null, string|int|null $right = null, string|int|null $top = null - ): mixed { + ): static|array { if ($bottom === null) { return [ 'bottom' => $this->_marginBottom, @@ -590,9 +606,10 @@ public function margin( * Get/Set bottom margin. * * @param string|int|null $margin margin to set - * @return mixed + * @return $this|string|int|null + * @psalm-return ($margin is null ? string|int|null : $this) */ - public function marginBottom(string|int|null $margin = null): mixed + public function marginBottom(string|int|null $margin = null): static|string|int|null { if ($margin === null) { return $this->_marginBottom; @@ -606,9 +623,10 @@ public function marginBottom(string|int|null $margin = null): mixed * Get/Set left margin. * * @param string|int|null $margin margin to set - * @return mixed + * @return $this|string|int|null + * @psalm-return ($margin is null ? string|int|null : $this) */ - public function marginLeft(string|int|null $margin = null): mixed + public function marginLeft(string|int|null $margin = null): static|string|int|null { if ($margin === null) { return $this->_marginLeft; @@ -622,9 +640,10 @@ public function marginLeft(string|int|null $margin = null): mixed * Get/Set right margin. * * @param string|int|null $margin margin to set - * @return mixed + * @return $this|string|int|null + * @psalm-return ($margin is null ? string|int|null : $this) */ - public function marginRight(string|int|null $margin = null): mixed + public function marginRight(string|int|null $margin = null): static|string|int|null { if ($margin === null) { return $this->_marginRight; @@ -638,9 +657,10 @@ public function marginRight(string|int|null $margin = null): mixed * Get/Set top margin. * * @param string|int|null $margin margin to set - * @return mixed + * @return $this|string|int|null + * @psalm-return ($margin is null ? string|int|null : $this) */ - public function marginTop(string|int|null $margin = null): mixed + public function marginTop(string|int|null $margin = null): static|string|int|null { if ($margin === null) { return $this->_marginTop; @@ -654,9 +674,10 @@ public function marginTop(string|int|null $margin = null): mixed * Get/Set document title. * * @param string|null $title title to set - * @return mixed + * @return $this|string|null + * @psalm-return ($title is null ? string|null : $this) */ - public function title(?string $title = null): mixed + public function title(?string $title = null): static|string|null { if ($title === null) { return $this->_title; @@ -670,9 +691,10 @@ public function title(?string $title = null): mixed * Get/Set javascript delay. * * @param int|null $delay delay to set in milliseconds - * @return mixed + * @return $this|int|null + * @psalm-return ($delay is null ? int|null : $this) */ - public function delay(?int $delay = null): mixed + public function delay(?int $delay = null): static|int|null { if ($delay === null) { return $this->_delay; @@ -687,9 +709,10 @@ public function delay(?int $delay = null): mixed * Waits until the status is equal to the string before rendering the pdf * * @param string|null $status status to set as string - * @return mixed + * @return $this|string|null + * @psalm-return ($status is null ? string|null : $this) */ - public function windowStatus(?string $status = null): mixed + public function windowStatus(?string $status = null): static|string|null { if ($status === null) { return $this->_windowStatus; @@ -703,9 +726,10 @@ public function windowStatus(?string $status = null): mixed * Get/Set protection. * * @param bool|null $protect True or false - * @return mixed + * @return $this|bool + * @psalm-return ($protect is null ? bool : $this) */ - public function protect(?bool $protect = null): mixed + public function protect(?bool $protect = null): static|bool { if ($protect === null) { return $this->_protect; @@ -721,9 +745,10 @@ public function protect(?bool $protect = null): mixed * The user password is used to control who can open the PDF document. * * @param string|null $password password to set - * @return mixed + * @return $this|string|null + * @psalm-return ($password is null ? string|null : $this) */ - public function userPassword(?string $password = null): mixed + public function userPassword(?string $password = null): static|string|null { if ($password === null) { return $this->_userPassword; @@ -739,9 +764,10 @@ public function userPassword(?string $password = null): mixed * The owner password is used to control who can modify, print, manage the PDF document. * * @param string|null $password password to set - * @return mixed + * @return $this|string|null + * @psalm-return ($password is null ? string|null : $this) */ - public function ownerPassword(?string $password = null): mixed + public function ownerPassword(?string $password = null): static|string|null { if ($password === null) { return $this->_ownerPassword; @@ -760,9 +786,10 @@ public function ownerPassword(?string $password = null): mixed * * @param array|string|bool|null $permissions Permissions to set * @throws \Cake\Core\Exception\CakeException - * @return mixed + * @return $this|array|string|bool|null + * @psalm-return ($permissions is null ? array|string|bool|null : $this) */ - public function permissions(bool|array|string|null $permissions = null): mixed + public function permissions(bool|array|string|null $permissions = null): static|array|string|bool|null { if (!$this->protect()) { return $this; @@ -772,15 +799,14 @@ public function permissions(bool|array|string|null $permissions = null): mixed return $this->_allow; } - if (is_string($permissions) && $permissions === 'all') { - $permissions = true; - } - - if (is_string($permissions) && $permissions === 'none') { - $permissions = false; - } - - if (is_array($permissions)) { + if (is_string($permissions)) { + $permissions = strtolower($permissions); + $permissions = match ($permissions) { + 'all' => true, + 'none' => false, + default => throw new CakeException(sprintf('Invalid permission: %s', $permissions)), + }; + } elseif (is_array($permissions)) { foreach ($permissions as $permission) { if (!in_array($permission, $this->_availablePermissions)) { throw new CakeException(sprintf('Invalid permission: %s', $permission)); @@ -802,9 +828,10 @@ public function permissions(bool|array|string|null $permissions = null): mixed * * @param string|bool|null $cache Cache config name to use, If true is passed, 'cake_pdf' will be used. * @throws \Cake\Core\Exception\CakeException - * @return mixed + * @return $this|string|false + * @psalm-return ($cache is null ? string|false : $this) */ - public function cache(bool|string|null $cache = null): mixed + public function cache(bool|string|null $cache = null): static|string|false { if ($cache === null) { return $this->_cache; @@ -832,11 +859,12 @@ public function cache(bool|string|null $cache = null): mixed /** * Template and layout * - * @param mixed $template Template name or null to not use - * @param mixed $layout Layout name or null to not use - * @return mixed + * @param string|false|null $template Template name or null to not use + * @param ?string $layout Layout name or null to not use + * @return $this|array + * @psalm-return ($template is false ? array : $this) */ - public function template(mixed $template = false, mixed $layout = null): mixed + public function template(string|false|null $template = false, ?string $layout = null): static|array { if ($template === false) { return [ @@ -855,12 +883,13 @@ public function template(mixed $template = false, mixed $layout = null): mixed /** * Template path * - * @param mixed $templatePath The path of the template to use - * @return mixed + * @param ?string $templatePath The path of the template to use + * @return $this|string + * @psalm-return ($templatePath is null ? string : $this) */ - public function templatePath(mixed $templatePath = false): mixed + public function templatePath(?string $templatePath = null): static|string { - if ($templatePath === false) { + if ($templatePath === null) { return $this->_templatePath; } @@ -872,12 +901,13 @@ public function templatePath(mixed $templatePath = false): mixed /** * Layout path * - * @param mixed $layoutPath The path of the layout file to use - * @return mixed + * @param ?string $layoutPath The path of the layout file to use + * @return $this|string + * @psalm-return ($layoutPath is null ? string : $this) */ - public function layoutPath(mixed $layoutPath = false): mixed + public function layoutPath(?string $layoutPath = null): static|string { - if ($layoutPath === false) { + if ($layoutPath === null) { return $this->_layoutPath; } @@ -890,9 +920,10 @@ public function layoutPath(mixed $layoutPath = false): mixed * View class for render * * @param string|null $viewClass name of the view class to use - * @return mixed + * @return $this|string + * @psalm-return ($viewClass is null ? string : $this) */ - public function viewRender(?string $viewClass = null): mixed + public function viewRender(?string $viewClass = null): static|string { if ($viewClass === null) { return $this->_viewRender; @@ -906,9 +937,10 @@ public function viewRender(?string $viewClass = null): mixed * Variables to be set on render * * @param array $viewVars view variables to set - * @return mixed + * @return $this|array + * @psalm-return ($viewVars is null ? array : $this) */ - public function viewVars(?array $viewVars = null): mixed + public function viewVars(?array $viewVars = null): static|array { if ($viewVars === null) { return $this->_viewVars; @@ -922,9 +954,10 @@ public function viewVars(?array $viewVars = null): mixed * Theme to use when rendering * * @param string $theme theme to use - * @return mixed + * @return $this|string|null + * @psalm-return ($theme is null ? string|null : $this) */ - public function theme(?string $theme = null): mixed + public function theme(?string $theme = null): static|string|null { if ($theme === null) { return $this->_theme; @@ -938,9 +971,10 @@ public function theme(?string $theme = null): mixed * Helpers to be used in render * * @param array $helpers helpers to use - * @return mixed + * @return $this|array + * @psalm-return ($helpers is null ? array : $this) */ - public function helpers(?array $helpers = null): mixed + public function helpers(?array $helpers = null): static|array { if ($helpers === null) { return $this->_helpers; @@ -958,7 +992,8 @@ public function helpers(?array $helpers = null): mixed protected function _render(): string { $viewClass = $this->viewRender(); - /** @psalm-var class-string<\Cake\View\View> */ + + /** @var class-string<\Cake\View\View> $viewClass */ $viewClass = App::className($viewClass, 'View', $viewClass === 'View' ? '' : 'View'); $viewVars = [ diff --git a/src/Pdf/Crypto/PdftkCrypto.php b/src/Pdf/Crypto/PdftkCrypto.php index cb0483b6..0c9adf0a 100644 --- a/src/Pdf/Crypto/PdftkCrypto.php +++ b/src/Pdf/Crypto/PdftkCrypto.php @@ -99,7 +99,7 @@ public function encrypt(string $data): string $exitcode = proc_close($prochandle); if ($exitcode !== 0) { - throw new CakeException(sprintf("Crypto: (exit code %d)\n%s", $exitcode, $stderr)); + throw new CakeException(sprintf("Crypto: (exit code %d)\n%s", $exitcode, (string)$stderr)); } return (string)$stdout; diff --git a/src/Pdf/Engine/MpdfEngine.php b/src/Pdf/Engine/MpdfEngine.php index 574c238e..8ff39bb0 100644 --- a/src/Pdf/Engine/MpdfEngine.php +++ b/src/Pdf/Engine/MpdfEngine.php @@ -18,8 +18,7 @@ public function output(): string $orientation = $this->_Pdf->orientation() === 'landscape' ? 'L' : 'P'; $format = $this->_Pdf->pageSize(); if ( - is_string($format) - && $orientation === 'L' + $orientation === 'L' && strpos($format, '-L') === false ) { $format .= '-' . $orientation; diff --git a/src/Pdf/Engine/WkHtmlToPdfEngine.php b/src/Pdf/Engine/WkHtmlToPdfEngine.php index d4c6cf50..5ec4cb08 100644 --- a/src/Pdf/Engine/WkHtmlToPdfEngine.php +++ b/src/Pdf/Engine/WkHtmlToPdfEngine.php @@ -139,6 +139,7 @@ protected function _getCommand(): string } $command .= $this->parseOptions($key, $value); } + /** @var array $footer */ $footer = $this->_Pdf->footer(); foreach ($footer as $location => $text) { if ($text !== null) { @@ -146,6 +147,7 @@ protected function _getCommand(): string } } + /** @var array $header */ $header = $this->_Pdf->header(); foreach ($header as $location => $text) { if ($text !== null) { diff --git a/tests/TestCase/Pdf/CakePdfTest.php b/tests/TestCase/Pdf/CakePdfTest.php index 3bc18f11..27169239 100644 --- a/tests/TestCase/Pdf/CakePdfTest.php +++ b/tests/TestCase/Pdf/CakePdfTest.php @@ -7,6 +7,7 @@ use Cake\Core\Exception\CakeException; use Cake\TestSuite\TestCase; use CakePdf\Pdf\CakePdf; +use PHPUnit\Framework\Attributes\DataProvider; use TestApp\Pdf\Engine\PdfTestEngine; class CakePdfTest extends TestCase @@ -48,9 +49,8 @@ public function testNonExistingEngineException() /** * testOutput - * - * @dataProvider provider */ + #[DataProvider('provider')] public function testOutput($config) { $pdf = new CakePdf($config); @@ -71,9 +71,8 @@ public function testOutput($config) /** * Test the custom paths for Layouts - * - * @dataProvider provider */ + #[DataProvider('provider')] public function testCustomLayoutPaths($config) { $pdf = new CakePdf($config); @@ -87,9 +86,8 @@ public function testCustomLayoutPaths($config) /** * Test the custom paths for Templates - * - * @dataProvider provider */ + #[DataProvider('provider')] public function testCustomTemplatePaths($config) { $pdf = new CakePdf($config); @@ -103,9 +101,8 @@ public function testCustomTemplatePaths($config) /** * testPluginOutput - * - * @dataProvider provider */ + #[DataProvider('provider')] public function testPluginOutput($config) { $pdf = new CakePdf($config); @@ -131,9 +128,8 @@ public function testPluginOutput($config) /** * Tests that engine returns the proper object - * - * @dataProvider provider */ + #[DataProvider('provider')] public function testEngine($config) { $pdf = new CakePdf($config); @@ -141,9 +137,7 @@ public function testEngine($config) $this->assertEquals(PdfTestEngine::class, get_class($engine)); } - /** - * @dataProvider provider - */ + #[DataProvider('provider')] public function testMargin($config) { $pdf = new CakePdf($config); @@ -196,9 +190,7 @@ public function testMargin($config) $this->assertEquals($expected, $pdf->margin()); } - /** - * @dataProvider provider - */ + #[DataProvider('provider')] public function testConfigRead($config) { Configure::write('CakePdf', $config); diff --git a/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php b/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php index 15c7d81c..e4cd069a 100644 --- a/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php +++ b/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php @@ -27,11 +27,14 @@ public function setUp(): void */ public function testReceiveOptions() { - $engineClass = $this->getMockBuilder(DomPdfEngine::class)->disableOriginalConstructor()->onlyMethods(['_createInstance'])->getMock()::class; + $mock = $this->getMockBuilder(DomPdfEngine::class) + ->disableOriginalConstructor() + ->onlyMethods(['_createInstance']) + ->getMock(); $Pdf = new CakePdf([ 'engine' => [ - 'className' => '\\' . $engineClass, + 'className' => $mock, 'options' => [ 'isJavascriptEnabled' => false, 'isHtml5ParserEnabled' => true, @@ -39,6 +42,8 @@ public function testReceiveOptions() ], ]); + $mock->__construct($Pdf); + $expected = [ 'fontCache' => TMP, 'tempDir' => TMP, @@ -51,11 +56,11 @@ public function testReceiveOptions() ->expects($this->once()) ->method('_createInstance') ->with($expected) - ->will($this->returnCallback(function ($options) use ($expected) { + ->willReturnCallback(function ($options) use ($expected) { $this->assertEquals($expected, $options); return new Dompdf($options); - })); + }); $Pdf->engine()->output(); } @@ -65,10 +70,14 @@ public function testReceiveOptions() */ public function testSetOptions() { - $engineClass = $this->getMockBuilder(DomPdfEngine::class)->disableOriginalConstructor()->onlyMethods(['_output'])->getMock()::class; + $mock = $this->getMockBuilder(DomPdfEngine::class) + ->disableOriginalConstructor() + ->onlyMethods(['_output']) + ->getMock(); + $Pdf = new CakePdf([ 'engine' => [ - 'className' => '\\' . $engineClass, + 'className' => $mock, 'options' => [ 'isJavascriptEnabled' => false, 'isHtml5ParserEnabled' => true, @@ -76,11 +85,13 @@ public function testSetOptions() ], ]); + $mock->__construct($Pdf); + $Pdf ->engine() ->expects($this->once()) ->method('_output') - ->will($this->returnCallback(function ($Dompdf) { + ->willReturnCallback(function ($Dompdf) { $Options = $Dompdf->getOptions(); $this->assertEquals(TMP, $Options->getFontCache()); $this->assertEquals(TMP, $Options->getTempDir()); @@ -88,7 +99,7 @@ public function testSetOptions() $this->assertTrue($Options->getIsHtml5ParserEnabled()); return $Dompdf->output(); - })); + }); $Pdf->engine()->output(); } @@ -113,19 +124,21 @@ public function testOutput() */ public function testControlFlow() { - $engineClass = $this->getMockBuilder(DomPdfEngine::class) + $mock = $this->getMockBuilder(DomPdfEngine::class) ->disableOriginalConstructor() ->onlyMethods([ '_createInstance', '_render', '_output', ]) - ->getMock()::class; + ->getMock(); $Pdf = new CakePdf([ - 'engine' => '\\' . $engineClass, + 'engine' => $mock, ]); + $mock->__construct($Pdf); + $DomPDF = new Dompdf(); $Engine = $Pdf->engine(); @@ -151,21 +164,23 @@ public function testControlFlow() */ public function testDompdfControlFlow() { - $engineClass = $this->getMockBuilder(DomPdfEngine::class) + $mock = $this->getMockBuilder(DomPdfEngine::class) ->disableOriginalConstructor() ->onlyMethods(['_createInstance']) - ->getMock()::class; + ->getMock(); $Pdf = new CakePdf([ - 'engine' => '\\' . $engineClass, + 'engine' => $mock, ]); + $mock->__construct($Pdf); + $Pdf ->engine() ->expects($this->once()) ->method('_createInstance') - ->will($this->returnCallback(function ($options) { - $Dompdf = $this->getMockBuilder('\Dompdf\Dompdf') + ->willReturnCallback(function ($options) { + $Dompdf = $this->getMockBuilder(Dompdf::class) ->onlyMethods(['setPaper', 'loadHtml', 'render', 'output']) ->setConstructorArgs([$options]) ->getMock(); @@ -185,7 +200,7 @@ public function testDompdfControlFlow() ->method('output'); return $Dompdf; - })); + }); $Pdf->engine()->output(); } diff --git a/tests/TestCase/Pdf/Engine/MpdfEngineTest.php b/tests/TestCase/Pdf/Engine/MpdfEngineTest.php index 6f9e07d0..9ef77e1a 100644 --- a/tests/TestCase/Pdf/Engine/MpdfEngineTest.php +++ b/tests/TestCase/Pdf/Engine/MpdfEngineTest.php @@ -27,14 +27,14 @@ public function setUp(): void */ public function testSetOptions() { - $engineClass = $this->getMockBuilder(MpdfEngine::class) + $mock = $this->getMockBuilder(MpdfEngine::class) ->disableOriginalConstructor() ->onlyMethods(['_createInstance']) - ->getMock()::class; + ->getMock(); $Pdf = new CakePdf([ 'engine' => [ - 'className' => '\\' . $engineClass, + 'className' => $mock, ], 'pageSize' => 'A4', 'orientation' => 'landscape', @@ -42,18 +42,20 @@ public function testSetOptions() ]); $Pdf->html(''); + $mock->__construct($Pdf); + $Pdf ->engine() ->expects($this->once()) ->method('_createInstance') - ->will($this->returnCallback(function ($config) { + ->willReturnCallback(function ($config) { $Mpdf = new Mpdf($config); $this->assertSame(TMP, $Mpdf->tempDir); $this->assertSame('L', $Mpdf->CurOrientation); return $Mpdf; - })); + }); $Pdf->engine()->output(); }