Passed
Push — master ( a90ece...2f3895 )
by Jeremy
01:27 queued 11s
created

ElementArrayTest::testContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
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 Tests\Smalot\PdfParser\Integration\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 Test\Smalot\PdfParser\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
54
        $offset = 0;
55
        $element = ElementArray::parse(' / [ 4 2 ] ', $document, $offset);
56
        $this->assertFalse($element);
57
        $this->assertEquals(0, $offset);
58
59
        $offset = 0;
60
        $element = ElementArray::parse(' 0 [ 4 2 ] ', $document, $offset);
61
        $this->assertFalse($element);
62
        $this->assertEquals(0, $offset);
63
64
        $offset = 0;
65
        $element = ElementArray::parse(" 0 \n [ 4 2 ] ", $document, $offset);
66
        $this->assertFalse($element);
67
        $this->assertEquals(0, $offset);
68
69
        // Valid.
70
        $offset = 0;
71
        $element = ElementArray::parse(' [ 4 2 ] ', $document, $offset);
72
        $this->assertTrue($element->contains(2));
73
        $this->assertTrue($element->contains(4));
74
        $this->assertFalse($element->contains(8));
75
        $this->assertEquals(8, $offset);
76
77
        $offset = 0;
78
        $element = ElementArray::parse(' [ 4 2 ]', $document, $offset);
79
        $this->assertTrue($element->contains(2));
80
        $this->assertTrue($element->contains(4));
81
        $this->assertFalse($element->contains(8));
82
        $this->assertEquals(8, $offset);
83
84
        $offset = 0;
85
        $element = ElementArray::parse('[ 4 2 ]', $document, $offset);
86
        $this->assertTrue($element->contains(2));
87
        $this->assertTrue($element->contains(4));
88
        $this->assertFalse($element->contains(8));
89
        $this->assertEquals(7, $offset);
90
91
        $offset = 0;
92
        $element = ElementArray::parse(" \n [ 4 2 ] ", $document, $offset);
93
        $this->assertTrue($element->contains(2));
94
        $this->assertTrue($element->contains(4));
95
        $this->assertFalse($element->contains(8));
96
        $this->assertEquals(10, $offset);
97
    }
98
99
    public function testGetContent()
100
    {
101
        $val_4 = new ElementNumeric('4');
102
        $val_2 = new ElementNumeric('2');
103
        $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

103
        $element = new ElementArray(/** @scrutinizer ignore-type */ [$val_4, $val_2]);
Loading history...
104
105
        $content = $element->getContent();
106
        $this->assertCount(2, $content);
107
    }
108
109
    public function testContains()
110
    {
111
        $val_4 = new ElementNumeric('4');
112
        $val_2 = new ElementNumeric('2');
113
        $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

113
        $element = new ElementArray(/** @scrutinizer ignore-type */ [$val_4, $val_2]);
Loading history...
114
115
        $this->assertTrue($element->contains(2));
116
        $this->assertTrue($element->contains(4));
117
118
        $this->assertFalse($element->contains(8));
119
    }
120
121
    public function testResolveXRef()
122
    {
123
        // Document with text.
124
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
125
        $parser = $this->getParserInstance();
126
        $document = $parser->parseFile($filename);
127
        $object = $document->getObjectById('3_0');
128
        $kids = $object->get('Kids');
129
130
        $this->assertTrue($kids instanceof ElementArray);
131
        $this->assertCount(1, $kids->getContent());
132
133
        $pages = $kids->getContent();
134
        $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

134
        $this->assertTrue(reset(/** @scrutinizer ignore-type */ $pages) instanceof Page);
Loading history...
135
    }
136
137
    public function testGetDetails()
138
    {
139
        $document = $this->getDocumentInstance();
140
        $content = '<</Type/Page/Types[8]/Sizes[1 2 3 4 5 <</Subtype/XObject>> [8 [9 <</FontSize 10>>]]]>>';
141
        $details_reference = [
142
            'Type' => 'Page',
143
            'Types' => [
144
                8,
145
            ],
146
            'Sizes' => [
147
                1,
148
                2,
149
                3,
150
                4,
151
                5,
152
                [
153
                    'Subtype' => 'XObject',
154
                ],
155
                [
156
                    8,
157
                    [
158
                        9,
159
                        [
160
                            'FontSize' => 10,
161
                        ],
162
                    ],
163
                ],
164
            ],
165
        ];
166
        $header = Header::parse($content, $document);
167
        $details = $header->getDetails();
168
169
        $this->assertCount(3, $details);
170
        $this->assertEquals($details_reference, $details);
171
    }
172
173
    public function test__toString()
174
    {
175
        $val_4 = new ElementNumeric('4');
176
        $val_2 = new ElementNumeric('2');
177
        $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

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