diff --git a/CHANGELOG.md b/CHANGELOG.md index dbd6053..ad42bf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## v2.1.1 (21. Mar 2022) + +Bug Fixes: +- Fixed the post body deserialization. Billbee sends the payload as `x-www-form-urlencoded`, not a json body. +- Fixed the UnitTests +- Removed the `\Throwable` usage to support PHP 5.6. +- Using `NewStateTypeId` instead of `NewStateId` when updating the order state. `NewStateTypeId` contains an int, + `NewStateId` a string. + ## v2.1.0 (11. Feb 2022) Updated: diff --git a/phpunit.xml b/phpunit.xml index 955ce9d..28f7a6a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,3 +1,4 @@ + - - - - - ./tests/ - - - - - ./src - - + + + + ./src + + + + + ./tests/ + + diff --git a/src/Exception/NotImplementedException.php b/src/Exception/NotImplementedException.php index d13943d..7097eb1 100644 --- a/src/Exception/NotImplementedException.php +++ b/src/Exception/NotImplementedException.php @@ -13,11 +13,10 @@ namespace Billbee\CustomShopApi\Exception; use Exception; -use Throwable; class NotImplementedException extends Exception { - public function __construct($code = 0, Throwable $previous = null) + public function __construct($code = 0, $previous = null) { parent::__construct("Diese Aktion ist nicht implementiert", $code, $previous); } diff --git a/src/RequestHandler/OrderRequestHandler.php b/src/RequestHandler/OrderRequestHandler.php index 764e4ad..d0a2b57 100644 --- a/src/RequestHandler/OrderRequestHandler.php +++ b/src/RequestHandler/OrderRequestHandler.php @@ -114,8 +114,8 @@ private function setOrderState(RequestInterface $request) return Response::badRequest('Es wurde keine OrderId übergeben'); } - if (!isset($data['NewStateId']) || empty($newStateId = (int)trim($data['NewStateId']))) { - return Response::badRequest('Es wurde keine NewStateId übergeben'); + if (!isset($data['NewStateTypeId']) || empty($newStateId = (int)trim($data['NewStateTypeId']))) { + return Response::badRequest('Es wurde keine NewStateTypeId übergeben'); }; diff --git a/src/RequestHandler/RequestHandlerBase.php b/src/RequestHandler/RequestHandlerBase.php index 9ffb6cf..7f5f9ba 100644 --- a/src/RequestHandler/RequestHandlerBase.php +++ b/src/RequestHandler/RequestHandlerBase.php @@ -22,7 +22,8 @@ abstract class RequestHandlerBase implements RequestHandlerInterface protected function deserializeBody(RequestInterface $request) { $requestBody = (string)$request->getBody(); - return json_decode($requestBody, true); + parse_str($requestBody, $deserializedBody); + return $deserializedBody; } public function canHandle(RequestInterface $request, $queryArgs = []) diff --git a/tests/RequestHandler/OrderRequestHandlerTest.php b/tests/RequestHandler/OrderRequestHandlerTest.php index d0abf3d..816296a 100644 --- a/tests/RequestHandler/OrderRequestHandlerTest.php +++ b/tests/RequestHandler/OrderRequestHandlerTest.php @@ -25,6 +25,7 @@ use Exception; use MintWare\Streams\MemoryStream; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\RequestInterface; class OrderRequestHandlerTest extends TestCase { @@ -62,7 +63,7 @@ public function testGetOrders() $this->assertEquals(1, $data['paging']['totalPages']); $this->assertEquals(1, $data['paging']['totalCount']); $body = '{"paging":{"page":1,"totalCount":1,"totalPages":1},"orders":[{"order_id":"1234","order_number":"456","currency_code":"EUR","delivery_source_country_code":"DE","nick_name":"GirlWhoCanFly","ship_cost":4.9,"invoice_address":{"firstname":"Kara","lastname":"Zor-El","street":"Argo Street","housenumber":"1022","address2":"Window","postcode":"90012","city":"National City","country_code":"US","company":"D.E.O.","state":"CA"},"delivery_address":{"firstname":"Kara","lastname":"Zor-El","street":"Argo Street","housenumber":"1022","address2":"Window","postcode":"90012","city":"National City","country_code":"US","company":"D.E.O.","state":"CA"},"order_date":"2019-01-01T20:00:15+0000","email":"secret@deo.tld","phone1":"0123456789","pay_date":"2019-01-01T23:00:15+0000","ship_date":"2019-01-02T02:00:15+0000","payment_method":1,"order_status_id":2,"seller_comment":"Psst","shippingprofile_id":"super-fast","vat_id":"DE-123456","payment_transaction_id":"123444"}]}'; - $this->assertEquals($body, $response->getBody()); + $this->assertEquals($body, (string)$response->getBody()); } public function testGetOrdersFailsInvalidDAte() @@ -103,10 +104,11 @@ public function testAckOrderFailsNotFound() ->willThrowException(new OrderNotFoundException()); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=AckOrder'); $req = $request->withUri($uri) - ->withBody(new MemoryStream(json_encode(['OrderId' => '1']))); + ->withBody(new MemoryStream(http_build_query(['OrderId' => '1']))); $response = $handler->handle($req, ['Action' => 'AckOrder']); @@ -121,10 +123,11 @@ public function testAckOrderFailsUnknownError() ->willThrowException(new Exception('Unknown Error')); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=AckOrder&OrderId=1'); $req = $request->withUri($uri) - ->withBody(new MemoryStream(json_encode(['OrderId' => '1']))); + ->withBody(new MemoryStream(http_build_query(['OrderId' => '1']))); $response = $handler->handle($req, ['Action' => 'AckOrder']); @@ -140,10 +143,11 @@ public function testAckOrder() ->willReturn(null); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=AckOrder&OrderId=1'); $req = $request->withUri($uri) - ->withBody(new MemoryStream(json_encode(['OrderId' => '1']))); + ->withBody(new MemoryStream(http_build_query(['OrderId' => '1']))); $response = $handler->handle($req, ['Action' => 'AckOrder']); @@ -234,19 +238,20 @@ public function testSetOrderStateFailsNoOrderId() $this->assertEquals('Es wurde keine OrderId übergeben', (string)$response->getBody()); } - public function testSetOrderStateFailsNoNewStateId() + public function testSetOrderStateFailsNoNewStateTypeId() { $repo = $this->createMock(OrdersRepositoryInterface::class); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=SetOrderState'); - $req = $request->withUri($uri)->withBody(new MemoryStream(json_encode(['OrderId' => '1']))); + $req = $request->withUri($uri)->withBody(new MemoryStream(http_build_query(['OrderId' => '1']))); $response = $handler->handle($req, ['Action' => 'SetOrderState']); $this->assertEquals(400, $response->getStatusCode()); - $this->assertEquals('Es wurde keine NewStateId übergeben', (string)$response->getBody()); + $this->assertEquals('Es wurde keine NewStateTypeId übergeben', (string)$response->getBody()); } public function testSetOrderStateFailsNotFound() @@ -256,9 +261,10 @@ public function testSetOrderStateFailsNotFound() ->willThrowException(new OrderNotFoundException()); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=SetOrderState'); - $req = $request->withUri($uri)->withBody(new MemoryStream(json_encode(['OrderId' => 1, 'NewStateId' => 1]))); + $req = $request->withUri($uri)->withBody(new MemoryStream(http_build_query(['OrderId' => 1, 'NewStateTypeId' => 1]))); $response = $handler->handle($req, ['Action' => 'SetOrderState']); @@ -273,9 +279,10 @@ public function testSetOrderStateUnknownError() ->willThrowException(new Exception('Unknown Error')); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=SetOrderState'); - $req = $request->withUri($uri)->withBody(new MemoryStream(json_encode(['OrderId' => 1, 'NewStateId' => 1]))); + $req = $request->withUri($uri)->withBody(new MemoryStream(http_build_query(['OrderId' => 1, 'NewStateTypeId' => 1]))); $response = $handler->handle($req, ['Action' => 'SetOrderState', 'OrderId' => '1']); @@ -290,9 +297,10 @@ public function testSetOrderState() ->willReturn(true); $handler = new OrderRequestHandler($repo); + /** @var RequestInterface $request */ $request = new Request(); $uri = new Uri('http://localhost/?Action=SetOrderState'); - $req = $request->withUri($uri)->withBody(new MemoryStream(json_encode(['OrderId' => 1, 'NewStateId' => 1]))); + $req = $request->withUri($uri)->withBody(new MemoryStream(http_build_query(['OrderId' => 1, 'NewStateTypeId' => 1]))); $response = $handler->handle($req, ['Action' => 'SetOrderState', 'OrderId' => '1']); diff --git a/tests/RequestHandler/ProductRequestHandlerTest.php b/tests/RequestHandler/ProductRequestHandlerTest.php index ad6869c..18a7a10 100644 --- a/tests/RequestHandler/ProductRequestHandlerTest.php +++ b/tests/RequestHandler/ProductRequestHandlerTest.php @@ -142,7 +142,7 @@ public function testGetProducts() $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $json = '{"paging":{"page":1,"totalCount":1,"totalPages":1},"products":[{"material":"Wood","shortdescription":"A chair","basic_attributes":"black, wood","description":"A black wooden chair","id":"4711","images":[{"url":"http:\/\/my.shop.url\/image.jpeg","isDefault":true,"Position":1}],"title":"Black wooden chair","price":29.99,"quantity":12,"sku":"CH_BLK","ean":"9876543210789","manufacturer":"Lumberjack Furnitures","isdigital":false,"weight":5.99,"vat_rate":19,"lengthcm":45,"widthcm":45,"heightcm":120,"customfields":{}}]}'; - $this->assertEquals($json, $response->getBody()); + $this->assertEquals($json, (string)$response->getBody()); } private function createDemoProduct() diff --git a/tests/RequestHandler/StockSyncRequestHandlerTest.php b/tests/RequestHandler/StockSyncRequestHandlerTest.php index 5ceef55..a86cd9c 100644 --- a/tests/RequestHandler/StockSyncRequestHandlerTest.php +++ b/tests/RequestHandler/StockSyncRequestHandlerTest.php @@ -59,7 +59,7 @@ public function testSetStockFailsNoAvailableStock() $repo = $this->createMock(StockSyncRepositoryInterface::class); $handler = new StockRequestHandler($repo); $request = new Request(); - $request = $request->withBody(new MemoryStream('{"ProductId": "4177"}')); + $request = $request->withBody(new MemoryStream(http_build_query(['ProductId' => "4177"]))); $response = $handler->handle($request, ['Action' => 'SetStock']); $this->assertEquals(400, $response->getStatusCode()); @@ -71,11 +71,13 @@ public function testSetStockFailsProductNotFound() { $repo = $this->createMock(StockSyncRepositoryInterface::class); $repo->method('SetStock') - ->willThrowException(new ProductNotFoundException()); + ->willThrowException(new ProductNotFoundException()); $handler = new StockRequestHandler($repo); $request = new Request(); - $request = $request->withBody(new MemoryStream('{"ProductId": "4177", "AvailableStock": 123}')); + $request = $request->withBody( + new MemoryStream(http_build_query(['ProductId' => '4177', "AvailableStock" => 123])) + ); $response = $handler->handle($request, ['Action' => 'SetStock']); $this->assertEquals(404, $response->getStatusCode()); @@ -87,11 +89,11 @@ public function testSetStockFailsException() { $repo = $this->createMock(StockSyncRepositoryInterface::class); $repo->method('SetStock') - ->willThrowException(new RuntimeException("Unknown Error")); + ->willThrowException(new RuntimeException("Unknown Error")); $handler = new StockRequestHandler($repo); $request = new Request(); - $request = $request->withBody(new MemoryStream('{"ProductId": "4177", "AvailableStock": 123}')); + $request = $request->withBody(new MemoryStream(http_build_query(['ProductId' => '4177', "AvailableStock" => 123]))); $response = $handler->handle($request, ['Action' => 'SetStock']); $this->assertEquals(500, $response->getStatusCode()); @@ -106,7 +108,7 @@ public function testSetStock() $handler = new StockRequestHandler($repo); $request = new Request(); - $request = $request->withBody(new MemoryStream('{"ProductId": "4177", "AvailableStock": 123}')); + $request = $request->withBody(new MemoryStream(http_build_query(['ProductId' => '4177', "AvailableStock" => 123]))); $response = $handler->handle($request, ['Action' => 'SetStock']); $this->assertEquals(200, $response->getStatusCode());