Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 7.4, 8.0, 8.1, 8.2, 8.3 ]
php: [ 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5 ]
include:
- php: 8.2
- php: 8.4
analysis: true

steps:
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"twig/twig": "^3.11"
},
"require-dev": {
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.10.59",
"phpunit/phpunit": "^9.6 || ^10",
"psr/http-factory": "^1.0",
Expand Down
7 changes: 3 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@

namespace Slim\Tests;

use Prophecy\PhpUnit\ProphecyTrait;
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
use ReflectionProperty;

use function get_class;

abstract class TestCase extends PhpUnitTestCase
{
use ProphecyTrait;

protected function assertInaccessiblePropertySame($expected, $obj, string $name)
{
$prop = new ReflectionProperty(get_class($obj), $name);
$prop->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$prop->setAccessible(true);
}
$this->assertSame($expected, $prop->getValue($obj));
}
}
114 changes: 55 additions & 59 deletions tests/TwigMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Slim\Tests;

use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -30,35 +28,34 @@
class TwigMiddlewareTest extends TestCase
{
/**
* Create a twig prophecy given a uri prophecy and a base path.
* Create a twig mock given a uri mock and a base path.
*
* @param ObjectProphecy $uriProphecy
* @param UriInterface $uri
* @param string $basePath
*
* @return ObjectProphecy&Twig
* @return Twig
*/
private function createTwigProphecy(ObjectProphecy $uriProphecy, string $basePath)
private function createTwigMock(UriInterface $uri, string $basePath)
{
$self = $this;

$twigProphecy = $this->prophesize(Twig::class);
$twigMock = $this->createMock(Twig::class);

$twigProphecy
->addRuntimeLoader(Argument::type(RuntimeLoaderInterface::class))
->will(function ($args) use ($self, $uriProphecy, $basePath) {
$twigMock->expects($this->once())
->method('addRuntimeLoader')
->with($this->isInstanceOf(RuntimeLoaderInterface::class))
->willReturnCallback(function ($runtimeLoader) use ($self, $uri, $basePath) {
/** @var TwigRuntimeLoader $runtimeLoader */
$runtimeLoader = $args[0];
$runtimeExtension = $runtimeLoader->load(TwigRuntimeExtension::class);

$self->assertInstanceOf(TwigRuntimeExtension::class, $runtimeExtension);

/** @var TwigRuntimeExtension $runtimeExtension */
$self->assertSame($uriProphecy->reveal(), $runtimeExtension->getUri());
$self->assertSame($uri, $runtimeExtension->getUri());
$self->assertSame($basePath, $runtimeExtension->getBasePath());
})
->shouldBeCalledOnce();
});

return $twigProphecy;
return $twigMock;
}

public function testCreateFromContainer()
Expand Down Expand Up @@ -164,79 +161,78 @@ public function testCreate()
public function testProcess()
{
$basePath = '/base-path';
$uriProphecy = $this->prophesize(UriInterface::class);
$twigProphecy = $this->createTwigProphecy($uriProphecy, $basePath);
$routeParserProphecy = $this->prophesize(RouteParserInterface::class);
$uri = $this->createMock(UriInterface::class);
$twig = $this->createTwigMock($uri, $basePath);
$routeParser = $this->createMock(RouteParserInterface::class);

$twigMiddleware = new TwigMiddleware(
$twigProphecy->reveal(),
$routeParserProphecy->reveal(),
$twig,
$routeParser,
$basePath
);

$responseProphecy = $this->prophesize(ResponseInterface::class);
$response = $this->createMock(ResponseInterface::class);

$requestProphecy = $this->prophesize(ServerRequestInterface::class);
$requestProphecy
->getUri()
->willReturn($uriProphecy->reveal())
->shouldBeCalledOnce();
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())
->method('getUri')
->willReturn($uri);

$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
$requestHandlerProphecy
->handle($requestProphecy->reveal())
->shouldBeCalledOnce()
->willReturn($responseProphecy->reveal());
$requestHandler = $this->createMock(RequestHandlerInterface::class);
$requestHandler->expects($this->once())
->method('handle')
->with($request)
->willReturn($response);

$twigMiddleware->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
$twigMiddleware->process($request, $requestHandler);
}

public function testProcessWithRequestAttribute()
{
$routeParser = $this->createMock(RouteParserInterface::class);
$uriProphecy = $this->prophesize(UriInterface::class);
$uri = $this->createMock(UriInterface::class);

/** @var Twig $twig */
$twig = $this->createTwigProphecy($uriProphecy, '')->reveal();
$twig = $this->createTwigMock($uri, '');

$twigMiddleware = new TwigMiddleware($twig, $routeParser, '', 'view');

$responseProphecy = $this->prophesize(ResponseInterface::class);
$response = $this->createMock(ResponseInterface::class);

// Prophesize the server request that would be returned in the `withAttribute` method.
$requestProphecy2 = $this->prophesize(ServerRequestInterface::class);
// Create the server request that would be returned in the `withAttribute` method.
$request2 = $this->createMock(ServerRequestInterface::class);

// Prophesize the server request.
$requestProphecy = $this->prophesize(ServerRequestInterface::class);
$requestProphecy->withAttribute('view', Argument::type(Twig::class))
->shouldBeCalledOnce()
->will(function ($args) use ($requestProphecy2): ServerRequestInterface {
$requestProphecy2->getAttribute('view')
->shouldBeCalledOnce()
->willReturn($args[1]);
// Create the server request.
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())
->method('withAttribute')
->with('view', $this->isInstanceOf(Twig::class))
->willReturnCallback(function ($name, $value) use ($request2) {
$request2->expects($this->once())
->method('getAttribute')
->with('view')
->willReturn($value);

return $requestProphecy2->reveal();
return $request2;
});

$requestProphecy
->getUri()
->willReturn($uriProphecy->reveal())
->shouldBeCalledOnce();
$request->expects($this->once())
->method('getUri')
->willReturn($uri);

// Prophesize the request handler.
$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
// Create the request handler.
$requestHandler = $this->createMock(RequestHandlerInterface::class);
$that = $this;
$requestHandlerProphecy
->handle($requestProphecy2->reveal())
->shouldBeCalledOnce()
->will(function ($args) use ($that, $twig, $responseProphecy): ResponseInterface {
$requestHandler->expects($this->once())
->method('handle')
->with($request2)
->willReturnCallback(function ($serverRequest) use ($that, $twig, $response): ResponseInterface {
/** @var ServerRequestInterface $serverRequest */
$serverRequest = $args[0];
$that->assertSame($twig, $serverRequest->getAttribute('view'));

return $responseProphecy->reveal();
return $response;
});

$twigMiddleware->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
$twigMiddleware->process($request, $requestHandler);
}
}
54 changes: 19 additions & 35 deletions tests/TwigRuntimeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,10 @@ public function testIsCurrentUrl(string $pattern, array $data, string $path, ?st
$routeName = 'route';
$this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);

$uriProphecy = $this->prophesize(UriInterface::class);

$uriProphecy
->getPath()
->willReturn($path)
->shouldBeCalledOnce();

/** @var UriInterface $uri */
$uri = $uriProphecy->reveal();
$uri = $this->createMock(UriInterface::class);
$uri->expects($this->once())
->method('getPath')
->willReturn($path);

$extension = new TwigRuntimeExtension($routeParser, $uri, $basePath);
$result = $extension->isCurrentUrl($routeName, $data);
Expand Down Expand Up @@ -135,29 +130,24 @@ public function testCurrentUrl(string $pattern, string $url, string $basePath, b
$routeName = 'route';
$this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);

$uriProphecy = $this->prophesize(UriInterface::class);
$uri = $this->createMock(UriInterface::class);

$path = parse_url($url, PHP_URL_PATH);
$query = parse_url($url, PHP_URL_QUERY) ?? '';

$uriProphecy
->getPath()
->willReturn($path)
->shouldBeCalledOnce();
$uri->expects($this->once())
->method('getPath')
->willReturn($path);

$uriProphecy
->getQuery()
->willReturn($query)
->shouldBeCalledOnce();
$uri->expects($this->once())
->method('getQuery')
->willReturn($query);

$expected = $basePath . $path;
if ($withQueryString) {
$expected .= '?' . $query;
}

/** @var UriInterface $uri */
$uri = $uriProphecy->reveal();

$extension = new TwigRuntimeExtension($routeParser, $uri, $basePath);
$result = $extension->getCurrentUrl($withQueryString);

Expand Down Expand Up @@ -195,10 +185,7 @@ public function testUrlFor(
$routeCollector = $this->createRouteCollector($basePath);
$this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);

$uriProphecy = $this->prophesize(UriInterface::class);

/** @var UriInterface $uri */
$uri = $uriProphecy->reveal();
$uri = $this->createMock(UriInterface::class);

$extension = new TwigRuntimeExtension($routeCollector->getRouteParser(), $uri, $routeCollector->getBasePath());
$this->assertEquals($expectedUrl, $extension->urlFor($routeName, $routeData, $queryParams));
Expand Down Expand Up @@ -241,18 +228,15 @@ public function testFullUrlFor(
$routeCollector = $this->createRouteCollector($basePath);
$this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);

$uriProphecy = $this->prophesize(UriInterface::class);

$uriProphecy->getScheme()
->willReturn('http')
->shouldBeCalledOnce();
$uri = $this->createMock(UriInterface::class);

$uriProphecy->getAuthority()
->willReturn('localhost')
->shouldBeCalledOnce();
$uri->expects($this->once())
->method('getScheme')
->willReturn('http');

/** @var UriInterface $uri */
$uri = $uriProphecy->reveal();
$uri->expects($this->once())
->method('getAuthority')
->willReturn('localhost');

$extension = new TwigRuntimeExtension($routeCollector->getRouteParser(), $uri, $routeCollector->getBasePath());
$this->assertEquals($expectedFullUrl, $extension->fullUrlFor($routeName, $routeData, $queryParams));
Expand Down
32 changes: 16 additions & 16 deletions tests/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ public function testFromRequest()
{
$twig = $this->createMock(Twig::class);

$serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$serverRequestProphecy->getAttribute('view')
$serverRequest = $this->createMock(ServerRequestInterface::class);
$serverRequest->expects($this->once())
->method('getAttribute')
->with('view')
->willReturn($twig);

/** @var ServerRequestInterface $serverRequest */
$serverRequest = $serverRequestProphecy->reveal();
$this->assertSame($twig, Twig::fromRequest($serverRequest));
}

public function testFromRequestCustomAttributeName()
{
$twig = $this->createMock(Twig::class);

$serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$serverRequestProphecy->getAttribute('foo')
$serverRequest = $this->createMock(ServerRequestInterface::class);
$serverRequest->expects($this->once())
->method('getAttribute')
->with('foo')
->willReturn($twig);

/** @var ServerRequestInterface $serverRequest */
$serverRequest = $serverRequestProphecy->reveal();
$this->assertSame($twig, Twig::fromRequest($serverRequest, 'foo'));
}

Expand All @@ -54,12 +54,12 @@ public function testFromRequestTwigNotFound()
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Twig could not be found in the server request attributes using the key "view".');

$serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$serverRequestProphecy->getAttribute('view')
$serverRequest = $this->createMock(ServerRequestInterface::class);
$serverRequest->expects($this->once())
->method('getAttribute')
->with('view')
->willReturn(null);

/** @var ServerRequestInterface $serverRequest */
$serverRequest = $serverRequestProphecy->reveal();
Twig::fromRequest($serverRequest);
}

Expand All @@ -68,12 +68,12 @@ public function testFromRequestNotTwig()
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Twig could not be found in the server request attributes using the key "view".');

$serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$serverRequestProphecy->getAttribute('view')
$serverRequest = $this->createMock(ServerRequestInterface::class);
$serverRequest->expects($this->once())
->method('getAttribute')
->with('view')
->willReturn('twiggy');

/** @var ServerRequestInterface $serverRequest */
$serverRequest = $serverRequestProphecy->reveal();
Twig::fromRequest($serverRequest);
}

Expand Down