Passed
Pull Request — master (#345)
by
unknown
01:57
created

TestCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 22
c 5
b 0
f 1
dl 0
loc 75
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A catchAllErrors() 0 2 1
A tearDown() 0 10 2
A getParserInstance() 0 3 1
A getElementInstance() 0 3 1
A __construct() 0 12 1
A getDocumentInstance() 0 3 1
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
            restore_error_handler();
58
            $this->fail(
59
                sprintf('"%s" in %s:%d',
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 getDocumentInstance()
103
    {
104
        return new Document();
105
    }
106
107
    protected function getElementInstance($value)
108
    {
109
        return new Element($value);
110
    }
111
112
    protected function getParserInstance()
113
    {
114
        return new Parser();
115
    }
116
}
117