Passed
Pull Request — 2.7 (#7956)
by Luís
13:50
created

assertNotHasDeprecationMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests;
6
7
use const E_USER_DEPRECATED;
8
use function in_array;
9
use function set_error_handler;
10
11
trait VerifyDeprecations
12
{
13
    /** @var string[] */
14
    private $expectedDeprecations = [];
15
16
    /** @var string[] */
17
    private $actualDeprecations = [];
18
19
    /** @var string[] */
20
    private $ignoredDeprecations = [];
21
22
    /** @var callable|null */
23
    private $originalHandler;
24
25
    /** @before */
26
    public function resetDeprecations() : void
27
    {
28
        $this->actualDeprecations   = [];
29
        $this->expectedDeprecations = [];
30
        $this->ignoredDeprecations  = [];
31
32
        $this->originalHandler = set_error_handler(
33
            function (int $errorNumber, string $errorMessage) : void {
34
                if (in_array($errorMessage, $this->ignoredDeprecations, true)) {
35
                    return;
36
                }
37
38
                $this->actualDeprecations[] = $errorMessage;
39
            },
40
            E_USER_DEPRECATED
41
        );
42
    }
43
44
    /** @after */
45
    public function resetErrorHandler() : void
46
    {
47
        set_error_handler($this->originalHandler, E_USER_DEPRECATED);
48
        $this->originalHandler = null;
49
    }
50
51
    /** @after */
52
    public function validateDeprecationExpectations() : void
53
    {
54
        if ($this->expectedDeprecations === []) {
55
            return;
56
        }
57
58
        self::assertSame(
59
            $this->expectedDeprecations,
60
            $this->actualDeprecations,
61
            'Triggered deprecation messages do not match with expected ones.'
62
        );
63
    }
64
65
    protected function ignoreDeprecationMessage(string $message) : void
66
    {
67
        $this->ignoredDeprecations[] = $message;
68
    }
69
70
    protected function expectDeprecationMessage(string $message) : void
71
    {
72
        $this->expectedDeprecations[] = $message;
73
    }
74
75
    protected function assertHasDeprecationMessages() : void
76
    {
77
        self::assertNotSame([], $this->actualDeprecations, 'Failed asserting that test has triggered deprecation messages.');
78
    }
79
80
    protected function assertNotHasDeprecationMessages() : void
81
    {
82
        self::assertSame([], $this->actualDeprecations, 'Failed asserting that test has not triggered deprecation messages.');
83
    }
84
}
85