PdfSpy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 1
dl 0
loc 37
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLastCommand() 0 4 1
A getOutput() 0 7 1
A executeCommand() 0 6 1
A checkOutput() 0 4 1
1
<?php
2
3
namespace Tests\Knp\Snappy;
4
5
use Knp\Snappy\Pdf;
6
use PHPUnit\Framework\Error\Error;
7
use PHPUnit\Framework\TestCase;
8
use RecursiveDirectoryIterator;
9
use FilesystemIterator;
10
use CallbackFilterIterator;
11
use DirectoryIterator;
12
use ReflectionMethod;
13
14
class PdfTest extends TestCase
15
{
16
    const SHELL_ARG_QUOTE_REGEX = '(?:"|\')'; // escapeshellarg produces double quotes on Windows, single quotes otherwise
17
18
    public function tearDown(): void
19
    {
20
        $directory = __DIR__ . '/i-dont-exist';
21
22
        if (\file_exists($directory)) {
23
            $iterator = new RecursiveDirectoryIterator(
24
                $directory,
25
                FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
26
            );
27
28
            foreach ($iterator as $item) {
29
                \unlink((string) $item);
30
            }
31
32
            \rmdir($directory);
33
        }
34
35
        $htmlFiles = new CallbackFilterIterator(
36
            new DirectoryIterator(__DIR__),
37
            function ($filename) {
38
                return \preg_match('/\.html$/', $filename) === 1;
39
            }
40
        );
41
42
        foreach ($htmlFiles as $file) {
43
            \unlink($file->getPathname());
44
        }
45
    }
46
47
    public function testCreateInstance(): void
48
    {
49
        $testObject = new Pdf();
50
        $this->assertInstanceOf(Pdf::class, $testObject);
51
    }
52
53
    public function testThatSomethingUsingTmpFolder(): void
54
    {
55
        $q = self::SHELL_ARG_QUOTE_REGEX;
56
        $testObject = new PdfSpy();
57
        $testObject->setTemporaryFolder(__DIR__);
58
59
        $testObject->getOutputFromHtml('<html></html>', ['footer-html' => 'footer']);
60
        $this->assertRegExp('/emptyBinary --lowquality --footer-html ' . $q . '.*' . $q . ' ' . $q . '.*' . $q . ' ' . $q . '.*' . $q . '/', $testObject->getLastCommand());
61
    }
62
63
    public function testThatSomethingUsingNonexistentTmpFolder(): void
64
    {
65
        $temporaryFolder = \sys_get_temp_dir() . '/i-dont-exist';
66
67
        $testObject = new PdfSpy();
68
        $testObject->setTemporaryFolder($temporaryFolder);
69
70
        $testObject->getOutputFromHtml('<html></html>', ['footer-html' => 'footer']);
71
72
        $this->assertDirectoryExists($temporaryFolder);
73
    }
74
75
    public function testRemovesLocalFilesOnError(): void
76
    {
77
        $pdf = new PdfSpy();
78
        $method = new ReflectionMethod($pdf, 'createTemporaryFile');
79
        $method->setAccessible(true);
80
        $method->invoke($pdf, 'test', $pdf->getDefaultExtension());
81
        $this->assertEquals(1, \count($pdf->temporaryFiles));
82
        $this->expectException(Error::class);
83
        \trigger_error('test error', \E_USER_ERROR);
84
        $this->assertFileNotExists(\reset($pdf->temporaryFiles));
85
    }
86
87
    /**
88
     * @dataProvider dataOptions
89
     */
90
    public function testOptions(array $options, string $expectedRegex): void
91
    {
92
        $testObject = new PdfSpy();
93
        $testObject->getOutputFromHtml('<html></html>', $options);
94
        $this->assertRegExp($expectedRegex, $testObject->getLastCommand());
95
    }
96
97
    public function dataOptions(): array
98
    {
99
        $q = self::SHELL_ARG_QUOTE_REGEX;
100
101
        return [
102
            // no options
103
            [
104
                [],
105
                '/emptyBinary --lowquality ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
106
            ],
107
            // just pass the given footer URL
108
            [
109
                ['footer-html' => 'http://google.com'],
110
                '/emptyBinary --lowquality --footer-html ' . $q . 'http:\/\/google\.com' . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
111
            ],
112
            // just pass the given footer file
113
            [
114
                ['footer-html' => __FILE__],
115
                '/emptyBinary --lowquality --footer-html ' . $q . \preg_quote(__FILE__, '/') . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
116
            ],
117
            // save the given footer HTML string into a temporary file and pass that filename
118
            [
119
                ['footer-html' => 'footer'],
120
                '/emptyBinary --lowquality --footer-html ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
121
            ],
122
            // save the content of the given XSL URL to a file and pass that filename
123
            [
124
                ['xsl-style-sheet' => 'http://google.com'],
125
                '/emptyBinary --lowquality --xsl-style-sheet ' . $q . '.*\.xsl' . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
126
            ],
127
            // set toc options after toc argument
128
            [
129
                [
130
                    'grayscale' => true,
131
                    'toc' => true,
132
                    'disable-dotted-lines' => true,
133
                ],
134
                '/emptyBinary --grayscale --lowquality toc --disable-dotted-lines ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
135
            ],
136
        ];
137
    }
138
139
    public function testRemovesLocalFilesOnDestruct(): void
140
    {
141
        $pdf = new PdfSpy();
142
        $method = new ReflectionMethod($pdf, 'createTemporaryFile');
143
        $method->setAccessible(true);
144
        $method->invoke($pdf, 'test', $pdf->getDefaultExtension());
145
        $this->assertEquals(1, \count($pdf->temporaryFiles));
146
        $this->assertFileExists(\reset($pdf->temporaryFiles));
147
        $pdf->__destruct();
148
        $this->assertFileNotExists(\reset($pdf->temporaryFiles));
149
    }
150
}
151
152
class PdfSpy extends Pdf
153
{
154
    /**
155
     * @var string
156
     */
157
    private $lastCommand;
158
159
    public function __construct()
160
    {
161
        parent::__construct('emptyBinary');
162
    }
163
164
    public function getLastCommand(): string
165
    {
166
        return $this->lastCommand;
167
    }
168
169
    public function getOutput($input, array $options = []): string
170
    {
171
        $filename = $this->createTemporaryFile(null, $this->getDefaultExtension());
172
        $this->generate($input, $filename, $options, true);
173
174
        return 'output';
175
    }
176
177
    protected function executeCommand(string $command): array
178
    {
179
        $this->lastCommand = $command;
180
181
        return [0, 'output', 'errorOutput'];
182
    }
183
184
    protected function checkOutput(string $output, string $command): void
185
    {
186
        //let's say everything went right
187
    }
188
}
189