1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use Loevgaard\DandomainAltapayBundle\EventListener\ControllerListener; |
6
|
|
|
use Loevgaard\DandomainAltapayBundle\EventListener\ResponseListener; |
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Http\TransactionLogger; |
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Tests\EventListener\Fixture\ControllerWithAnnotation; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
14
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
15
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
16
|
|
|
|
17
|
|
|
class ResponseListenerTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Request |
21
|
|
|
*/ |
22
|
|
|
protected $request; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Response |
26
|
|
|
*/ |
27
|
|
|
protected $response; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResponseListener |
31
|
|
|
*/ |
32
|
|
|
protected $listener; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TransactionLogger|\PHPUnit_Framework_MockObject_MockObject |
36
|
|
|
*/ |
37
|
|
|
protected $transactionLogger; |
38
|
|
|
|
39
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->transactionLogger = $this->getMockBuilder(TransactionLogger::class) |
42
|
|
|
->disableOriginalConstructor() |
43
|
|
|
->getMock() |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
$this->listener = new ResponseListener($this->transactionLogger); |
47
|
|
|
$this->request = new Request(); |
48
|
|
|
$this->response = new Response(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function tearDown() |
52
|
|
|
{ |
53
|
|
|
$this->listener = null; |
54
|
|
|
$this->request = null; |
55
|
|
|
$this->response = null; |
56
|
|
|
$this->transactionLogger = null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testNotMasterRequest() |
60
|
|
|
{ |
61
|
|
|
$event = $this->getFilterResponseEvent($this->request, $this->response, HttpKernelInterface::SUB_REQUEST); |
62
|
|
|
$res = $this->listener->onKernelResponse($event); |
63
|
|
|
$this->assertTrue($res === false); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSetResponse() |
67
|
|
|
{ |
68
|
|
|
$calls = 0; |
69
|
|
|
|
70
|
|
|
$this->transactionLogger |
|
|
|
|
71
|
|
|
->expects($this->any()) |
72
|
|
|
->method('setResponse') |
73
|
|
|
->willReturnCallback(function () use (&$calls) { |
74
|
|
|
$calls++; |
75
|
|
|
}); |
76
|
|
|
; |
77
|
|
|
|
78
|
|
|
$event = $this->getFilterResponseEvent($this->request, $this->response); |
79
|
|
|
$this->listener->onKernelResponse($event); |
80
|
|
|
|
81
|
|
|
$this->assertSame(1, $calls); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getFilterResponseEvent(Request $request, Response $response, int $requestType = HttpKernelInterface::MASTER_REQUEST) |
85
|
|
|
{ |
86
|
|
|
$mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', '')); |
87
|
|
|
|
88
|
|
|
return new FilterResponseEvent($mockKernel, $request, $requestType, $response); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.