Test Failed
Push — master ( 70433d...3ef8bc )
by Konrad
02:17
created

EncodingTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

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