diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3703ef1..981940e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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: diff --git a/composer.json b/composer.json index a917e10..8d63670 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/tests/TestCase.php b/tests/TestCase.php index d4de5a8..c9de350 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,7 +10,6 @@ namespace Slim\Tests; -use Prophecy\PhpUnit\ProphecyTrait; use PHPUnit\Framework\TestCase as PhpUnitTestCase; use ReflectionProperty; @@ -18,12 +17,12 @@ 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)); } } diff --git a/tests/TwigMiddlewareTest.php b/tests/TwigMiddlewareTest.php index 0a6c2f8..a730c31 100644 --- a/tests/TwigMiddlewareTest.php +++ b/tests/TwigMiddlewareTest.php @@ -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; @@ -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() @@ -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); } } diff --git a/tests/TwigRuntimeExtensionTest.php b/tests/TwigRuntimeExtensionTest.php index b68660a..a6f1d4d 100644 --- a/tests/TwigRuntimeExtensionTest.php +++ b/tests/TwigRuntimeExtensionTest.php @@ -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); @@ -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); @@ -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)); @@ -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)); diff --git a/tests/TwigTest.php b/tests/TwigTest.php index 661f992..d2f3cf8 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -27,12 +27,12 @@ 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)); } @@ -40,12 +40,12 @@ 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')); } @@ -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); } @@ -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); }