Passed
Pull Request — master (#310)
by
unknown
02:17
created

ParserTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 25
c 2
b 0
f 1
dl 0
loc 62
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testParseFile() 0 24 8
A setUp() 0 5 1
A testIssue267() 0 11 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\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 notices:
81
     *
82
     *      Notice: Trying to access array offset on value of type int
83
     *
84
     * and to an exception:
85
     *
86
     *      Missing catalog.
87
     *
88
     * @see https://github.com/smalot/pdfparser/issues/267
89
     */
90
    public function testIssue267()
91
    {
92
        $this->expectException(Exception::class);
93
        $this->expectExceptionMessage('Missing catalog.');
94
95
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
96
97
        $document = $this->fixture->parseFile($filename);
98
99
        // triggers the exception
100
        $document->getPages();
101
    }
102
}
103