Test Failed
Push — master ( 4b70df...4db3b8 )
by Konrad
02:46
created

EncodingTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 *
8
 * @date    2020-06-01
9
 *
10
 * @author  Sébastien MALOT <[email protected]>
11
 *
12
 * @date    2017-01-03
13
 *
14
 * @license LGPLv3
15
 *
16
 * @url     <https://github.com/smalot/pdfparser>
17
 *
18
 *  PdfParser is a pdf library written in PHP, extraction oriented.
19
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
20
 *
21
 *  This program is free software: you can redistribute it and/or modify
22
 *  it under the terms of the GNU Lesser General Public License as published by
23
 *  the Free Software Foundation, either version 3 of the License, or
24
 *  (at your option) any later version.
25
 *
26
 *  This program is distributed in the hope that it will be useful,
27
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 *  GNU Lesser General Public License for more details.
30
 *
31
 *  You should have received a copy of the GNU Lesser General Public License
32
 *  along with this program.
33
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
34
 */
35
36
namespace PHPUnitTests\Integration;
37
38
use PHPUnitTests\TestCase;
39
use Smalot\PdfParser\Document;
40
use Smalot\PdfParser\Element;
41
use Smalot\PdfParser\Encoding;
42
use Smalot\PdfParser\Encoding\StandardEncoding;
43
use Smalot\PdfParser\Exception\EncodingNotFoundException;
44
use Smalot\PdfParser\Header;
45
use Smalot\PdfParser\Parser;
46
47
class EncodingTest extends TestCase
48
{
49
    protected function setUp(): void
50
    {
51
        parent::setUp();
52
53
        $this->fixture = new Parser();
54
    }
55
56
    public function testGetEncodingClass(): void
57
    {
58
        $header = new Header(['BaseEncoding' => new Element('StandardEncoding')]);
59
60
        $encoding = new Encoding(new Document(), $header);
61
        $encoding->init();
62
63
        $this->assertEquals('\\'.StandardEncoding::class, $encoding->__toString());
64
    }
65
66
    /**
67
     * This tests checks behavior if given Encoding class doesn't exist.
68
     *
69
     * Protected method getEncodingClass is called in init and __toString.
70
     * It throws an exception if class is not available.
71
     * Calling init is enough to trigger the exception, but __toString call afterwards
72
     * makes sure that we don't missing it.
73
     */
74
    public function testInitGetEncodingClassMissingClassException(): void
75
    {
76
        $this->expectException(EncodingNotFoundException::class);
77
        $this->expectExceptionMessage('Missing encoding data for: "invalid"');
78
79
        $header = new Header(['BaseEncoding' => new Element('invalid')]);
80
81
        $encoding = new Encoding(new Document(), $header);
82
        $encoding->init();
83
84
        $encoding->__toString();
85
    }
86
87
    /**
88
     * This tests focuses on behavior of Encoding::__toString when running PHP 7.4+ and prior.
89
     *
90
     * Prior PHP 7.4 we expect an empty string to be returned (based on PHP specification).
91
     * PHP 7.4+ we expect an exception to be thrown when class is invalid.
92
     */
93
    public function testToStringGetEncodingClassMissingClassException(): void
94
    {
95
        // prior to PHP 7.4 toString has to return an empty string.
96
        if (version_compare(\PHP_VERSION, '7.4.0', '<')) {
97
            $header = new Header(['BaseEncoding' => new Element('invalid')]);
98
99
            $encoding = new Encoding(new Document(), $header);
100
101
            $this->assertEquals('', $encoding->__toString());
102
        } else {
103
            // PHP 7.4+
104
            $this->expectException(\Exception::class);
105
            $this->expectExceptionMessage('Missing encoding data for: "invalid"');
106
107
            $header = new Header(['BaseEncoding' => new Element('invalid')]);
108
109
            $encoding = new Encoding(new Document(), $header);
110
111
            $encoding->__toString();
112
        }
113
    }
114
115
    /**
116
     * Fall back to 'StandardEncoding' when the document has none
117
     *
118
     * @see https://github.com/smalot/pdfparser/issues/665
119
     */
120
    public function testEmptyBaseEncodingFallback(): void
121
    {
122
        $filename = $this->rootDir.'/samples/bugs/Issue665.pdf';
123
124
        $document = $this->fixture->parseFile($filename);
125
        $objects = $document->getObjects();
126
127
        $this->assertEquals(25, \count($objects));
128
        $this->assertArrayHasKey('3_0', $objects);
129
    }
130
}
131