Passed
Push — feature/switch-to-phpunit ( f5d302...e3df65 )
by Konrad
03:58
created

DocumentTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageInstance() 0 3 1
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
A testGetPagesMissingCatalog() 0 8 1
B testGetPages() 0 80 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
 * @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 Exception;
36
use Smalot\PdfParser\Document;
37
use Smalot\PdfParser\Header;
38
use Smalot\PdfParser\Page;
39
use Smalot\PdfParser\Pages;
40
use Smalot\PdfParser\PDFObject;
41
use Test\Smalot\PdfParser\TestCase;
42
43
class DocumentTest extends TestCase
44
{
45
    protected function getDocumentInstance()
46
    {
47
        return new Document();
48
    }
49
50
    /**
51
     * @param Document $document
52
     */
53
    protected function getPDFObjectInstance($document, $header = null)
54
    {
55
        return new PDFObject($document, $header);
56
    }
57
58
    /**
59
     * @param Document $document
60
     * @param Header   $header
61
     */
62
    protected function getPageInstance($document, $header)
63
    {
64
        return new Page($document, $header);
65
    }
66
67
    /**
68
     * @param Document $document
69
     * @param Header   $header
70
     */
71
    protected function getPagesInstance($document, $header)
72
    {
73
        return new Pages($document, $header);
74
    }
75
76
    public function testSetObjects()
77
    {
78
        $document = $this->getDocumentInstance();
79
        $object = $this->getPDFObjectInstance($document);
80
81
        // Obj #1 is missing
82
        $this->assertNull($document->getObjectById(1));
83
        $document->setObjects([1 => $object]);
84
85
        // Obj #1 exists
86
        $this->assertTrue($document->getObjectById(1) instanceof PDFObject);
87
88
        $content = '<</Type/Page>>';
89
        $header = Header::parse($content, $document);
90
        $object = $this->getPDFObjectInstance($document, $header);
91
        $document->setObjects([2 => $object]);
92
93
        // Obj #1 is missing
94
        $this->assertNull($document->getObjectById(1));
95
96
        // Obj #2 exists
97
        $this->assertTrue($document->getObjectById(2) instanceof PDFObject);
98
    }
99
100
    public function testGetObjects()
101
    {
102
        $document = $this->getDocumentInstance();
103
        $object1 = $this->getPDFObjectInstance($document);
104
        $content = '<</Type/Page>>unparsed content';
105
        $header = Header::parse($content, $document);
106
107
        $object2 = $this->getPageInstance($document, $header);
108
        $document->setObjects([1 => $object1, 2 => $object2]);
109
110
        $objects = $document->getObjects();
111
        $this->assertEquals(2, \count($objects));
112
        $this->assertTrue($objects[1] instanceof PDFObject);
113
        $this->assertTrue($objects[2] instanceof PDFObject);
114
        $this->assertTrue($objects[2] instanceof Page);
115
    }
116
117
    public function testDictionary()
118
    {
119
        $document = $this->getDocumentInstance();
120
        $objects = $document->getDictionary();
121
        $this->assertEquals(0, \count($objects));
122
        $object1 = $this->getPDFObjectInstance($document);
123
124
        $content = '<</Type/Page>>';
125
        $header = Header::parse($content, $document);
126
        $object2 = $this->getPageInstance($document, $header);
127
        $document->setObjects([1 => $object1, 2 => $object2]);
128
129
        $objects = $document->getDictionary();
130
        $this->assertEquals(1, \count($objects));
131
        $this->assertEquals(1, \count($objects['Page']));
132
        $this->assertEquals(2, $objects['Page'][2]);
133
    }
134
135
    public function testGetObjectsByType()
136
    {
137
        $document = $this->getDocumentInstance();
138
        $object1 = $this->getPDFObjectInstance($document);
139
        $content = '<</Type/Page>>';
140
        $header = Header::parse($content, $document);
141
        $object2 = $this->getPageInstance($document, $header);
142
        $document->setObjects([1 => $object1, 2 => $object2]);
143
144
        $objects = $document->getObjectsByType('Page');
145
        $this->assertEquals(1, \count($objects));
146
        $this->assertTrue($objects[2] instanceof PDFObject);
147
        $this->assertTrue($objects[2] instanceof Page);
148
    }
149
150
    public function testGetPages()
151
    {
152
        $document = $this->getDocumentInstance();
153
154
        // Listing pages from type Page
155
        $content = '<</Type/Page>>';
156
        $header = Header::parse($content, $document);
157
        $object1 = $this->getPageInstance($document, $header);
158
        $header = Header::parse($content, $document);
159
        $object2 = $this->getPageInstance($document, $header);
160
        $document->setObjects([1 => $object1, 2 => $object2]);
161
        $pages = $document->getPages();
162
163
        $this->assertEquals(2, \count($pages));
164
        $this->assertTrue($pages[0] instanceof Page);
165
        $this->assertTrue($pages[1] instanceof Page);
166
167
        // Listing pages from type Pages (kids)
168
        $content = '<</Type/Page>>';
169
        $header = Header::parse($content, $document);
170
        $object1 = $this->getPageInstance($document, $header);
171
        $header = Header::parse($content, $document);
172
        $object2 = $this->getPageInstance($document, $header);
173
        $header = Header::parse($content, $document);
174
        $object3 = $this->getPageInstance($document, $header);
175
176
        $content = '<</Type/Pages/Kids[1 0 R 2 0 R]>>';
177
        $header = Header::parse($content, $document);
178
        $object4 = $this->getPagesInstance($document, $header);
179
180
        $content = '<</Type/Pages/Kids[3 0 R]>>';
181
        $header = Header::parse($content, $document);
182
        $object5 = $this->getPagesInstance($document, $header);
183
184
        $document->setObjects([
185
            '1_0' => $object1,
186
            '2_0' => $object2,
187
            '3_0' => $object3,
188
            '4_0' => $object4,
189
            '5_0' => $object5,
190
        ]);
191
        $pages = $document->getPages();
192
193
        $this->assertEquals(3, \count($pages));
194
        $this->assertTrue($pages[0] instanceof Page);
195
        $this->assertTrue($pages[1] instanceof Page);
196
        $this->assertTrue($pages[2] instanceof Page);
197
198
        // Listing pages from type Catalog
199
        $content = '<</Type/Page>>';
200
        $header = Header::parse($content, $document);
201
        $object1 = $this->getPageInstance($document, $header);
202
        $header = Header::parse($content, $document);
203
        $object2 = $this->getPageInstance($document, $header);
204
        $header = Header::parse($content, $document);
205
        $object3 = $this->getPageInstance($document, $header);
206
        $content = '<</Type/Pages/Kids[1 0 R 2 0 R]>>';
207
        $header = Header::parse($content, $document);
208
        $object4 = $this->getPagesInstance($document, $header);
209
        $content = '<</Type/Pages/Kids[4 0 R 3 0 R]>>';
210
        $header = Header::parse($content, $document);
211
        $object5 = $this->getPagesInstance($document, $header);
212
        $content = '<</Type/Catalog/Pages 5 0 R >>';
213
        $header = Header::parse($content, $document);
214
        $object6 = $this->getPagesInstance($document, $header);
215
        $document->setObjects(
216
            [
217
                '1_0' => $object1,
218
                '2_0' => $object2,
219
                '3_0' => $object3,
220
                '4_0' => $object4,
221
                '5_0' => $object5,
222
                '6_0' => $object6,
223
            ]
224
        );
225
        $pages = $document->getPages();
226
        $this->assertEquals(3, \count($pages));
227
        $this->assertTrue($pages[0] instanceof Page);
228
        $this->assertTrue($pages[1] instanceof Page);
229
        $this->assertTrue($pages[2] instanceof Page);
230
    }
231
232
    public function testGetPagesMissingCatalog()
233
    {
234
        $this->expectException(Exception::class);
235
        $this->expectExceptionMessage('Missing catalog.');
236
237
        // Missing catalog
238
        $document = $this->getDocumentInstance();
239
        $document->getPages();
240
    }
241
}
242