Passed
Branch master (f7fac8)
by Sebastien
02:47
created

Header::testResolveXRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
eloc 9
c 3
b 1
f 1
nc 1
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
8
 * @date    2017-01-03
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
31
namespace Smalot\PdfParser\Tests\Units;
32
33
use mageekguy\atoum;
34
35
/**
36
 * Class Header
37
 *
38
 * @package Smalot\PdfParser\Tests\Units
39
 */
40
class Header extends atoum\test
41
{
42
    public function testParse()
43
    {
44
        $document = new \Smalot\PdfParser\Document();
45
46
        $content  = '<</Type/Page/SubType/Text>>foo';
47
        $position = 0;
48
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
49
50
        $this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Header');
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Header. Since you implemented __get, consider adding a @property annotation.
Loading history...
51
        $this->assert->integer($position)->isEqualTo(27);
52
        $this->assert->array($header->getElements())->hasSize(2);
53
54
        // No header to parse
55
        $this->assert->castToString($header->get('Type'))->isEqualTo('Page');
56
        $content  = 'foo';
57
        $position = 0;
58
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
59
60
        $this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Header');
61
        $this->assert->integer($position)->isEqualTo(0);
62
        $this->assert->array($header->getElements())->hasSize(0);
63
64
        $position = 0;
65
        $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)>>";
66
        $header   = \Smalot\PdfParser\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...
67
        $this->assert->integer($position)->isEqualTo(212);
68
69
        $position = 0;
70
        $content  = '[5 0 R ] foo';
71
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
72
        $this->assert->integer($position)->isEqualTo(8);
73
        $this->assert->array($header->getElements())->hasSize(1);
74
    }
75
76
    public function testGetElements()
77
    {
78
        $document = new \Smalot\PdfParser\Document();
79
80
        $content  = '<</Type/Page/Subtype/Text>>foo';
81
        $position = 0;
82
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
83
84
        $this->assert->array($elements = $header->getElements())->hasSize(2);
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Header. Since you implemented __get, consider adding a @property annotation.
Loading history...
85
        $this->assert->object(current($elements))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
86
87
        $types = $header->getElementTypes();
88
        $this->assert->array($types);
89
        $this->assert->string($types['Type'])->isEqualTo('Smalot\PdfParser\Element\ElementName');
90
        $this->assert->string($types['Subtype'])->isEqualTo('Smalot\PdfParser\Element\ElementName');
91
    }
92
93
    public function testHas()
94
    {
95
        $document = new \Smalot\PdfParser\Document();
96
97
        $content  = '<</Type/Page/SubType/Text/Font 5 0 R>>foo';
98
        $position = 0;
99
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
100
101
        $this->assert->boolean($header->has('Type'))->isEqualTo(true);
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Header. Since you implemented __get, consider adding a @property annotation.
Loading history...
102
        $this->assert->boolean($header->has('SubType'))->isEqualTo(true);
103
        $this->assert->boolean($header->has('Font'))->isEqualTo(true);
104
        $this->assert->boolean($header->has('Text'))->isEqualTo(false);
105
    }
106
107
    public function testGet()
108
    {
109
        $document = new \Smalot\PdfParser\Document();
110
111
        $content  = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
112
        $position = 0;
113
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
114
        $object   = new \Smalot\PdfParser\Page($document, $header);
115
        $document->setObjects(array('5_0' => $object));
116
117
        $this->assert->object($header->get('Type'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Header. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
        $this->assert->object($header->get('SubType'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
119
        $this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Page');
120
        $this->assert->object($header->get('Image'))->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing');
121
        $this->assert->object($header->get('Resources'))->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing');
122
    }
123
124
    public function testResolveXRef()
125
    {
126
        $document = new \Smalot\PdfParser\Document();
127
        $content  = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
128
        $position = 0;
129
        $header   = \Smalot\PdfParser\Header::parse($content, $document, $position);
130
        $object   = new \Smalot\PdfParser\Page($document, $header);
131
        $document->setObjects(array('5_0' => $object));
132
133
        $this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\PDFObject');
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Header. Since you implemented __get, consider adding a @property annotation.
Loading history...
134
135
        $header = $header->get('Resources');
136
        $this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing');
137
    }
138
}
139