Passed
Pull Request — master (#345)
by
unknown
03:52 queued 01:39
created

TestCase::catchAllErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 2
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
        // this is a workaround for PHP providing a way to directly get the current error handler,
78
        // by temporarily setting it to var_dump and immediately resetting it.
79
        // This is also used in PHPUnit's phpunit-bridge, see
80
        // https://github.com/symfony/phpunit-bridge/blob/b6c713f26fd7ec6d0c77934e812f8c3d181e2fd2/DeprecationErrorHandler.php#L183-L184
81
        $currentErrorHandler = set_error_handler('var_dump');
82
        restore_error_handler();
83
        if($currentErrorHandler === [$this, 'catchAllErrorHandler']) {
84
            restore_error_handler();
85
        }
86
    }
87
88
    /**
89
     * This temporarily changes the PHP-internal error handler
90
     * in order to allow catching errors of type E_WARNING, E_NOTICE etc.,
91
     * which are not catchable via a try/catch statement.
92
     * It will fail the current test from which it is run,
93
     * giving a descriptive message.
94
     *
95
     * This can come in handy to make tests for making sure that such
96
     * errors are not triggered by the code.
97
     */
98
    protected function catchAllErrors() {
99
        set_error_handler($this->catchAllErrorHandler);
100
    }
101
102
    protected function getErrorType($typeNumber) {
103
        $errorConstants = [
104
            1 => 'E_ERROR',
105
            2 => 'E_WARNING',
106
            4 => 'E_PARSE',
107
            8 => 'E_NOTICE',
108
            16 => 'E_CORE_ERROR',
109
            32 => 'E_CORE_WARNING',
110
            64 => 'E_COMPILE_ERROR',
111
            128 => 'E_COMPILE_WARNING',
112
            256 => 'E_USER_ERROR',
113
            512 => 'E_USER_WARNING',
114
            1024 => 'E_USER_NOTICE',
115
            2048 => 'E_STRICT',
116
            4096 => 'E_RECOVERABLE_ERROR',
117
            8192 => 'E_DEPRECATED',
118
            16384 => 'E_USER_DEPRECATED',
119
            32767 => 'E_ALL'
120
        ];
121
        return $errorConstants[$typeNumber];
122
    }
123
124
    protected function getDocumentInstance()
125
    {
126
        return new Document();
127
    }
128
129
    protected function getElementInstance($value)
130
    {
131
        return new Element($value);
132
    }
133
134
    protected function getParserInstance()
135
    {
136
        return new Parser();
137
    }
138
}
139