|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the RequestListenerTest class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @version //autogentag// |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace eZ\Bundle\EzPublishRestBundle\Tests\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Server\View\AcceptHeaderVisitorDispatcher; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
16
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
17
|
|
|
use eZ\Bundle\EzPublishRestBundle\EventListener\RequestListener; |
|
18
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
19
|
|
|
|
|
20
|
|
|
class RequestListenerTest extends EventListenerTest |
|
21
|
|
|
{ |
|
22
|
|
|
const REST_ROUTE = '/api/ezp/v2/rest-route'; |
|
23
|
|
|
const NON_REST_ROUTE = '/non-rest-route'; |
|
24
|
|
|
|
|
25
|
|
|
public function provideExpectedSubscribedEventTypes() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
[ |
|
29
|
|
|
[KernelEvents::REQUEST], |
|
30
|
|
|
], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
public static function restRequestUrisProvider() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
['/api/ezp/v2/true'], |
|
38
|
|
|
['/api/bundle-name/v2/true'], |
|
39
|
|
|
['/api/MyBundle12/v2/true'], |
|
40
|
|
|
['/api/ThisIs_Bundle123/v2/true'], |
|
41
|
|
|
['/api/my-bundle/v1/true'], |
|
42
|
|
|
['/api/my-bundle/v2/true'], |
|
43
|
|
|
['/api/my-bundle/v2.7/true'], |
|
44
|
|
|
['/api/my-bundle/v122.73/true'], |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public static function nonRestRequestsUrisProvider() |
|
49
|
|
|
{ |
|
50
|
|
|
return [ |
|
51
|
|
|
['/ap/ezp/v2/false'], |
|
52
|
|
|
['/api/bundle name/v2/false'], |
|
53
|
|
|
['/api/My/Bundle/v2/false'], |
|
54
|
|
|
['/api//v2/false'], |
|
55
|
|
|
['/api/my-bundle/v/false'], |
|
56
|
|
|
['/api/my-bundle/v2-2/false'], |
|
57
|
|
|
['/api/my-bundle/v2 7/false'], |
|
58
|
|
|
['/api/my-bundle/v/7/false'], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testOnKernelRequestNotMasterRequest() |
|
63
|
|
|
{ |
|
64
|
|
|
$request = $this->performFakeRequest(self::REST_ROUTE, HttpKernelInterface::SUB_REQUEST); |
|
65
|
|
|
|
|
66
|
|
|
self::assertTrue($request->attributes->get('is_rest_request')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testOnKernelRequestNotRestRequest() |
|
70
|
|
|
{ |
|
71
|
|
|
$request = $this->performFakeRequest(self::NON_REST_ROUTE); |
|
72
|
|
|
|
|
73
|
|
|
self::assertFalse($request->attributes->get('is_rest_request')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testOnKernelRequestRestRequest() |
|
77
|
|
|
{ |
|
78
|
|
|
$request = $this->performFakeRequest(self::REST_ROUTE); |
|
79
|
|
|
|
|
80
|
|
|
self::assertTrue($request->attributes->get('is_rest_request')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @dataProvider restRequestUrisProvider |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testRestRequestVariations($uri) |
|
87
|
|
|
{ |
|
88
|
|
|
$request = $this->performFakeRequest($uri); |
|
89
|
|
|
|
|
90
|
|
|
self::assertTrue($request->attributes->get('is_rest_request')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @dataProvider nonRestRequestsUrisProvider |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testNonRestRequestVariations($uri) |
|
97
|
|
|
{ |
|
98
|
|
|
$request = $this->performFakeRequest($uri); |
|
99
|
|
|
|
|
100
|
|
|
self::assertFalse($request->attributes->get('is_rest_request')); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return RequestListener |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getEventListener() |
|
107
|
|
|
{ |
|
108
|
|
|
return new RequestListener( |
|
|
|
|
|
|
109
|
|
|
$this->getVisitorDispatcherMock() |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return AcceptHeaderVisitorDispatcher|\PHPUnit_Framework_MockObject_MockObject |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getVisitorDispatcherMock() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getMock( |
|
119
|
|
|
'eZ\Publish\Core\REST\Server\View\AcceptHeaderVisitorDispatcher' |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return Request |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function performFakeRequest($uri, $type = HttpKernelInterface::MASTER_REQUEST) |
|
127
|
|
|
{ |
|
128
|
|
|
$event = new GetResponseEvent( |
|
129
|
|
|
$this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), |
|
130
|
|
|
Request::create($uri), |
|
131
|
|
|
$type |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$this->getEventListener()->onKernelRequest($event); |
|
135
|
|
|
|
|
136
|
|
|
return $event->getRequest(); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.