Test Failed
Pull Request — master (#583)
by Konrad
04:39 queued 02:37
created

ElementTest::testEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
        $offset = 0;
62
63
        $elements = Element::parse($content, $document, $offset, false);
64
65
        $this->assertTrue(\array_key_exists('NameType', $elements));
66
        $this->assertTrue($elements['NameType'] instanceof ElementName);
67
        $this->assertEquals('FlateDecode', $elements['NameType']->getContent());
68
69
        $this->assertTrue(\array_key_exists('Contents', $elements));
70
        $this->assertTrue($elements['Contents'] instanceof ElementArray);
71
        $this->assertTrue($elements['Contents']->contains(42));
72
73
        $this->assertTrue(\array_key_exists('Fonts', $elements));
74
        $this->assertTrue($elements['Fonts'] instanceof Header);
75
76
        $this->assertTrue(\array_key_exists('NullType', $elements));
77
        $this->assertTrue($elements['NullType'] instanceof ElementNull);
78
        $this->assertEquals('null', (string) $elements['NullType']);
79
80
        $this->assertTrue(\array_key_exists('StringType', $elements));
81
        $this->assertTrue($elements['StringType'] instanceof ElementString);
82
        $this->assertEquals('hello', $elements['StringType']->getContent());
83
84
        $this->assertTrue(\array_key_exists('DateType', $elements));
85
        $this->assertTrue($elements['DateType'] instanceof ElementDate);
86
87
        $this->assertTrue(\array_key_exists('XRefType', $elements));
88
        $this->assertTrue($elements['XRefType'] instanceof ElementXRef);
89
        $this->assertEquals('2_0', $elements['XRefType']->getId());
90
91
        $this->assertTrue(\array_key_exists('NumericType', $elements));
92
        $this->assertTrue($elements['NumericType'] instanceof ElementNumeric);
93
        $this->assertEquals('8', (string) $elements['NumericType']);
94
95
        $this->assertTrue(\array_key_exists('HexaType', $elements));
96
        $this->assertTrue($elements['HexaType'] instanceof ElementString);
97
        $this->assertEquals(' ', (string) $elements['HexaType']);
98
99
        $this->assertTrue(\array_key_exists('BooleanType', $elements));
100
        $this->assertTrue($elements['BooleanType'] instanceof ElementBoolean);
101
        $this->assertFalse($elements['BooleanType']->getContent());
102
103
        // Only_values = true.
104
        $content = '/NameType /FlateDecode';
105
        $offset = 0;
106
        $elements = Element::parse($content, $document, $offset, true);
107
        $this->assertEquals(2, \count($elements));
108
        $this->assertEquals(22, $offset);
109
110
        // Test error.
111
        $content = '/NameType /FlateDecode $$$';
112
        $offset = 0;
113
        $elements = Element::parse($content, $document, $offset, false);
114
        $this->assertEquals(1, \count($elements));
115
        $this->assertEquals(22, $offset);
116
        $this->assertEquals('NameType', key($elements));
117
        $this->assertTrue(current($elements) instanceof ElementName);
118
119
        $content = '/NameType $$$';
120
        $offset = 0;
121
        $elements = Element::parse($content, $document, $offset, false);
122
        $this->assertEquals(0, $offset);
123
        $this->assertEquals(0, \count($elements));
124
    }
125
126
    public function testGetContent(): void
127
    {
128
        $element = $this->getElementInstance(42);
129
        $content = $element->getContent();
130
        $this->assertEquals(42, $content);
131
132
        $element = $this->getElementInstance([4, 2]);
133
        $this->assertEquals(2, \count($element->getContent()));
134
    }
135
136
    public function testEquals(): void
137
    {
138
        $element = $this->getElementInstance(2);
139
140
        $this->assertTrue($element->equals(2));
141
    }
142
143
    public function testContains(): void
144
    {
145
        $element = $this->getElementInstance([$this->getElementInstance(4), $this->getElementInstance(2)]);
146
147
        $this->assertTrue($element->contains(2));
148
        $this->assertFalse($element->contains(8));
149
    }
150
151
    public function testToString(): void
152
    {
153
        $this->assertEquals((string) $this->getElementInstance('2'), '2');
154
    }
155
}
156