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

DocumentTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 117
c 1
b 0
f 0
dl 0
loc 194
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageInstance() 0 3 1
B testGetPageInstances() 0 87 2
A testDictionary() 0 16 1
A getDocumentInstance() 0 3 1
A getPagesInstance() 0 3 1
A getPDFObjectInstance() 0 3 1
A testGetObjects() 0 15 1
A testSetObjects() 0 22 1
A testGetObjectsByType() 0 13 1
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 Exception;
33
use Smalot\PdfParser\Document;
34
use Smalot\PdfParser\Header;
35
use Smalot\PdfParser\Page;
36
use Smalot\PdfParser\Pages;
37
use Smalot\PdfParser\PDFObject;
38
use Smalot\PdfParser\Test\TestCase;
39
40
class DocumentTest extends TestCase
41
{
42
    protected function getDocumentInstance()
43
    {
44
        return new Document();
45
    }
46
47
    /**
48
     * @param Document $document
49
     */
50
    protected function getPDFObjectInstance($document, $header = null)
51
    {
52
        return new PDFObject($document, $header);
53
    }
54
55
    /**
56
     * @param Document $document
57
     * @param Header   $header
58
     */
59
    protected function getPageInstance($document, $header)
60
    {
61
        return new Page($document, $header);
62
    }
63
64
    /**
65
     * @param Document $document
66
     * @param Header   $header
67
     */
68
    protected function getPagesInstance($document, $header)
69
    {
70
        return new Pages($document, $header);
71
    }
72
73
    public function testSetObjects()
74
    {
75
        $document = $this->getDocumentInstance();
76
        $object = $this->getPDFObjectInstance($document);
77
78
        // Obj #1 is missing
79
        $this->assertNull($document->getObjectById(1));
80
        $document->setObjects([1 => $object]);
81
82
        // Obj #1 exists
83
        $this->assertTrue($document->getObjectById(1) instanceof PDFObject);
84
85
        $content = '<</Type/Page>>';
86
        $header = Header::parse($content, $document);
87
        $object = $this->getPDFObjectInstance($document, $header);
88
        $document->setObjects([2 => $object]);
89
90
        // Obj #1 is missing
91
        $this->assertNull($document->getObjectById(1));
92
93
        // Obj #2 exists
94
        $this->assertTrue($document->getObjectById(2) instanceof PDFObject);
95
    }
96
97
    public function testGetObjects()
98
    {
99
        $document = $this->getDocumentInstance();
100
        $object1 = $this->getPDFObjectInstance($document);
101
        $content = '<</Type/Page>>unparsed content';
102
        $header = Header::parse($content, $document);
103
104
        $object2 = $this->getPageInstance($document, $header);
105
        $document->setObjects([1 => $object1, 2 => $object2]);
106
107
        $objects = $document->getObjects();
108
        $this->assertEquals(2, \count($objects));
109
        $this->assertTrue($objects[1] instanceof PDFObject);
110
        $this->assertTrue($objects[2] instanceof PDFObject);
111
        $this->assertTrue($objects[2] instanceof Page);
112
    }
113
114
    public function testDictionary()
115
    {
116
        $document = $this->getDocumentInstance();
117
        $objects = $document->getDictionary();
118
        $this->assertEquals(0, \count($objects));
119
        $object1 = $this->getPDFObjectInstance($document);
120
121
        $content = '<</Type/Page>>';
122
        $header = Header::parse($content, $document);
123
        $object2 = $this->getPageInstance($document, $header);
124
        $document->setObjects([1 => $object1, 2 => $object2]);
125
126
        $objects = $document->getDictionary();
127
        $this->assertEquals(1, \count($objects));
128
        $this->assertEquals(1, \count($objects['Page']));
129
        $this->assertEquals(2, $objects['Page'][2]);
130
    }
131
132
    public function testGetObjectsByType()
133
    {
134
        $document = $this->getDocumentInstance();
135
        $object1 = $this->getPDFObjectInstance($document);
136
        $content = '<</Type/Page>>';
137
        $header = Header::parse($content, $document);
138
        $object2 = $this->getPageInstance($document, $header);
139
        $document->setObjects([1 => $object1, 2 => $object2]);
140
141
        $objects = $document->getObjectsByType('Page');
142
        $this->assertEquals(1, \count($objects));
143
        $this->assertTrue($objects[2] instanceof PDFObject);
144
        $this->assertTrue($objects[2] instanceof Page);
145
    }
146
147
    public function testGetPageInstances()
148
    {
149
        // Missing catalog
150
        $document = $this->getDocumentInstance();
151
        try {
152
            $pages = $document->getPages();
153
            $this->assertFalse($pages);
0 ignored issues
show
Bug introduced by
$pages of type Smalot\PdfParser\Page[] is incompatible with the type boolean expected by parameter $condition of PHPUnit\Framework\Assert::assertFalse(). ( Ignorable by Annotation )

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

153
            $this->assertFalse(/** @scrutinizer ignore-type */ $pages);
Loading history...
154
        } catch (\Exception $e) {
155
            $this->assertTrue($e instanceof Exception);
156
        }
157
158
        // Listing pages from type Page
159
        $content = '<</Type/Page>>';
160
        $header = Header::parse($content, $document);
161
        $object1 = $this->getPageInstance($document, $header);
162
        $header = Header::parse($content, $document);
163
        $object2 = $this->getPageInstance($document, $header);
164
        $document->setObjects([1 => $object1, 2 => $object2]);
165
        $pages = $document->getPages();
166
167
        $this->assertEquals(2, \count($pages));
168
        $this->assertTrue($pages[0] instanceof Page);
169
        $this->assertTrue($pages[1] instanceof Page);
170
171
        // Listing pages from type Pages (kids)
172
        $content = '<</Type/Page>>';
173
        $header = Header::parse($content, $document);
174
        $object1 = $this->getPageInstance($document, $header);
175
        $header = Header::parse($content, $document);
176
        $object2 = $this->getPageInstance($document, $header);
177
        $header = Header::parse($content, $document);
178
        $object3 = $this->getPageInstance($document, $header);
179
180
        $content = '<</Type/Pages/Kids[1 0 R 2 0 R]>>';
181
        $header = Header::parse($content, $document);
182
        $object4 = $this->getPagesInstance($document, $header);
183
184
        $content = '<</Type/Pages/Kids[3 0 R]>>';
185
        $header = Header::parse($content, $document);
186
        $object5 = $this->getPagesInstance($document, $header);
187
188
        $document->setObjects([
189
            '1_0' => $object1,
190
            '2_0' => $object2,
191
            '3_0' => $object3,
192
            '4_0' => $object4,
193
            '5_0' => $object5,
194
        ]);
195
        $pages = $document->getPages();
196
197
        $this->assertEquals(3, \count($pages));
198
        $this->assertTrue($pages[0] instanceof Page);
199
        $this->assertTrue($pages[1] instanceof Page);
200
        $this->assertTrue($pages[2] instanceof Page);
201
202
        // Listing pages from type Catalog
203
        $content = '<</Type/Page>>';
204
        $header = Header::parse($content, $document);
205
        $object1 = $this->getPageInstance($document, $header);
206
        $header = Header::parse($content, $document);
207
        $object2 = $this->getPageInstance($document, $header);
208
        $header = Header::parse($content, $document);
209
        $object3 = $this->getPageInstance($document, $header);
210
        $content = '<</Type/Pages/Kids[1 0 R 2 0 R]>>';
211
        $header = Header::parse($content, $document);
212
        $object4 = $this->getPagesInstance($document, $header);
213
        $content = '<</Type/Pages/Kids[4 0 R 3 0 R]>>';
214
        $header = Header::parse($content, $document);
215
        $object5 = $this->getPagesInstance($document, $header);
216
        $content = '<</Type/Catalog/Pages 5 0 R >>';
217
        $header = Header::parse($content, $document);
218
        $object6 = $this->getPagesInstance($document, $header);
219
        $document->setObjects(
220
            [
221
                '1_0' => $object1,
222
                '2_0' => $object2,
223
                '3_0' => $object3,
224
                '4_0' => $object4,
225
                '5_0' => $object5,
226
                '6_0' => $object6,
227
            ]
228
        );
229
        $pages = $document->getPages();
230
        $this->assertEquals(3, \count($pages));
231
        $this->assertTrue($pages[0] instanceof Page);
232
        $this->assertTrue($pages[1] instanceof Page);
233
        $this->assertTrue($pages[2] instanceof Page);
234
    }
235
}
236