Skip to content

Commit 5c80ba8

Browse files
committed
Merge remote-tracking branch 'origin/laravel-8-support'
2 parents 71c9960 + d14f639 commit 5c80ba8

12 files changed

Lines changed: 82 additions & 66 deletions

File tree

.github/workflows/main.yml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
name: Unit tests
1+
name: Tests
22

33
on: [push]
44

55
jobs:
6-
run:
7-
runs-on: [ubuntu-latest]
6+
build:
7+
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
php-versions: ['7.1', '7.2', '7.3', '7.4']
11-
name: Tests - ${{ matrix.php-versions }}
12-
steps:
13-
- name: Checkout
14-
uses: actions/checkout@v1
15-
16-
- name: Setup PHP
17-
uses: shivammathur/setup-php@v1
18-
with:
19-
php-version: ${{ matrix.php-versions }}
20-
coverage: xdebug
10+
php: [7.3, 7.4, 8.0]
11+
symfony_process: [3.4, 4.0, 5.0]
2112

22-
- name: Install deps
23-
run: composer install
13+
steps:
14+
- uses: actions/checkout@v2
2415

25-
- name: Run tests
26-
run: vendor/bin/phpunit -c phpunit.xml --coverage-clover=coverage.xml
16+
- name: test against Symfony/Process ${{ matrix.symfony_process }} on PHP ${{ matrix.php }}
17+
run: docker build . --build-arg PHP_VERSION=${{ matrix.php }} --build-arg SYMFONY_PROCESS=${{ matrix.symfony_process }}

.travis.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
ARG PHP_VERSION=8.0
2+
FROM php:$PHP_VERSION-cli-alpine
3+
4+
RUN apk add git zip unzip autoconf make g++
5+
6+
RUN if [ -z "${PHP_VERSION##7\.1*}" ]; then \
7+
pecl install xdebug-2.9.8 && docker-php-ext-enable xdebug; \
8+
else \
9+
pecl install xdebug && docker-php-ext-enable xdebug; \
10+
fi;
11+
12+
RUN curl -sS https://getcomposer.org/installer | php \
13+
&& mv composer.phar /usr/local/bin/composer
14+
15+
WORKDIR /package
16+
17+
COPY composer.json ./
18+
19+
ARG SYMFONY_PROCESS=5
20+
RUN composer require symfony/process ^$SYMFONY_PROCESS.0
21+
22+
RUN composer install
23+
24+
COPY . .
25+
26+
RUN composer test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PHP ChromePDF Renderer
22
![Unit tests](https://github.com/SynergiTech/chrome-pdf-php/workflows/Unit%20tests/badge.svg)
3-
[![Build Status](https://travis-ci.org/SynergiTech/chrome-pdf-php.svg?branch=master)](https://travis-ci.org/SynergiTech/chrome-pdf-php)
3+
[![Tests](https://github.com/SynergiTech/chrome-pdf-php/actions/workflows/main.yml/badge.svg)](https://github.com/SynergiTech/chrome-pdf-php/actions/workflows/main.yml)
44

55
_For pre-V1 documentation [click here](https://github.com/SynergiTech/chrome-pdf-php/blob/v0/README.md)_
66

composer.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=7.1",
14-
"symfony/process": "~3.4 || ~4.0",
13+
"php": "^7.3|^8.0",
14+
"symfony/process": "~3.4 || ~4.0 || ~5.0",
1515
"guzzlehttp/guzzle": "^6.3"
1616
},
1717
"autoload": {
@@ -22,6 +22,14 @@
2222
},
2323
"require-dev": {
2424
"phpunit/phpunit": "^7.5 || ^8.4",
25-
"friendsofphp/php-cs-fixer": "^2.15"
25+
"friendsofphp/php-cs-fixer": "^2.15",
26+
"phpstan/phpstan": "^0.12.83"
27+
},
28+
"scripts": {
29+
"test": [
30+
"Composer\\Config::disableProcessTimeout",
31+
"phpunit",
32+
"phpstan analyse"
33+
]
2634
}
2735
}

phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
paths:
3+
- src
4+
level: 8
5+
ignoreErrors:
6+
- '#Call to an undefined method object::mustRun\(\).#'

src/AbstractPDF.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,23 +462,23 @@ public function getLandscape(): ?bool
462462
* Renders a string of HTML to a PDF
463463
*
464464
* @param string $content Content to render
465-
* @return resource
465+
* @return resource|null
466466
*/
467467
abstract public function renderContent(string $content);
468468

469469
/**
470470
* Renders a URL to a PDF
471471
*
472-
* @param string $content URL to render
473-
* @return resource
472+
* @param string $url URL to render
473+
* @return resource|null
474474
*/
475475
abstract public function renderURL(string $url);
476476

477477
/**
478478
* Renders a local file to a PDF
479479
*
480-
* @param string $content Local file to render
481-
* @return resource
480+
* @param string $path Local file to render
481+
* @return resource|null
482482
*/
483483
abstract public function renderFile(string $path);
484484
}

src/Browserless.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Browserless extends AbstractPDF
2222
*/
2323
private $pdfEndpoint = '/pdf';
2424
/**
25-
* @var object
25+
* @var \GuzzleHttp\Client
2626
*/
2727
private $client;
2828
/**
@@ -40,7 +40,7 @@ class Browserless extends AbstractPDF
4040

4141
/**
4242
* @param string $apiKey api key from browserless.io
43-
* @param object $client custom Guzzle client
43+
* @param \GuzzleHttp\Client $client custom Guzzle client
4444
*/
4545
public function __construct(string $apiKey = null, $client = null)
4646
{
@@ -149,7 +149,7 @@ public function getRotation(): ?int
149149
/**
150150
* Gets the payload of JSON options to be sent to browserless, minus the `url` or `html` property
151151
*
152-
* @return array
152+
* @return array<string, mixed>
153153
*/
154154
public function getFormattedOptions(): array
155155
{
@@ -227,7 +227,7 @@ public function getFormattedOptions(): array
227227
}
228228

229229
/**
230-
* @param array $options
230+
* @param array<string, mixed> $options
231231
* @return resource
232232
*/
233233
private function render(array $options)
@@ -240,9 +240,10 @@ private function render(array $options)
240240
'json' => $options,
241241
]);
242242
} catch (\GuzzleHttp\Exception\ClientException $e) {
243-
$body = $e->getResponse()->getBody();
244-
$json = json_decode($body);
245-
$message = $e->getResponse()->getBody();
243+
$response = $e->getResponse();
244+
$body = $response ? $response->getBody() : '';
245+
$json = $response ? json_decode($body) : '';
246+
$message = $body;
246247
if (json_last_error() === JSON_ERROR_NONE) {
247248
$messages = [];
248249
foreach ($json as $error) {

src/Chrome.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Chrome extends AbstractPDF
1616
* an array of temporary file handles
1717
* they are unlinked after rendering
1818
*
19-
* @var array
19+
* @var array<string>
2020
*/
2121
private $handles = [];
2222

@@ -43,7 +43,7 @@ public function __construct($binary = 'chrome-pdf', $processClass = null)
4343
/**
4444
* A factory for creating process classes, to execute the binary
4545
*
46-
* @param string $cmdline The command to run
46+
* @param array<string> $cmdline The command to run
4747
* @return object
4848
*/
4949
public function createProcess($cmdline)
@@ -100,7 +100,7 @@ public function getMarginString(): ?string
100100
/**
101101
* Gets an array of options to pass to the command line
102102
*
103-
* @return array
103+
* @return array<mixed>
104104
*/
105105
private function getCommandFlags(): array
106106
{
@@ -139,12 +139,12 @@ private function getCommandFlags(): array
139139
$opts[] = "--no-displayHeaderFooter";
140140
}
141141
if ($this->getHeader()) {
142-
$headerFile = $this->getFileForString($this->getHeader());
142+
$headerFile = $this->getFileForString($this->getHeader() ?: '');
143143
$opts[] = "--headerTemplate";
144144
$opts[] = $headerFile;
145145
}
146146
if ($this->getFooter()) {
147-
$footerFile = $this->getFileForString($this->getFooter());
147+
$footerFile = $this->getFileForString($this->getFooter() ?: '');
148148
$opts[] = "--footerTemplate";
149149
$opts[] = $footerFile;
150150
}
@@ -188,7 +188,7 @@ private function getCommandFlags(): array
188188
*/
189189
private function getFileForString(string $content): string
190190
{
191-
$tmpfile = tempnam(sys_get_temp_dir(), 'chromepdf');
191+
$tmpfile = tempnam(sys_get_temp_dir(), 'chromepdf') ?: '';
192192
// Chrome must see the correct extension to load it as html
193193
rename($tmpfile, $tmpfile .= '.html');
194194
$this->handles[] = $tmpfile;
@@ -200,12 +200,12 @@ private function getFileForString(string $content): string
200200
/**
201201
* Execute the local renderer with the given options
202202
*
203-
* @param array $options
203+
* @param array<string> $options
204204
* @return string A path to a temporary file containing the rendered PDF
205205
*/
206206
private function executeBinary(array $options): string
207207
{
208-
$output = tempnam(sys_get_temp_dir(), 'chromepdf');
208+
$output = tempnam(sys_get_temp_dir(), 'chromepdf') ?: '';
209209
$options[] = "--path";
210210
$options[] = $output;
211211
array_unshift($options, $this->binary, 'pdf');
@@ -229,7 +229,7 @@ public function renderContent(string $content)
229229
$opts[] = $file;
230230

231231
$outputFile = $this->executeBinary($opts);
232-
return fopen($outputFile, 'r');
232+
return fopen($outputFile, 'r') ?: null;
233233
}
234234

235235
/**
@@ -242,7 +242,7 @@ public function renderURL(string $url)
242242
$opts[] = $url;
243243

244244
$outputFile = $this->executeBinary($opts);
245-
return fopen($outputFile, 'r');
245+
return fopen($outputFile, 'r') ?: null;
246246
}
247247

248248
/**
@@ -255,6 +255,6 @@ public function renderFile(string $path)
255255
$opts[] = $path;
256256

257257
$outputFile = $this->executeBinary($opts);
258-
return fopen($outputFile, 'r');
258+
return fopen($outputFile, 'r') ?: null;
259259
}
260260
}

test/BrowserlessTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use SynergiTech\ChromePDF\Browserless;
1414
use SynergiTech\ChromePDF\Browserless\APIException;
15+
use SynergiTech\ChromePDF\Chrome;
1516
use SynergiTech\ChromePDF\Test\TestCase;
1617

1718
use PHPUnit\Framework\Constraint\ArraySubset;

0 commit comments

Comments
 (0)