|
10 | 10 |
|
11 | 11 | namespace Slim\Tests; |
12 | 12 |
|
13 | | -use Prophecy\Argument; |
14 | | -use Prophecy\Prophecy\ObjectProphecy; |
15 | 13 | use Psr\Container\ContainerInterface; |
16 | 14 | use Psr\Http\Message\ResponseInterface; |
17 | 15 | use Psr\Http\Message\ServerRequestInterface; |
|
30 | 28 | class TwigMiddlewareTest extends TestCase |
31 | 29 | { |
32 | 30 | /** |
33 | | - * Create a twig prophecy given a uri prophecy and a base path. |
| 31 | + * Create a twig mock given a uri mock and a base path. |
34 | 32 | * |
35 | | - * @param ObjectProphecy $uriProphecy |
| 33 | + * @param UriInterface $uri |
36 | 34 | * @param string $basePath |
37 | 35 | * |
38 | | - * @return ObjectProphecy&Twig |
| 36 | + * @return Twig |
39 | 37 | */ |
40 | | - private function createTwigProphecy(ObjectProphecy $uriProphecy, string $basePath) |
| 38 | + private function createTwigMock(UriInterface $uri, string $basePath) |
41 | 39 | { |
42 | 40 | $self = $this; |
43 | 41 |
|
44 | | - $twigProphecy = $this->prophesize(Twig::class); |
| 42 | + $twigMock = $this->createMock(Twig::class); |
45 | 43 |
|
46 | | - $twigProphecy |
47 | | - ->addRuntimeLoader(Argument::type(RuntimeLoaderInterface::class)) |
48 | | - ->will(function ($args) use ($self, $uriProphecy, $basePath) { |
| 44 | + $twigMock->expects($this->once()) |
| 45 | + ->method('addRuntimeLoader') |
| 46 | + ->with($this->isInstanceOf(RuntimeLoaderInterface::class)) |
| 47 | + ->willReturnCallback(function ($runtimeLoader) use ($self, $uri, $basePath) { |
49 | 48 | /** @var TwigRuntimeLoader $runtimeLoader */ |
50 | | - $runtimeLoader = $args[0]; |
51 | 49 | $runtimeExtension = $runtimeLoader->load(TwigRuntimeExtension::class); |
52 | 50 |
|
53 | 51 | $self->assertInstanceOf(TwigRuntimeExtension::class, $runtimeExtension); |
54 | 52 |
|
55 | 53 | /** @var TwigRuntimeExtension $runtimeExtension */ |
56 | | - $self->assertSame($uriProphecy->reveal(), $runtimeExtension->getUri()); |
| 54 | + $self->assertSame($uri, $runtimeExtension->getUri()); |
57 | 55 | $self->assertSame($basePath, $runtimeExtension->getBasePath()); |
58 | | - }) |
59 | | - ->shouldBeCalledOnce(); |
| 56 | + }); |
60 | 57 |
|
61 | | - return $twigProphecy; |
| 58 | + return $twigMock; |
62 | 59 | } |
63 | 60 |
|
64 | 61 | public function testCreateFromContainer() |
@@ -164,79 +161,78 @@ public function testCreate() |
164 | 161 | public function testProcess() |
165 | 162 | { |
166 | 163 | $basePath = '/base-path'; |
167 | | - $uriProphecy = $this->prophesize(UriInterface::class); |
168 | | - $twigProphecy = $this->createTwigProphecy($uriProphecy, $basePath); |
169 | | - $routeParserProphecy = $this->prophesize(RouteParserInterface::class); |
| 164 | + $uri = $this->createMock(UriInterface::class); |
| 165 | + $twig = $this->createTwigMock($uri, $basePath); |
| 166 | + $routeParser = $this->createMock(RouteParserInterface::class); |
170 | 167 |
|
171 | 168 | $twigMiddleware = new TwigMiddleware( |
172 | | - $twigProphecy->reveal(), |
173 | | - $routeParserProphecy->reveal(), |
| 169 | + $twig, |
| 170 | + $routeParser, |
174 | 171 | $basePath |
175 | 172 | ); |
176 | 173 |
|
177 | | - $responseProphecy = $this->prophesize(ResponseInterface::class); |
| 174 | + $response = $this->createMock(ResponseInterface::class); |
178 | 175 |
|
179 | | - $requestProphecy = $this->prophesize(ServerRequestInterface::class); |
180 | | - $requestProphecy |
181 | | - ->getUri() |
182 | | - ->willReturn($uriProphecy->reveal()) |
183 | | - ->shouldBeCalledOnce(); |
| 176 | + $request = $this->createMock(ServerRequestInterface::class); |
| 177 | + $request->expects($this->once()) |
| 178 | + ->method('getUri') |
| 179 | + ->willReturn($uri); |
184 | 180 |
|
185 | | - $requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class); |
186 | | - $requestHandlerProphecy |
187 | | - ->handle($requestProphecy->reveal()) |
188 | | - ->shouldBeCalledOnce() |
189 | | - ->willReturn($responseProphecy->reveal()); |
| 181 | + $requestHandler = $this->createMock(RequestHandlerInterface::class); |
| 182 | + $requestHandler->expects($this->once()) |
| 183 | + ->method('handle') |
| 184 | + ->with($request) |
| 185 | + ->willReturn($response); |
190 | 186 |
|
191 | | - $twigMiddleware->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal()); |
| 187 | + $twigMiddleware->process($request, $requestHandler); |
192 | 188 | } |
193 | 189 |
|
194 | 190 | public function testProcessWithRequestAttribute() |
195 | 191 | { |
196 | 192 | $routeParser = $this->createMock(RouteParserInterface::class); |
197 | | - $uriProphecy = $this->prophesize(UriInterface::class); |
| 193 | + $uri = $this->createMock(UriInterface::class); |
198 | 194 |
|
199 | 195 | /** @var Twig $twig */ |
200 | | - $twig = $this->createTwigProphecy($uriProphecy, '')->reveal(); |
| 196 | + $twig = $this->createTwigMock($uri, ''); |
201 | 197 |
|
202 | 198 | $twigMiddleware = new TwigMiddleware($twig, $routeParser, '', 'view'); |
203 | 199 |
|
204 | | - $responseProphecy = $this->prophesize(ResponseInterface::class); |
| 200 | + $response = $this->createMock(ResponseInterface::class); |
205 | 201 |
|
206 | | - // Prophesize the server request that would be returned in the `withAttribute` method. |
207 | | - $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); |
| 202 | + // Create the server request that would be returned in the `withAttribute` method. |
| 203 | + $request2 = $this->createMock(ServerRequestInterface::class); |
208 | 204 |
|
209 | | - // Prophesize the server request. |
210 | | - $requestProphecy = $this->prophesize(ServerRequestInterface::class); |
211 | | - $requestProphecy->withAttribute('view', Argument::type(Twig::class)) |
212 | | - ->shouldBeCalledOnce() |
213 | | - ->will(function ($args) use ($requestProphecy2): ServerRequestInterface { |
214 | | - $requestProphecy2->getAttribute('view') |
215 | | - ->shouldBeCalledOnce() |
216 | | - ->willReturn($args[1]); |
| 205 | + // Create the server request. |
| 206 | + $request = $this->createMock(ServerRequestInterface::class); |
| 207 | + $request->expects($this->once()) |
| 208 | + ->method('withAttribute') |
| 209 | + ->with('view', $this->isInstanceOf(Twig::class)) |
| 210 | + ->willReturnCallback(function ($name, $value) use ($request2) { |
| 211 | + $request2->expects($this->once()) |
| 212 | + ->method('getAttribute') |
| 213 | + ->with('view') |
| 214 | + ->willReturn($value); |
217 | 215 |
|
218 | | - return $requestProphecy2->reveal(); |
| 216 | + return $request2; |
219 | 217 | }); |
220 | 218 |
|
221 | | - $requestProphecy |
222 | | - ->getUri() |
223 | | - ->willReturn($uriProphecy->reveal()) |
224 | | - ->shouldBeCalledOnce(); |
| 219 | + $request->expects($this->once()) |
| 220 | + ->method('getUri') |
| 221 | + ->willReturn($uri); |
225 | 222 |
|
226 | | - // Prophesize the request handler. |
227 | | - $requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class); |
| 223 | + // Create the request handler. |
| 224 | + $requestHandler = $this->createMock(RequestHandlerInterface::class); |
228 | 225 | $that = $this; |
229 | | - $requestHandlerProphecy |
230 | | - ->handle($requestProphecy2->reveal()) |
231 | | - ->shouldBeCalledOnce() |
232 | | - ->will(function ($args) use ($that, $twig, $responseProphecy): ResponseInterface { |
| 226 | + $requestHandler->expects($this->once()) |
| 227 | + ->method('handle') |
| 228 | + ->with($request2) |
| 229 | + ->willReturnCallback(function ($serverRequest) use ($that, $twig, $response): ResponseInterface { |
233 | 230 | /** @var ServerRequestInterface $serverRequest */ |
234 | | - $serverRequest = $args[0]; |
235 | 231 | $that->assertSame($twig, $serverRequest->getAttribute('view')); |
236 | 232 |
|
237 | | - return $responseProphecy->reveal(); |
| 233 | + return $response; |
238 | 234 | }); |
239 | 235 |
|
240 | | - $twigMiddleware->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal()); |
| 236 | + $twigMiddleware->process($request, $requestHandler); |
241 | 237 | } |
242 | 238 | } |
0 commit comments