Skip to content

Commit

Permalink
Fix body deserialization and SetOrderState (#9)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
devtronic authored Mar 21, 2022
1 parent 8d0973c commit 9d2523e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 34 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
24 changes: 12 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0"?>
<!--
~ This file is part of the Billbee Custom Shop API package.
~
Expand All @@ -8,16 +9,15 @@
~
~ Created by Julian Finkler <[email protected]>
-->

<phpunit bootstrap="tests/autoload.php">
<testsuites>
<testsuite name="UnitTests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="UnitTests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
3 changes: 1 addition & 2 deletions src/Exception/NotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/RequestHandler/OrderRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};


Expand Down
3 changes: 2 additions & 1 deletion src/RequestHandler/RequestHandlerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
28 changes: 18 additions & 10 deletions tests/RequestHandler/OrderRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Exception;
use MintWare\Streams\MemoryStream;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

class OrderRequestHandlerTest extends TestCase
{
Expand Down Expand Up @@ -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":"[email protected]","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()
Expand Down Expand Up @@ -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']);

Expand All @@ -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']);
Expand All @@ -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']);
Expand Down Expand Up @@ -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()
Expand All @@ -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']);

Expand All @@ -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']);

Expand All @@ -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']);

Expand Down
2 changes: 1 addition & 1 deletion tests/RequestHandler/ProductRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions tests/RequestHandler/StockSyncRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 9d2523e

Please sign in to comment.