1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use Loevgaard\DandomainAltapayBundle\EventListener\ExceptionListener; |
6
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\Exception; |
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
13
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
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
|
|
|
|
45
|
|
|
$this->listener = new ExceptionListener($this->engine); |
46
|
|
|
$this->request = new Request(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function tearDown() |
50
|
|
|
{ |
51
|
|
|
$this->engine = null; |
52
|
|
|
$this->listener = null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testNotBundleException() |
56
|
|
|
{ |
57
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, new \Exception()); |
58
|
|
|
$res = $this->listener->onKernelException($event); |
59
|
|
|
$this->assertTrue($res === false); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testNormalException() |
63
|
|
|
{ |
64
|
|
|
$exception = $this->getMockForAbstractClass(Exception::class); |
65
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, $exception); |
66
|
|
|
$this->listener->onKernelException($event); |
67
|
|
|
|
68
|
|
|
$this->assertSame('response', $event->getResponse()->getContent()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function testPaymentExceptionWithReferrer() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$payment = $this->getMockForAbstractClass(Payment::class); |
74
|
|
|
$payment->setReferrer('referrer'); |
75
|
|
|
|
76
|
|
|
$exception = PaymentException::create('exception', $this->request, $payment); |
77
|
|
|
|
78
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, $exception); |
79
|
|
|
$this->listener->onKernelException($event); |
80
|
|
|
|
81
|
|
|
$this->assertSame('responsereferrer', $event->getResponse()->getContent()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testPaymentExceptionWithCallbackServerUrl() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$payment = $this->getMockForAbstractClass(Payment::class); |
87
|
|
|
$payment->setCallBackServerUrl('example.com'); |
88
|
|
|
|
89
|
|
|
$exception = PaymentException::create('exception', $this->request, $payment); |
90
|
|
|
|
91
|
|
|
$event = $this->getGetResponseForExceptionEvent($this->request, $exception); |
92
|
|
|
$this->listener->onKernelException($event); |
93
|
|
|
|
94
|
|
|
$this->assertSame('responsehttp://example.com', $event->getResponse()->getContent()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getGetResponseForExceptionEvent(Request $request, \Exception $e) |
98
|
|
|
{ |
99
|
|
|
$mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', '')); |
100
|
|
|
|
101
|
|
|
return new GetResponseForExceptionEvent($mockKernel, $request, HttpKernelInterface::MASTER_REQUEST, $e); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.