-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathTcpdfSmokeTest.php
More file actions
109 lines (97 loc) · 3.61 KB
/
Copy pathTcpdfSmokeTest.php
File metadata and controls
109 lines (97 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
/**
* Seed smoke test for the TCPDF compatibility facade.
*
* Exercises the thinnest vertical slice of the legacy API
* (construct, AddPage, setFont, Cell, Write, Output/getPDFData)
* and asserts that a real, parseable PDF document is produced.
*
* @package com.tecnick.tcpdf
*/
use PHPUnit\Framework\TestCase;
class TcpdfSmokeTest extends TestCase
{
/**
* Build the document used by most assertions in this suite.
*/
private function buildDocument(): TCPDF
{
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setCreator('tcpdf-facade-test');
$pdf->setAuthor('PHPUnit');
$pdf->setTitle('Facade Smoke Test');
$pdf->setSubject('seed smoke test');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setMargins(15, 27, 15);
$pdf->setAutoPageBreak(true, 25);
$pdf->setFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->Cell(0, 0, 'Hello facade', 0, 1, 'L');
$pdf->Write(0, 'Second line of text.', '', false, 'L', true);
return $pdf;
}
public function testProducesParseablePdf(): void
{
$pdf = $this->buildDocument();
$raw = $pdf->getPDFData();
$this->assertIsString($raw);
$this->assertNotSame('', $raw, 'PDF output must not be empty');
$this->assertStringStartsWith('%PDF-', $raw, 'output must start with the PDF header');
$this->assertStringContainsString('%%EOF', $raw, 'output must contain the PDF trailer');
// The document must be parseable by an independent parser.
$tmpfile = tempnam(sys_get_temp_dir(), 'tcpdf_smoke_');
$this->assertNotFalse($tmpfile);
file_put_contents($tmpfile, $raw);
try {
$info = [];
$code = 1;
exec('pdfinfo ' . escapeshellarg($tmpfile) . ' 2>&1', $info, $code);
$this->assertSame(0, $code, 'pdfinfo must parse the document: ' . implode("\n", $info));
$this->assertMatchesRegularExpression(
'/^Pages:\s+1$/m',
implode("\n", $info),
'document must contain exactly one page',
);
} finally {
unlink($tmpfile);
}
}
public function testTextIsRendered(): void
{
$pdf = $this->buildDocument();
$raw = $pdf->getPDFData();
$tmpfile = tempnam(sys_get_temp_dir(), 'tcpdf_smoke_');
$this->assertNotFalse($tmpfile);
file_put_contents($tmpfile, $raw);
try {
$lines = [];
$code = 1;
exec('pdftotext ' . escapeshellarg($tmpfile) . ' - 2>/dev/null', $lines, $code);
$this->assertSame(0, $code, 'pdftotext must extract text from the document');
$text = implode("\n", $lines);
$this->assertStringContainsString('Hello facade', $text);
$this->assertStringContainsString('Second line of text.', $text);
} finally {
unlink($tmpfile);
}
}
public function testPageStateIsTracked(): void
{
$pdf = $this->buildDocument();
$this->assertSame(1, $pdf->getPage());
$this->assertSame(1, $pdf->getNumPages());
$this->assertSame(1, $pdf->PageNo());
$pdf->AddPage();
$this->assertSame(2, $pdf->getPage());
$this->assertSame(2, $pdf->getNumPages());
}
public function testOutputStringDestination(): void
{
$pdf = $this->buildDocument();
$raw = $pdf->Output('smoke.pdf', 'S');
$this->assertIsString($raw);
$this->assertStringStartsWith('%PDF-', $raw);
}
}