Passed
Push — feature/switch-to-phpunit ( 82614e )
by Konrad
03:39
created

PDFObjectTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 74
c 0
b 0
f 0
dl 0
loc 192
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCommandsText() 0 100 1
A getPdfObjectInstance() 0 3 1
A testCleanContent() 0 43 1
A testGetSectionText() 0 32 1
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 * @date    2020-06-01
8
 *
9
 * @license LGPLv3
10
 * @url     <https://github.com/smalot/pdfparser>
11
 *
12
 *  PdfParser is a pdf library written in PHP, extraction oriented.
13
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
14
 *
15
 *  This program is free software: you can redistribute it and/or modify
16
 *  it under the terms of the GNU Lesser General Public License as published by
17
 *  the Free Software Foundation, either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  This program is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU Lesser General Public License for more details.
24
 *
25
 *  You should have received a copy of the GNU Lesser General Public License
26
 *  along with this program.
27
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
28
 */
29
30
namespace Tests\Smalot\PdfParser\Integration;
31
32
use Smalot\PdfParser\Document;
33
use Smalot\PdfParser\PDFObject;
34
use Smalot\PdfParser\Test\TestCase;
35
36
class PDFObjectTest extends TestCase
37
{
38
    const TYPE = 't';
39
40
    const OPERATOR = 'o';
41
42
    const COMMAND = 'c';
43
44
    protected function getPdfObjectInstance($document)
45
    {
46
        return new PDFObject($document);
47
    }
48
49
    public function testGetCommandsText()
50
    {
51
        $content = "/R14 30 Tf 0.999016 0 0 1 137.4
52
342.561 Tm
53
[(A)-168.854( BC D)-220.905(\\(E\\))20.905<20>]
54
TJ /R14 17.16 Tf <20> Tj
55
0.999014 0 0 1 336.84 319.161 Tm T* ( \x00m)Tj
56
/R14 20.04 Tf
57
ET Q
58
q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
59
BI";
60
61
        $offset = 0;
62
        $parts = $this->getPdfObjectInstance(new Document())->getCommandsText($content, $offset);
63
        $reference = [
64
            [
65
                self::TYPE => '/',
66
                self::OPERATOR => 'Tf',
67
                self::COMMAND => 'R14 30',
68
            ],
69
            [
70
                self::TYPE => '',
71
                self::OPERATOR => 'Tm',
72
                self::COMMAND => "0.999016 0 0 1 137.4\n342.561",
73
            ],
74
            [
75
                self::TYPE => '[',
76
                self::OPERATOR => 'TJ',
77
                self::COMMAND => [
78
                    [
79
                        self::TYPE => '(',
80
                        self::OPERATOR => '',
81
                        self::COMMAND => 'A',
82
                    ],
83
                    [
84
                        self::TYPE => 'n',
85
                        self::OPERATOR => '',
86
                        self::COMMAND => '-168.854',
87
                    ],
88
                    [
89
                        self::TYPE => '(',
90
                        self::OPERATOR => '',
91
                        self::COMMAND => ' BC D',
92
                    ],
93
                    [
94
                        self::TYPE => 'n',
95
                        self::OPERATOR => '',
96
                        self::COMMAND => '-220.905',
97
                    ],
98
                    [
99
                        self::TYPE => '(',
100
                        self::OPERATOR => '',
101
                        self::COMMAND => '\\(E\\)',
102
                    ],
103
                    [
104
                        self::TYPE => 'n',
105
                        self::OPERATOR => '',
106
                        self::COMMAND => '20.905',
107
                    ],
108
                    [
109
                        self::TYPE => '<',
110
                        self::OPERATOR => '',
111
                        self::COMMAND => '20',
112
                    ],
113
                ],
114
            ],
115
            [
116
                self::TYPE => '/',
117
                self::OPERATOR => 'Tf',
118
                self::COMMAND => 'R14 17.16',
119
            ],
120
            [
121
                self::TYPE => '<',
122
                self::OPERATOR => 'Tj',
123
                self::COMMAND => '20',
124
            ],
125
            [
126
                self::TYPE => '',
127
                self::OPERATOR => 'Tm',
128
                self::COMMAND => '0.999014 0 0 1 336.84 319.161',
129
            ],
130
            [
131
                self::TYPE => '',
132
                self::OPERATOR => 'T*',
133
                self::COMMAND => '',
134
            ],
135
            [
136
                self::TYPE => '(',
137
                self::OPERATOR => 'Tj',
138
                self::COMMAND => " \x00m",
139
            ],
140
            [
141
                self::TYPE => '/',
142
                self::OPERATOR => 'Tf',
143
                self::COMMAND => 'R14 20.04',
144
            ],
145
        ];
146
147
        $this->assertEquals($parts, $reference);
148
        $this->assertEquals(172, $offset);
149
    }
150
151
    public function testCleanContent()
152
    {
153
        $content = '/Shape <</MCID << /Font<8>>> BT >>BDC
154
Q
155
/CS0 cs 1 1 0  scn
156
1 i
157
/GS0 gs
158
BT
159
/TT0 1 Tf
160
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
161
(Modificatio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
162
EMC
163
(ABC) Tj
164
165
[ (a)-4.5(b)6(c)8.8 ( fsdfsdfsdf[]sd) ] TD
166
167
ET
168
/Shape <</MCID 2 >>BDC
169
q
170
0.03 841';
171
172
        $expected = '_____________________________________
173
Q
174
/CS0 cs 1 1 0  scn
175
1 i
176
/GS0 gs
177
BT
178
/TT0 1 Tf
179
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
180
(________________________________________________)Tj
181
___
182
(___) Tj
183
184
[_____________________________________] TD
185
186
ET
187
______________________
188
q
189
0.03 841';
190
191
        $cleaned = $this->getPdfObjectInstance(new Document())->cleanContent($content, '_');
192
193
        $this->assertEquals($cleaned, $expected);
194
    }
195
196
    public function testGetSectionText()
197
    {
198
        $content = '/Shape <</MCID 1 >>BDC
199
Q
200
/CS0 cs 1 1 0  scn
201
1 i
202
/GS0 gs
203
BT
204
/TT0 1 Tf
205
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
206
(Mod BT atio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
207
EMC
208
(ABC) Tj
209
210
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD
211
212
ET
213
/Shape <</MCID [BT] >>BDC BT /TT1 1.5 Tf (BT )Tj ET
214
q
215
0.03 841';
216
217
        $sections = $this->getPdfObjectInstance(new Document())->getSectionsText($content);
218
219
        $this->assertEquals(
220
            ['/TT0 1 Tf
221
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
222
(Mod BT atio[ns] au \(14\) septembre 2009 ET 2010)Tj
223
EMC
224
(ABC) Tj
225
226
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD ', '/TT1 1.5 Tf (BT )Tj '],
227
            $sections
228
        );
229
    }
230
}
231