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