Passed
Branch master (f7fac8)
by Sebastien
02:47
created

PDFObject::testGetCommandsText()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 102
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 72
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 102
rs 8.6109

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
8
 * @date    2017-01-03
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
31
namespace Smalot\PdfParser\Tests\Units;
32
33
use mageekguy\atoum;
34
35
/**
36
 * Class PDFObject
37
 *
38
 * @package Smalot\PdfParser\Tests\Units
39
 */
40
class PDFObject extends atoum\test
41
{
42
    const TYPE = 't';
43
44
    const OPERATOR = 'o';
45
46
    const COMMAND = 'c';
47
48
    public function testGetTextParts()
49
    {
50
    }
51
52
//    public function testGetCommandsImage()
53
//    {
54
//        $content = "/CS/RGB
55
///W 22
56
///H 1
57
///BPC 8
58
///F/Fl
59
///DP<</Predictor 15
60
///Columns 22
61
///Colors 3>>
62
//ID \x00\x50c\x63
63
//EI Q
64
//q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
65
//BI
66
//";
67
//
68
//        $document  = new \Smalot\PdfParser\Document();
69
//        $object    = new \Smalot\PdfParser\PDFObject($document);
70
//        $offset    = 0;
71
//        $parts     = $object->getCommandsImage($content, $offset);
72
//        $reference = array(
73
//            array(
74
//                self::TYPE => '/',
75
//                self::OPERATOR => 'CS',
76
//                self::COMMAND => 'RGB',
77
//            ),
78
//            array(
79
//                self::TYPE => '/',
80
//                self::OPERATOR => 'W',
81
//                self::COMMAND => '22',
82
//            ),
83
//            array(
84
//                self::TYPE => '/',
85
//                self::OPERATOR => 'H',
86
//                self::COMMAND => '1',
87
//            ),
88
//            array(
89
//                self::TYPE => '/',
90
//                self::OPERATOR => 'BPC',
91
//                self::COMMAND => '8',
92
//            ),
93
//            array(
94
//                self::TYPE => '/',
95
//                self::OPERATOR => 'F',
96
//                self::COMMAND => 'Fl',
97
//            ),
98
//            array(
99
//                self::TYPE => 'struct',
100
//                self::OPERATOR => 'DP',
101
//                self::COMMAND => array(
102
//                    array(
103
//                        self::TYPE => '/',
104
//                        self::OPERATOR => 'Predictor',
105
//                        self::COMMAND => '15',
106
//                    ),
107
//                    array(
108
//                        self::TYPE => '/',
109
//                        self::OPERATOR => 'Columns',
110
//                        self::COMMAND => '22',
111
//                    ),
112
//                    array(
113
//                        self::TYPE => '/',
114
//                        self::OPERATOR => 'Colors',
115
//                        self::COMMAND => '3',
116
//                    ),
117
//                ),
118
//            ),
119
//            array(
120
//                self::TYPE => '',
121
//                self::OPERATOR => 'ID',
122
//                self::COMMAND => "\x00\x50c\x63",
123
//            ),
124
//        );
125
//
126
//        $this->assert->array($parts)->isEqualTo($reference);
127
//        $this->assert->integer($offset)->isEqualTo(83);
128
//    }
129
130
    public function testGetCommandsText()
131
    {
132
        $content = "/R14 30 Tf 0.999016 0 0 1 137.4
133
342.561 Tm
134
[(A)-168.854( BC D)-220.905(\\(E\\))20.905<20>]
135
TJ /R14 17.16 Tf <20> Tj
136
0.999014 0 0 1 336.84 319.161 Tm T* ( \x00m)Tj
137
/R14 20.04 Tf
138
ET Q
139
q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
140
BI";
141
142
        $document  = new \Smalot\PdfParser\Document();
143
        $object    = new \Smalot\PdfParser\PDFObject($document);
144
        $offset    = 0;
145
        $parts     = $object->getCommandsText($content, $offset);
146
        $reference = array(
147
            array(
148
                self::TYPE     => '/',
149
                self::OPERATOR => 'Tf',
150
                self::COMMAND  => 'R14 30',
151
            ),
152
            array(
153
                self::TYPE     => '',
154
                self::OPERATOR => 'Tm',
155
                self::COMMAND  => "0.999016 0 0 1 137.4\n342.561",
156
            ),
157
            array(
158
                self::TYPE     => '[',
159
                self::OPERATOR => 'TJ',
160
                self::COMMAND  => array(
161
                    array(
162
                        self::TYPE     => '(',
163
                        self::OPERATOR => '',
164
                        self::COMMAND  => 'A',
165
                    ),
166
                    array(
167
                        self::TYPE     => 'n',
168
                        self::OPERATOR => '',
169
                        self::COMMAND  => '-168.854',
170
                    ),
171
                    array(
172
                        self::TYPE     => '(',
173
                        self::OPERATOR => '',
174
                        self::COMMAND  => ' BC D',
175
                    ),
176
                    array(
177
                        self::TYPE     => 'n',
178
                        self::OPERATOR => '',
179
                        self::COMMAND  => '-220.905',
180
                    ),
181
                    array(
182
                        self::TYPE     => '(',
183
                        self::OPERATOR => '',
184
                        self::COMMAND  => '\\(E\\)',
185
                    ),
186
                    array(
187
                        self::TYPE     => 'n',
188
                        self::OPERATOR => '',
189
                        self::COMMAND  => '20.905',
190
                    ),
191
                    array(
192
                        self::TYPE     => '<',
193
                        self::OPERATOR => '',
194
                        self::COMMAND  => '20',
195
                    ),
196
                ),
197
            ),
198
            array(
199
                self::TYPE     => '/',
200
                self::OPERATOR => 'Tf',
201
                self::COMMAND  => 'R14 17.16',
202
            ),
203
            array(
204
                self::TYPE     => '<',
205
                self::OPERATOR => 'Tj',
206
                self::COMMAND  => '20',
207
            ),
208
            array(
209
                self::TYPE     => '',
210
                self::OPERATOR => 'Tm',
211
                self::COMMAND  => '0.999014 0 0 1 336.84 319.161',
212
            ),
213
            array(
214
                self::TYPE     => '',
215
                self::OPERATOR => 'T*',
216
                self::COMMAND  => '',
217
            ),
218
            array(
219
                self::TYPE     => '(',
220
                self::OPERATOR => 'Tj',
221
                self::COMMAND  => " \x00m",
222
            ),
223
            array(
224
                self::TYPE     => '/',
225
                self::OPERATOR => 'Tf',
226
                self::COMMAND  => 'R14 20.04',
227
            ),
228
        );
229
230
        $this->assert->array($parts)->isEqualTo($reference);
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\PDFObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
231
        $this->assert->integer($offset)->isEqualTo(172);
232
    }
233
234
    public function testCleanContent()
235
    {
236
        $content = '/Shape <</MCID << /Font<8>>> BT >>BDC
237
Q
238
/CS0 cs 1 1 0  scn
239
1 i
240
/GS0 gs
241
BT
242
/TT0 1 Tf
243
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
244
(Modificatio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
245
EMC
246
(ABC) Tj
247
248
[ (a)-4.5(b)6(c)8.8 ( fsdfsdfsdf[]sd) ] TD
249
250
ET
251
/Shape <</MCID 2 >>BDC
252
q
253
0.03 841';
254
255
        $expected = '_____________________________________
256
Q
257
/CS0 cs 1 1 0  scn
258
1 i
259
/GS0 gs
260
BT
261
/TT0 1 Tf
262
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
263
(________________________________________________)Tj
264
___
265
(___) Tj
266
267
[_____________________________________] TD
268
269
ET
270
______________________
271
q
272
0.03 841';
273
274
        $document = new \Smalot\PdfParser\Document();
275
        $object   = new \Smalot\PdfParser\PDFObject($document);
276
        $cleaned  = $object->cleanContent($content, '_');
277
278
        $this->assert->string($cleaned)->length->isEqualTo(strlen($content));
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\PDFObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
279
        $this->assert->string($cleaned)->isEqualTo($expected);
280
    }
281
282
    public function testGetSectionText()
283
    {
284
        $content = '/Shape <</MCID 1 >>BDC
285
Q
286
/CS0 cs 1 1 0  scn
287
1 i
288
/GS0 gs
289
BT
290
/TT0 1 Tf
291
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
292
(Mod BT atio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
293
EMC
294
(ABC) Tj
295
296
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD
297
298
ET
299
/Shape <</MCID [BT] >>BDC BT /TT1 1.5 Tf (BT )Tj ET
300
q
301
0.03 841';
302
303
        $document = new \Smalot\PdfParser\Document();
304
        $object   = new \Smalot\PdfParser\PDFObject($document);
305
        $sections = $object->getSectionsText($content);
0 ignored issues
show
Unused Code introduced by
The assignment to $sections is dead and can be removed.
Loading history...
306
307
//        $this->assert->string($cleaned)->length->isEqualTo(strlen($content));
308
//        $this->assert->string($cleaned)->isEqualTo($expected);
309
    }
310
}
311