1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Tests\Security\Http; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
17
|
|
|
use Symfony\Component\Routing\RequestContext; |
18
|
|
|
use Yarhon\RouteGuardBundle\Routing\RouteContext; |
19
|
|
|
use Yarhon\RouteGuardBundle\Routing\GeneratedUrlAwareRouteContext; |
20
|
|
|
use Yarhon\RouteGuardBundle\Security\Http\RequestContextFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class RequestContextFactoryTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
private $requestStack; |
28
|
|
|
|
29
|
|
|
private $request; |
30
|
|
|
|
31
|
|
|
private $urlGenerator; |
32
|
|
|
|
33
|
|
|
private $urlGeneratorContext; |
34
|
|
|
|
35
|
|
|
private $factory; |
36
|
|
|
|
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->requestStack = $this->createMock(RequestStack::class); |
40
|
|
|
$this->request = $this->createMock(Request::class); |
41
|
|
|
|
42
|
|
|
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class); |
43
|
|
|
$this->urlGeneratorContext = $this->createMock(RequestContext::class); |
44
|
|
|
$this->urlGenerator->method('getContext') |
|
|
|
|
45
|
|
|
->willReturn($this->urlGeneratorContext); |
46
|
|
|
|
47
|
|
|
$this->requestStack->method('getCurrentRequest') |
48
|
|
|
->willReturn($this->request); |
49
|
|
|
|
50
|
|
|
$this->factory = new RequestContextFactory($this->requestStack, $this->urlGenerator); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testPathInfoClosure() |
54
|
|
|
{ |
55
|
|
|
$routeContext = new GeneratedUrlAwareRouteContext('main'); |
56
|
|
|
$routeContext->setReferenceType(UrlGeneratorInterface::ABSOLUTE_PATH); |
57
|
|
|
|
58
|
|
|
$context = $this->factory->createContext($routeContext); |
59
|
|
|
|
60
|
|
|
$this->urlGenerator->expects($this->once()) |
61
|
|
|
->method('generate') |
62
|
|
|
->with($routeContext->getName(), $routeContext->getParameters(), $routeContext->getReferenceType()) |
63
|
|
|
->willReturn('http://site.com/foo'); |
64
|
|
|
|
65
|
|
|
$this->assertEquals('/foo', $context->getPathInfo()); |
66
|
|
|
|
67
|
|
|
$this->assertEquals('http://site.com/foo', $routeContext->getGeneratedUrl()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testPathInfoClosureWithRelativePath() |
71
|
|
|
{ |
72
|
|
|
$routeContext = new GeneratedUrlAwareRouteContext('main', [], 'POST'); |
73
|
|
|
$routeContext->setReferenceType(UrlGeneratorInterface::RELATIVE_PATH); |
74
|
|
|
|
75
|
|
|
$context = $this->factory->createContext($routeContext); |
76
|
|
|
|
77
|
|
|
$this->urlGenerator->expects($this->once()) |
78
|
|
|
->method('generate') |
79
|
|
|
->with($routeContext->getName(), $routeContext->getParameters(), UrlGeneratorInterface::ABSOLUTE_URL) |
80
|
|
|
->willReturn('http://site.com/foo'); |
81
|
|
|
|
82
|
|
|
$this->assertEquals('/foo', $context->getPathInfo()); |
83
|
|
|
|
84
|
|
|
$this->assertNull($routeContext->getGeneratedUrl()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testPathInfoClosureWithContextBaseUrl() |
88
|
|
|
{ |
89
|
|
|
$routeContext = new GeneratedUrlAwareRouteContext('main'); |
90
|
|
|
$routeContext->setReferenceType(UrlGeneratorInterface::ABSOLUTE_PATH); |
91
|
|
|
|
92
|
|
|
$context = $this->factory->createContext($routeContext); |
93
|
|
|
|
94
|
|
|
$this->urlGenerator->method('generate') |
95
|
|
|
->willReturn('http://site.com/foo'); |
96
|
|
|
|
97
|
|
|
$this->urlGeneratorContext->method('getBaseUrl') |
98
|
|
|
->willReturn('/foo'); |
99
|
|
|
|
100
|
|
|
$this->assertEquals('/', $context->getPathInfo()); |
101
|
|
|
$this->assertEquals('http://site.com/foo', $routeContext->getGeneratedUrl()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testPathInfoClosureNoReferenceType() |
105
|
|
|
{ |
106
|
|
|
$routeContext = new GeneratedUrlAwareRouteContext('main'); |
107
|
|
|
|
108
|
|
|
$context = $this->factory->createContext($routeContext); |
109
|
|
|
|
110
|
|
|
$this->urlGenerator->expects($this->once()) |
111
|
|
|
->method('generate') |
112
|
|
|
->with($routeContext->getName(), $routeContext->getParameters(), UrlGeneratorInterface::ABSOLUTE_PATH) |
113
|
|
|
->willReturn('http://site.com/foo'); |
114
|
|
|
|
115
|
|
|
$this->assertEquals('/foo', $context->getPathInfo()); |
116
|
|
|
$this->assertNull($routeContext->getGeneratedUrl()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testHostClosure() |
120
|
|
|
{ |
121
|
|
|
$routeContext = new GeneratedUrlAwareRouteContext('main'); |
122
|
|
|
$routeContext->setReferenceType(UrlGeneratorInterface::ABSOLUTE_PATH); |
123
|
|
|
|
124
|
|
|
$context = $this->factory->createContext($routeContext); |
125
|
|
|
|
126
|
|
|
$this->urlGenerator->expects($this->once()) |
127
|
|
|
->method('generate') |
128
|
|
|
->with($routeContext->getName(), $routeContext->getParameters(), $routeContext->getReferenceType()) |
129
|
|
|
->willReturn('http://site.com/foo'); |
130
|
|
|
|
131
|
|
|
$this->assertEquals('site.com', $context->getHost()); |
132
|
|
|
$this->assertEquals('http://site.com/foo', $routeContext->getGeneratedUrl()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testHostClosureWithContextHost() |
136
|
|
|
{ |
137
|
|
|
$routeContext = new GeneratedUrlAwareRouteContext('main'); |
138
|
|
|
$routeContext->setReferenceType(UrlGeneratorInterface::ABSOLUTE_PATH); |
139
|
|
|
|
140
|
|
|
$context = $this->factory->createContext($routeContext); |
141
|
|
|
|
142
|
|
|
$this->urlGenerator->method('generate') |
143
|
|
|
->willReturn('/foo'); |
144
|
|
|
|
145
|
|
|
$this->urlGeneratorContext->method('getHost') |
146
|
|
|
->willReturn('site.com'); |
147
|
|
|
|
148
|
|
|
$this->assertEquals('site.com', $context->getHost()); |
149
|
|
|
$this->assertEquals('/foo', $routeContext->getGeneratedUrl()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testMethod() |
153
|
|
|
{ |
154
|
|
|
$routeContext = new RouteContext('main', [], 'POST'); |
155
|
|
|
|
156
|
|
|
$context = $this->factory->createContext($routeContext); |
157
|
|
|
|
158
|
|
|
$this->assertEquals('POST', $context->getMethod()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testClientIp() |
162
|
|
|
{ |
163
|
|
|
$routeContext = new RouteContext('main'); |
164
|
|
|
|
165
|
|
|
$this->request->method('getClientIp') |
166
|
|
|
->willReturn('127.0.0.1'); |
167
|
|
|
|
168
|
|
|
$context = $this->factory->createContext($routeContext); |
169
|
|
|
|
170
|
|
|
$this->assertEquals('127.0.0.1', $context->getClientIp()); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.