Completed
Pull Request — master (#345)
by
unknown
03:55
created

TestCase::getElementInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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-02
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;
34
35
use PHPUnit\Framework\TestCase as PHPTestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use Smalot\PdfParser\Document;
37
use Smalot\PdfParser\Element;
38
use Smalot\PdfParser\Parser;
39
40
abstract class TestCase extends PHPTestCase
41
{
42
    /**
43
     * Contains an instance of the class to test.
44
     */
45
    protected $fixture;
46
47
    protected $rootDir;
48
49
    private $catchAllErrorHandler;
50
51
    function __construct() {
52
        parent::__construct();
53
54
        // PHP does not implement setting a class property to an anonymous function,
55
        // so we have to do it in the constructor.
56
        $this->catchAllErrorHandler = function ($typeNumber, $message, $file, $lineNumber) {
57
            $this->fail(
58
                sprintf('%s: "%s" in %s:%d',
59
                    $this->getErrorType($typeNumber),
60
                    $message,
61
                    $file,
62
                    $lineNumber
63
                )
64
            );
65
        };
66
    }
67
68
    public function setUp()
69
    {
70
        parent::setUp();
71
72
        $this->rootDir = __DIR__.'/..';
73
    }
74
75
    public function tearDown() {
76
        // if we changed the error handler using catchAllErrors(), reset it now
77
        $currentErrorHandler = set_error_handler('var_dump');
78
        restore_error_handler();
79
        if($currentErrorHandler === [$this, 'catchAllErrorHandler']) {
80
            restore_error_handler();
81
        }
82
    }
83
84
    /**
85
     * This temporarily changes the PHP-internal error handler
86
     * in order to allow catching errors of type E_WARNING, E_NOTICE etc.,
87
     * which are not catchable via a try/catch statement.
88
     * It will fail the current test from which it is run,
89
     * giving a descriptive message.
90
     *
91
     * This can come in handy to make tests for making sure that such
92
     * errors are not triggered by the code.
93
     */
94
    protected function catchAllErrors() {
95
        set_error_handler($this->catchAllErrorHandler);
96
    }
97
98
    protected function getErrorType($typeNumber) {
99
        $errorConstants = [
100
            1 => 'E_ERROR',
101
            2 => 'E_WARNING',
102
            4 => 'E_PARSE',
103
            8 => 'E_NOTICE',
104
            16 => 'E_CORE_ERROR',
105
            32 => 'E_CORE_WARNING',
106
            64 => 'E_COMPILE_ERROR',
107
            128 => 'E_COMPILE_WARNING',
108
            256 => 'E_USER_ERROR',
109
            512 => 'E_USER_WARNING',
110
            1024 => 'E_USER_NOTICE',
111
            2048 => 'E_STRICT',
112
            4096 => 'E_RECOVERABLE_ERROR',
113
            8192 => 'E_DEPRECATED',
114
            16384 => 'E_USER_DEPRECATED',
115
            32767 => 'E_ALL'
116
        ];
117
        return $errorConstants[$typeNumber];
118
    }
119
120
    protected function getDocumentInstance()
121
    {
122
        return new Document();
123
    }
124
125
    protected function getElementInstance($value)
126
    {
127
        return new Element($value);
128
    }
129
130
    protected function getParserInstance()
131
    {
132
        return new Parser();
133
    }
134
}
135