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

ElementTest::testContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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\Units;
31
32
use Smalot\PdfParser\Document;
33
use Smalot\PdfParser\Element;
34
use Smalot\PdfParser\Element\ElementArray;
35
use Smalot\PdfParser\Element\ElementBoolean;
36
use Smalot\PdfParser\Element\ElementDate;
37
use Smalot\PdfParser\Element\ElementName;
38
use Smalot\PdfParser\Element\ElementNull;
39
use Smalot\PdfParser\Element\ElementNumeric;
40
use Smalot\PdfParser\Element\ElementString;
41
use Smalot\PdfParser\Element\ElementXRef;
42
use Smalot\PdfParser\Header;
43
use Smalot\PdfParser\Test\TestCase;
44
45
class ElementTest extends TestCase
46
{
47
    public function testParse()
48
    {
49
        $document = new Document([]);
0 ignored issues
show
Unused Code introduced by
The call to Smalot\PdfParser\Document::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $document = /** @scrutinizer ignore-call */ new Document([]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
50
51
        // Only_values = false.
52
        $content = '/NameType /FlateDecode
53
        /Contents[4 0 R 42]/Fonts<</F1 41/F2 43>>/NullType
54
        null/StringType(hello)/DateType(D:20130901235555+02\'00\')/XRefType 2 0 R
55
        /NumericType 8/HexaType<0020>/BooleanType false';
56
        $offset = 0;
57
58
        $elements = Element::parse($content, $document, $offset, false);
59
60
        $this->assertTrue(\array_key_exists('NameType', $elements));
61
        $this->assertTrue($elements['NameType'] instanceof ElementName);
62
        $this->assertEquals('FlateDecode', $elements['NameType']->getContent());
63
64
        $this->assertTrue(\array_key_exists('Contents', $elements));
65
        $this->assertTrue($elements['Contents'] instanceof ElementArray);
66
        $this->assertTrue($elements['Contents']->contains(42));
67
68
        $this->assertTrue(\array_key_exists('Fonts', $elements));
69
        $this->assertTrue($elements['Fonts'] instanceof Header);
70
71
        $this->assertTrue(\array_key_exists('NullType', $elements));
72
        $this->assertTrue($elements['NullType'] instanceof ElementNull);
73
        $this->assertEquals('null', (string) $elements['NullType']);
74
75
        $this->assertTrue(\array_key_exists('StringType', $elements));
76
        $this->assertTrue($elements['StringType'] instanceof ElementString);
77
        $this->assertEquals('hello', $elements['StringType']->getContent());
78
79
        $this->assertTrue(\array_key_exists('DateType', $elements));
80
        $this->assertTrue($elements['DateType'] instanceof ElementDate);
81
82
        $this->assertTrue(\array_key_exists('XRefType', $elements));
83
        $this->assertTrue($elements['XRefType'] instanceof ElementXRef);
84
        $this->assertEquals('2_0', $elements['XRefType']->getId());
85
86
        $this->assertTrue(\array_key_exists('NumericType', $elements));
87
        $this->assertTrue($elements['NumericType'] instanceof ElementNumeric);
88
        $this->assertEquals('8', (string) $elements['NumericType']);
89
90
        $this->assertTrue(\array_key_exists('HexaType', $elements));
91
        $this->assertTrue($elements['HexaType'] instanceof ElementString);
92
        $this->assertEquals(' ', (string) $elements['HexaType']);
93
94
        $this->assertTrue(\array_key_exists('BooleanType', $elements));
95
        $this->assertTrue($elements['BooleanType'] instanceof ElementBoolean);
96
        $this->assertFalse($elements['BooleanType']->getContent());
97
98
        // Only_values = true.
99
        $content = '/NameType /FlateDecode';
100
        $offset = 0;
101
        $elements = Element::parse($content, $document, $offset, true);
102
        $this->assertEquals(2, \count($elements));
103
        $this->assertEquals(22, $offset);
104
105
        // Test error.
106
        $content = '/NameType /FlateDecode $$$';
107
        $offset = 0;
108
        $elements = Element::parse($content, $document, $offset, false);
109
        $this->assertEquals(1, \count($elements));
110
        $this->assertEquals(22, $offset);
111
        $this->assertEquals('NameType', key($elements));
112
        $this->assertTrue(current($elements) instanceof ElementName);
113
114
        $content = '/NameType $$$';
115
        $offset = 0;
116
        $elements = Element::parse($content, $document, $offset, false);
117
        $this->assertEquals(0, $offset);
118
        $this->assertEquals(0, \count($elements));
119
    }
120
121
    public function testGetContent()
122
    {
123
        $element = $this->getElementInstance(42);
124
        $content = $element->getContent();
125
        $this->assertEquals(42, $content);
126
127
        $element = $this->getElementInstance([4, 2]);
128
        $this->assertEquals(2, \count($element->getContent()));
129
    }
130
131
    public function testEquals()
132
    {
133
        $element = $this->getElementInstance(2);
134
135
        $this->assertTrue($element->equals(2));
136
    }
137
138
    public function testContains()
139
    {
140
        $element = $this->getElementInstance([$this->getElementInstance(4), $this->getElementInstance(2)]);
141
142
        $this->assertTrue($element->contains(2));
143
        $this->assertFalse($element->contains(8));
144
    }
145
146
    public function test__toString()
147
    {
148
        $this->assertEquals((string) $this->getElementInstance('2'), '2');
149
    }
150
}
151