Passed
Pull Request — master (#716)
by Konrad
03:03
created

ElementHexaTest::testParse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 60
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 88
rs 8.8727

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 This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 *
8
 * @date    2020-06-02
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\Element;
37
38
use PHPUnitTests\TestCase;
39
use Smalot\PdfParser\Element\ElementDate;
40
use Smalot\PdfParser\Element\ElementHexa;
41
use Smalot\PdfParser\Element\ElementString;
42
43
class ElementHexaTest extends TestCase
44
{
45
    public function testParse(): void
46
    {
47
        // Skipped.
48
        $offset = 0;
49
        $element = ElementHexa::parse('ABC', null, $offset);
50
        $this->assertFalse($element);
51
        $this->assertEquals(0, $offset);
52
53
        $offset = 0;
54
        $element = ElementHexa::parse(' [ <0020> 5 6 ]', null, $offset);
55
        $this->assertFalse($element);
56
        $this->assertEquals(0, $offset);
57
58
        $offset = 0;
59
        $element = ElementHexa::parse(' << <0020> >>', null, $offset);
60
        $this->assertFalse($element);
61
        $this->assertEquals(0, $offset);
62
63
        $offset = 0;
64
        $element = ElementHexa::parse(' / <0020> ', null, $offset);
65
        $this->assertFalse($element);
66
        $this->assertEquals(0, $offset);
67
68
        $offset = 0;
69
        $element = ElementHexa::parse(' 0 <0020> ', null, $offset);
70
        $this->assertFalse($element);
71
        $this->assertEquals(0, $offset);
72
73
        $offset = 0;
74
        $element = ElementHexa::parse(" 0 \n <0020> ", null, $offset);
75
        $this->assertFalse($element);
76
        $this->assertEquals(0, $offset);
77
78
        // Valid.
79
        $offset = 0;
80
        $element = ElementHexa::parse(' <0020> ', null, $offset);
81
        $this->assertEquals(' ', $element->getContent());
82
        $this->assertEquals(7, $offset);
83
84
        $offset = 0;
85
        $element = ElementHexa::parse(' <0020> ', null, $offset);
86
        $this->assertEquals(' ', $element->getContent());
87
        $this->assertEquals(7, $offset);
88
89
        $offset = 0;
90
        $element = ElementHexa::parse(' <0020>', null, $offset);
91
        $this->assertEquals(' ', $element->getContent());
92
        $this->assertEquals(7, $offset);
93
94
        $offset = 0;
95
        $element = ElementHexa::parse('<0020>', null, $offset);
96
        $this->assertEquals(' ', $element->getContent());
97
        $this->assertEquals(6, $offset);
98
99
        $offset = 0;
100
        $element = ElementHexa::parse(" \n <0020> ", null, $offset);
101
        $this->assertEquals(' ', $element->getContent());
102
        $this->assertEquals(9, $offset);
103
104
        $offset = 0;
105
        $element = ElementHexa::parse(" \n <5465616d204d616e6167656d656e742053797374656d73> ", null, $offset);
106
        $this->assertEquals('Team Management Systems', $element->getContent());
107
        $this->assertEquals(51, $offset);
108
109
        $offset = 0;
110
        $element = ElementHexa::parse(" \n <5265706f72744275696c646572> ", null, $offset);
111
        $this->assertTrue($element instanceof ElementString);
112
        $this->assertEquals('ReportBuilder', $element->getContent());
113
        $this->assertEquals(31, $offset);
114
115
        $offset = 0;
116
        $element = ElementHexa::parse(" \n <443a3230313331323137313334303435303027303027> ", null, $offset);
117
        $this->assertTrue($element instanceof ElementDate);
118
        $this->assertEquals('2013-12-17T13:40:45+00:00', (string) $element);
119
        $this->assertEquals(49, $offset);
120
121
        // Test that a hexadecimal string 'dirty' with extra characters
122
        // such as newlines or spaces is properly decoded
123
        $element = ElementHexa::decode(' <feff007000610073007100750061002c0020007000720069006d00610076006500720061002c0020
124
00720065007  30075007200720065007a0069006f006e0065002c0020006600650073007400610020
125
0063007200690073007400690061006e0061002c002000670065007300f9002c00200075006f0076
126
0061002000640069 & 002000630069006f00630063006f006c00610074006100 Y 2c00200063006f006e
127
00690067006c00690065007400740069002c0020007000750 / 06c00630069006e0069002c00200070
128
00610073007100750061006c0065  002c002000630061006d00700061006e0065002c002000640069
129
006e006100200072006500620075006300630069002c00200075006f007600610020006400690020
130
007000610  073007100750061002c0020> ');
131
132
        $this->assertEquals('pasqua, primavera, resurrezione, festa cristiana, gesù, uova di cioccolata, coniglietti, pulcini, pasquale, campane, dina rebucci, uova di pasqua, ', $element);
133
    }
134
135
    /**
136
     * Closing round bracket encoded in hexadecimal format breaks parsing - string is truncated.
137
     *
138
     * @see https://github.com/smalot/pdfparser/issues/715
139
     */
140
    public function testIssue715(): void
141
    {
142
        $offset = 0;
143
        $testString = '()\\';
144
        $element = ElementHexa::parse('<'.bin2hex($testString).'>', null, $offset);
145
        $this->assertEquals($testString, (string) $element);
146
    }
147
}
148