Completed
Pull Request — master (#203)
by Tobias
34:49
created

DeprecationListenerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 66
dl 0
loc 108
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Ekino New Relic bundle.
7
 *
8
 * (c) Ekino - Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\NewRelicBundle\Tests\Listener;
15
16
use Ekino\NewRelicBundle\Exception\DeprecationException;
17
use Ekino\NewRelicBundle\Listener\DeprecationListener;
18
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
19
use PHPUnit\Framework\TestCase;
20
21
class DeprecationListenerTest extends TestCase
22
{
23
    public function testDeprecationIsReported()
24
    {
25
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
26
        $interactor->expects($this->once())->method('noticeThrowable')->with($this->isInstanceOf(DeprecationException::class));
27
28
        $listener = new DeprecationListener($interactor);
29
30
        \set_error_handler(function () {
31
            return false;
32
        });
33
34
        try {
35
            $listener->register();
36
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
37
        } finally {
38
            $listener->unregister();
39
            \restore_error_handler();
40
        }
41
    }
42
43
    public function testDeprecationIsReportedRegardlessErrorReporting()
44
    {
45
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
46
        $interactor->expects($this->once())->method('noticeThrowable');
47
48
        $listener = new DeprecationListener($interactor);
49
50
        \set_error_handler(function () {
51
            return false;
52
        });
53
        $e = \error_reporting(0);
54
55
        try {
56
            $listener->register();
57
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
58
        } finally {
59
            $listener->unregister();
60
            \error_reporting($e);
61
            \restore_error_handler();
62
        }
63
    }
64
65
    public function testOtherErrorAreIgnored()
66
    {
67
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
68
        $interactor->expects($this->never())->method('noticeThrowable');
69
70
        $listener = new DeprecationListener($interactor);
71
72
        \set_error_handler(function () {
73
            return false;
74
        });
75
76
        try {
77
            $listener->register();
78
            @\trigger_error('This is a notice', E_USER_NOTICE);
79
        } finally {
80
            $listener->unregister();
81
            \restore_error_handler();
82
        }
83
    }
84
85
    public function testInitialHandlerIsCalled()
86
    {
87
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
88
        $interactor->expects($this->once())->method('noticeThrowable');
89
90
        $handler = $this->createPartialMock(\stdClass::class, ['__invoke']);
91
        $handler->expects($this->once())->method('__invoke');
92
93
        $listener = new DeprecationListener($interactor);
94
95
        \set_error_handler($handler);
96
97
        try {
98
            $listener->register();
99
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
100
        } finally {
101
            $listener->unregister();
102
            \restore_error_handler();
103
        }
104
    }
105
106
    public function testUnregisterRemovesHandler()
107
    {
108
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
109
        $interactor->expects($this->never())->method('noticeThrowable');
110
111
        $listener = new DeprecationListener($interactor);
112
113
        \set_error_handler(function () {
114
            return false;
115
        });
116
117
        try {
118
            $listener->register();
119
            $listener->unregister();
120
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
121
        } finally {
122
            \restore_error_handler();
123
        }
124
    }
125
126
    public function testUnregisterRestorePreviousHandler()
127
    {
128
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
129
130
        $handler = $this->createPartialMock(\stdClass::class, ['__invoke']);
131
        $handler->expects($this->once())->method('__invoke');
132
133
        $listener = new DeprecationListener($interactor);
134
135
        \set_error_handler($handler);
136
137
        try {
138
            $listener->register();
139
            $listener->unregister();
140
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
141
        } finally {
142
            \restore_error_handler();
143
        }
144
    }
145
}
146