Test Failed
Pull Request — master (#345)
by
unknown
02:03
created

TestCase::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
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 $errorHandlerChanged = false;
50
51
    private $catchAllErrorHandler;
52
53
    function __construct() {
54
        parent::__construct();
55
56
        // PHP does not implement setting a class property to an anonymous function,
57
        // so we have to do it in the constructor.
58
        $this->catchAllErrorHandler = function ($typeNumber, $message, $file, $lineNumber) {
59
            $this->fail(
60
                sprintf('%s: "%s" in %s:%d',
61
                    $this->getErrorType($typeNumber),
62
                    $message,
63
                    $file,
64
                    $lineNumber
65
                )
66
            );
67
        };
68
    }
69
70
    public function setUp()
71
    {
72
        parent::setUp();
73
74
        $this->rootDir = __DIR__.'/..';
75
    }
76
77
    public function tearDown() {
78
        // if we changed the error handler using catchAllErrors(), reset it now
79
        if($this->errorHandlerChanged) {
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
        $this->errorHandlerChanged = true;
96
        set_error_handler($this->catchAllErrorHandler);
97
    }
98
99
    protected function getErrorType($typeNumber) {
100
        $errorConstants = [
101
            1 => 'E_ERROR',
102
            2 => 'E_WARNING',
103
            4 => 'E_PARSE',
104
            8 => 'E_NOTICE',
105
            16 => 'E_CORE_ERROR',
106
            32 => 'E_CORE_WARNING',
107
            64 => 'E_COMPILE_ERROR',
108
            128 => 'E_COMPILE_WARNING',
109
            256 => 'E_USER_ERROR',
110
            512 => 'E_USER_WARNING',
111
            1024 => 'E_USER_NOTICE',
112
            2048 => 'E_STRICT',
113
            4096 => 'E_RECOVERABLE_ERROR',
114
            8192 => 'E_DEPRECATED',
115
            16384 => 'E_USER_DEPRECATED',
116
            32767 => 'E_ALL'
117
        ];
118
        return $errorConstants[$typeNumber];
119
    }
120
121
    protected function getDocumentInstance()
122
    {
123
        return new Document();
124
    }
125
126
    protected function getElementInstance($value)
127
    {
128
        return new Element($value);
129
    }
130
131
    protected function getParserInstance()
132
    {
133
        return new Parser();
134
    }
135
}
136