diff --git a/Makefile b/Makefile index 2acbc01..6eafe70 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ tests: .PHONY: infection infection: - docker-compose run --rm php vendor/bin/infection + docker-compose run --rm php php -d memory_limit=512M vendor/bin/infection .PHONY: psalm psalm: diff --git a/composer.json b/composer.json index 1174387..7dbd12d 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "symfony/yaml": "^8.0" }, "require-dev": { - "phpunit/phpunit": "^11.0", + "phpunit/phpunit": "^13.0", "friendsofphp/php-cs-fixer": "^3.80", "vimeo/psalm": "^6.10", "rector/rector": "^2.0", diff --git a/infection.log b/infection.log deleted file mode 100644 index e8de8fe..0000000 --- a/infection.log +++ /dev/null @@ -1,111 +0,0 @@ -Note: Pass `--log-verbosity=all` to log information about killed and errored mutants. -Note: Pass `--debug` to log test-framework output. - -Escaped mutants: -================ - -1) /app/src/Validator/Request/BodyParser/XmlBodyParser.php:22 [M] FunctionCallRemoval [ID] 4496607e11f60767e5da8d170dc17d8d - -@@ @@ - return ''; - } - -- libxml_set_external_entity_loader(null); -+ - libxml_use_internal_errors(true); - - try { - - -2) /app/src/Validator/Request/BodyParser/XmlBodyParser.php:23 [M] FunctionCallRemoval [ID] 9a161dc7643e664ae4cc051c8d80fd3d - -@@ @@ - } - - libxml_set_external_entity_loader(null); -- libxml_use_internal_errors(true); -+ - - try { - $xml = simplexml_load_string($body); - - -3) /app/src/Validator/Request/BodyParser/XmlBodyParser.php:25 [M] UnwrapFinally [ID] 1b6b30c40d097a34decbe297c4f22e59 - -@@ @@ - - libxml_set_external_entity_loader(null); - libxml_use_internal_errors(true); -- - try { - $xml = simplexml_load_string($body); -- - if (false === $xml) { - return $body; - } -- - $encoded = json_encode($xml); - if (false === $encoded) { - return $body; - } -- - $decoded = json_decode($encoded, true); - if (false === is_array($decoded)) { - return $body; - } -- - return $decoded; - } catch (ValueError) { - return $body; -- } finally { -- libxml_clear_errors(); - } -+ libxml_clear_errors(); - } - } - - -4) /app/src/Validator/Request/BodyParser/XmlBodyParser.php:29 [M] ReturnRemoval [ID] 040d60e037e033a9b595f9ed9c7efbf0 - -@@ @@ - $xml = simplexml_load_string($body); - - if (false === $xml) { -- return $body; -+ - } - - $encoded = json_encode($xml); - - -5) /app/src/Validator/Request/BodyParser/XmlBodyParser.php:45 [M] Finally_ [ID] d4933893a94af46f25a423d898882a1c - -@@ @@ - return $decoded; - } catch (ValueError) { - return $body; -- } finally { -- libxml_clear_errors(); - } - } - } - - -6) /app/src/Validator/Request/BodyParser/XmlBodyParser.php:46 [M] FunctionCallRemoval [ID] 2ee18910bd1cb84b30c81cc02c9306ce - -@@ @@ - } catch (ValueError) { - return $body; - } finally { -- libxml_clear_errors(); -+ - } - } - } - - -Timed Out mutants: -================== - -Skipped mutants: -================ diff --git a/phpunit.xml b/phpunit.xml index 0a88d72..1f2b57c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + tests diff --git a/tests/Integration/Builder/OpenApiValidatorBuilderTest.php b/tests/Integration/Builder/OpenApiValidatorBuilderTest.php index 6c06a24..45eb63a 100644 --- a/tests/Integration/Builder/OpenApiValidatorBuilderTest.php +++ b/tests/Integration/Builder/OpenApiValidatorBuilderTest.php @@ -134,7 +134,7 @@ public function use_custom_validator_pool(): void public function use_custom_cache(): void { $yaml = "openapi: 3.0.3\ninfo:\n title: Test\n version: 1.0.0\npaths: []"; - $cache = new SchemaCache($this->createMock(CacheItemPoolInterface::class)); + $cache = new SchemaCache($this->createStub(CacheItemPoolInterface::class)); $builder = OpenApiValidatorBuilder::create() ->fromYamlString($yaml) @@ -301,12 +301,12 @@ public function build_with_cache_enabled(): void { $yaml = "openapi: 3.0.3\ninfo:\n title: Test\n version: 1.0.0\npaths: []"; - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(false); - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('getItem') ->willReturn($cacheItem); @@ -417,12 +417,12 @@ public function build_preserves_all_configuration(): void $formatter = new DetailedFormatter(); $logger = new class {}; - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(false); - $cachePool = $this->createMock(CacheItemPoolInterface::class); + $cachePool = $this->createStub(CacheItemPoolInterface::class); $cachePool ->method('getItem') ->willReturn($cacheItem); @@ -503,7 +503,7 @@ public function build_uses_cache_from_file(): void $tempFile = sys_get_temp_dir() . '/test_cache.yaml'; file_put_contents($tempFile, "openapi: 3.0.3\ninfo:\n title: Cached\n version: 1.0.0\npaths: []"); - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -514,7 +514,7 @@ public function build_uses_cache_from_file(): void info: new InfoObject(title: 'From Cache', version: '1.0.0'), )); - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('getItem') ->willReturn($cacheItem); @@ -536,7 +536,7 @@ public function build_uses_cache_from_string(): void { $yaml = "openapi: 3.0.3\ninfo:\n title: Original\n version: 1.0.0\npaths: []"; - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -547,7 +547,7 @@ public function build_uses_cache_from_string(): void info: new InfoObject(title: 'From Cache', version: '1.0.0'), )); - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('getItem') ->willReturn($cacheItem); @@ -568,7 +568,7 @@ public function build_with_json_file_and_cache(): void $tempFile = sys_get_temp_dir() . '/test_json_cache.json'; file_put_contents($tempFile, '{"openapi":"3.0.3","info":{"title":"JSON Cached","version":"1.0.0"},"paths":{}}'); - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -579,7 +579,7 @@ public function build_with_json_file_and_cache(): void info: new InfoObject(title: 'From JSON Cache', version: '1.0.0'), )); - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('getItem') ->willReturn($cacheItem); @@ -602,7 +602,7 @@ public function build_with_real_file_path_generates_cache_key(): void $tempFile = sys_get_temp_dir() . '/test_realpath.yaml'; file_put_contents($tempFile, "openapi: 3.0.3\ninfo:\n title: Test\n version: 1.0.0\npaths: []"); - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(false); @@ -613,7 +613,7 @@ public function build_with_real_file_path_generates_cache_key(): void ->method('expiresAfter') ->willReturnSelf(); - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('getItem') ->willReturn($cacheItem); diff --git a/tests/Integration/CacheIntegrationTest.php b/tests/Integration/CacheIntegrationTest.php index ebf760c..b1e283a 100644 --- a/tests/Integration/CacheIntegrationTest.php +++ b/tests/Integration/CacheIntegrationTest.php @@ -19,7 +19,7 @@ public function cache_hit_on_second_load(): void $isHitFirstCall = false; $isHitSecondCall = true; - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cacheItemMiss = $this->createCacheItem(); $cacheItemMiss @@ -58,7 +58,7 @@ public function cache_hit_on_second_load(): void #[Test] public function cache_miss_on_first_load(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cacheItem = $this->createCacheItem(); $cacheItem @@ -83,7 +83,7 @@ public function cache_miss_on_first_load(): void #[Test] public function cache_with_custom_ttl(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cacheItem = $this->createCacheItem(); $cacheItem @@ -102,7 +102,7 @@ public function cache_with_custom_ttl(): void #[Test] public function cache_delete_works(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('deleteItem') @@ -118,7 +118,7 @@ public function cache_delete_works(): void #[Test] public function cache_clear_works(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('clear') @@ -134,7 +134,7 @@ public function cache_clear_works(): void #[Test] public function cache_has_works(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $pool ->method('hasItem') @@ -147,7 +147,7 @@ public function cache_has_works(): void private function createCacheItem(): CacheItemInterface { - $item = $this->createMock(CacheItemInterface::class); + $item = $this->createStub(CacheItemInterface::class); $item ->method('set') ->willReturnSelf(); diff --git a/tests/Integration/EventIntegrationTest.php b/tests/Integration/EventIntegrationTest.php index cfdd80c..1a6a030 100644 --- a/tests/Integration/EventIntegrationTest.php +++ b/tests/Integration/EventIntegrationTest.php @@ -162,13 +162,13 @@ private function createPsr7Request( array $headers = [], string $body = '', ): object { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request ->method('getMethod') ->willReturn($method); - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock ->method('getPath') ->willReturn($uri); @@ -198,7 +198,7 @@ private function createPsr7Request( return ''; }); - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream ->method('__toString') ->willReturn($body); diff --git a/tests/Integration/Psr7IntegrationTest.php b/tests/Integration/Psr7IntegrationTest.php index 7ce1cb5..2fadc77 100644 --- a/tests/Integration/Psr7IntegrationTest.php +++ b/tests/Integration/Psr7IntegrationTest.php @@ -108,13 +108,13 @@ private function createPsr7Request( array $headers = [], string $body = '', ): object { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request ->method('getMethod') ->willReturn($method); - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock ->method('getPath') ->willReturn($uri); @@ -144,7 +144,7 @@ private function createPsr7Request( return ''; }); - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream ->method('__toString') ->willReturn($body); @@ -161,7 +161,7 @@ private function createPsr7Response( array $headers = [], string $body = '', ): object { - $response = $this->createMock(ResponseInterface::class); + $response = $this->createStub(ResponseInterface::class); $response ->method('getStatusCode') @@ -175,7 +175,7 @@ private function createPsr7Response( ->method('getHeaderLine') ->willReturn('application/json'); - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream ->method('__toString') ->willReturn($body); diff --git a/tests/Integration/RealOpenApiSpecTest.php b/tests/Integration/RealOpenApiSpecTest.php index 9328168..3f6e609 100644 --- a/tests/Integration/RealOpenApiSpecTest.php +++ b/tests/Integration/RealOpenApiSpecTest.php @@ -141,11 +141,11 @@ private function createPsr7Request( array $headers = [], string $body = '', ): ServerRequestInterface { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request->method('getMethod')->willReturn($method); - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock->method('getPath')->willReturn($uri); $uriMock->method('getQuery')->willReturn(''); @@ -155,7 +155,7 @@ private function createPsr7Request( fn($headerName) => $headers[$headerName] ?? '', ); - $bodyMock = $this->createMock(StreamInterface::class); + $bodyMock = $this->createStub(StreamInterface::class); $bodyMock->method('__toString')->willReturn($body); $request->method('getBody')->willReturn($bodyMock); @@ -167,13 +167,13 @@ private function createPsr7Response( array $headers = [], string $body = '', ): ResponseInterface { - $response = $this->createMock(ResponseInterface::class); + $response = $this->createStub(ResponseInterface::class); $response->method('getStatusCode')->willReturn($statusCode); $response->method('getHeaders')->willReturn($headers); $response->method('getHeaderLine')->willReturn('application/json'); - $bodyMock = $this->createMock(StreamInterface::class); + $bodyMock = $this->createStub(StreamInterface::class); $bodyMock->method('__toString')->willReturn($body); $response->method('getBody')->willReturn($bodyMock); diff --git a/tests/Integration/Validator/OpenApiValidatorTest.php b/tests/Integration/Validator/OpenApiValidatorTest.php index 41b28bd..ec63a3d 100644 --- a/tests/Integration/Validator/OpenApiValidatorTest.php +++ b/tests/Integration/Validator/OpenApiValidatorTest.php @@ -190,7 +190,7 @@ public function validate_response_with_operation(): void */ private function createMockServerRequest(string $method, string $uri) { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request->method('getMethod')->willReturn($method); $request->method('getUri')->willReturn($this->createMockUri($uri)); @@ -203,7 +203,7 @@ private function createMockServerRequest(string $method, string $uri) private function createMockUri(string $uri) { - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock->method('getPath')->willReturn(parse_url($uri, PHP_URL_PATH) ?? $uri); $uriMock->method('getQuery')->willReturn(parse_url($uri, PHP_URL_QUERY) ?? ''); @@ -212,7 +212,7 @@ private function createMockUri(string $uri) private function createMockStream(string $content) { - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream->method('__toString')->willReturn($content); return $stream; @@ -227,7 +227,7 @@ private function createValidator(): OpenApiValidator private function createMockResponse() { - $response = $this->createMock(ResponseInterface::class); + $response = $this->createStub(ResponseInterface::class); $response->method('getStatusCode')->willReturn(200); $response->method('getHeaders')->willReturn([]); $response->method('getHeaderLine')->willReturn('application/json'); diff --git a/tests/Integration/Validator/Request/RequestValidatorIntegrationTest.php b/tests/Integration/Validator/Request/RequestValidatorIntegrationTest.php index 0732c95..a4d345e 100644 --- a/tests/Integration/Validator/Request/RequestValidatorIntegrationTest.php +++ b/tests/Integration/Validator/Request/RequestValidatorIntegrationTest.php @@ -186,9 +186,9 @@ private function createMockServerRequest( string $body, string $contentType, ): ServerRequestInterface { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock->method('getPath')->willReturn($uri); $uriMock->method('getQuery')->willReturn(http_build_query($queryParams)); @@ -201,7 +201,7 @@ private function createMockServerRequest( ]); $request->method('getCookieParams')->willReturn($cookies); - $bodyMock = $this->createMock(StreamInterface::class); + $bodyMock = $this->createStub(StreamInterface::class); $bodyMock->method('__toString')->willReturn($body); $request->method('getBody')->willReturn($bodyMock); diff --git a/tests/Integration/Validator/Response/ResponseValidatorIntegrationTest.php b/tests/Integration/Validator/Response/ResponseValidatorIntegrationTest.php index ac279e6..0321be5 100644 --- a/tests/Integration/Validator/Response/ResponseValidatorIntegrationTest.php +++ b/tests/Integration/Validator/Response/ResponseValidatorIntegrationTest.php @@ -177,7 +177,7 @@ private function createMockResponse( string $body, string $contentType, ): ResponseInterface { - $response = $this->createMock(ResponseInterface::class); + $response = $this->createStub(ResponseInterface::class); $response->method('getStatusCode')->willReturn($statusCode); $response->method('getHeaders')->willReturn($headers); @@ -185,7 +185,7 @@ private function createMockResponse( ['Content-Type', $contentType], ]); - $bodyMock = $this->createMock(StreamInterface::class); + $bodyMock = $this->createStub(StreamInterface::class); $bodyMock->method('__toString')->willReturn($body); $response->method('getBody')->willReturn($bodyMock); diff --git a/tests/Integration/Validator/Webhook/WebhookValidatorTest.php b/tests/Integration/Validator/Webhook/WebhookValidatorTest.php index 0f1aa1e..32622ca 100644 --- a/tests/Integration/Validator/Webhook/WebhookValidatorTest.php +++ b/tests/Integration/Validator/Webhook/WebhookValidatorTest.php @@ -580,12 +580,11 @@ private function createPsr7Request( string $body = '', string $contentType = 'application/json', ): ServerRequestInterface { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request->method('getMethod')->willReturn($method); - // For webhooks, the URI should match the webhook name (used as path template) - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock->method('getPath')->willReturn($uri); $uriMock->method('getQuery')->willReturn(http_build_query($queryParams)); @@ -598,7 +597,7 @@ private function createPsr7Request( ]); $request->method('getCookieParams')->willReturn($cookies); - $bodyMock = $this->createMock(StreamInterface::class); + $bodyMock = $this->createStub(StreamInterface::class); $bodyMock->method('__toString')->willReturn($body); $request->method('getBody')->willReturn($bodyMock); @@ -614,14 +613,12 @@ private function createPsr7RequestForWebhook( string $contentType = 'application/json', string $webhookName = '', ): ServerRequestInterface { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request->method('getMethod')->willReturn($method); - // Use webhook name as path to match the template passed to RequestValidator - // WebhookValidator passes webhook name (with dots) as path template $path = '' !== $webhookName ? $webhookName : 'webhook'; - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock->method('getPath')->willReturn($path); $uriMock->method('getQuery')->willReturn(http_build_query($queryParams)); @@ -634,7 +631,7 @@ private function createPsr7RequestForWebhook( ]); $request->method('getCookieParams')->willReturn($cookies); - $bodyMock = $this->createMock(StreamInterface::class); + $bodyMock = $this->createStub(StreamInterface::class); $bodyMock->method('__toString')->willReturn($body); $request->method('getBody')->willReturn($bodyMock); diff --git a/tests/Performance/CompiledValidatorBenchTest.php b/tests/Performance/CompiledValidatorBenchTest.php index b4dec2f..a99a161 100644 --- a/tests/Performance/CompiledValidatorBenchTest.php +++ b/tests/Performance/CompiledValidatorBenchTest.php @@ -31,8 +31,6 @@ public function compilation_overhead_is_acceptable(): void $compilationTime = microtime(true) - $start; $this->assertLessThan(1.0, $compilationTime, '100 compilations should take less than 1 second'); - - printf("\n100 compilations: %.4fs (%.4fs per compilation)\n", $compilationTime, $compilationTime / 100.0); } #[Test] diff --git a/tests/Performance/MemoryLeakTest.php b/tests/Performance/MemoryLeakTest.php index 1811faf..a02fff3 100644 --- a/tests/Performance/MemoryLeakTest.php +++ b/tests/Performance/MemoryLeakTest.php @@ -91,13 +91,13 @@ private function createPsr7Request( string $body = '', ): ServerRequestInterface { /** @var ServerRequestInterface $request */ - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request ->method('getMethod') ->willReturn($method); - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock ->method('getPath') ->willReturn($uri); @@ -127,7 +127,7 @@ private function createPsr7Request( return ''; }); - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream ->method('__toString') ->willReturn($body); diff --git a/tests/Performance/ValidationBenchTest.php b/tests/Performance/ValidationBenchTest.php index d405fec..617dc3b 100644 --- a/tests/Performance/ValidationBenchTest.php +++ b/tests/Performance/ValidationBenchTest.php @@ -119,13 +119,13 @@ private function createPsr7Request( string $body = '', ): ServerRequestInterface { /** @var ServerRequestInterface $request */ - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request ->method('getMethod') ->willReturn($method); - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock ->method('getPath') ->willReturn($uri); @@ -155,7 +155,7 @@ private function createPsr7Request( return ''; }); - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream ->method('__toString') ->willReturn($body); diff --git a/tests/Unit/Cache/SchemaCacheTest.php b/tests/Unit/Cache/SchemaCacheTest.php index c7bf968..6d46a27 100644 --- a/tests/Unit/Cache/SchemaCacheTest.php +++ b/tests/Unit/Cache/SchemaCacheTest.php @@ -20,7 +20,7 @@ public function get_returns_cached_document(): void $pool = $this->createMockCachePool(); $document = $this->createDocument(); - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -45,11 +45,19 @@ public function get_returns_null_when_cache_miss(): void { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(false); + $cacheItem + ->method('get') + ->willReturn(null); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem(null, false)); + ->willReturn($cacheItem); $cache = new SchemaCache($pool); $result = $cache->get('test_key'); @@ -62,11 +70,19 @@ public function get_returns_null_when_cached_value_is_not_document(): void { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(true); + $cacheItem + ->method('get') + ->willReturn('invalid_value'); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem('invalid_value', true)); + ->willReturn($cacheItem); $cache = new SchemaCache($pool); $result = $cache->get('test_key'); @@ -79,26 +95,25 @@ public function set_saves_document_to_cache(): void { $pool = $this->createMockCachePool(); $document = $this->createDocument(); - $cacheItem = $this->createCacheItem(null, false); - - $pool - ->expects($this->once()) - ->method('getItem') - ->with('test_key') - ->willReturn($cacheItem); + $cacheItem = $this->createMock(CacheItemInterface::class); $cacheItem ->expects($this->once()) ->method('set') ->with($document) ->willReturnSelf(); - $cacheItem ->expects($this->once()) ->method('expiresAfter') ->with(3600) ->willReturnSelf(); + $pool + ->expects($this->once()) + ->method('getItem') + ->with('test_key') + ->willReturn($cacheItem); + $pool ->expects($this->once()) ->method('save') @@ -176,19 +191,11 @@ public function set_uses_custom_ttl_when_provided(): void $document = $this->createDocument(); $cacheItem = $this->createMock(CacheItemInterface::class); - $cacheItem - ->method('get') - ->willReturn(null); - $cacheItem - ->method('isHit') - ->willReturn(false); - $cacheItem ->expects($this->once()) ->method('set') ->with($document) ->willReturn($cacheItem); - $cacheItem ->expects($this->once()) ->method('expiresAfter') @@ -196,7 +203,9 @@ public function set_uses_custom_ttl_when_provided(): void ->willReturn($cacheItem); $pool + ->expects($this->once()) ->method('getItem') + ->with('test_key') ->willReturn($cacheItem); $pool @@ -215,19 +224,11 @@ public function set_uses_default_ttl_when_not_provided(): void $document = $this->createDocument(); $cacheItem = $this->createMock(CacheItemInterface::class); - $cacheItem - ->method('get') - ->willReturn(null); - $cacheItem - ->method('isHit') - ->willReturn(false); - $cacheItem ->expects($this->once()) ->method('set') ->with($document) ->willReturn($cacheItem); - $cacheItem ->expects($this->once()) ->method('expiresAfter') @@ -235,7 +236,9 @@ public function set_uses_default_ttl_when_not_provided(): void ->willReturn($cacheItem); $pool + ->expects($this->once()) ->method('getItem') + ->with('test_key') ->willReturn($cacheItem); $pool @@ -252,28 +255,6 @@ private function createMockCachePool(): CacheItemPoolInterface return $this->createMock(CacheItemPoolInterface::class); } - private function createCacheItem(mixed $value, bool $isHit): CacheItemInterface - { - $item = $this->createMock(CacheItemInterface::class); - $item - ->method('get') - ->willReturn($value); - - $item - ->method('isHit') - ->willReturn($isHit); - - $item - ->method('set') - ->willReturnSelf(); - - $item - ->method('expiresAfter') - ->willReturnSelf(); - - return $item; - } - private function createDocument(): OpenApiDocument { return new OpenApiDocument( diff --git a/tests/Unit/Cache/TypedCacheDecoratorTest.php b/tests/Unit/Cache/TypedCacheDecoratorTest.php index b124664..cb7b408 100644 --- a/tests/Unit/Cache/TypedCacheDecoratorTest.php +++ b/tests/Unit/Cache/TypedCacheDecoratorTest.php @@ -20,11 +20,19 @@ public function get_returns_cached_value_of_expected_type(): void $pool = $this->createMockCachePool(); $schema = $this->createSchema(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(true); + $cacheItem + ->method('get') + ->willReturn($schema); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem($schema, true)); + ->willReturn($cacheItem); $decorator = new TypedCacheDecorator($pool); $result = $decorator->get('test_key', Schema::class); @@ -37,11 +45,19 @@ public function get_returns_null_when_cache_miss(): void { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(false); + $cacheItem + ->method('get') + ->willReturn(null); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem(null, false)); + ->willReturn($cacheItem); $decorator = new TypedCacheDecorator($pool); $result = $decorator->get('test_key', Schema::class); @@ -54,7 +70,7 @@ public function get_returns_null_when_cached_value_is_null(): void { $pool = $this->createMockCachePool(); - $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -79,11 +95,19 @@ public function get_returns_null_when_cached_value_is_not_of_expected_type(): vo { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(true); + $cacheItem + ->method('get') + ->willReturn('invalid_value'); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem('invalid_value', true)); + ->willReturn($cacheItem); $decorator = new TypedCacheDecorator($pool); $result = $decorator->get('test_key', Schema::class); @@ -96,11 +120,19 @@ public function get_throws_exception_when_expected_type_does_not_exist(): void { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(true); + $cacheItem + ->method('get') + ->willReturn('value'); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem('value', true)); + ->willReturn($cacheItem); $decorator = new TypedCacheDecorator($pool); @@ -115,7 +147,18 @@ public function set_saves_value_to_cache(): void { $pool = $this->createMockCachePool(); $schema = $this->createSchema(); - $cacheItem = $this->createCacheItem(null, false); + + $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem + ->expects($this->once()) + ->method('set') + ->with($schema) + ->willReturnSelf(); + $cacheItem + ->expects($this->once()) + ->method('expiresAfter') + ->with(3600) + ->willReturnSelf(); $pool ->expects($this->once()) @@ -137,15 +180,23 @@ public function set_uses_custom_ttl_when_provided(): void { $pool = $this->createMockCachePool(); $schema = $this->createSchema(); - $cacheItem = $this->createCacheItem(null, false); + $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem + ->expects($this->once()) + ->method('set') + ->with($schema) + ->willReturnSelf(); $cacheItem ->expects($this->once()) ->method('expiresAfter') - ->with(7200); + ->with(7200) + ->willReturnSelf(); $pool + ->expects($this->once()) ->method('getItem') + ->with('test_key') ->willReturn($cacheItem); $pool @@ -223,15 +274,23 @@ public function set_uses_default_ttl_when_not_provided(): void { $pool = $this->createMockCachePool(); $schema = $this->createSchema(); - $cacheItem = $this->createCacheItem(null, false); + $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem + ->expects($this->once()) + ->method('set') + ->with($schema) + ->willReturnSelf(); $cacheItem ->expects($this->once()) ->method('expiresAfter') - ->with(3600); + ->with(3600) + ->willReturnSelf(); $pool + ->expects($this->once()) ->method('getItem') + ->with('test_key') ->willReturn($cacheItem); $pool @@ -248,28 +307,6 @@ private function createMockCachePool(): CacheItemPoolInterface return $this->createMock(CacheItemPoolInterface::class); } - private function createCacheItem(mixed $value, bool $isHit): CacheItemInterface - { - $item = $this->createMock(CacheItemInterface::class); - $item - ->method('get') - ->willReturn($value); - - $item - ->method('isHit') - ->willReturn($isHit); - - $item - ->method('set') - ->willReturnSelf(); - - $item - ->method('expiresAfter') - ->willReturnSelf(); - - return $item; - } - private function createSchema(): Schema { return new Schema(type: 'string'); diff --git a/tests/Unit/Cache/ValidatorCacheTest.php b/tests/Unit/Cache/ValidatorCacheTest.php index 1d54e75..72fdc18 100644 --- a/tests/Unit/Cache/ValidatorCacheTest.php +++ b/tests/Unit/Cache/ValidatorCacheTest.php @@ -19,11 +19,19 @@ public function get_returns_cached_schema(): void $pool = $this->createMockCachePool(); $schema = $this->createSchema(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(true); + $cacheItem + ->method('get') + ->willReturn($schema); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem($schema, true)); + ->willReturn($cacheItem); $cache = new ValidatorCache($pool); $result = $cache->get('test_key'); @@ -36,11 +44,19 @@ public function get_returns_null_when_cache_miss(): void { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(false); + $cacheItem + ->method('get') + ->willReturn(null); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem(null, false)); + ->willReturn($cacheItem); $cache = new ValidatorCache($pool); $result = $cache->get('test_key'); @@ -53,11 +69,19 @@ public function get_returns_null_when_cached_value_is_not_schema(): void { $pool = $this->createMockCachePool(); + $cacheItem = $this->createStub(CacheItemInterface::class); + $cacheItem + ->method('isHit') + ->willReturn(true); + $cacheItem + ->method('get') + ->willReturn('invalid_value'); + $pool ->expects($this->once()) ->method('getItem') ->with('test_key') - ->willReturn($this->createCacheItem('invalid_value', true)); + ->willReturn($cacheItem); $cache = new ValidatorCache($pool); $result = $cache->get('test_key'); @@ -70,7 +94,18 @@ public function set_saves_schema_to_cache(): void { $pool = $this->createMockCachePool(); $schema = $this->createSchema(); - $cacheItem = $this->createCacheItem(null, false); + + $cacheItem = $this->createMock(CacheItemInterface::class); + $cacheItem + ->expects($this->once()) + ->method('set') + ->with($schema) + ->willReturnSelf(); + $cacheItem + ->expects($this->once()) + ->method('expiresAfter') + ->with(3600) + ->willReturnSelf(); $pool ->expects($this->once()) @@ -153,28 +188,6 @@ private function createMockCachePool(): CacheItemPoolInterface return $this->createMock(CacheItemPoolInterface::class); } - private function createCacheItem(mixed $value, bool $isHit): CacheItemInterface - { - $item = $this->createMock(CacheItemInterface::class); - $item - ->method('get') - ->willReturn($value); - - $item - ->method('isHit') - ->willReturn($isHit); - - $item - ->method('set') - ->willReturnSelf(); - - $item - ->method('expiresAfter') - ->willReturnSelf(); - - return $item; - } - private function createSchema(): Schema { return new Schema(type: 'string'); diff --git a/tests/Unit/Compiler/CompilationCacheTest.php b/tests/Unit/Compiler/CompilationCacheTest.php index 3b88191..f176048 100644 --- a/tests/Unit/Compiler/CompilationCacheTest.php +++ b/tests/Unit/Compiler/CompilationCacheTest.php @@ -18,12 +18,13 @@ public function get_returns_null_when_cache_miss(): void { $pool = $this->createMock(CacheItemPoolInterface::class); - $cacheItem = $this->createCacheItem(); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(false); $pool + ->expects($this->once()) ->method('getItem') ->willReturn($cacheItem); @@ -39,7 +40,7 @@ public function get_returns_code_when_cache_hit(): void { $pool = $this->createMock(CacheItemPoolInterface::class); - $cacheItem = $this->createCacheItem(); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -48,6 +49,7 @@ public function get_returns_code_when_cache_hit(): void ->willReturn('expects($this->once()) ->method('getItem') ->willReturn($cacheItem); @@ -63,7 +65,7 @@ public function get_returns_null_when_cache_hit_but_not_string(): void { $pool = $this->createMock(CacheItemPoolInterface::class); - $cacheItem = $this->createCacheItem(); + $cacheItem = $this->createStub(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); @@ -72,6 +74,7 @@ public function get_returns_null_when_cache_hit_but_not_string(): void ->willReturn(['not', 'a', 'string']); $pool + ->expects($this->once()) ->method('getItem') ->willReturn($cacheItem); @@ -87,32 +90,35 @@ public function set_stores_compiled_code(): void { $pool = $this->createMock(CacheItemPoolInterface::class); - $cacheItem = $this->createCacheItem(); + $cacheItem = $this->createMock(CacheItemInterface::class); $cacheItem + ->expects($this->once()) ->method('set') + ->with('willReturnSelf(); $cacheItem + ->expects($this->once()) ->method('expiresAfter') ->willReturnSelf(); $pool + ->expects($this->once()) ->method('getItem') ->willReturn($cacheItem); $pool + ->expects($this->once()) ->method('save') - ->willReturn(true); + ->with($cacheItem); $cache = new CompilationCache($pool); $cache->set('test_hash', 'expectNotToPerformAssertions(); } #[Test] public function generateKey_creates_unique_hash_for_schema(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema1 = new Schema( @@ -136,7 +142,7 @@ public function generateKey_creates_unique_hash_for_schema(): void #[Test] public function generateKey_creates_different_hash_for_different_schemas(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema1 = new Schema( @@ -158,7 +164,7 @@ public function generateKey_creates_different_hash_for_different_schemas(): void #[Test] public function generateKey_includes_namespace(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema = new Schema(type: 'string'); @@ -171,7 +177,7 @@ public function generateKey_includes_namespace(): void #[Test] public function generateKey_with_custom_namespace(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool, 'custom_namespace'); $schema = new Schema(type: 'string'); @@ -184,7 +190,7 @@ public function generateKey_with_custom_namespace(): void #[Test] public function generateKey_hashes_all_schema_properties(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema1 = new Schema( @@ -212,7 +218,7 @@ enum: ['a', 'b'], #[Test] public function generateKey_different_for_nested_schemas(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema1 = new Schema( @@ -238,7 +244,7 @@ public function generateKey_different_for_nested_schemas(): void #[Test] public function generateKey_different_for_array_schemas(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema1 = new Schema( @@ -260,7 +266,7 @@ public function generateKey_different_for_array_schemas(): void #[Test] public function generateKey_handles_null_properties(): void { - $pool = $this->createMock(CacheItemPoolInterface::class); + $pool = $this->createStub(CacheItemPoolInterface::class); $cache = new CompilationCache($pool); $schema1 = new Schema(type: 'string', minLength: null); @@ -271,16 +277,4 @@ public function generateKey_handles_null_properties(): void self::assertSame($key1, $key2); } - - private function createCacheItem(): CacheItemInterface - { - $item = $this->createMock(CacheItemInterface::class); - $item - ->method('set') - ->willReturnSelf(); - $item - ->method('expiresAfter') - ->willReturnSelf(); - return $item; - } } diff --git a/tests/Unit/Event/ArrayDispatcherTest.php b/tests/Unit/Event/ArrayDispatcherTest.php index 23ec4b3..2a78f97 100644 --- a/tests/Unit/Event/ArrayDispatcherTest.php +++ b/tests/Unit/Event/ArrayDispatcherTest.php @@ -24,7 +24,7 @@ public function dispatch_calls_listeners(): void ValidationStartedEvent::class => [$listener], ]); - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $event = new ValidationStartedEvent($request, '/test', 'GET'); $dispatcher->dispatch($event); @@ -37,7 +37,7 @@ public function dispatch_returns_event(): void { $dispatcher = new ArrayDispatcher(); - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $event = new ValidationStartedEvent($request, '/test', 'GET'); $result = $dispatcher->dispatch($event); @@ -50,7 +50,7 @@ public function dispatch_does_not_fail_when_no_listeners(): void { $dispatcher = new ArrayDispatcher(); - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $event = new ValidationStartedEvent($request, '/test', 'GET'); $result = $dispatcher->dispatch($event); @@ -69,7 +69,7 @@ public function listen_adds_listener(): void $dispatcher = new ArrayDispatcher(); $dispatcher = $dispatcher->listen(ValidationStartedEvent::class, $listener); - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $event = new ValidationStartedEvent($request, '/test', 'GET'); $dispatcher->dispatch($event); @@ -103,7 +103,7 @@ public function dispatch_calls_multiple_listeners(): void ValidationStartedEvent::class => [$listener1, $listener2], ]); - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $event = new ValidationStartedEvent($request, '/test', 'GET'); $dispatcher->dispatch($event); diff --git a/tests/Unit/Validator/PathParametersCoercionTest.php b/tests/Unit/Validator/PathParametersCoercionTest.php index f6c34dc..b7c8c23 100644 --- a/tests/Unit/Validator/PathParametersCoercionTest.php +++ b/tests/Unit/Validator/PathParametersCoercionTest.php @@ -147,7 +147,7 @@ public function validate_boolean_path_param_with_coercion(): void private function createMockServerRequest(string $method, string $uri): ServerRequestInterface { - $request = $this->createMock(ServerRequestInterface::class); + $request = $this->createStub(ServerRequestInterface::class); $request->method('getMethod')->willReturn($method); $request->method('getUri')->willReturn($this->createMockUri($uri)); @@ -160,7 +160,7 @@ private function createMockServerRequest(string $method, string $uri): ServerReq private function createMockUri(string $uri): UriInterface { - $uriMock = $this->createMock(UriInterface::class); + $uriMock = $this->createStub(UriInterface::class); $uriMock->method('getPath')->willReturn(parse_url($uri, PHP_URL_PATH) ?? $uri); $uriMock->method('getQuery')->willReturn(parse_url($uri, PHP_URL_QUERY) ?? ''); @@ -169,7 +169,7 @@ private function createMockUri(string $uri): UriInterface private function createMockStream(string $content): StreamInterface { - $stream = $this->createMock(StreamInterface::class); + $stream = $this->createStub(StreamInterface::class); $stream->method('__toString')->willReturn($content); return $stream; diff --git a/tests/Unit/Validator/SchemaValidator/ValidationResultTest.php b/tests/Unit/Validator/SchemaValidator/ValidationResultTest.php index 050ea03..f14e7d2 100644 --- a/tests/Unit/Validator/SchemaValidator/ValidationResultTest.php +++ b/tests/Unit/Validator/SchemaValidator/ValidationResultTest.php @@ -37,7 +37,7 @@ public function create_result_with_errors(): void #[Test] public function create_result_with_abstract_errors(): void { - $abstractError = $this->createMock(AbstractValidationError::class); + $abstractError = $this->createStub(AbstractValidationError::class); $result = new ValidationResult(0, [], [$abstractError]); $this->assertSame(0, $result->validCount); @@ -59,8 +59,8 @@ public function create_result_with_multiple_errors_and_abstract_errors(): void { $error1 = new ValidationException('Error 1'); $error2 = new ValidationException('Error 2'); - $abstractError1 = $this->createMock(AbstractValidationError::class); - $abstractError2 = $this->createMock(AbstractValidationError::class); + $abstractError1 = $this->createStub(AbstractValidationError::class); + $abstractError2 = $this->createStub(AbstractValidationError::class); $result = new ValidationResult( 1,