Passed
Push — master ( d7ad8d...2766ff )
by Alessandro
55s queued 10s
created

PdfTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 137
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 28 4
A testCreateInstance() 0 5 1
A testThatSomethingUsingTmpFolder() 0 9 1
A testThatSomethingUsingNonexistentTmpFolder() 0 11 1
A testRemovesLocalFilesOnError() 0 11 1
A testOptions() 0 6 1
A dataOptions() 0 41 1
A testRemovesLocalFilesOnDestruct() 0 11 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
9
class PdfTest extends TestCase
10
{
11
    const SHELL_ARG_QUOTE_REGEX = '(?:"|\')'; // escapeshellarg produces double quotes on Windows, single quotes otherwise
12
13
    public function tearDown()
14
    {
15
        $directory = __DIR__ . '/i-dont-exist';
16
17
        if (file_exists($directory)) {
18
            $iterator = new \RecursiveDirectoryIterator(
19
                $directory,
20
                \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
21
            );
22
23
            foreach ($iterator as $item) {
24
                unlink(strval($item));
25
            }
26
27
            rmdir($directory);
28
        }
29
30
        $htmlFiles = new \CallbackFilterIterator(
31
            new \DirectoryIterator(__DIR__),
32
            function ($filename) {
33
                return preg_match('/\.html$/', $filename) === 1;
34
            }
35
        );
36
37
        foreach ($htmlFiles as $file) {
38
            unlink($file->getPathname());
39
        }
40
    }
41
42
    public function testCreateInstance()
43
    {
44
        $testObject = new Pdf();
45
        $this->assertInstanceOf(Pdf::class, $testObject);
46
    }
47
48
    public function testThatSomethingUsingTmpFolder()
49
    {
50
        $q = self::SHELL_ARG_QUOTE_REGEX;
51
        $testObject = new PdfSpy();
52
        $testObject->setTemporaryFolder(__DIR__);
53
54
        $testObject->getOutputFromHtml('<html></html>', ['footer-html' => 'footer']);
55
        $this->assertRegExp('/emptyBinary --lowquality --footer-html ' . $q . '.*' . $q . ' ' . $q . '.*' . $q . ' ' . $q . '.*' . $q . '/', $testObject->getLastCommand());
56
    }
57
58
    public function testThatSomethingUsingNonexistentTmpFolder()
59
    {
60
        $temporaryFolder = sys_get_temp_dir() . '/i-dont-exist';
61
62
        $testObject = new PdfSpy();
63
        $testObject->setTemporaryFolder($temporaryFolder);
64
65
        $output = $testObject->getOutputFromHtml('<html></html>', ['footer-html' => 'footer']);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
67
        $this->assertDirectoryExists($temporaryFolder);
68
    }
69
70
    public function testRemovesLocalFilesOnError()
71
    {
72
        $pdf = new PdfSpy();
73
        $method = new \ReflectionMethod($pdf, 'createTemporaryFile');
74
        $method->setAccessible(true);
75
        $method->invoke($pdf, 'test', $pdf->getDefaultExtension());
76
        $this->assertEquals(1, count($pdf->temporaryFiles));
77
        $this->expectException(Error::class);
78
        trigger_error('test error', E_USER_ERROR);
79
        $this->assertFileNotExists(reset($pdf->temporaryFiles));
80
    }
81
82
    /**
83
     * @dataProvider dataOptions
84
     */
85
    public function testOptions(array $options, $expectedRegex)
86
    {
87
        $testObject = new PdfSpy();
88
        $testObject->getOutputFromHtml('<html></html>', $options);
89
        $this->assertRegExp($expectedRegex, $testObject->getLastCommand());
90
    }
91
92
    public function dataOptions()
93
    {
94
        $q = self::SHELL_ARG_QUOTE_REGEX;
95
96
        return [
97
            // no options
98
            [
99
                [],
100
                '/emptyBinary --lowquality ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
101
            ],
102
            // just pass the given footer URL
103
            [
104
                ['footer-html' => 'http://google.com'],
105
                '/emptyBinary --lowquality --footer-html ' . $q . 'http:\/\/google\.com' . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
106
            ],
107
            // just pass the given footer file
108
            [
109
                ['footer-html' => __FILE__],
110
                '/emptyBinary --lowquality --footer-html ' . $q . preg_quote(__FILE__, '/') . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
111
            ],
112
            // save the given footer HTML string into a temporary file and pass that filename
113
            [
114
                ['footer-html' => 'footer'],
115
                '/emptyBinary --lowquality --footer-html ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
116
            ],
117
            // save the content of the given XSL URL to a file and pass that filename
118
            [
119
                ['xsl-style-sheet' => 'http://google.com'],
120
                '/emptyBinary --lowquality --xsl-style-sheet ' . $q . '.*\.xsl' . $q . ' ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
121
            ],
122
            // set toc options after toc argument
123
            [
124
                [
125
                    'grayscale'            => true,
126
                    'toc'                  => true,
127
                    'disable-dotted-lines' => true,
128
                ],
129
                '/emptyBinary --grayscale --lowquality toc --disable-dotted-lines ' . $q . '.*\.html' . $q . ' ' . $q . '.*\.pdf' . $q . '/',
130
            ],
131
        ];
132
    }
133
134
    public function testRemovesLocalFilesOnDestruct()
135
    {
136
        $pdf = new PdfSpy();
137
        $method = new \ReflectionMethod($pdf, 'createTemporaryFile');
138
        $method->setAccessible(true);
139
        $method->invoke($pdf, 'test', $pdf->getDefaultExtension());
140
        $this->assertEquals(1, count($pdf->temporaryFiles));
141
        $this->assertFileExists(reset($pdf->temporaryFiles));
142
        $pdf->__destruct();
143
        $this->assertFileNotExists(reset($pdf->temporaryFiles));
144
    }
145
}
146
147
class PdfSpy extends Pdf
148
{
149
    private $lastCommand;
150
151
    public function __construct()
152
    {
153
        parent::__construct('emptyBinary');
154
    }
155
156
    public function getLastCommand()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
157
    {
158
        return $this->lastCommand;
159
    }
160
161
    protected function executeCommand($command)
162
    {
163
        $this->lastCommand = $command;
164
165
        return [0, 'output', 'errorOutput'];
166
    }
167
168
    protected function checkOutput($output, $command)
169
    {
170
        //let's say everything went right
171
    }
172
173
    public function getOutput($input, array $options = [])
174
    {
175
        $filename = $this->createTemporaryFile(null, $this->getDefaultExtension());
176
        $this->generate($input, $filename, $options, true);
177
178
        return 'output';
179
    }
180
}
181