Passed
Push — master ( a4bb6d...8b8a15 )
by Konrad
06:35
created

HeaderTest::testInitInvalidElement()   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
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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-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\Element;
36
use Smalot\PdfParser\Element\ElementMissing;
37
use Smalot\PdfParser\Element\ElementName;
38
use Smalot\PdfParser\Header;
39
use Smalot\PdfParser\Page;
40
use Smalot\PdfParser\PDFObject;
41
use Tests\Smalot\PdfParser\TestCase;
42
43
/**
44
 * Class Header
45
 */
46
class HeaderTest extends TestCase
47
{
48
    /**
49
     * Checks that init function is called for each element.
50
     */
51
    public function testInitHappyPath()
52
    {
53
        $element = $this->createMock(Element::class);
54
        $element->expects($this->exactly(1))->method('init');
55
56
        $fixture = new Header([$element]);
57
        $fixture->init();
58
    }
59
60
    /**
61
     * Checks buggy behavior if an element was given which is not of type Element.
62
     *
63
     * Problem was, it always called $element::init(), even if its not an object at all.
64
     *
65
     * @see https://github.com/smalot/pdfparser/issues/367
66
     *
67
     * @doesNotPerformAssertions
68
     */
69
    public function testInitInvalidElement()
70
    {
71
        $element = false;
72
73
        $fixture = new Header([$element]);
0 ignored issues
show
Bug introduced by
array($element) of type array<integer,false> is incompatible with the type Smalot\PdfParser\Element[] expected by parameter $elements of Smalot\PdfParser\Header::__construct(). ( Ignorable by Annotation )

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

73
        $fixture = new Header(/** @scrutinizer ignore-type */ [$element]);
Loading history...
74
        $fixture->init();
75
    }
76
77
    public function testParse()
78
    {
79
        $document = $this->getDocumentInstance();
80
81
        $content = '<</Type/Page/SubType/Text>>foo';
82
        $position = 0;
83
        $header = Header::parse($content, $document, $position);
84
85
        $this->assertTrue($header instanceof Header);
86
        $this->assertEquals(27, $position);
87
        $this->assertEquals(2, \count($header->getElements()));
88
89
        // No header to parse
90
        $this->assertEquals('Page', (string) $header->get('Type'));
91
        $content = 'foo';
92
        $position = 0;
93
        $header = Header::parse($content, $document, $position);
94
95
        $this->assertTrue($header instanceof Header);
96
        $this->assertEquals(0, $position);
97
        $this->assertEquals(0, \count($header->getElements()));
98
99
        $position = 0;
100
        $content = "<</CreationDate(D:20100309184803+01'00')/Author(Utilisateur)/Creator(PScript5.dll Version 5.2.2)/Producer(Acrobat Distiller 7.0.5 \(Windows\))/ModDate(D:20100310104810+01'00')/Title(Microsoft Word - CLEMI.docx)>>";
101
        Header::parse($content, $document, $position);
102
        $this->assertEquals(212, $position);
103
104
        $position = 0;
105
        $content = '[5 0 R ] foo';
106
        $header = Header::parse($content, $document, $position);
107
        $this->assertEquals(8, $position);
108
        $this->assertEquals(1, \count($header->getElements()));
109
    }
110
111
    public function testGetElements()
112
    {
113
        $document = $this->getDocumentInstance();
114
115
        $content = '<</Type/Page/Subtype/Text>>foo';
116
        $position = 0;
117
        $header = Header::parse($content, $document, $position);
118
119
        $elements = $header->getElements();
120
        $this->assertEquals(2, \count($elements));
121
        $this->assertTrue(current($elements) instanceof ElementName);
122
123
        $types = $header->getElementTypes();
124
        $this->assertTrue(\is_array($types));
125
        $this->assertEquals(ElementName::class, $types['Type']);
126
        $this->assertEquals(ElementName::class, $types['Subtype']);
127
    }
128
129
    public function testHas()
130
    {
131
        $document = $this->getDocumentInstance();
132
133
        $content = '<</Type/Page/SubType/Text/Font 5 0 R>>foo';
134
        $position = 0;
135
        $header = Header::parse($content, $document, $position);
136
137
        $this->assertTrue($header->has('Type'));
138
        $this->assertTrue($header->has('SubType'));
139
        $this->assertTrue($header->has('Font'));
140
        $this->assertFalse($header->has('Text'));
141
    }
142
143
    public function testGet()
144
    {
145
        $document = $this->getDocumentInstance();
146
147
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
148
        $position = 0;
149
        $header = Header::parse($content, $document, $position);
150
        $object = new Page($document, $header);
151
        $document->setObjects(['5_0' => $object]);
152
153
        $this->assertTrue($header->get('Type') instanceof ElementName);
154
        $this->assertTrue($header->get('SubType') instanceof ElementName);
155
        $this->assertTrue($header->get('Font') instanceof Page);
156
        $this->assertTrue($header->get('Image') instanceof ElementMissing);
157
        $this->assertTrue($header->get('Resources') instanceof ElementMissing);
158
    }
159
160
    public function testResolveXRef()
161
    {
162
        $document = $this->getDocumentInstance();
163
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
164
        $position = 0;
165
        $header = Header::parse($content, $document, $position);
166
        $object = new Page($document, $header);
167
        $document->setObjects(['5_0' => $object]);
168
169
        $this->assertTrue($header->get('Font') instanceof PDFObject);
170
171
        $header = $header->get('Resources');
172
        $this->assertTrue($header instanceof ElementMissing);
173
    }
174
}
175