Test Failed
Pull Request — master (#546)
by
unknown
03:36 queued 27s
created

ParserTest::testUsageOfConfigObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 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 Exception;
36
use Smalot\PdfParser\Config;
37
use Smalot\PdfParser\Document;
38
use Smalot\PdfParser\Parser;
39
use Smalot\PdfParser\XObject\Image;
40
use Tests\Smalot\PdfParser\TestCase;
41
42
class ParserTest extends TestCase
43
{
44
    protected function setUp(): void
45
    {
46
        parent::setUp();
47
48
        $this->fixture = new Parser();
49
    }
50
51
    /**
52
     * Notice: it may fail to run in Scrutinizer because of memory limitations.
53
     *
54
     * @group memory-heavy
55
     */
56
    public function testParseFile(): void
57
    {
58
        $directory = $this->rootDir.'/samples/bugs';
59
60
        if (is_dir($directory)) {
61
            $files = scandir($directory);
62
63
            foreach ($files as $file) {
64
                if (preg_match('/^.*\.pdf$/i', $file)) {
65
                    try {
66
                        $document = $this->fixture->parseFile($directory.'/'.$file);
67
                        $pages = $document->getPages();
68
                        $this->assertTrue(0 < \count($pages));
69
70
                        foreach ($pages as $page) {
71
                            $content = $page->getText();
72
                            $this->assertTrue(0 < \strlen($content));
73
                        }
74
                    } catch (Exception $e) {
75
                        if (
76
                            'Secured pdf file are currently not supported.' !== $e->getMessage()
77
                            && 0 != strpos($e->getMessage(), 'TCPDF_PARSER')
78
                        ) {
79
                            throw $e;
80
                        }
81
                    }
82
                }
83
            }
84
        }
85
    }
86
87
    /**
88
     * Properly decode international unicode characters
89
     *
90
     * @todo the other languages in the test document need work because of issues with UTF-16 decoding (Chinese, Japanese) and missing right-to-left language support
91
     */
92
    public function testUnicodeDecoding(): void
93
    {
94
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
95
96
        $document = $this->fixture->parseFile($filename);
97
98
        $testString_cyrillic = "Лорем ипсум долор сит амет, еу сед либрис долорем инцоррупте. Ут лорем долоре граеце хис, модо \nаппареат сапиентем ут мел. Хис ат лаборе омнесяуе сигниферумяуе, тале анциллае ан еум, ех сед синт \nнобис. Сед модус вивендо цопиосае еа, сапиентем цонцептам хис не, яуандо сплендиде еум те.";
99
        $testString_greek = "Λορεμ ιπσθμ δολορ σιτ αμετ, τατιον cονστιτθαμ ομιτταντθρ εα σεα, αθδιαμ μανδαμθσ μελ τε. Διcο μθτατ \nινδοcτθμ εοσ ει, ει vιξ σονετ παρτιενδο ινcορρθπτε. Επιcθρι αντιοπαμ εθ νεc, ναμ λεγιμθσ γθβεργρεν ιν. \nVιξ σολετ ρεcτεqθε εα, ηασ νο αλιqθαμ μινιμθμ. Ιδ προ περcιπιτ περιcθλισ δετερρθισσετ, ιν νεc αππετερε \nομιτταντθρ ελοqθεντιαμ, ορατιο δοcτθσ ναμ αδ. Ετ σιτ σολθμ ρεcθσαβο, vιξ θτ λοβορτισ σπλενδιδε \nρεπθδιανδαε.";
100
        $testString_armenian = "լոռեմ իպսում դոլոռ սիթ ամեթ վիս ին իմպեդիթ ադմոդում ծու ապպառեաթ սծռիպսեռիթ մել մել եթ \nդոմինգ ծոնսեքուունթուռ ծիվիբուս վիվենդում պռոդեսսեթ ադ մեի թիբիքուե ապպառեաթ սիմիլիքուե թե \nվիմ վիխ ծասե սեմպեռ դոլոռեմ եխ եամ եա սթեթ մեդիոծռեմ ծոնսեթեթուռ ռաթիոնիբուս ինթելլեգամ \nմել թե";
101
        $testString_georgean = "ლორემ იფსუმ დოლორ სით ამეთ ესთ ეთ სონეთ ზრილ მელიუს ელიგენდი თორყუათოს \nელოყუენთიამ ესთ ეხ უსუ ფალლი ალთერა ცეთეროს ინ ეთ ომითთამ თრაცთათოს ჰის ეუ ველ \nალთერუმ ვოლუფთათუმ მაზიმ ფერთინახ ჰენდრერით ინ ფრი ნეც ინ თემფორ ფეთენთიუმ ვერო \nფოსთულანთ ელოყუენთიამ უსუ ნე ან ყუი ლიბერ ეფიცური ასსუევერით იდ ნიბჰ ყუას ჰაბემუს სეა";
102
        $testString_korean = "그 임기는 4년으로 하며. 이 경우 그 명령에 의하여 개정 또는 폐지되었던 법률은 그 명령이 승인을 얻지 못한 때부터 당연히 효력을 \n회복한다. 가부동수인 때에는 부결된 것으로 본다. 법률과 적법한 절차에 의하지 아니하고는 처벌·보안처분 또는 강제노역을 받지 \n아니한다.";
103
        $testString_western = 'ÄÖÜöäüßẞ Ññ¡¿ øÅå';
104
105
        $this->assertStringContainsString($testString_cyrillic, $document->getText());
106
        $this->assertStringContainsString($testString_greek, $document->getText());
107
        $this->assertStringContainsString($testString_armenian, $document->getText());
108
        $this->assertStringContainsString($testString_georgean, $document->getText());
109
        $this->assertStringContainsString($testString_korean, $document->getText());
110
        $this->assertStringContainsString($testString_western, $document->getText());
111
    }
112
113
    /**
114
     * Tests that xrefs with line breaks between id and position are parsed correctly
115
     *
116
     * @see https://github.com/smalot/pdfparser/issues/336
117
     */
118
    public function testIssue19(): void
119
    {
120
        $fixture = new ParserSub();
121
        $structure = [
122
            [
123
                '<<',
124
                [
125
                    [
126
                        '/',
127
                        'Type',
128
                        7735,
129
                    ],
130
                    [
131
                        '/',
132
                        'ObjStm',
133
                        7742,
134
                    ],
135
                ],
136
            ],
137
            [
138
                'stream',
139
                '',
140
                7804,
141
                [
142
                    "17\n0",
143
                    [],
144
                ],
145
            ],
146
        ];
147
        $document = new Document();
148
149
        $fixture->exposedParseObject('19_0', $structure, $document);
150
        $objects = $fixture->getObjects();
151
152
        $this->assertArrayHasKey('17_0', $objects);
153
    }
154
155
    /**
156
     * Properly decode ANSI encodings without producing scrambled UTF-8 characters
157
     *
158
     * @see https://github.com/smalot/pdfparser/issues/202
159
     * @see https://github.com/smalot/pdfparser/pull/257
160
     */
161
    public function testIssue202(): void
162
    {
163
        $filename = $this->rootDir.'/samples/bugs/Issue202.pdf';
164
165
        $document = $this->fixture->parseFile($filename);
166
167
        $this->assertEquals('„fööbär“', $document->getText());
168
    }
169
170
    /**
171
     * Test that issue related pdf can now be parsed
172
     *
173
     * @see https://github.com/smalot/pdfparser/issues/267
174
     */
175
    public function testIssue267(): void
176
    {
177
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
178
179
        $document = $this->fixture->parseFile($filename);
180
181
        $this->assertEquals(Image::class, \get_class($document->getObjectById('128_0')));
182
        $this->assertStringContainsString('4 von 4', $document->getText());
183
    }
184
185
    /**
186
     * Test that issue related pdf can now be parsed:
187
     * Too many slashes were being stripped and resulted
188
     * in malformed encoding of parts of the text content.
189
     *
190
     * @see https://github.com/smalot/pdfparser/issues/322
191
     */
192
    public function testIssue322(): void
193
    {
194
        $filename = $this->rootDir.'/samples/bugs/Issue322.pdf';
195
196
        $document = $this->fixture->parseFile($filename);
197
198
        $this->assertStringContainsString('this text isn’t working properly, I’ve edited it in Google Documents', $document->getText());
199
    }
200
201
    /**
202
     * Test that issue related pdf can now be parsed:
203
     * Too many slashes were being stripped and resulted
204
     * in malformed encoding of parts of the text content.
205
     *
206
     * License of the content taken from https://stackoverflow.com in the sample PDF:
207
     * CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5/
208
     *
209
     * @see https://github.com/smalot/pdfparser/issues/334
210
     */
211
    public function testIssue334(): void
212
    {
213
        $filename = $this->rootDir.'/samples/bugs/Issue334.pdf';
214
215
        $document = $this->fixture->parseFile($filename);
216
217
        $this->assertStringContainsString('This question already has an answer here', $document->getText());
218
    }
219
220
    /**
221
     * Test that issue related pdf can now be parsed:
222
     * Glyphs not in the Postscript lookup table would cause "Notice: Undefined offset"
223
     *
224
     * @see https://github.com/smalot/pdfparser/issues/359
225
     */
226
    public function testIssue359(): void
227
    {
228
        $filename = $this->rootDir.'/samples/bugs/Issue359.pdf';
229
230
        $document = $this->fixture->parseFile($filename);
231
232
        $this->assertStringContainsString(
233
            'dnia 10 maja 2018 roku o ochronie danych osobowych',
234
            $document->getText()
235
        );
236
        $this->assertStringContainsString('sprawie ochrony osób fizycznych w związku', $document->getText());
237
        /*
238
         * @todo Note that the "ł" in przepływu is decoded as a space character. This was already
239
         * the case before the PR that caused this issue and is not currently covered by this
240
         * test case. However, this issue should be addressed in the future and its fix can then
241
         * be incorporated into this test by uncommenting the following assertion.
242
         */
243
        // $this->assertStringContainsString('sprawie swobodnego przepływu takich danych oraz uchylenia dyrektywy', $document->getText());
244
    }
245
246
    /**
247
     * Tests if PDF triggers "Call to undefined method Smalot\PdfParser\Header::__toString()".
248
     *
249
     * It happened because there was a check missing in Font.php (~ line 109).
250
     *
251
     * @see https://github.com/smalot/pdfparser/issues/391
252
     */
253
    public function testIssue391(): void
254
    {
255
        /**
256
         * PDF provided by @dhildreth for usage in our test environment.
257
         *
258
         * @see https://github.com/smalot/pdfparser/issues/391#issuecomment-783504599
259
         */
260
        $filename = $this->rootDir.'/samples/bugs/Issue391.pdf';
261
262
        $document = $this->fixture->parseFile($filename);
263
264
        // check for an example string (PDF consists of many pages)
265
        $this->assertStringContainsString(
266
            '(This Code will be changed while mass production)',
267
            $document->getText()
268
        );
269
    }
270
271
    /**
272
     * Tests behavior when changing default font space limit (-50).
273
     *
274
     * Test is based on testIssue359 (above).
275
     */
276
    public function testChangedFontSpaceLimit(): void
277
    {
278
        $filename = $this->rootDir.'/samples/bugs/Issue359.pdf';
279
280
        $config = new Config();
281
        $config->setFontSpaceLimit(1); // change default value
282
283
        $this->fixture = new Parser([], $config);
284
        $document = $this->fixture->parseFile($filename);
285
286
        $this->assertStringContainsString('dni a  10  maj a  2018', $document->getText());
287
    }
288
289
    /**
290
     * Tests if a given Config object is really used.
291
     * Or if a default one is generated, if null was given.
292
     */
293
    public function testUsageOfConfigObject(): void
294
    {
295
        // check default
296
        $this->fixture = new Parser([]);
297
        $this->assertEquals(new Config(), $this->fixture->getConfig());
298
299
        // check default 2
300
        $this->fixture = new Parser([], null);
301
        $this->assertEquals(new Config(), $this->fixture->getConfig());
302
303
        // check given
304
        $config = new Config();
305
        $config->setFontSpaceLimit(1000);
306
        $this->fixture = new Parser([], $config);
307
        $this->assertEquals($config, $this->fixture->getConfig());
308
    }
309
310
    /**
311
     * Tests the impact of the retainImageContent config setting on memory usage
312
     *
313
     * @group memory-heavy
314
     *
315
     * @see https://github.com/smalot/pdfparser/issues/104#issuecomment-883422508
316
     */
317
    public function testRetainImageContentImpact(): void
318
    {
319
        if (version_compare(\PHP_VERSION, '7.3.0', '<')) {
320
            $this->markTestSkipped('Garbage collection doesn\'t work reliably enough for this test in PHP < 7.3');
321
        }
322
323
        gc_collect_cycles();
324
        $baselineMemory = memory_get_usage(true);
325
326
        $filename = $this->rootDir.'/samples/bugs/Issue104a.pdf';
327
        $iterations = 2;
328
329
        /*
330
         * check default (= true)
331
         */
332
        $this->fixture = new Parser([]);
333
        $this->assertTrue($this->fixture->getConfig()->getRetainImageContent());
334
        $document = null;
335
336
        for ($i = 0; $i < $iterations; ++$i) {
337
            $document = $this->fixture->parseFile($filename);
338
        }
339
340
        $usedMemory = memory_get_usage(true);
341
        $this->assertTrue($usedMemory > ($baselineMemory * 1.5), 'Memory is only '.$usedMemory);
342
        $this->assertTrue(null != $document && 0 < \strlen($document->getText()));
343
344
        // force garbage collection
345
        $this->fixture = $document = null;
346
        gc_collect_cycles();
347
348
        /*
349
         * check false
350
         */
351
        $config = new Config();
352
        $config->setRetainImageContent(false);
353
        $this->fixture = new Parser([], $config);
354
        $this->assertEquals($config, $this->fixture->getConfig());
355
356
        for ($i = 0; $i < $iterations; ++$i) {
357
            $document = $this->fixture->parseFile($filename);
358
        }
359
360
        $usedMemory = memory_get_usage(true);
361
        /*
362
         * note: the following memory value is set manually and may differ from system to system.
363
         *       it must be high enough to not produce a false negative though.
364
         */
365
        $this->assertTrue($usedMemory < ($baselineMemory * 1.05), 'Memory is '.$usedMemory);
366
        $this->assertTrue(0 < \strlen($document->getText()));
367
    }
368
369
    /**
370
     * Test for not showing some characters "ö, ü and ç " in some files
371
     * 
372
     * @see https://github.com/smalot/pdfparser/issues/495
373
     */
374
    public function testIssue495(): void
375
    {
376
        $filename2021113033 = $this->rootDir.'/samples/bugs/Issue495_20211130-3-3.pdf';
377
        $filename20211130422 = $this->rootDir.'/samples/bugs/Issue495_20211130-4-22.pdf';
378
        $filename20211201420 = $this->rootDir.'/samples/bugs/Issue495_20211201-4-20.pdf';
379
380
        $document2021113033 = $this->fixture->parseFile($filename2021113033);
381
        $document20211130422 = $this->fixture->parseFile($filename20211130422);
382
        $document20211201420 = $this->fixture->parseFile($filename20211201420);
383
384
        $this->assertStringContainsString('İhale 14.12.2021 tarihinde Saat 11.00’de yapılacaktır. Katılmak isteyenler 2886 sayılı Devlet İhale Kanununun 36ncı maddesine istinaden tahmin edilen bedel Genel Bütçe Kanununun 45’inci maddesinde belirtilen bedeli aştığından ihale için verilecek “Kapalı Teklif Usulü” ile yapılması gerektiğinden, ihaleden bir gün önce yani 13.12.2021 tarihi Saat:16.00’ya kadar tekliflerini ve istenilen belgeleri dış zarf içerisinde Emlak ve İstimlak Müdürlüğü’ne teslim etmeleri gerekir. 13.12.2021 Pazartesi Günü Saat 16.00’dan sonra teklif zarfı kabul edilmeyecek ve teklifler ihale dışı kalacaktır. 2886 sayılı Devlet İhale Kanununun 37nci maddesindeki hükümler esas alınacaktır', $document2021113033->getText());
385
        $this->assertStringContainsString('Edebiyat veya Fen-Edebiyat Fakültelerinin Tarih Bölümü mezunu olup Orta Çağ Tarihi alanında yüksek lisans yapıyor olmak.', $document20211130422->getText());
386
        $this->assertStringContainsString('Çocuk Cerrahisi', $document20211201420->getText());
387
    }
388
389
}
390
391
class ParserSub extends Parser
392
{
393
    public function exposedParseObject($id, $structure, $document)
394
    {
395
        return $this->parseObject($id, $structure, $document);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->parseObject($id, $structure, $document) targeting Smalot\PdfParser\Parser::parseObject() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
396
    }
397
398
    public function getObjects(): array
399
    {
400
        return $this->objects;
401
    }
402
}
403