Completed
Push — master ( 558007...566e39 )
by Jérémy
23s queued 11s
created

DummyHandler   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 2 1
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;
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...
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(
27
            $this->isInstanceOf(DeprecationException::class)
28
        );
29
30
        $listener = new DeprecationListener($interactor);
31
32
        \set_error_handler(function () { return false; });
33
        try {
34
            $listener->register();
35
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
36
        } finally {
37
            $listener->unregister();
38
            \restore_error_handler();
39
        }
40
    }
41
42
    public function testDeprecationIsReportedRegardlessErrorReporting()
43
    {
44
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
45
        $interactor->expects($this->once())->method('noticeThrowable');
46
47
        $listener = new DeprecationListener($interactor);
48
49
        \set_error_handler(function () { return false; });
50
        $e = \error_reporting(0);
51
        try {
52
            $listener->register();
53
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
54
        } finally {
55
            $listener->unregister();
56
            \error_reporting($e);
57
            \restore_error_handler();
58
        }
59
    }
60
61
    public function testOtherErrorAreIgnored()
62
    {
63
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
64
        $interactor->expects($this->never())->method('noticeThrowable');
65
66
        $listener = new DeprecationListener($interactor);
67
68
        \set_error_handler(function () { return false; });
69
        try {
70
            $listener->register();
71
            @\trigger_error('This is a notice', E_USER_NOTICE);
72
        } finally {
73
            $listener->unregister();
74
            \restore_error_handler();
75
        }
76
    }
77
78
    public function testInitialHandlerIsCalled()
79
    {
80
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
81
        $interactor->expects($this->once())->method('noticeThrowable');
82
83
        $handler = $this->createPartialMock(DummyHandler::class, ['__invoke']);
84
        $handler->expects($this->once())->method('__invoke');
85
86
        $listener = new DeprecationListener($interactor);
87
88
        \set_error_handler($handler);
89
        try {
90
            $listener->register();
91
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
92
        } finally {
93
            $listener->unregister();
94
            \restore_error_handler();
95
        }
96
    }
97
98
    public function testUnregisterRemovesHandler()
99
    {
100
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
101
        $interactor->expects($this->never())->method('noticeThrowable');
102
103
        $listener = new DeprecationListener($interactor);
104
105
        \set_error_handler(function () { return false; });
106
        try {
107
            $listener->register();
108
            $listener->unregister();
109
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
110
        } finally {
111
            \restore_error_handler();
112
        }
113
    }
114
115
    public function testUnregisterRestorePreviousHandler()
116
    {
117
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
118
119
        $handler = $this->createPartialMock(DummyHandler::class, ['__invoke']);
120
        $handler->expects($this->once())->method('__invoke');
121
122
        $listener = new DeprecationListener($interactor);
123
124
        \set_error_handler($handler);
125
        try {
126
            $listener->register();
127
            $listener->unregister();
128
            @\trigger_error('This is a deprecation', E_USER_DEPRECATED);
129
        } finally {
130
            \restore_error_handler();
131
        }
132
    }
133
}
134
135
class DummyHandler
136
{
137
    public function __invoke()
138
    {
139
    }
140
}
141