From 9974dd0c7e07a1b09f35b239738c560f4f233dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 28 Dec 2021 19:16:39 +0100 Subject: [PATCH] Fix: Do not use phpspec/prophecy --- test/Unit/AutoFormatNormalizerTest.php | 23 ++-- test/Unit/ChainNormalizerTest.php | 10 +- test/Unit/FixedFormatNormalizerTest.php | 23 ++-- test/Unit/Format/FormatterTest.php | 16 +-- test/Unit/IndentNormalizerTest.php | 14 +-- test/Unit/SchemaNormalizerTest.php | 122 ++++++++++---------- test/Unit/Validator/SchemaValidatorTest.php | 24 ++-- 7 files changed, 118 insertions(+), 114 deletions(-) diff --git a/test/Unit/AutoFormatNormalizerTest.php b/test/Unit/AutoFormatNormalizerTest.php index 18d17e4a..3611fa73 100644 --- a/test/Unit/AutoFormatNormalizerTest.php +++ b/test/Unit/AutoFormatNormalizerTest.php @@ -17,7 +17,6 @@ use Ergebnis\Json\Normalizer\Format; use Ergebnis\Json\Normalizer\Json; use Ergebnis\Json\Normalizer\NormalizerInterface; -use Prophecy\Argument; /** * @internal @@ -58,26 +57,28 @@ public function testNormalizeNormalizesAndFormatsUsingJsonFormat(): void JSON ); - $composedNormalizer = $this->prophesize(NormalizerInterface::class); + $composedNormalizer = $this->createMock(NormalizerInterface::class); $composedNormalizer - ->normalize(Argument::is($json)) - ->shouldBeCalled() + ->expects(self::once()) + ->method('normalize') + ->with(self::identicalTo($json)) ->willReturn($normalized); - $formatter = $this->prophesize(Format\FormatterInterface::class); + $formatter = $this->createMock(Format\FormatterInterface::class); $formatter - ->format( - Argument::is($normalized), - Argument::is($json->format()), + ->expects(self::once()) + ->method('format') + ->with( + self::identicalTo($normalized), + self::identicalTo($json->format()), ) - ->shouldBeCalled() ->willReturn($formatted); $normalizer = new AutoFormatNormalizer( - $composedNormalizer->reveal(), - $formatter->reveal(), + $composedNormalizer, + $formatter, ); self::assertSame($formatted, $normalizer->normalize($json)); diff --git a/test/Unit/ChainNormalizerTest.php b/test/Unit/ChainNormalizerTest.php index 92dbe8c2..a14a73de 100644 --- a/test/Unit/ChainNormalizerTest.php +++ b/test/Unit/ChainNormalizerTest.php @@ -16,7 +16,6 @@ use Ergebnis\Json\Normalizer\ChainNormalizer; use Ergebnis\Json\Normalizer\Json; use Ergebnis\Json\Normalizer\NormalizerInterface; -use Prophecy\Argument; /** * @internal @@ -56,16 +55,17 @@ public function testNormalizePassesJsonThroughNormalizers(): void $previous = $json; } - $normalizer = $this->prophesize(NormalizerInterface::class); + $normalizer = $this->createMock(NormalizerInterface::class); $normalizer - ->normalize(Argument::is($previous)) - ->shouldBeCalled() + ->expects(self::once()) + ->method('normalize') + ->with(self::identicalTo($previous)) ->willReturn($result); $previous = $result; - return $normalizer->reveal(); + return $normalizer; }, $results); $normalizer = new ChainNormalizer(...$normalizers); diff --git a/test/Unit/FixedFormatNormalizerTest.php b/test/Unit/FixedFormatNormalizerTest.php index c6e463ed..f76f118c 100644 --- a/test/Unit/FixedFormatNormalizerTest.php +++ b/test/Unit/FixedFormatNormalizerTest.php @@ -17,7 +17,6 @@ use Ergebnis\Json\Normalizer\Format; use Ergebnis\Json\Normalizer\Json; use Ergebnis\Json\Normalizer\NormalizerInterface; -use Prophecy\Argument; /** * @internal @@ -67,27 +66,29 @@ public function testNormalizeNormalizesAndFormatsUsingFormat(): void JSON ); - $composedNormalizer = $this->prophesize(NormalizerInterface::class); + $composedNormalizer = $this->createMock(NormalizerInterface::class); $composedNormalizer - ->normalize(Argument::is($json)) - ->shouldBeCalled() + ->expects(self::once()) + ->method('normalize') + ->with(self::identicalTo($json)) ->willReturn($normalized); - $formatter = $this->prophesize(Format\FormatterInterface::class); + $formatter = $this->createMock(Format\FormatterInterface::class); $formatter - ->format( - Argument::is($normalized), - Argument::is($format), + ->expects(self::once()) + ->method('format') + ->with( + self::identicalTo($normalized), + self::identicalTo($format), ) - ->shouldBeCalled() ->willReturn($formatted); $normalizer = new FixedFormatNormalizer( - $composedNormalizer->reveal(), + $composedNormalizer, $format, - $formatter->reveal(), + $formatter, ); self::assertSame($formatted, $normalizer->normalize($json)); diff --git a/test/Unit/Format/FormatterTest.php b/test/Unit/Format/FormatterTest.php index 340671f5..c398944f 100644 --- a/test/Unit/Format/FormatterTest.php +++ b/test/Unit/Format/FormatterTest.php @@ -23,7 +23,6 @@ use Ergebnis\Json\Printer; use Ergebnis\Test\Util\Helper; use PHPUnit\Framework; -use Prophecy\Argument; /** * @internal @@ -91,18 +90,19 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes $hasFinalNewLine, ); - $printer = $this->prophesize(Printer\PrinterInterface::class); + $printer = $this->createMock(Printer\PrinterInterface::class); $printer - ->print( - Argument::is($encodedWithJsonEncodeOptions), - Argument::is($format->indent()->__toString()), - Argument::is($format->newLine()->__toString()), + ->expects(self::once()) + ->method('print') + ->with( + self::identicalTo($encodedWithJsonEncodeOptions), + self::identicalTo($format->indent()->__toString()), + self::identicalTo($format->newLine()->__toString()), ) - ->shouldBeCalled() ->willReturn($printedWithIndentAndNewLine); - $formatter = new Formatter($printer->reveal()); + $formatter = new Formatter($printer); $formatted = $formatter->format( $json, diff --git a/test/Unit/IndentNormalizerTest.php b/test/Unit/IndentNormalizerTest.php index ede4826f..71f2e24b 100644 --- a/test/Unit/IndentNormalizerTest.php +++ b/test/Unit/IndentNormalizerTest.php @@ -17,7 +17,6 @@ use Ergebnis\Json\Normalizer\IndentNormalizer; use Ergebnis\Json\Normalizer\Json; use Ergebnis\Json\Printer\PrinterInterface; -use Prophecy\Argument; /** * @internal @@ -48,19 +47,20 @@ public function testNormalizeUsesPrinterToNormalizeJsonWithIndent(): void } JSON; - $printer = $this->prophesize(PrinterInterface::class); + $printer = $this->createMock(PrinterInterface::class); $printer - ->print( - Argument::is($json->encoded()), - Argument::is($indent->__toString()), + ->expects(self::once()) + ->method('print') + ->with( + self::identicalTo($json->encoded()), + self::identicalTo($indent->__toString()), ) - ->shouldBeCalled() ->willReturn($indented); $normalizer = new IndentNormalizer( $indent, - $printer->reveal(), + $printer, ); $normalized = $normalizer->normalize($json); diff --git a/test/Unit/SchemaNormalizerTest.php b/test/Unit/SchemaNormalizerTest.php index 7c9418c4..ccb6841e 100644 --- a/test/Unit/SchemaNormalizerTest.php +++ b/test/Unit/SchemaNormalizerTest.php @@ -25,7 +25,6 @@ use JsonSchema\Exception\UriResolverException; use JsonSchema\SchemaStorage; use JsonSchema\Validator; -use Prophecy\Argument; /** * @internal @@ -57,17 +56,18 @@ public function testNormalizeThrowsSchemaUriCouldNotBeResolvedExceptionWhenSchem $schemaUri = self::faker()->url; - $schemaStorage = $this->prophesize(SchemaStorage::class); + $schemaStorage = $this->createMock(SchemaStorage::class); $schemaStorage - ->getSchema(Argument::is($schemaUri)) - ->shouldBeCalled() - ->willThrow(new UriResolverException()); + ->expects(self::once()) + ->method('getSchema') + ->with(self::identicalTo($schemaUri)) + ->willThrowException(new UriResolverException()); $normalizer = new SchemaNormalizer( $schemaUri, - $schemaStorage->reveal(), - $this->prophesize(SchemaValidatorInterface::class)->reveal(), + $schemaStorage, + $this->createStub(SchemaValidatorInterface::class), ); $this->expectException(Exception\SchemaUriCouldNotBeResolvedException::class); @@ -88,17 +88,18 @@ public function testNormalizeThrowsSchemaUriCouldNotBeReadExceptionWhenSchemaUri $schemaUri = self::faker()->url; - $schemaStorage = $this->prophesize(SchemaStorage::class); + $schemaStorage = $this->createMock(SchemaStorage::class); $schemaStorage - ->getSchema(Argument::is($schemaUri)) - ->shouldBeCalled() - ->willThrow(new ResourceNotFoundException()); + ->expects(self::once()) + ->method('getSchema') + ->with(self::identicalTo($schemaUri)) + ->willThrowException(new ResourceNotFoundException()); $normalizer = new SchemaNormalizer( $schemaUri, - $schemaStorage->reveal(), - $this->prophesize(SchemaValidatorInterface::class)->reveal(), + $schemaStorage, + $this->createStub(SchemaValidatorInterface::class), ); $this->expectException(Exception\SchemaUriCouldNotBeReadException::class); @@ -119,17 +120,18 @@ public function testNormalizeThrowsSchemaUriReferencesDocumentWithInvalidMediaTy $schemaUri = self::faker()->url; - $schemaStorage = $this->prophesize(SchemaStorage::class); + $schemaStorage = $this->createMock(SchemaStorage::class); $schemaStorage - ->getSchema(Argument::is($schemaUri)) - ->shouldBeCalled() - ->willThrow(new InvalidSchemaMediaTypeException()); + ->expects(self::once()) + ->method('getSchema') + ->with(self::identicalTo($schemaUri)) + ->willThrowException(new InvalidSchemaMediaTypeException()); $normalizer = new SchemaNormalizer( $schemaUri, - $schemaStorage->reveal(), - $this->prophesize(SchemaValidatorInterface::class)->reveal(), + $schemaStorage, + $this->createStub(SchemaValidatorInterface::class), ); $this->expectException(Exception\SchemaUriReferencesDocumentWithInvalidMediaTypeException::class); @@ -150,17 +152,18 @@ public function testNormalizeThrowsRuntimeExceptionIfSchemaUriReferencesResource $schemaUri = self::faker()->url; - $schemaStorage = $this->prophesize(SchemaStorage::class); + $schemaStorage = $this->createMock(SchemaStorage::class); $schemaStorage - ->getSchema($schemaUri) - ->shouldBeCalled() - ->willThrow(new JsonDecodingException()); + ->expects(self::once()) + ->method('getSchema') + ->with(self::identicalTo($schemaUri)) + ->willThrowException(new JsonDecodingException()); $normalizer = new SchemaNormalizer( $schemaUri, - $schemaStorage->reveal(), - $this->prophesize(SchemaValidatorInterface::class)->reveal(), + $schemaStorage, + $this->createStub(SchemaValidatorInterface::class), ); $this->expectException(Exception\SchemaUriReferencesInvalidJsonDocumentException::class); @@ -191,21 +194,23 @@ public function testNormalizeThrowsOriginalInvalidAccordingToSchemaExceptionWhen $schemaDecoded = \json_decode($schema); - $schemaStorage = $this->prophesize(SchemaStorage::class); + $schemaStorage = $this->createMock(SchemaStorage::class); $schemaStorage - ->getSchema(Argument::is($schemaUri)) - ->shouldBeCalled() + ->expects(self::once()) + ->method('getSchema') + ->with(self::identicalTo($schemaUri)) ->willReturn($schemaDecoded); - $schemaValidator = $this->prophesize(SchemaValidatorInterface::class); + $schemaValidator = $this->createMock(SchemaValidatorInterface::class); $schemaValidator - ->validate( - Argument::is($json->decoded()), - Argument::is($schemaDecoded), + ->expects(self::once()) + ->method('validate') + ->with( + self::identicalTo($json->decoded()), + self::identicalTo($schemaDecoded), ) - ->shouldBeCalled() ->willReturn(Result::create( $faker->sentence, $faker->sentence, @@ -214,8 +219,8 @@ public function testNormalizeThrowsOriginalInvalidAccordingToSchemaExceptionWhen $normalizer = new SchemaNormalizer( $schemaUri, - $schemaStorage->reveal(), - $schemaValidator->reveal(), + $schemaStorage, + $schemaValidator, ); $this->expectException(Exception\OriginalInvalidAccordingToSchemaException::class); @@ -264,41 +269,38 @@ public function testNormalizeThrowsNormalizedInvalidAccordingToSchemaExceptionWh $schemaDecoded = \json_decode($schema); - $schemaStorage = $this->prophesize(SchemaStorage::class); + $schemaStorage = $this->createMock(SchemaStorage::class); $schemaStorage - ->getSchema(Argument::is($schemaUri)) - ->shouldBeCalled() + ->expects(self::once()) + ->method('getSchema') + ->with(self::identicalTo($schemaUri)) ->willReturn($schemaDecoded); - $schemaValidator = $this->prophesize(SchemaValidatorInterface::class); + $schemaValidator = $this->createMock(SchemaValidatorInterface::class); $schemaValidator - ->validate( - Argument::is($json->decoded()), - Argument::is($schemaDecoded), + ->expects(self::exactly(2)) + ->method('validate') + ->withConsecutive( + [ + self::identicalTo($json->decoded()), + self::identicalTo($schemaDecoded), + ], + [ + self::equalTo($normalized->decoded()), + self::identicalTo($schemaDecoded), + ], ) - ->shouldBeCalled() - ->will(function () use ($schemaValidator, $normalized, $schemaDecoded, $faker): Result { - $schemaValidator - ->validate( - Argument::exact($normalized->decoded()), - Argument::is($schemaDecoded), - ) - ->shouldBeCalled() - ->willReturn(Result::create( - $faker->sentence, - $faker->sentence, - $faker->sentence, - )); - - return Result::create(); - }); + ->willReturnOnConsecutiveCalls( + Result::create(), + Result::create($faker->sentence()), + ); $normalizer = new SchemaNormalizer( $schemaUri, - $schemaStorage->reveal(), - $schemaValidator->reveal(), + $schemaStorage, + $schemaValidator, ); $this->expectException(Exception\NormalizedInvalidAccordingToSchemaException::class); diff --git a/test/Unit/Validator/SchemaValidatorTest.php b/test/Unit/Validator/SchemaValidatorTest.php index e9d6628c..e24469f6 100644 --- a/test/Unit/Validator/SchemaValidatorTest.php +++ b/test/Unit/Validator/SchemaValidatorTest.php @@ -18,7 +18,6 @@ use Ergebnis\Test\Util\Helper; use JsonSchema\Validator; use PHPUnit\Framework; -use Prophecy\Argument; /** * @internal @@ -60,25 +59,26 @@ public function testIsValidUsesSchemaValidator(bool $isValid): void $data = \json_decode($dataJson); $schema = \json_decode($schemaJson); - $schemaValidator = $this->prophesize(Validator::class); + $schemaValidator = $this->createMock(Validator::class); $schemaValidator - ->reset() - ->shouldBeCalled(); + ->expects(self::once()) + ->method('reset'); $schemaValidator - ->check( - Argument::is($data), - Argument::is($schema), - ) - ->shouldBeCalled(); + ->expects(self::once()) + ->method('check') + ->with( + self::identicalTo($data), + self::identicalTo($schema), + ); $schemaValidator - ->isValid() - ->shouldBeCalled() + ->expects(self::once()) + ->method('isValid') ->willReturn($isValid); - $validator = new SchemaValidator($schemaValidator->reveal()); + $validator = new SchemaValidator($schemaValidator); self::assertSame($isValid, $validator->isValid($data, $schema)); }