1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
6
|
|
|
use Loevgaard\DandomainAltapayBundle\EventListener\ExceptionListener; |
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\Exception; |
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
13
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
14
|
|
|
|
15
|
|
|
class ExceptionListenerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EngineInterface|\PHPUnit_Framework_MockObject_MockObject |
19
|
|
|
*/ |
20
|
|
|
protected $engine; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ExceptionListener |
24
|
|
|
*/ |
25
|
|
|
protected $listener; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Request |
29
|
|
|
*/ |
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->engine = $this->getMockForAbstractClass(EngineInterface::class); |
35
|
|
|
|
36
|
|
|
// the engines render method will always return 'response' concatenated with the redirect variable |
37
|
|
|
$this->engine |
38
|
|
|
->expects($this->any()) |
39
|
|
|
->method('render') |
40
|
|
|
->willReturnCallback(function ($template, $parameters) { |
41
|
|
|
return 'response'.$parameters['redirect']; |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
$this->listener = new ExceptionListener($this->engine); |
|
|
|
|
45
|
|
|
$this->request = new Request(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function tearDown() |
49
|
|
|
{ |
50
|
|
|
$this->engine = null; |
51
|
|
|
$this->listener = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testNotBundleException() |
55
|
|
|
{ |
56
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, new \Exception()); |
57
|
|
|
$res = $this->listener->onKernelException($event); |
58
|
|
|
$this->assertTrue(false === $res); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testNormalException() |
62
|
|
|
{ |
63
|
|
|
$exception = $this->getMockForAbstractClass(Exception::class); |
64
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, $exception); |
|
|
|
|
65
|
|
|
$this->listener->onKernelException($event); |
66
|
|
|
|
67
|
|
|
$this->assertSame('response', $event->getResponse()->getContent()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function testPaymentExceptionWithReferrer() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$payment = $this->getMockForAbstractClass(Payment::class); |
73
|
|
|
$payment->setReferrer('referrer'); |
74
|
|
|
|
75
|
|
|
$exception = PaymentException::create('exception', $this->request, $payment); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, $exception); |
78
|
|
|
$this->listener->onKernelException($event); |
79
|
|
|
|
80
|
|
|
$this->assertSame('responsereferrer', $event->getResponse()->getContent()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testPaymentExceptionWithCallbackServerUrl() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$payment = $this->getMockForAbstractClass(Payment::class); |
86
|
|
|
$payment->setCallBackServerUrl('example.com'); |
87
|
|
|
|
88
|
|
|
$exception = PaymentException::create('exception', $this->request, $payment); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, $exception); |
91
|
|
|
$this->listener->onKernelException($event); |
92
|
|
|
|
93
|
|
|
$this->assertSame('responsehttp://example.com', $event->getResponse()->getContent()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function getGetResponseForExceptionEvent(Request $request, \Exception $e) |
97
|
|
|
{ |
98
|
|
|
$mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', ['', '']); |
99
|
|
|
|
100
|
|
|
return new GetResponseForExceptionEvent($mockKernel, $request, HttpKernelInterface::MASTER_REQUEST, $e); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: