Passed
Pull Request — master (#342)
by
unknown
01:54
created

ParserTest::testParseFile()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 24
rs 8.4444
cc 8
nc 16
nop 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\Parser;
37
use Smalot\PdfParser\XObject\Image;
38
use Tests\Smalot\PdfParser\TestCase;
39
40
class ParserTest extends TestCase
41
{
42
    public function setUp()
43
    {
44
        parent::setUp();
45
46
        $this->fixture = new Parser();
47
    }
48
49
    public function testParseFile()
50
    {
51
        $directory = $this->rootDir.'/samples/bugs';
52
53
        if (is_dir($directory)) {
54
            $files = scandir($directory);
55
56
            foreach ($files as $file) {
57
                if (preg_match('/^.*\.pdf$/i', $file)) {
58
                    try {
59
                        $document = $this->fixture->parseFile($directory.'/'.$file);
60
                        $pages = $document->getPages();
61
                        $this->assertTrue(0 < \count($pages));
62
63
                        foreach ($pages as $page) {
64
                            $content = $page->getText();
65
                            $this->assertTrue(0 < \strlen($content));
66
                        }
67
                    } catch (Exception $e) {
68
                        if (
69
                            'Secured pdf file are currently not supported.' !== $e->getMessage()
70
                            && 0 != strpos($e->getMessage(), 'TCPDF_PARSER')
71
                        ) {
72
                            throw $e;
73
                        }
74
                    }
75
                }
76
            }
77
        }
78
    }
79
80
    /**
81
     * Test that issue related pdf can now be parsed
82
     *
83
     * @see https://github.com/smalot/pdfparser/issues/267
84
     */
85
    public function testIssue267()
86
    {
87
        $filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf';
88
89
        $document = $this->fixture->parseFile($filename);
90
91
        $this->assertEquals(Image::class, \get_class($document->getObjectById('128_0')));
92
        $this->assertStringContainsString('4 von 4', $document->getText());
93
    }
94
95
    /**
96
     * Test that issue related pdf can now be parsed
97
     *
98
     * @see https://github.com/smalot/pdfparser/issues/322
99
     */
100
    public function testIssue322()
101
    {
102
        $filename = $this->rootDir.'/samples/bugs/Issue322.pdf';
103
104
        $document = $this->fixture->parseFile($filename);
105
106
        $this->assertStringContainsString('this text isn’t working properly, I’ve edited it in Google Documents', $document->getText());
107
    }
108
109
    /**
110
     * Test that issue related pdf can now be parsed
111
     * License of the content taken from https://stackoverflow.com in the sample PDF:
112
     * CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5/
113
     *
114
     * @see https://github.com/smalot/pdfparser/issues/334
115
     */
116
    public function testIssue334()
117
    {
118
        $filename = $this->rootDir.'/samples/bugs/Issue334.pdf';
119
120
        $document = $this->fixture->parseFile($filename);
121
122
        $this->assertStringContainsString('This question already has an answer here', $document->getText());
123
    }
124
}
125