Passed
Pull Request — master (#345)
by
unknown
02:09
created

TestCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 21
c 4
b 0
f 1
dl 0
loc 74
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getParserInstance() 0 3 1
A setUp() 0 5 1
A getElementInstance() 0 3 1
A __construct() 0 11 1
A getDocumentInstance() 0 3 1
A catchAllErrors() 0 2 1
A tearDown() 0 10 2
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" in %s:%d',
59
                    $message,
60
                    $file,
61
                    $lineNumber
62
                )
63
            );
64
        };
65
    }
66
67
    public function setUp()
68
    {
69
        parent::setUp();
70
71
        $this->rootDir = __DIR__.'/..';
72
    }
73
74
    public function tearDown() {
75
        // if we changed the error handler using catchAllErrors(), reset it now
76
        // this is a workaround for PHP providing a way to directly get the current error handler,
77
        // by temporarily setting it to var_dump and immediately resetting it.
78
        // This is also used in PHPUnit's phpunit-bridge, see
79
        // https://github.com/symfony/phpunit-bridge/blob/b6c713f26fd7ec6d0c77934e812f8c3d181e2fd2/DeprecationErrorHandler.php#L183-L184
80
        $currentErrorHandler = set_error_handler('var_dump');
81
        restore_error_handler();
82
        if($currentErrorHandler === [$this, 'catchAllErrorHandler']) {
83
            restore_error_handler();
84
        }
85
    }
86
87
    /**
88
     * This temporarily changes the PHP-internal error handler
89
     * in order to allow catching errors of type E_WARNING, E_NOTICE etc.,
90
     * which are not catchable via a try/catch statement.
91
     * It will fail the current test from which it is run,
92
     * giving a descriptive message.
93
     *
94
     * This can come in handy to make tests for making sure that such
95
     * errors are not triggered by the code.
96
     */
97
    protected function catchAllErrors() {
98
        set_error_handler($this->catchAllErrorHandler);
99
    }
100
101
    protected function getDocumentInstance()
102
    {
103
        return new Document();
104
    }
105
106
    protected function getElementInstance($value)
107
    {
108
        return new Element($value);
109
    }
110
111
    protected function getParserInstance()
112
    {
113
        return new Parser();
114
    }
115
}
116