Test Failed
Push — fix/failing-tests ( 0c7645 )
by Konrad
03:03
created

ElementStringTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testContains() 0 9 1
A testToString() 0 4 1
A testGetContent() 0 4 1
B testParse() 0 91 1
A testEquals() 0 13 1
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\ElementString;
40
41
class ElementStringTest extends TestCase
42
{
43
    public function testParse(): void
44
    {
45
        // Skipped.
46
        $offset = 0;
47
        $element = ElementString::parse('ABC', null, $offset);
48
        $this->assertFalse($element);
49
        $this->assertEquals(0, $offset);
50
51
        $offset = 0;
52
        $element = ElementString::parse(' [ (ABC) 5 6 ]', null, $offset);
53
        $this->assertFalse($element);
54
        $this->assertEquals(0, $offset);
55
56
        $offset = 0;
57
        $element = ElementString::parse(' << (invalid) >>', null, $offset);
58
        $this->assertFalse($element);
59
        $this->assertEquals(0, $offset);
60
61
        $offset = 0;
62
        $element = ElementString::parse(' / (FlateDecode) ', null, $offset);
63
        $this->assertFalse($element);
64
        $this->assertEquals(0, $offset);
65
66
        $offset = 0;
67
        $element = ElementString::parse(' 0 (FlateDecode) ', null, $offset);
68
        $this->assertFalse($element);
69
        $this->assertEquals(0, $offset);
70
71
        $offset = 0;
72
        $element = ElementString::parse(" 0 \n (FlateDecode) ", null, $offset);
73
        $this->assertFalse($element);
74
        $this->assertEquals(0, $offset);
75
76
        // Valid.
77
        $offset = 0;
78
        $element = ElementString::parse(' (Copyright) ', null, $offset);
79
        $this->assertEquals('Copyright', $element->getContent());
80
        $this->assertEquals(12, $offset);
81
82
        $offset = 0;
83
        $element = ElementString::parse(' (Copyright) ', null, $offset);
84
        $this->assertEquals('Copyright', $element->getContent());
85
        $this->assertEquals(12, $offset);
86
87
        $offset = 0;
88
        $element = ElementString::parse(' (Copyright)', null, $offset);
89
        $this->assertEquals('Copyright', $element->getContent());
90
        $this->assertEquals(12, $offset);
91
92
        $offset = 0;
93
        $element = ElementString::parse('(Copyright)', null, $offset);
94
        $this->assertEquals('Copyright', $element->getContent());
95
        $this->assertEquals(11, $offset);
96
97
        $offset = 0;
98
        $element = ElementString::parse('(Copy-right2)', null, $offset);
99
        $this->assertEquals('Copy-right2', $element->getContent());
100
        $this->assertEquals(13, $offset);
101
102
        $offset = 0;
103
        $element = ElementString::parse(" \n (Copyright) ", null, $offset);
104
        $this->assertEquals('Copyright', $element->getContent());
105
        $this->assertEquals(14, $offset);
106
107
        $offset = 0;
108
        $element = ElementString::parse('()', null, $offset);
109
        $this->assertEquals('', $element->getContent());
110
        $this->assertEquals(2, $offset);
111
112
        /*
113
         * Complex study case : Unicode + octal.
114
         */
115
        $offset = 0;
116
        $element = ElementString::parse('(ABC\\))', null, $offset);
117
        $this->assertEquals('ABC)', $element->getContent());
118
        $this->assertEquals(7, $offset);
119
120
        $offset = 0;
121
        $element = ElementString::parse("(\xFE\xFF\\000M)", null, $offset);
122
        $this->assertEquals('M', $element->getContent());
123
        $this->assertEquals(9, $offset);
124
125
        $offset = 0;
126
        $element = ElementString::parse('(<20>)', null, $offset);
127
        $this->assertEquals(' ', $element->getContent());
128
        $this->assertEquals(6, $offset);
129
130
        $offset = 0;
131
        $element = ElementString::parse('(Gutter\\ console\\ assembly)', null, $offset);
132
        $this->assertEquals('Gutter console assembly', $element->getContent());
133
        $this->assertEquals(27, $offset);
134
    }
135
136
    public function testGetContent(): void
137
    {
138
        $element = new ElementString('Copyright');
139
        $this->assertEquals('Copyright', $element->getContent());
140
    }
141
142
    public function testEquals(): void
143
    {
144
        $element = new ElementString('CopyRight');
145
        $this->assertTrue($element->equals('CopyRight'));
146
        $this->assertFalse($element->equals('Flatedecode'));
147
148
        $element = new ElementString('CopyRight2');
149
        $this->assertTrue($element->equals('CopyRight2'));
150
        $this->assertFalse($element->equals('CopyRight3'));
151
152
        $element = new ElementString('Flate-Decode2');
153
        $this->assertTrue($element->equals('Flate-Decode2'));
154
        $this->assertFalse($element->equals('Flate-Decode3'));
155
    }
156
157
    public function testContains(): void
158
    {
159
        $element = new ElementString('CopyRight');
160
        $this->assertTrue($element->contains('CopyRight'));
161
        $this->assertFalse($element->contains('Copyright'));
162
163
        $element = new ElementString('CopyRight2');
164
        $this->assertTrue($element->contains('CopyRight2'));
165
        $this->assertFalse($element->contains('CopyRight3'));
166
    }
167
168
    public function testToString(): void
169
    {
170
        $element = new ElementString('CopyRight');
171
        $this->assertEquals('CopyRight', (string) $element);
172
    }
173
}
174