From 31719a8548692941c7b2b943eff32c46e61312ca Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Mon, 2 Nov 2020 01:29:03 +0900 Subject: [PATCH] Improve tests --- examples/140_GetOrder.php | 3 ++ examples/150_OrdersStatus.php | 3 ++ .../BadRequestResponse/ValidationError.php | 4 ++ .../OrderStatusesResponseTest.php | 4 +- .../OrdersStatusResponseTest.php | 16 +++++++ .../SubmitOrderResponseTest.php | 9 ++++ tests/Deserialization/ValidationErrorTest.php | 48 +++++++++++++------ tests/Fixtures/responses/empty.json | 1 + .../responses/orders-status-empty.json | 20 ++++++++ .../responses/validation-error-code.json | 4 ++ .../responses/validation-error-simple.json | 4 ++ .../Fixtures/responses/validation-error.json | 8 ++++ .../Fixtures/responses/validation-error2.json | 5 ++ 13 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 tests/Fixtures/responses/empty.json create mode 100644 tests/Fixtures/responses/orders-status-empty.json create mode 100644 tests/Fixtures/responses/validation-error-code.json create mode 100644 tests/Fixtures/responses/validation-error-simple.json create mode 100644 tests/Fixtures/responses/validation-error.json create mode 100644 tests/Fixtures/responses/validation-error2.json diff --git a/examples/140_GetOrder.php b/examples/140_GetOrder.php index 91130fe..ef38577 100644 --- a/examples/140_GetOrder.php +++ b/examples/140_GetOrder.php @@ -40,6 +40,9 @@ $order = $client->getOrder((int) $argv[1]); +$logger->addFile('order-status-request.json'); +$logger->addFile('order-status-response.json'); + $statuses = $client->getOrderStatuses($order->id); echo "Order ID: {$statuses->id}\n"; diff --git a/examples/150_OrdersStatus.php b/examples/150_OrdersStatus.php index 9c5dd6f..90c1784 100644 --- a/examples/150_OrdersStatus.php +++ b/examples/150_OrdersStatus.php @@ -48,6 +48,9 @@ $order = $request->addOrder(); $order->externalId = '100'; +$logger->addFile('orders-status-request.json'); +$logger->addFile('orders-status-response.json'); + $response = $client->sendOrdersStatusRequest($request); if ($response->hasErrors()) { diff --git a/src/Responses/Bad/BadRequestResponse/ValidationError.php b/src/Responses/Bad/BadRequestResponse/ValidationError.php index 2d5d135..84f14b4 100644 --- a/src/Responses/Bad/BadRequestResponse/ValidationError.php +++ b/src/Responses/Bad/BadRequestResponse/ValidationError.php @@ -151,6 +151,10 @@ public function getMessage(): string return \sprintf('%s->%s %s', $this->objectName, $this->field, $this->message); } + if ($this->message !== null) { + return $this->message; + } + foreach (\get_object_vars($this) as $key => $value) { if (!\is_string($value)) { continue; diff --git a/tests/Deserialization/OrderStatusesResponseTest.php b/tests/Deserialization/OrderStatusesResponseTest.php index 84a24b9..115e4ef 100644 --- a/tests/Deserialization/OrderStatusesResponseTest.php +++ b/tests/Deserialization/OrderStatusesResponseTest.php @@ -33,11 +33,11 @@ /** * @covers \YDeliverySDK\Responses\OrderStatusesResponse - * @covers \YDeliverySDK\Responses\Types\Order + * @covers \YDeliverySDK\Responses\Types\Status */ class OrderStatusesResponseTest extends TestCase { - public function test_empty_order() + public function test_regular_order() { $response = $this->loadFixture('statuses.json'); diff --git a/tests/Deserialization/OrdersStatusResponseTest.php b/tests/Deserialization/OrdersStatusResponseTest.php index 849998a..550ca5f 100644 --- a/tests/Deserialization/OrdersStatusResponseTest.php +++ b/tests/Deserialization/OrdersStatusResponseTest.php @@ -34,6 +34,7 @@ /** * @covers \YDeliverySDK\Responses\OrdersStatusResponse * @covers \YDeliverySDK\Responses\Types\OrderStatus + * @covers \YDeliverySDK\Responses\Types\Status */ class OrdersStatusResponseTest extends TestCase { @@ -60,6 +61,21 @@ public function test_successful_request() } } + public function test_invalid_order() + { + $response = $this->loadFixture('orders-status-empty.json'); + + $this->assertFalse($response->hasErrors()); + + $this->assertCount(2, $response); + + foreach ($response as $item) { + /** @var $item OrderStatus */ + $this->assertNull($item->status->datetime); + $this->assertGreaterThan(0, $item->status->timestamp->getTimestamp()); + } + } + private function loadFixture(string $filename): OrdersStatusResponse { return $this->loadFixtureWithType($filename, OrdersStatusResponse::class); diff --git a/tests/Deserialization/SubmitOrderResponseTest.php b/tests/Deserialization/SubmitOrderResponseTest.php index 7f10c05..4da35f7 100644 --- a/tests/Deserialization/SubmitOrderResponseTest.php +++ b/tests/Deserialization/SubmitOrderResponseTest.php @@ -37,6 +37,7 @@ /** * @covers \YDeliverySDK\Responses\SubmitOrderResponse * @covers \YDeliverySDK\Responses\Types\SubmittedOrder + * @covers \YDeliverySDK\Responses\Types\SubmittedOrder\Error */ class SubmitOrderResponseTest extends TestCase { @@ -93,6 +94,14 @@ public function test_with_validation_error() ); } + public function test_empty() + { + $response = $this->loadFixture('empty.json'); + + $this->assertCount(0, $response); + $this->assertFalse($response->hasErrors()); + } + private function toErrorsArray(Response $response): array { return take($response->getMessages())->map(function (HasErrorCode $message) { diff --git a/tests/Deserialization/ValidationErrorTest.php b/tests/Deserialization/ValidationErrorTest.php index 099f266..5f40889 100644 --- a/tests/Deserialization/ValidationErrorTest.php +++ b/tests/Deserialization/ValidationErrorTest.php @@ -31,27 +31,47 @@ use YDeliverySDK\Responses\Bad\BadRequestResponse\ValidationError; /** - * @covers \YDeliverySDK\Responses\PostalCodeResponse - * @covers \YDeliverySDK\Responses\Types\PostalCode + * @covers \YDeliverySDK\Responses\Bad\BadRequestResponse\ValidationError */ class ValidationErrorTest extends TestCase { - private const EXAMPLE_ERROR = '{ - "objectName": "senderOrderDraft", - "field": "deliveryType", - "message": "must not be null", - "conditionCode": "NotNull", - "arguments": {}, - "errorCode": "FIELD_NOT_VALID" - }'; - - public function test_it_deserializes() + public function test_it_deserializes_simple_type() { - /** @var ValidationError $error */ - $error = $this->getSerializer()->deserialize(self::EXAMPLE_ERROR, ValidationError::class); + $error = $this->loadFixture('validation-error-simple.json'); + + $this->assertSame('BOO_ERROR', $error->getErrorCode()); + $this->assertSame('Boo', $error->getMessage()); + } + + public function test_it_deserializes_standard_type() + { + $error = $this->loadFixture('validation-error.json'); $this->assertSame('FIELD_NOT_VALID', $error->getErrorCode()); $this->assertSame('senderOrderDraft->deliveryType must not be null', $error->getMessage()); $this->assertSame('NotNull', $error->conditionCode); } + + public function test_it_deserializes_rare_type() + { + $error = $this->loadFixture('validation-error2.json'); + + $this->assertSame('OPTION_CALCULATED_DATE_MIN_MISMATCH', $error->getErrorCode()); + $this->assertSame('2020-10-31', $error->getMessage()); + $this->assertNull($error->currentValue); + } + + public function test_it_deserializes_even_rare_type() + { + $error = $this->loadFixture('validation-error-code.json'); + + $this->assertSame('FATAL_ERROR', $error->getErrorCode()); + $this->assertSame('FATAL_ERROR', $error->getMessage()); + $this->assertSame('FATAL_ERROR', $error->message); + } + + private function loadFixture(string $filename): ValidationError + { + return $this->loadFixtureWithType($filename, ValidationError::class); + } } diff --git a/tests/Fixtures/responses/empty.json b/tests/Fixtures/responses/empty.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/tests/Fixtures/responses/empty.json @@ -0,0 +1 @@ +[] diff --git a/tests/Fixtures/responses/orders-status-empty.json b/tests/Fixtures/responses/orders-status-empty.json new file mode 100644 index 0000000..8c88b3b --- /dev/null +++ b/tests/Fixtures/responses/orders-status-empty.json @@ -0,0 +1,20 @@ +[ + { + "id": 111, + "externalId": null, + "status": { + "code": "NOT_FOUND", + "description": "Заказ не найден.", + "datetime": null + } + }, + { + "id": null, + "externalId": "100", + "status": { + "code": "NOT_FOUND", + "description": "Заказ не найден.", + "datetime": null + } + } +] \ No newline at end of file diff --git a/tests/Fixtures/responses/validation-error-code.json b/tests/Fixtures/responses/validation-error-code.json new file mode 100644 index 0000000..73779fa --- /dev/null +++ b/tests/Fixtures/responses/validation-error-code.json @@ -0,0 +1,4 @@ +{ + "errorCode": "FATAL_ERROR", + "message": null +} diff --git a/tests/Fixtures/responses/validation-error-simple.json b/tests/Fixtures/responses/validation-error-simple.json new file mode 100644 index 0000000..760ed67 --- /dev/null +++ b/tests/Fixtures/responses/validation-error-simple.json @@ -0,0 +1,4 @@ +{ + "errorCode": "BOO_ERROR", + "message": "Boo" +} diff --git a/tests/Fixtures/responses/validation-error.json b/tests/Fixtures/responses/validation-error.json new file mode 100644 index 0000000..9aafb52 --- /dev/null +++ b/tests/Fixtures/responses/validation-error.json @@ -0,0 +1,8 @@ +{ + "objectName": "senderOrderDraft", + "field": "deliveryType", + "message": "must not be null", + "conditionCode": "NotNull", + "arguments": {}, + "errorCode": "FIELD_NOT_VALID" +} diff --git a/tests/Fixtures/responses/validation-error2.json b/tests/Fixtures/responses/validation-error2.json new file mode 100644 index 0000000..effef16 --- /dev/null +++ b/tests/Fixtures/responses/validation-error2.json @@ -0,0 +1,5 @@ +{ + "errorCode": "OPTION_CALCULATED_DATE_MIN_MISMATCH", + "currentValue": null, + "actualValue": "2020-10-31" +}