Passed
Push — feature/switch-to-phpunit ( 82614e )
by Konrad
03:39
created

HeaderTest::testResolveXRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 1
nc 1
nop 0
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
 * @license LGPLv3
10
 * @url     <https://github.com/smalot/pdfparser>
11
 *
12
 *  PdfParser is a pdf library written in PHP, extraction oriented.
13
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
14
 *
15
 *  This program is free software: you can redistribute it and/or modify
16
 *  it under the terms of the GNU Lesser General Public License as published by
17
 *  the Free Software Foundation, either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  This program is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU Lesser General Public License for more details.
24
 *
25
 *  You should have received a copy of the GNU Lesser General Public License
26
 *  along with this program.
27
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
28
 */
29
30
namespace Tests\Smalot\PdfParser\Integration;
31
32
use Smalot\PdfParser\Element\ElementMissing;
33
use Smalot\PdfParser\Element\ElementName;
34
use Smalot\PdfParser\Header;
35
use Smalot\PdfParser\Page;
36
use Smalot\PdfParser\PDFObject;
37
use Smalot\PdfParser\Test\TestCase;
38
39
/**
40
 * Class Header
41
 */
42
class HeaderTest extends TestCase
43
{
44
    public function testParse()
45
    {
46
        $document = $this->getDocumentInstance();
47
48
        $content = '<</Type/Page/SubType/Text>>foo';
49
        $position = 0;
50
        $header = Header::parse($content, $document, $position);
51
52
        $this->assertTrue($header instanceof Header);
53
        $this->assertEquals(27, $position);
54
        $this->assertEquals(2, \count($header->getElements()));
55
56
        // No header to parse
57
        $this->assertEquals('Page', (string) $header->get('Type'));
58
        $content = 'foo';
59
        $position = 0;
60
        $header = Header::parse($content, $document, $position);
61
62
        $this->assertTrue($header instanceof Header);
63
        $this->assertEquals(0, $position);
64
        $this->assertEquals(0, \count($header->getElements()));
65
66
        $position = 0;
67
        $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)>>";
68
        $header = Header::parse($content, $document, $position);
0 ignored issues
show
Unused Code introduced by
The assignment to $header is dead and can be removed.
Loading history...
69
        $this->assertEquals(212, $position);
70
71
        $position = 0;
72
        $content = '[5 0 R ] foo';
73
        $header = Header::parse($content, $document, $position);
74
        $this->assertEquals(8, $position);
75
        $this->assertEquals(1, \count($header->getElements()));
76
    }
77
78
    public function testGetElements()
79
    {
80
        $document = $this->getDocumentInstance();
81
82
        $content = '<</Type/Page/Subtype/Text>>foo';
83
        $position = 0;
84
        $header = Header::parse($content, $document, $position);
85
86
        $elements = $header->getElements();
87
        $this->assertEquals(2, \count($elements));
88
        $this->assertTrue(current($elements) instanceof ElementName);
89
90
        $types = $header->getElementTypes();
91
        $this->assertTrue(\is_array($types));
92
        $this->assertEquals(ElementName::class, $types['Type']);
93
        $this->assertEquals(ElementName::class, $types['Subtype']);
94
    }
95
96
    public function testHas()
97
    {
98
        $document = $this->getDocumentInstance();
99
100
        $content = '<</Type/Page/SubType/Text/Font 5 0 R>>foo';
101
        $position = 0;
102
        $header = Header::parse($content, $document, $position);
103
104
        $this->assertTrue($header->has('Type'));
105
        $this->assertTrue($header->has('SubType'));
106
        $this->assertTrue($header->has('Font'));
107
        $this->assertFalse($header->has('Text'));
108
    }
109
110
    public function testGet()
111
    {
112
        $document = $this->getDocumentInstance();
113
114
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
115
        $position = 0;
116
        $header = Header::parse($content, $document, $position);
117
        $object = new Page($document, $header);
118
        $document->setObjects(['5_0' => $object]);
119
120
        $this->assertTrue($header->get('Type') instanceof ElementName);
121
        $this->assertTrue($header->get('SubType') instanceof ElementName);
122
        $this->assertTrue($header->get('Font') instanceof Page);
123
        $this->assertTrue($header->get('Image') instanceof ElementMissing);
124
        $this->assertTrue($header->get('Resources') instanceof ElementMissing);
125
    }
126
127
    public function testResolveXRef()
128
    {
129
        $document = $this->getDocumentInstance();
130
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
131
        $position = 0;
132
        $header = Header::parse($content, $document, $position);
133
        $object = new Page($document, $header);
134
        $document->setObjects(['5_0' => $object]);
135
136
        $this->assertTrue($header->get('Font') instanceof PDFObject);
137
138
        $header = $header->get('Resources');
139
        $this->assertTrue($header instanceof ElementMissing);
140
    }
141
}
142