From 76e605c729eca5781adf762173047d6f1620367b Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Sat, 22 Feb 2025 22:12:10 +0100 Subject: [PATCH] chubbyphp-mock-v2 --- composer.json | 8 +- tests/Unit/StaticFileMiddlewareTest.php | 206 +++++++++++++----------- 2 files changed, 112 insertions(+), 102 deletions(-) diff --git a/composer.json b/composer.json index c6fd229..3babde3 100644 --- a/composer.json +++ b/composer.json @@ -22,12 +22,12 @@ }, "require-dev": { "chubbyphp/chubbyphp-dev-helper": "dev-master", - "chubbyphp/chubbyphp-mock": "^1.8", - "infection/infection": "^0.29.8", + "chubbyphp/chubbyphp-mock": "^2.0@dev", + "infection/infection": "^0.29.12", "php-coveralls/php-coveralls": "^2.7.0", "phpstan/extension-installer": "^1.4.3", - "phpstan/phpstan": "^2.0.3", - "phpunit/phpunit": "^11.5.0" + "phpstan/phpstan": "^2.1.6", + "phpunit/phpunit": "^11.5.9" }, "autoload": { "psr-4": { "Chubbyphp\\StaticFile\\": "src/" } diff --git a/tests/Unit/StaticFileMiddlewareTest.php b/tests/Unit/StaticFileMiddlewareTest.php index cc84c73..cacffe4 100644 --- a/tests/Unit/StaticFileMiddlewareTest.php +++ b/tests/Unit/StaticFileMiddlewareTest.php @@ -4,10 +4,10 @@ namespace Chubbyphp\Tests\Unit\StaticFile; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockMethod\WithReturnSelf; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\StaticFile\StaticFileMiddleware; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; @@ -23,31 +23,31 @@ */ final class StaticFileMiddlewareTest extends TestCase { - use MockByCallsTrait; - public function testIsNotReadable(): void { $publicDirectory = sys_get_temp_dir(); $requestTarget = '/'.uniqid().uniqid().'.xml'; - /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class, [ - Call::create('getRequestTarget')->with()->willReturn($requestTarget), + $builder = new MockObjectBuilder(); + + /** @var ServerRequestInterface $request */ + $request = $builder->create(ServerRequestInterface::class, [ + new WithReturn('getRequestTarget', [], $requestTarget), ]); - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class); + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, []); - /** @var MockObject|RequestHandlerInterface $handler */ - $handler = $this->getMockByCalls(RequestHandlerInterface::class, [ - Call::create('handle')->with($request)->willReturn($response), + /** @var RequestHandlerInterface $handler */ + $handler = $builder->create(RequestHandlerInterface::class, [ + new WithReturn('handle', [$request], $response), ]); - /** @var MockObject|ResponseFactoryInterface $responseFactory */ - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class); + /** @var ResponseFactoryInterface $responseFactory */ + $responseFactory = $builder->create(ResponseFactoryInterface::class, []); - /** @var MockObject|StreamFactoryInterface $streamFactory */ - $streamFactory = $this->getMockByCalls(StreamFactoryInterface::class); + /** @var StreamFactoryInterface $streamFactory */ + $streamFactory = $builder->create(StreamFactoryInterface::class, []); $middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory); @@ -62,11 +62,13 @@ public function testInvalidHashAlgorythm(): void $publicDirectory = sys_get_temp_dir(); $hashAlgorithm = 'unknown'; - /** @var MockObject|ResponseFactoryInterface $responseFactory */ - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class); + $builder = new MockObjectBuilder(); - /** @var MockObject|StreamFactoryInterface $streamFactory */ - $streamFactory = $this->getMockByCalls(StreamFactoryInterface::class); + /** @var ResponseFactoryInterface $responseFactory */ + $responseFactory = $builder->create(ResponseFactoryInterface::class, []); + + /** @var StreamFactoryInterface $streamFactory */ + $streamFactory = $builder->create(StreamFactoryInterface::class, []); new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory, $hashAlgorithm); } @@ -85,37 +87,39 @@ public function testIfMatch(string $body, string $contentLength, ?string $conten $hash = hash_file($hashAlgorithm, $filename); - /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class, [ - Call::create('getRequestTarget')->with()->willReturn($requestTarget), - Call::create('getHeaderLine')->with('If-None-Match')->willReturn($hash), + $builder = new MockObjectBuilder(); + + /** @var ServerRequestInterface $request */ + $request = $builder->create(ServerRequestInterface::class, [ + new WithReturn('getRequestTarget', [], $requestTarget), + new WithReturn('getHeaderLine', ['If-None-Match'], $hash), ]); if (null !== $contentType) { - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class, [ - Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(), - Call::create('withHeader')->with('Content-Type', $contentType)->willReturnSelf(), - Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(), + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, [ + new WithReturnSelf('withHeader', ['Content-Length', $contentLength]), + new WithReturnSelf('withHeader', ['Content-Type', $contentType]), + new WithReturnSelf('withHeader', ['ETag', $hash]), ]); } else { - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class, [ - Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(), - Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(), + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, [ + new WithReturnSelf('withHeader', ['Content-Length', $contentLength]), + new WithReturnSelf('withHeader', ['ETag', $hash]), ]); } - /** @var MockObject|RequestHandlerInterface $handler */ - $handler = $this->getMockByCalls(RequestHandlerInterface::class); + /** @var RequestHandlerInterface $handler */ + $handler = $builder->create(RequestHandlerInterface::class, []); - /** @var MockObject|ResponseFactoryInterface $responseFactory */ - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [ - Call::create('createResponse')->with(304, '')->willReturn($response), + /** @var ResponseFactoryInterface $responseFactory */ + $responseFactory = $builder->create(ResponseFactoryInterface::class, [ + new WithReturn('createResponse', [304, ''], $response), ]); - /** @var MockObject|StreamFactoryInterface $streamFactory */ - $streamFactory = $this->getMockByCalls(StreamFactoryInterface::class); + /** @var StreamFactoryInterface $streamFactory */ + $streamFactory = $builder->create(StreamFactoryInterface::class, []); $middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory, $hashAlgorithm); @@ -139,37 +143,39 @@ public function testIfMatchWithDefaultHashAlgorithm( $hash = hash_file('md5', $filename); - /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class, [ - Call::create('getRequestTarget')->with()->willReturn($requestTarget), - Call::create('getHeaderLine')->with('If-None-Match')->willReturn($hash), + $builder = new MockObjectBuilder(); + + /** @var ServerRequestInterface $request */ + $request = $builder->create(ServerRequestInterface::class, [ + new WithReturn('getRequestTarget', [], $requestTarget), + new WithReturn('getHeaderLine', ['If-None-Match'], $hash), ]); if (null !== $contentType) { - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class, [ - Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(), - Call::create('withHeader')->with('Content-Type', $contentType)->willReturnSelf(), - Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(), + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, [ + new WithReturnSelf('withHeader', ['Content-Length', $contentLength]), + new WithReturnSelf('withHeader', ['Content-Type', $contentType]), + new WithReturnSelf('withHeader', ['ETag', $hash]), ]); } else { - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class, [ - Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(), - Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(), + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, [ + new WithReturnSelf('withHeader', ['Content-Length', $contentLength]), + new WithReturnSelf('withHeader', ['ETag', $hash]), ]); } - /** @var MockObject|RequestHandlerInterface $handler */ - $handler = $this->getMockByCalls(RequestHandlerInterface::class); + /** @var RequestHandlerInterface $handler */ + $handler = $builder->create(RequestHandlerInterface::class, []); - /** @var MockObject|ResponseFactoryInterface $responseFactory */ - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [ - Call::create('createResponse')->with(304, '')->willReturn($response), + /** @var ResponseFactoryInterface $responseFactory */ + $responseFactory = $builder->create(ResponseFactoryInterface::class, [ + new WithReturn('createResponse', [304, ''], $response), ]); - /** @var MockObject|StreamFactoryInterface $streamFactory */ - $streamFactory = $this->getMockByCalls(StreamFactoryInterface::class); + /** @var StreamFactoryInterface $streamFactory */ + $streamFactory = $builder->create(StreamFactoryInterface::class, []); $middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory); @@ -190,43 +196,45 @@ public function testIfNoneMatch(string $body, string $contentLength, ?string $co $hash = hash_file($hashAlgorithm, $filename); - /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class, [ - Call::create('getRequestTarget')->with()->willReturn($requestTarget), - Call::create('getHeaderLine')->with('If-None-Match')->willReturn(''), + $builder = new MockObjectBuilder(); + + /** @var ServerRequestInterface $request */ + $request = $builder->create(ServerRequestInterface::class, [ + new WithReturn('getRequestTarget', [], $requestTarget), + new WithReturn('getHeaderLine', ['If-None-Match'], ''), ]); - /** @var MockObject|StreamInterface $responseBody */ - $responseBody = $this->getMockByCalls(StreamInterface::class); + /** @var StreamInterface $responseBody */ + $responseBody = $builder->create(StreamInterface::class, []); if (null !== $contentType) { - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class, [ - Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(), - Call::create('withHeader')->with('Content-Type', $contentType)->willReturnSelf(), - Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(), - Call::create('withBody')->with($responseBody)->willReturnSelf(), + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, [ + new WithReturnSelf('withHeader', ['Content-Length', $contentLength]), + new WithReturnSelf('withHeader', ['Content-Type', $contentType]), + new WithReturnSelf('withHeader', ['ETag', $hash]), + new WithReturnSelf('withBody', [$responseBody]), ]); } else { - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class, [ - Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(), - Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(), - Call::create('withBody')->with($responseBody)->willReturnSelf(), + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, [ + new WithReturnSelf('withHeader', ['Content-Length', $contentLength]), + new WithReturnSelf('withHeader', ['ETag', $hash]), + new WithReturnSelf('withBody', [$responseBody]), ]); } - /** @var MockObject|RequestHandlerInterface $handler */ - $handler = $this->getMockByCalls(RequestHandlerInterface::class); + /** @var RequestHandlerInterface $handler */ + $handler = $builder->create(RequestHandlerInterface::class, []); - /** @var MockObject|ResponseFactoryInterface $responseFactory */ - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [ - Call::create('createResponse')->with(200, '')->willReturn($response), + /** @var ResponseFactoryInterface $responseFactory */ + $responseFactory = $builder->create(ResponseFactoryInterface::class, [ + new WithReturn('createResponse', [200, ''], $response), ]); - /** @var MockObject|StreamFactoryInterface $streamFactory */ - $streamFactory = $this->getMockByCalls(StreamFactoryInterface::class, [ - Call::create('createStreamFromFile')->with($filename, 'r')->willReturn($responseBody), + /** @var StreamFactoryInterface $streamFactory */ + $streamFactory = $builder->create(StreamFactoryInterface::class, [ + new WithReturn('createStreamFromFile', [$filename, 'r'], $responseBody), ]); $middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory, $hashAlgorithm); @@ -239,24 +247,26 @@ public function testBaseDirectory(): void $publicDirectory = sys_get_temp_dir(); $requestTarget = '/'; - /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class, [ - Call::create('getRequestTarget')->with()->willReturn($requestTarget), + $builder = new MockObjectBuilder(); + + /** @var ServerRequestInterface $request */ + $request = $builder->create(ServerRequestInterface::class, [ + new WithReturn('getRequestTarget', [], $requestTarget), ]); - /** @var MockObject|ResponseInterface $response */ - $response = $this->getMockByCalls(ResponseInterface::class); + /** @var ResponseInterface $response */ + $response = $builder->create(ResponseInterface::class, []); - /** @var MockObject|RequestHandlerInterface $handler */ - $handler = $this->getMockByCalls(RequestHandlerInterface::class, [ - Call::create('handle')->with($request)->willReturn($response), + /** @var RequestHandlerInterface $handler */ + $handler = $builder->create(RequestHandlerInterface::class, [ + new WithReturn('handle', [$request], $response), ]); - /** @var MockObject|ResponseFactoryInterface $responseFactory */ - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class); + /** @var ResponseFactoryInterface $responseFactory */ + $responseFactory = $builder->create(ResponseFactoryInterface::class, []); - /** @var MockObject|StreamFactoryInterface $streamFactory */ - $streamFactory = $this->getMockByCalls(StreamFactoryInterface::class); + /** @var StreamFactoryInterface $streamFactory */ + $streamFactory = $builder->create(StreamFactoryInterface::class, []); $middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory);