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

Element::testGetContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 6
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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 Element
37
 *
38
 * @package Smalot\PdfParser\Tests\Units
39
 */
40
class Element extends atoum\test
41
{
42
    public function testParse()
43
    {
44
        $document = new \Smalot\PdfParser\Document(array());
0 ignored issues
show
Unused Code introduced by
The call to Smalot\PdfParser\Document::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

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

44
        $document = /** @scrutinizer ignore-call */ new \Smalot\PdfParser\Document(array());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
45
46
        // Only_values = false.
47
        $content  = '/NameType /FlateDecode
48
        /Contents[4 0 R 42]/Fonts<</F1 41/F2 43>>/NullType
49
        null/StringType(hello)/DateType(D:20130901235555+02\'00\')/XRefType 2 0 R
50
        /NumericType 8/HexaType<0020>/BooleanType false';
51
        $offset   = 0;
52
        $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false);
53
54
        $this->assert->array($elements)->hasKey('NameType');
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
        $this->assert->object($elements['NameType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
56
        $this->assert->string($elements['NameType']->getContent())->isEqualTo('FlateDecode');
57
58
        $this->assert->boolean(array_key_exists('Contents', $elements))->isEqualTo(true);
59
        $this->assert->object($elements['Contents'])->isInstanceOf('\Smalot\PdfParser\Element\ElementArray');
60
        $this->assert->boolean($elements['Contents']->contains(42))->isEqualTo(true);
61
62
        $this->assert->boolean(array_key_exists('Fonts', $elements))->isEqualTo(true);
63
        $this->assert->object($elements['Fonts'])->isInstanceOf('\Smalot\PdfParser\Header');
64
65
        $this->assert->boolean(array_key_exists('NullType', $elements))->isEqualTo(true);
66
        $this->assert->object($elements['NullType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementNull');
67
        $this->assert->castToString($elements['NullType'])->isEqualTo('null');
68
69
        $this->assert->boolean(array_key_exists('StringType', $elements))->isEqualTo(true);
70
        $this->assert->object($elements['StringType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementString');
71
        $this->assert->string($elements['StringType']->getContent())->isEqualTo('hello');
72
73
        $this->assert->boolean(array_key_exists('DateType', $elements))->isEqualTo(true);
74
        $this->assert->object($elements['DateType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementDate');
75
//        $this->assert->castToString($elements['DateType'])->isEqualTo('2013-09-01T23:55:55+02:00');
76
77
        $this->assert->boolean(array_key_exists('XRefType', $elements))->isEqualTo(true);
78
        $this->assert->object($elements['XRefType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementXRef');
79
        $this->assert->string($elements['XRefType']->getId())->isEqualTo('2_0');
80
81
        $this->assert->boolean(array_key_exists('NumericType', $elements))->isEqualTo(true);
82
        $this->assert->object($elements['NumericType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementNumeric');
83
        $this->assert->castToString($elements['NumericType'])->isEqualTo('8');
84
85
        $this->assert->boolean(array_key_exists('HexaType', $elements))->isEqualTo(true);
86
        $this->assert->object($elements['HexaType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementString');
87
        $this->assert->string($elements['HexaType']->getContent())->isEqualTo(' ');
88
89
        $this->assert->boolean(array_key_exists('BooleanType', $elements))->isEqualTo(true);
90
        $this->assert->object($elements['BooleanType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementBoolean');
91
        $this->assert->boolean($elements['BooleanType']->getContent())->isEqualTo(false);
92
93
        // Only_values = true.
94
        $content  = '/NameType /FlateDecode';
95
        $offset   = 0;
96
        $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, true);
97
        $this->assert->array($elements)->hasSize(2);
98
        $this->assert->integer($offset)->isEqualTo(22);
99
100
        // Test error.
101
        $content  = '/NameType /FlateDecode $$$';
102
        $offset   = 0;
103
        $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false);
104
        $this->assert->array($elements)->hasSize(1);
105
        $this->assert->integer($offset)->isEqualTo(22);
106
        $this->assert->string(key($elements))->isEqualTo('NameType');
107
        $this->assert->object(current($elements))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
108
109
        $content  = '/NameType $$$';
110
        $offset   = 0;
111
        $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false);
112
        $this->assert->integer($offset)->isEqualTo(0);
113
        $this->assert->array($elements)->isEmpty();
114
115
        /*$this->assert->boolean(array_key_exists('NameType', $elements))->isEqualTo(true);
116
        $this->assert->boolean($elements['NameType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementName)->isEqualTo(true);
117
        $this->assert->string($elements['NameType']->getContent())->isEqualTo('FlateDecode');*/
118
    }
119
120
    public function testGetContent()
121
    {
122
        $element = new \Smalot\PdfParser\Element(42);
123
        $content = $element->getContent();
124
        $this->assert->integer($content)->isEqualTo(42);
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
125
126
        $element = new \Smalot\PdfParser\Element(array(4, 2));
127
        $content = $element->getContent();
128
        $this->assert->array($content)->hasSize(2);
129
    }
130
131
    public function testEquals()
132
    {
133
        $element = new \Smalot\PdfParser\Element(2);
134
135
        $this->assert->boolean($element->equals(2))->isEqualTo(true);
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
136
        $this->assert->boolean($element->equals(8))->isEqualTo(false);
137
    }
138
139
    public function testContains()
140
    {
141
        $val_4   = new \Smalot\PdfParser\Element(4);
142
        $val_2   = new \Smalot\PdfParser\Element(2);
143
        $element = new \Smalot\PdfParser\Element(array($val_4, $val_2));
144
145
        $this->assert->boolean($element->contains(2))->isEqualTo(true);
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
        $this->assert->boolean($element->contains(8))->isEqualTo(false);
147
    }
148
149
    public function test__toString()
150
    {
151
        $element = new \Smalot\PdfParser\Element(2);
152
        $this->assert->castToString($element)->isEqualTo('2');
0 ignored issues
show
Bug Best Practice introduced by
The property assert does not exist on Smalot\PdfParser\Tests\Units\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
153
    }
154
}
155