Test Failed
Push — feature/switch-to-phpunit ( 82614e...d3db35 )
by Konrad
04:06
created

ElementTest::testParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

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

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
 * @date    2020-06-01
8
 *
9
 * @author  Sébastien MALOT <[email protected]>
10
 * @date    2017-01-03
11
 *
12
 * @license LGPLv3
13
 * @url     <https://github.com/smalot/pdfparser>
14
 *
15
 *  PdfParser is a pdf library written in PHP, extraction oriented.
16
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
17
 *
18
 *  This program is free software: you can redistribute it and/or modify
19
 *  it under the terms of the GNU Lesser General Public License as published by
20
 *  the Free Software Foundation, either version 3 of the License, or
21
 *  (at your option) any later version.
22
 *
23
 *  This program is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU Lesser General Public License for more details.
27
 *
28
 *  You should have received a copy of the GNU Lesser General Public License
29
 *  along with this program.
30
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
31
 */
32
33
namespace Tests\Smalot\PdfParser\Integration;
34
35
use Smalot\PdfParser\Document;
36
use Smalot\PdfParser\Element;
37
use Smalot\PdfParser\Element\ElementArray;
38
use Smalot\PdfParser\Element\ElementBoolean;
39
use Smalot\PdfParser\Element\ElementDate;
40
use Smalot\PdfParser\Element\ElementName;
41
use Smalot\PdfParser\Element\ElementNull;
42
use Smalot\PdfParser\Element\ElementNumeric;
43
use Smalot\PdfParser\Element\ElementString;
44
use Smalot\PdfParser\Element\ElementXRef;
45
use Smalot\PdfParser\Header;
46
use Smalot\PdfParser\Test\TestCase;
47
48
class ElementTest extends TestCase
49
{
50
    public function testParse()
51
    {
52
        $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

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