Passed
Push — master ( 6135c2...320582 )
by Konrad
08:01
created

testInitGetEncodingClassMissingClassException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
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\Document;
37
use Smalot\PdfParser\Element;
38
use Smalot\PdfParser\Encoding;
39
use Smalot\PdfParser\Encoding\StandardEncoding;
40
use Smalot\PdfParser\Header;
41
use Tests\Smalot\PdfParser\TestCase;
42
43
class EncodingTest extends TestCase
44
{
45
    public function testGetEncodingClass()
46
    {
47
        $header = new Header(['BaseEncoding' => new Element('StandardEncoding')]);
48
49
        $encoding = new Encoding(new Document(), $header);
50
        $encoding->init();
51
52
        $this->assertEquals('\\'.StandardEncoding::class, $encoding->__toString());
53
    }
54
55
    /**
56
     * This tests checks behavior if given Encoding class doesn't exist.
57
     *
58
     * Protected method getEncodingClass is called in init and __toString.
59
     * It throws an exception if class is not available.
60
     * Calling init is enough to trigger the exception, but __toString call afterwards
61
     * makes sure that we don't missing it.
62
     */
63
    public function testInitGetEncodingClassMissingClassException()
64
    {
65
        $this->expectException(Exception::class);
66
        $this->expectExceptionMessage('Missing encoding data for: "invalid"');
67
68
        $header = new Header(['BaseEncoding' => new Element('invalid')]);
69
70
        $encoding = new Encoding(new Document(), $header);
71
        $encoding->init();
72
73
        $encoding->__toString();
74
    }
75
76
    /**
77
     * This tests focuses on behavior of Encoding::__toString when running PHP 7.4+ and prior.
78
     *
79
     * Prior PHP 7.4 we expect an empty string to be returned (based on PHP specification).
80
     * PHP 7.4+ we expect an exception to be thrown when class is invalid.
81
     */
82
    public function testToStringGetEncodingClassMissingClassException()
83
    {
84
        // prior to PHP 7.4 toString has to return an empty string.
85
        if (version_compare(\PHP_VERSION, '7.4.0', '<')) {
86
            $header = new Header(['BaseEncoding' => new Element('invalid')]);
87
88
            $encoding = new Encoding(new Document(), $header);
89
90
            $this->assertEquals('', $encoding->__toString());
91
        } else {
92
            // PHP 7.4+
93
            $this->expectException(Exception::class);
94
            $this->expectExceptionMessage('Missing encoding data for: "invalid"');
95
96
            $header = new Header(['BaseEncoding' => new Element('invalid')]);
97
98
            $encoding = new Encoding(new Document(), $header);
99
100
            $encoding->__toString();
101
        }
102
    }
103
}
104