1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file This file is part of the PdfParser library. |
5
|
|
|
* |
6
|
|
|
* @author Konrad Abicht <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @date 2020-06-01 |
9
|
|
|
* |
10
|
|
|
* @author Sébastien MALOT <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @date 2017-01-03 |
13
|
|
|
* |
14
|
|
|
* @license LGPLv3 |
15
|
|
|
* |
16
|
|
|
* @url <https://github.com/smalot/pdfparser> |
17
|
|
|
* |
18
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
19
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
20
|
|
|
* |
21
|
|
|
* This program is free software: you can redistribute it and/or modify |
22
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
23
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
24
|
|
|
* (at your option) any later version. |
25
|
|
|
* |
26
|
|
|
* This program is distributed in the hope that it will be useful, |
27
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
28
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
29
|
|
|
* GNU Lesser General Public License for more details. |
30
|
|
|
* |
31
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
32
|
|
|
* along with this program. |
33
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
namespace PHPUnitTests\Integration; |
37
|
|
|
|
38
|
|
|
use PHPUnitTests\TestCase; |
39
|
|
|
use Smalot\PdfParser\Element; |
40
|
|
|
use Smalot\PdfParser\Element\ElementArray; |
41
|
|
|
use Smalot\PdfParser\Element\ElementBoolean; |
42
|
|
|
use Smalot\PdfParser\Element\ElementDate; |
43
|
|
|
use Smalot\PdfParser\Element\ElementName; |
44
|
|
|
use Smalot\PdfParser\Element\ElementNull; |
45
|
|
|
use Smalot\PdfParser\Element\ElementNumeric; |
46
|
|
|
use Smalot\PdfParser\Element\ElementString; |
47
|
|
|
use Smalot\PdfParser\Element\ElementXRef; |
48
|
|
|
use Smalot\PdfParser\Header; |
49
|
|
|
|
50
|
|
|
class ElementTest extends TestCase |
51
|
|
|
{ |
52
|
|
|
public function testParse(): void |
53
|
|
|
{ |
54
|
|
|
$document = $this->getDocumentInstance(); |
55
|
|
|
|
56
|
|
|
// Only_values = false. |
57
|
|
|
$content = '/NameType /FlateDecode |
58
|
|
|
/Contents[4 0 R 42]/Fonts<</F1 41/F2 43>>/NullType |
59
|
|
|
null/StringType(hello)/DateType(D:20130901235555+02\'00\')/XRefType 2 0 R |
60
|
|
|
/NumericType 8/HexaType<0020>/BooleanType false |
61
|
|
|
/Space#20Test(Templates)/Hyphen#2DTest(Templates)'; |
62
|
|
|
$offset = 0; |
63
|
|
|
|
64
|
|
|
$elements = Element::parse($content, $document, $offset, false); |
65
|
|
|
|
66
|
|
|
$this->assertTrue(\array_key_exists('NameType', $elements)); |
67
|
|
|
$this->assertTrue($elements['NameType'] instanceof ElementName); |
68
|
|
|
$this->assertEquals('FlateDecode', $elements['NameType']->getContent()); |
69
|
|
|
|
70
|
|
|
$this->assertTrue(\array_key_exists('Contents', $elements)); |
71
|
|
|
$this->assertTrue($elements['Contents'] instanceof ElementArray); |
72
|
|
|
$this->assertTrue($elements['Contents']->contains(42)); |
73
|
|
|
|
74
|
|
|
$this->assertTrue(\array_key_exists('Fonts', $elements)); |
75
|
|
|
$this->assertTrue($elements['Fonts'] instanceof Header); |
76
|
|
|
|
77
|
|
|
$this->assertTrue(\array_key_exists('NullType', $elements)); |
78
|
|
|
$this->assertTrue($elements['NullType'] instanceof ElementNull); |
79
|
|
|
$this->assertEquals('null', (string) $elements['NullType']); |
80
|
|
|
|
81
|
|
|
$this->assertTrue(\array_key_exists('StringType', $elements)); |
82
|
|
|
$this->assertTrue($elements['StringType'] instanceof ElementString); |
83
|
|
|
$this->assertEquals('hello', $elements['StringType']->getContent()); |
84
|
|
|
|
85
|
|
|
$this->assertTrue(\array_key_exists('DateType', $elements)); |
86
|
|
|
$this->assertTrue($elements['DateType'] instanceof ElementDate); |
87
|
|
|
|
88
|
|
|
$this->assertTrue(\array_key_exists('XRefType', $elements)); |
89
|
|
|
$this->assertTrue($elements['XRefType'] instanceof ElementXRef); |
90
|
|
|
$this->assertEquals('2_0', $elements['XRefType']->getId()); |
91
|
|
|
|
92
|
|
|
$this->assertTrue(\array_key_exists('NumericType', $elements)); |
93
|
|
|
$this->assertTrue($elements['NumericType'] instanceof ElementNumeric); |
94
|
|
|
$this->assertEquals('8', (string) $elements['NumericType']); |
95
|
|
|
|
96
|
|
|
$this->assertTrue(\array_key_exists('HexaType', $elements)); |
97
|
|
|
$this->assertTrue($elements['HexaType'] instanceof ElementString); |
98
|
|
|
$this->assertEquals(' ', (string) $elements['HexaType']); |
99
|
|
|
|
100
|
|
|
$this->assertTrue(\array_key_exists('BooleanType', $elements)); |
101
|
|
|
$this->assertTrue($elements['BooleanType'] instanceof ElementBoolean); |
102
|
|
|
$this->assertFalse($elements['BooleanType']->getContent()); |
103
|
|
|
|
104
|
|
|
$this->assertTrue(\array_key_exists('Space Test', $elements)); |
105
|
|
|
|
106
|
|
|
$this->assertTrue(\array_key_exists('Hyphen-Test', $elements)); |
107
|
|
|
|
108
|
|
|
// Only_values = true. |
109
|
|
|
$content = '/NameType /FlateDecode'; |
110
|
|
|
$offset = 0; |
111
|
|
|
$elements = Element::parse($content, $document, $offset, true); |
112
|
|
|
$this->assertEquals(2, \count($elements)); |
113
|
|
|
$this->assertEquals(22, $offset); |
114
|
|
|
|
115
|
|
|
// Test error. |
116
|
|
|
$content = '/NameType /FlateDecode $$$'; |
117
|
|
|
$offset = 0; |
118
|
|
|
$elements = Element::parse($content, $document, $offset, false); |
119
|
|
|
$this->assertEquals(1, \count($elements)); |
120
|
|
|
$this->assertEquals(22, $offset); |
121
|
|
|
$this->assertEquals('NameType', key($elements)); |
122
|
|
|
$this->assertTrue(current($elements) instanceof ElementName); |
123
|
|
|
|
124
|
|
|
$content = '/NameType $$$'; |
125
|
|
|
$offset = 0; |
126
|
|
|
$elements = Element::parse($content, $document, $offset, false); |
127
|
|
|
$this->assertEquals(0, $offset); |
128
|
|
|
$this->assertEquals(0, \count($elements)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetContent(): void |
132
|
|
|
{ |
133
|
|
|
$element = $this->getElementInstance(42); |
134
|
|
|
$content = $element->getContent(); |
135
|
|
|
$this->assertEquals(42, $content); |
136
|
|
|
|
137
|
|
|
$element = $this->getElementInstance([4, 2]); |
138
|
|
|
$this->assertEquals(2, \count($element->getContent())); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testEquals(): void |
142
|
|
|
{ |
143
|
|
|
$element = $this->getElementInstance(2); |
144
|
|
|
|
145
|
|
|
$this->assertTrue($element->equals(2)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testContains(): void |
149
|
|
|
{ |
150
|
|
|
$element = $this->getElementInstance([$this->getElementInstance(4), $this->getElementInstance(2)]); |
151
|
|
|
|
152
|
|
|
$this->assertTrue($element->contains(2)); |
153
|
|
|
$this->assertFalse($element->contains(8)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testToString(): void |
157
|
|
|
{ |
158
|
|
|
$this->assertEquals((string) $this->getElementInstance('2'), '2'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|