Skip to content

Commit

Permalink
Merge pull request #1 from enflow/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer authored Jun 25, 2021
2 parents 63c4f9c + a0048a5 commit b6a9542
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 82 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: https://enflow.nl/contact
23 changes: 23 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Check & fix styling

on: [push]

jobs:
php-cs-fixer:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.dist.php --allow-risky=yes

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
29 changes: 29 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: run-tests

on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.0, 7.4]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
coverage: none

- name: Install dependencies
run: composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.phpunit.result.cache
composer.lock
vendor
.php-cs-fixer.cache
42 changes: 42 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->notPath('bootstrap/*')
->notPath('storage/*')
->notPath('resources/view/mail/*')
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true)
;

return (new Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
13 changes: 0 additions & 13 deletions .php_cs

This file was deleted.

27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
}
],
"require": {
"php": "^7.2.5",
"phpoffice/phpword": "^0.17.0",
"php": "^7.4|^8.0",
"phpoffice/phpword": "^0.18.2",
"symfony/process": "^4.2 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.1"
"phpunit/phpunit": "^9.0",
"friendsofphp/php-cs-fixer": "^3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -33,6 +34,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
}
}
23 changes: 7 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Enflow's Document Replacer">
<testsuite name="enflow/document-replacer">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
2 changes: 1 addition & 1 deletion src/Converters/AbstractConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

abstract class AbstractConverter
{
private $documentReplacer;
private DocumentReplacer $documentReplacer;

private function __construct(DocumentReplacer $documentReplacer)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Converters/UnoconvConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class UnoconvConverter extends AbstractConverter
{
public $binary = '/usr/bin/unoconv';
public string $binary = '/usr/bin/unoconv';

public function convert(string $input, string $output): void
{
Expand All @@ -21,11 +21,11 @@ public function convert(string $input, string $output): void
$process->mustRun();

// unoconv 0.7 always appends extension to --output filename https://github.com/unoconv/unoconv/issues/307
if (!pathinfo($output, PATHINFO_EXTENSION) && file_exists($output . '.pdf')) {
if (! pathinfo($output, PATHINFO_EXTENSION) && file_exists($output . '.pdf')) {
rename($output . '.pdf', $output);
}

if (!file_exists($output)) {
if (! file_exists($output)) {
throw new ConversionFailed("Unable to convert document to PDF through unoconv");
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/DocumentReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Enflow\DocumentReplacer;

use Enflow\DocumentReplacer\Converters\AbstractConverter;
use Enflow\DocumentReplacer\Exceptions\InvalidReplacement;
use Exception;
use PhpOffice\PhpWord\TemplateProcessor;
use Enflow\DocumentReplacer\Converters\AbstractConverter;

class DocumentReplacer
{
Expand All @@ -30,8 +30,9 @@ public function replace(array $keyValue): self
if ($value instanceof ValueTypes\Image) {
$this->templateProcessor->setImageValue($key, $value->replacements());
} else {
if (!is_scalar($value) && $value !== null) {
if (! is_scalar($value) && $value !== null) {
$type = gettype($value);

throw new InvalidReplacement("Could not replace '{$key}' in template. Value must be non-scalar or null. Type is: {$type}");
}

Expand Down Expand Up @@ -61,7 +62,7 @@ public function save(string $outputPath): string

$class::make($this)->convert($temporaryFile, $outputPath);

if (!file_exists($outputPath) || !filesize($outputPath)) {
if (! file_exists($outputPath) || ! filesize($outputPath)) {
throw new Exception("Converter failed to output valid file to {$outputPath}");
}

Expand Down
4 changes: 2 additions & 2 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class Template
{
private function __construct(string $path)
{
if (!file_exists($path)) {
if (! file_exists($path)) {
throw new InvalidArgumentException("File at '{$path}' cannot be found.");
}

if (!is_file($path)) {
if (! is_file($path)) {
throw new InvalidArgumentException("File at '{$path}' must be a file.");
}

Expand Down
8 changes: 5 additions & 3 deletions src/ValueTypes/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

class Image
{
/** @var string|Closure */
private $image;
private $ratio;
private $width;
private $height;

private ?bool $ratio = null;
private ?int $width = null;
private ?int $height = null;

private function __construct($image)
{
Expand Down
20 changes: 12 additions & 8 deletions tests/ImageValueTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Enflow\DocumentReplacer\Test;

use Enflow\DocumentReplacer\Converters\UnoconvConverter;
use Enflow\DocumentReplacer\DocumentReplacer;
use Enflow\DocumentReplacer\ValueTypes\Image;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,20 +14,26 @@ public function test_image_create_from_path()

public function test_image_create_from_base64_with_prefix()
{
$this->assertEquals('4e7e0753fd7068d368bbe516f09e321a',
Image::forBase64('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB8SURBVDhP3YxBDoAgDAR5iEf//zPfgCutCGwp4MXEyZI0W6bh2PaYyMM8Aa/0l05UsjDvGzIoG+fWJQPHfyMDKccy4E9oOLpLeLLQ9OWJRwamz2VXNqPrm1xWMjB/M56coy0hK0PWKdHz5fRABj0f/Efm6I5o5SW+kmM8AS/fakEk7YJkAAAAAElFTkSuQmCC')->signature());
$this->assertEquals(
'4e7e0753fd7068d368bbe516f09e321a',
Image::forBase64('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB8SURBVDhP3YxBDoAgDAR5iEf//zPfgCutCGwp4MXEyZI0W6bh2PaYyMM8Aa/0l05UsjDvGzIoG+fWJQPHfyMDKccy4E9oOLpLeLLQ9OWJRwamz2VXNqPrm1xWMjB/M56coy0hK0PWKdHz5fRABj0f/Efm6I5o5SW+kmM8AS/fakEk7YJkAAAAAElFTkSuQmCC')->signature()
);
}

public function test_image_create_from_base64_without_prefix()
{
$this->assertEquals('4e7e0753fd7068d368bbe516f09e321a',
Image::forBase64('iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB8SURBVDhP3YxBDoAgDAR5iEf//zPfgCutCGwp4MXEyZI0W6bh2PaYyMM8Aa/0l05UsjDvGzIoG+fWJQPHfyMDKccy4E9oOLpLeLLQ9OWJRwamz2VXNqPrm1xWMjB/M56coy0hK0PWKdHz5fRABj0f/Efm6I5o5SW+kmM8AS/fakEk7YJkAAAAAElFTkSuQmCC')->signature());
$this->assertEquals(
'4e7e0753fd7068d368bbe516f09e321a',
Image::forBase64('iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB8SURBVDhP3YxBDoAgDAR5iEf//zPfgCutCGwp4MXEyZI0W6bh2PaYyMM8Aa/0l05UsjDvGzIoG+fWJQPHfyMDKccy4E9oOLpLeLLQ9OWJRwamz2VXNqPrm1xWMjB/M56coy0hK0PWKdHz5fRABj0f/Efm6I5o5SW+kmM8AS/fakEk7YJkAAAAAElFTkSuQmCC')->signature()
);
}

public function test_image_create_from_binary()
{
$this->assertEquals('4e7e0753fd7068d368bbe516f09e321a',
Image::forBinary(base64_decode('iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB8SURBVDhP3YxBDoAgDAR5iEf//zPfgCutCGwp4MXEyZI0W6bh2PaYyMM8Aa/0l05UsjDvGzIoG+fWJQPHfyMDKccy4E9oOLpLeLLQ9OWJRwamz2VXNqPrm1xWMjB/M56coy0hK0PWKdHz5fRABj0f/Efm6I5o5SW+kmM8AS/fakEk7YJkAAAAAElFTkSuQmCC'))->signature());
$this->assertEquals(
'4e7e0753fd7068d368bbe516f09e321a',
Image::forBinary(base64_decode('iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB8SURBVDhP3YxBDoAgDAR5iEf//zPfgCutCGwp4MXEyZI0W6bh2PaYyMM8Aa/0l05UsjDvGzIoG+fWJQPHfyMDKccy4E9oOLpLeLLQ9OWJRwamz2VXNqPrm1xWMjB/M56coy0hK0PWKdHz5fRABj0f/Efm6I5o5SW+kmM8AS/fakEk7YJkAAAAAElFTkSuQmCC'))->signature()
);
}

public function test_image_create_lazy()
Expand Down
2 changes: 0 additions & 2 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Enflow\DocumentReplacer\Test;

use Enflow\DocumentReplacer\Converters\UnoconvConverter;
use Enflow\DocumentReplacer\DocumentReplacer;
use Enflow\DocumentReplacer\Template;
use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit b6a9542

Please sign in to comment.