Test Failed
Pull Request — master (#583)
by Konrad
04:39 queued 02:37
created

HeaderTest::testInitHappyPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 *
8
 * @date    2020-06-01
9
 *
10
 * @author  Sébastien MALOT <[email protected]>
11
 *
12
 * @date    2017-01-03
13
 *
14
 * @license LGPLv3
15
 *
16
 * @url     <https://github.com/smalot/pdfparser>
17
 *
18
 *  PdfParser is a pdf library written in PHP, extraction oriented.
19
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
20
 *
21
 *  This program is free software: you can redistribute it and/or modify
22
 *  it under the terms of the GNU Lesser General Public License as published by
23
 *  the Free Software Foundation, either version 3 of the License, or
24
 *  (at your option) any later version.
25
 *
26
 *  This program is distributed in the hope that it will be useful,
27
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 *  GNU Lesser General Public License for more details.
30
 *
31
 *  You should have received a copy of the GNU Lesser General Public License
32
 *  along with this program.
33
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
34
 */
35
36
namespace PHPUnitTests\Integration;
37
38
use PHPUnitTests\TestCase;
39
use Smalot\PdfParser\Element;
40
use Smalot\PdfParser\Element\ElementMissing;
41
use Smalot\PdfParser\Element\ElementName;
42
use Smalot\PdfParser\Header;
43
use Smalot\PdfParser\Page;
44
use Smalot\PdfParser\PDFObject;
45
46
/**
47
 * Class Header
48
 */
49
class HeaderTest extends TestCase
50
{
51
    /**
52
     * Checks that init function is called for each element.
53
     */
54
    public function testInitHappyPath(): void
55
    {
56
        $element = $this->createMock(Element::class);
57
        $element->expects($this->exactly(1))->method('init');
58
59
        $fixture = new Header([$element]);
60
        $fixture->init();
61
    }
62
63
    /**
64
     * Checks buggy behavior if an element was given which is not of type Element.
65
     *
66
     * Problem was, it always called $element::init(), even if its not an object at all.
67
     *
68
     * @see https://github.com/smalot/pdfparser/issues/367
69
     *
70
     * @doesNotPerformAssertions
71
     */
72
    public function testInitInvalidElement(): void
73
    {
74
        $element = false;
75
76
        $fixture = new Header([$element]);
77
        $fixture->init();
78
    }
79
80
    public function testParse(): void
81
    {
82
        $document = $this->getDocumentInstance();
83
84
        $content = '<</Type/Page/SubType/Text>>foo';
85
        $position = 0;
86
        $header = Header::parse($content, $document, $position);
87
88
        $this->assertTrue($header instanceof Header);
89
        $this->assertEquals(27, $position);
90
        $this->assertEquals(2, \count($header->getElements()));
91
92
        // No header to parse
93
        $this->assertEquals('Page', (string) $header->get('Type'));
94
        $content = 'foo';
95
        $position = 0;
96
        $header = Header::parse($content, $document, $position);
97
98
        $this->assertTrue($header instanceof Header);
99
        $this->assertEquals(0, $position);
100
        $this->assertEquals(0, \count($header->getElements()));
101
102
        $position = 0;
103
        $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)>>";
104
        Header::parse($content, $document, $position);
105
        $this->assertEquals(212, $position);
106
107
        $position = 0;
108
        $content = '[5 0 R ] foo';
109
        $header = Header::parse($content, $document, $position);
110
        $this->assertEquals(8, $position);
111
        $this->assertEquals(1, \count($header->getElements()));
112
    }
113
114
    public function testGetElements(): void
115
    {
116
        $document = $this->getDocumentInstance();
117
118
        $content = '<</Type/Page/Subtype/Text>>foo';
119
        $position = 0;
120
        $header = Header::parse($content, $document, $position);
121
122
        $elements = $header->getElements();
123
        $this->assertEquals(2, \count($elements));
124
        $this->assertTrue(current($elements) instanceof ElementName);
125
126
        $types = $header->getElementTypes();
127
        $this->assertTrue(\is_array($types));
128
        $this->assertEquals(ElementName::class, $types['Type']);
129
        $this->assertEquals(ElementName::class, $types['Subtype']);
130
    }
131
132
    public function testHas(): void
133
    {
134
        $document = $this->getDocumentInstance();
135
136
        $content = '<</Type/Page/SubType/Text/Font 5 0 R>>foo';
137
        $position = 0;
138
        $header = Header::parse($content, $document, $position);
139
140
        $this->assertTrue($header->has('Type'));
141
        $this->assertTrue($header->has('SubType'));
142
        $this->assertTrue($header->has('Font'));
143
        $this->assertFalse($header->has('Text'));
144
    }
145
146
    public function testGet(): void
147
    {
148
        $document = $this->getDocumentInstance();
149
150
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
151
        $position = 0;
152
        $header = Header::parse($content, $document, $position);
153
        $object = new Page($document, $header);
154
        $document->setObjects(['5_0' => $object]);
155
156
        $this->assertTrue($header->get('Type') instanceof ElementName);
157
        $this->assertTrue($header->get('SubType') instanceof ElementName);
158
        $this->assertTrue($header->get('Font') instanceof Page);
159
        $this->assertTrue($header->get('Image') instanceof ElementMissing);
160
        $this->assertTrue($header->get('Resources') instanceof ElementMissing);
161
162
        /**
163
         * A double forward slash in the header's content results in a falsy element
164
         * that should be parsed to ElementMissing instead.
165
         *
166
         * @see https://github.com/smalot/pdfparser/pull/525
167
         */
168
        $content = '<</Type/Page/SubType//Text>>foo';
169
        $position = 0;
170
        $header = Header::parse($content, $document, $position);
171
172
        $this->assertTrue($header->get('SubType') instanceof ElementMissing);
173
    }
174
175
    public function testResolveXRef(): void
176
    {
177
        $document = $this->getDocumentInstance();
178
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
179
        $position = 0;
180
        $header = Header::parse($content, $document, $position);
181
        $object = new Page($document, $header);
182
        $document->setObjects(['5_0' => $object]);
183
184
        $this->assertTrue($header->get('Font') instanceof PDFObject);
185
186
        $header = $header->get('Resources');
187
        $this->assertTrue($header instanceof ElementMissing);
188
    }
189
}
190