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

ElementArrayTest::testGetContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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-02
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 TestsIntegration\Element;
34
35
use Smalot\PdfParser\Document;
36
use Smalot\PdfParser\Element\ElementArray;
37
use Smalot\PdfParser\Element\ElementNumeric;
38
use Smalot\PdfParser\Header;
39
use Smalot\PdfParser\Page;
40
use Smalot\PdfParser\Test\TestCase;
41
42
class ElementArrayTest extends TestCase
43
{
44
    public function testParse()
45
    {
46
        $document = $this->getDocumentInstance();
47
48
        // Skipped.
49
        $offset = 0;
50
        $element = ElementArray::parse('ABC', $document, $offset);
51
        $this->assertFalse($element);
0 ignored issues
show
Bug introduced by
It seems like $element can also be of type Smalot\PdfParser\Element\ElementArray; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

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

51
        $this->assertFalse(/** @scrutinizer ignore-type */ $element);
Loading history...
52
        $this->assertEquals(0, $offset);
53
        $offset = 0;
54
        $element = ElementArray::parse(' / [ 4 2 ] ', $document, $offset);
55
        $this->assertFalse($element);
56
        $this->assertEquals(0, $offset);
57
        $offset = 0;
58
        $element = ElementArray::parse(' 0 [ 4 2 ] ', $document, $offset);
59
        $this->assertFalse($element);
60
        $this->assertEquals(0, $offset);
61
        $offset = 0;
62
        $element = ElementArray::parse(" 0 \n [ 4 2 ] ", $document, $offset);
63
        $this->assertFalse($element);
64
        $this->assertEquals(0, $offset);
65
66
        // Valid.
67
        $offset = 0;
68
        $element = ElementArray::parse(' [ 4 2 ] ', $document, $offset);
69
        $this->assertTrue($element->contains(2));
70
        $this->assertTrue($element->contains(4));
71
        $this->assertFalse($element->contains(8));
72
        $this->assertEquals(8, $offset);
73
74
        $offset = 0;
75
        $element = ElementArray::parse(' [ 4 2 ]', $document, $offset);
76
        $this->assertTrue($element->contains(2));
77
        $this->assertTrue($element->contains(4));
78
        $this->assertFalse($element->contains(8));
79
        $this->assertEquals(8, $offset);
80
81
        $offset = 0;
82
        $element = ElementArray::parse('[ 4 2 ]', $document, $offset);
83
        $this->assertTrue($element->contains(2));
84
        $this->assertTrue($element->contains(4));
85
        $this->assertFalse($element->contains(8));
86
        $this->assertEquals(7, $offset);
87
88
        $offset = 0;
89
        $element = ElementArray::parse(" \n [ 4 2 ] ", $document, $offset);
90
        $this->assertTrue($element->contains(2));
91
        $this->assertTrue($element->contains(4));
92
        $this->assertFalse($element->contains(8));
93
        $this->assertEquals(10, $offset);
94
    }
95
96
    public function testGetContent()
97
    {
98
        $val_4 = new ElementNumeric('4');
99
        $val_2 = new ElementNumeric('2');
100
        $element = new ElementArray([$val_4, $val_2]);
0 ignored issues
show
Bug introduced by
array($val_4, $val_2) of type array<integer,Smalot\Pdf...Element\ElementNumeric> is incompatible with the type string expected by parameter $value of Smalot\PdfParser\Element...entArray::__construct(). ( Ignorable by Annotation )

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

100
        $element = new ElementArray(/** @scrutinizer ignore-type */ [$val_4, $val_2]);
Loading history...
101
102
        $content = $element->getContent();
103
        $this->assertCount(2, $content);
104
    }
105
106
    public function testContains()
107
    {
108
        $val_4 = new ElementNumeric('4');
109
        $val_2 = new ElementNumeric('2');
110
        $element = new ElementArray([$val_4, $val_2]);
0 ignored issues
show
Bug introduced by
array($val_4, $val_2) of type array<integer,Smalot\Pdf...Element\ElementNumeric> is incompatible with the type string expected by parameter $value of Smalot\PdfParser\Element...entArray::__construct(). ( Ignorable by Annotation )

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

110
        $element = new ElementArray(/** @scrutinizer ignore-type */ [$val_4, $val_2]);
Loading history...
111
112
        $this->assertTrue($element->contains(2));
113
        $this->assertTrue($element->contains(4));
114
115
        $this->assertFalse($element->contains(8));
116
    }
117
118
    public function testResolveXRef()
119
    {
120
        // Document with text.
121
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
122
        $parser = $this->getParserInstance();
123
        $document = $parser->parseFile($filename);
124
        $object = $document->getObjectById('3_0');
125
        $kids = $object->get('Kids');
126
127
        $this->assertTrue($kids instanceof ElementArray);
128
        $this->assertCount(1, $kids->getContent());
129
130
        $pages = $kids->getContent();
131
        $this->assertTrue(reset($pages) instanceof Page);
0 ignored issues
show
Bug introduced by
It seems like $pages can also be of type Countable and Traversable; however, parameter $array of reset() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

131
        $this->assertTrue(reset(/** @scrutinizer ignore-type */ $pages) instanceof Page);
Loading history...
132
    }
133
134
    public function testGetDetails()
135
    {
136
        $document = $this->getDocumentInstance();
137
        $content = '<</Type/Page/Types[8]/Sizes[1 2 3 4 5 <</Subtype/XObject>> [8 [9 <</FontSize 10>>]]]>>';
138
        $details_reference = [
139
            'Type' => 'Page',
140
            'Types' => [
141
                8,
142
            ],
143
            'Sizes' => [
144
                1,
145
                2,
146
                3,
147
                4,
148
                5,
149
                [
150
                    'Subtype' => 'XObject',
151
                ],
152
                [
153
                    8,
154
                    [
155
                        9,
156
                        [
157
                            'FontSize' => 10,
158
                        ],
159
                    ],
160
                ],
161
            ],
162
        ];
163
        $header = Header::parse($content, $document);
164
        $details = $header->getDetails();
165
166
        $this->assertCount(3, $details);
167
        $this->assertEquals($details_reference, $details);
168
    }
169
170
    public function test__toString()
171
    {
172
        $val_4 = new ElementNumeric('4');
173
        $val_2 = new ElementNumeric('2');
174
        $element = new ElementArray([$val_4, $val_2]);
0 ignored issues
show
Bug introduced by
array($val_4, $val_2) of type array<integer,Smalot\Pdf...Element\ElementNumeric> is incompatible with the type string expected by parameter $value of Smalot\PdfParser\Element...entArray::__construct(). ( Ignorable by Annotation )

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

174
        $element = new ElementArray(/** @scrutinizer ignore-type */ [$val_4, $val_2]);
Loading history...
175
        $this->assertEquals('4,2', (string) $element);
176
177
        $document = $this->getDocumentInstance();
178
        $element = ElementArray::parse(' [ 4 2 ]', $document);
179
        $this->assertEquals('4,2', (string) $element);
180
    }
181
}
182