From ee498eff36498f28ec61088fc309ca7e18d53380 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Fri, 15 Sep 2023 10:01:16 -0500 Subject: [PATCH] Add assertions for phpstan --- .github/workflows/phpstan.yml | 2 +- composer.json | 2 ++ src/Testing/GivenWhenThen/Scenario.php | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index ed35207..d52dceb 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -36,5 +36,5 @@ jobs: with: composer-options: "--no-progress --prefer-dist --optimize-autoloader" - - name: Larastan + - name: PHPStan run: php ./vendor/bin/phpstan analyse --memory-limit=2G --error-format=github diff --git a/composer.json b/composer.json index 7ebe20d..0859490 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,8 @@ } }, "scripts": { + "lint": "@phpstan", + "phpstan": "@php ./vendor/bin/phpstan analyze", "style:check": "@php ./vendor/bin/php-cs-fixer fix --dry-run", "style:fix": "@php ./vendor/bin/php-cs-fixer fix" } diff --git a/src/Testing/GivenWhenThen/Scenario.php b/src/Testing/GivenWhenThen/Scenario.php index e25dbfc..365a63a 100644 --- a/src/Testing/GivenWhenThen/Scenario.php +++ b/src/Testing/GivenWhenThen/Scenario.php @@ -272,12 +272,22 @@ public function usingMessageRepository(InMemoryMessageRepository $messageReposit public function assertObjectSurvivesSerializationRoundTrip(mixed $input): void { - $payloadSerializer = $this->payloadSerializer ?? DefaultPayloadSerializer::resolve(); + if (!isset($this->payloadSerializer)) { + $this->payloadSerializer = DefaultPayloadSerializer::resolve(); + } + + assert(is_object($input), 'Input must be an object.'); - $serializedPayload = $payloadSerializer->serializePayload($input); + $serializedPayload = $this->payloadSerializer->serializePayload($input); $jsonEncodedSerializedPayload = json_encode($serializedPayload); + + assert(is_string($jsonEncodedSerializedPayload), 'JSON encoding failed.'); + $jsonDecodedSerializedPayload = json_decode($jsonEncodedSerializedPayload, true); - $unserializedPayload = $payloadSerializer->unserializePayload(get_class($input), $jsonDecodedSerializedPayload); + + assert(is_array($jsonDecodedSerializedPayload), 'JSON decoding failed.'); + + $unserializedPayload = $this->payloadSerializer->unserializePayload(get_class($input), $jsonDecodedSerializedPayload); Assert::assertEquals($input, $unserializedPayload, 'Payload serialization failed round trip.'); }