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