Completed
Pull Request — master (#300)
by Konrad
13:31 queued 09:23
created

HeaderTest::testHas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10
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
 * @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\ElementMissing;
36
use Smalot\PdfParser\Element\ElementName;
37
use Smalot\PdfParser\Header;
38
use Smalot\PdfParser\Page;
39
use Smalot\PdfParser\PDFObject;
40
use Test\Smalot\PdfParser\TestCase;
41
42
/**
43
 * Class Header
44
 */
45
class HeaderTest extends TestCase
46
{
47
    public function testParse()
48
    {
49
        $document = $this->getDocumentInstance();
50
51
        $content = '<</Type/Page/SubType/Text>>foo';
52
        $position = 0;
53
        $header = Header::parse($content, $document, $position);
54
55
        $this->assertTrue($header instanceof Header);
56
        $this->assertEquals(27, $position);
57
        $this->assertEquals(2, \count($header->getElements()));
58
59
        // No header to parse
60
        $this->assertEquals('Page', (string) $header->get('Type'));
61
        $content = 'foo';
62
        $position = 0;
63
        $header = Header::parse($content, $document, $position);
64
65
        $this->assertTrue($header instanceof Header);
66
        $this->assertEquals(0, $position);
67
        $this->assertEquals(0, \count($header->getElements()));
68
69
        $position = 0;
70
        $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)>>";
71
        $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...
72
        $this->assertEquals(212, $position);
73
74
        $position = 0;
75
        $content = '[5 0 R ] foo';
76
        $header = Header::parse($content, $document, $position);
77
        $this->assertEquals(8, $position);
78
        $this->assertEquals(1, \count($header->getElements()));
79
    }
80
81
    public function testGetElements()
82
    {
83
        $document = $this->getDocumentInstance();
84
85
        $content = '<</Type/Page/Subtype/Text>>foo';
86
        $position = 0;
87
        $header = Header::parse($content, $document, $position);
88
89
        $elements = $header->getElements();
90
        $this->assertEquals(2, \count($elements));
91
        $this->assertTrue(current($elements) instanceof ElementName);
92
93
        $types = $header->getElementTypes();
94
        $this->assertTrue(\is_array($types));
95
        $this->assertEquals(ElementName::class, $types['Type']);
96
        $this->assertEquals(ElementName::class, $types['Subtype']);
97
    }
98
99
    public function testHas()
100
    {
101
        $document = $this->getDocumentInstance();
102
103
        $content = '<</Type/Page/SubType/Text/Font 5 0 R>>foo';
104
        $position = 0;
105
        $header = Header::parse($content, $document, $position);
106
107
        $this->assertTrue($header->has('Type'));
108
        $this->assertTrue($header->has('SubType'));
109
        $this->assertTrue($header->has('Font'));
110
        $this->assertFalse($header->has('Text'));
111
    }
112
113
    public function testGet()
114
    {
115
        $document = $this->getDocumentInstance();
116
117
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
118
        $position = 0;
119
        $header = Header::parse($content, $document, $position);
120
        $object = new Page($document, $header);
121
        $document->setObjects(['5_0' => $object]);
122
123
        $this->assertTrue($header->get('Type') instanceof ElementName);
124
        $this->assertTrue($header->get('SubType') instanceof ElementName);
125
        $this->assertTrue($header->get('Font') instanceof Page);
126
        $this->assertTrue($header->get('Image') instanceof ElementMissing);
127
        $this->assertTrue($header->get('Resources') instanceof ElementMissing);
128
    }
129
130
    public function testResolveXRef()
131
    {
132
        $document = $this->getDocumentInstance();
133
        $content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
134
        $position = 0;
135
        $header = Header::parse($content, $document, $position);
136
        $object = new Page($document, $header);
137
        $document->setObjects(['5_0' => $object]);
138
139
        $this->assertTrue($header->get('Font') instanceof PDFObject);
140
141
        $header = $header->get('Resources');
142
        $this->assertTrue($header instanceof ElementMissing);
143
    }
144
}
145