Completed
Push — pr/238 ( 9ef5ec )
by Konrad
03:54
created

ParserTest::testIssue229()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 8
rs 10
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\Parser;
37
use Test\Smalot\PdfParser\TestCase;
38
39
class ParserTest extends TestCase
40
{
41
    public function setUp()
42
    {
43
        parent::setUp();
44
45
        $this->fixture = new Parser();
46
    }
47
48
    public function testParseFile()
49
    {
50
        $directory = $this->rootDir.'/samples/bugs';
51
52
        if (is_dir($directory)) {
53
            $files = scandir($directory);
54
55
            foreach ($files as $file) {
56
                if (preg_match('/^.*\.pdf$/i', $file)) {
57
                    try {
58
                        $document = $this->fixture->parseFile($directory.'/'.$file);
59
                        $pages = $document->getPages();
60
                        $this->assertTrue(0 < \count($pages));
61
62
                        foreach ($pages as $page) {
63
                            $content = $page->getText();
64
                            $this->assertTrue(0 < \strlen($content));
65
                        }
66
                    } catch (Exception $e) {
67
                        if (
68
                            'Secured pdf file are currently not supported.' !== $e->getMessage()
69
                            && 0 != strpos($e->getMessage(), 'TCPDF_PARSER')
70
                        ) {
71
                            throw $e;
72
                        }
73
                    }
74
                }
75
            }
76
        }
77
    }
78
79
    /**
80
     * Parsing certain PDFs may lead to following error:
81
     *
82
     *      mb_convert_encoding(): Illegal character encoding specified
83
     *
84
     * @see https://github.com/smalot/pdfparser/issues/229
85
     */
86
    public function testIssue229()
87
    {
88
        $filename = $this->rootDir.'/samples/bugs/Issue229_mac_roman_encoding.pdf';
89
90
        $document = $this->fixture->parseFile($filename);
91
92
        // calls Font::decodeContent and uses function mb_convert_encoding
93
        $document->getPages()[0]->extractDecodedRawData();
94
    }
95
96
    /**
97
     * Parsing certain PDFs may lead to following notices:
98
     *
99
     *      Notice: Trying to access array offset on value of type int
100
     *
101
     * and to an exception:
102
     *
103
     *      Missing catalog.
104
     *
105
     * @see https://github.com/smalot/pdfparser/issues/267
106
     */
107
    public function testIssue267()
108
    {
109
        $this->expectException(Exception::class);
110
        $this->expectExceptionMessage('Missing catalog.');
111
112
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
113
114
        $document = $this->fixture->parseFile($filename);
115
116
        // triggers the exception
117
        $document->getPages();
118
    }
119
}
120