From 4aa6fd383e358db92a62f0214382efbb298b5f5f Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 30 Jul 2024 21:27:50 +0200 Subject: [PATCH] Correct misconfigured mocks in JsonSchema\Tests\Uri\UriRetrieverTest (#741) * test: Correct misconfigured mocks * test: Refactor to use willReturn() method * test: Make use of already imported classname * test: Ignore return value when exception is expected to be thrown * test: Mock complete interface * docs: Add changelog entry --- CHANGELOG.md | 2 ++ tests/Uri/UriRetrieverTest.php | 47 ++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b58dc66e..fc3114ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Correct misconfigured mocks in JsonSchema\Tests\Uri\UriRetrieverTest ([#741](https://github.com/jsonrainbow/json-schema/pull/741)) ## [6.0.0] - 2024-07-30 ### Added diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index 24714a26..3c500699 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -39,7 +39,7 @@ private function getRetrieverMock($returnSchema) $retriever->expects($this->at(0)) ->method('retrieve') ->with($this->equalTo(null), $this->equalTo('http://some.host.at/somewhere/parent')) - ->will($this->returnValue($jsonSchema)); + ->willReturn($jsonSchema); return $retriever; } @@ -153,7 +153,7 @@ public function testResolvePointerNoFragment() 'title' => 'schema' ); - $retriever = new \JsonSchema\Uri\UriRetriever(); + $retriever = new UriRetriever(); $this->assertEquals( $schema, $retriever->resolvePointer( @@ -173,7 +173,7 @@ public function testResolvePointerFragment() 'title' => 'schema' ); - $retriever = new \JsonSchema\Uri\UriRetriever(); + $retriever = new UriRetriever(); $this->assertEquals( $schema->definitions->foo, $retriever->resolvePointer( @@ -196,7 +196,7 @@ public function testResolvePointerFragmentNotFound() 'title' => 'schema' ); - $retriever = new \JsonSchema\Uri\UriRetriever(); + $retriever = new UriRetriever(); $retriever->resolvePointer( $schema, 'http://example.org/schema.json#/definitions/bar' ); @@ -216,7 +216,7 @@ public function testResolvePointerFragmentNoArray() 'title' => 'schema' ); - $retriever = new \JsonSchema\Uri\UriRetriever(); + $retriever = new UriRetriever(); $retriever->resolvePointer( $schema, 'http://example.org/schema.json#/definitions/foo' ); @@ -227,7 +227,7 @@ public function testResolvePointerFragmentNoArray() */ public function testResolveExcessLevelUp() { - $retriever = new \JsonSchema\Uri\UriRetriever(); + $retriever = new UriRetriever(); $retriever->resolve( '../schema.json#', 'http://example.org/schema.json#' ); @@ -235,24 +235,26 @@ public function testResolveExcessLevelUp() public function testConfirmMediaTypeAcceptsJsonSchemaType() { - $retriever = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $uriRetriever = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $retriever = new UriRetriever(); - $retriever->expects($this->at(0)) + $uriRetriever->expects($this->at(0)) ->method('getContentType') - ->will($this->returnValue('application/schema+json')); + ->willReturn('application/schema+json'); - $this->assertEquals(null, $retriever->confirmMediaType($retriever, null)); + $this->assertEquals(null, $retriever->confirmMediaType($uriRetriever, null)); } public function testConfirmMediaTypeAcceptsJsonType() { - $retriever = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $uriRetriever = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $retriever = new UriRetriever(); - $retriever->expects($this->at(0)) + $uriRetriever->expects($this->at(0)) ->method('getContentType') - ->will($this->returnValue('application/json')); + ->willReturn('application/json'); - $this->assertEquals(null, $retriever->confirmMediaType($retriever, null)); + $this->assertEquals(null, $retriever->confirmMediaType($uriRetriever, null)); } /** @@ -260,13 +262,14 @@ public function testConfirmMediaTypeAcceptsJsonType() */ public function testConfirmMediaTypeThrowsExceptionForUnsupportedTypes() { - $retriever = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $uriRetriever = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $retriever = new UriRetriever(); - $retriever->expects($this->at(0)) + $uriRetriever->expects($this->at(0)) ->method('getContentType') - ->will($this->returnValue('text/html')); + ->willReturn('text/html'); - $this->assertEquals(null, $retriever->confirmMediaType($retriever, null)); + $this->assertEquals(null, $retriever->confirmMediaType($uriRetriever, null)); } private function mockRetriever($schema) @@ -332,7 +335,7 @@ public function testRetrieveSchemaFromPackage() public function testInvalidContentTypeEndpointsDefault() { - $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $mock = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); @@ -345,7 +348,7 @@ public function testInvalidContentTypeEndpointsDefault() */ public function testInvalidContentTypeEndpointsUnknown() { - $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $mock = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); @@ -354,7 +357,7 @@ public function testInvalidContentTypeEndpointsUnknown() public function testInvalidContentTypeEndpointsAdded() { - $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $mock = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); $retriever->addInvalidContentTypeEndpoint('http://example.com'); @@ -389,7 +392,7 @@ public function testLoadSchemaJSONDecodingException() 'JsonSchema\Exception\JsonDecodingException', 'JSON syntax is malformed' ); - $schema = $retriever->retrieve('package://tests/fixtures/bad-syntax.json'); + $retriever->retrieve('package://tests/fixtures/bad-syntax.json'); } public function testGenerateURI()