Skip to content

Commit

Permalink
Merge pull request #23 from consilience/issue21
Browse files Browse the repository at this point in the history
Issue #21 add same fix to abstract collection
  • Loading branch information
judgej authored Jan 14, 2021
2 parents d182150 + fc640d2 commit 42578be
Show file tree
Hide file tree
Showing 3 changed files with 3,903 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public static function fromResponse(ResponseInterface $response)
{
// We can only deal with JSON response bodies.

if ($response->getHeaderLine('Content-Type') === 'application/json') {
[$contentType] = explode(';', $response->getHeaderLine('Content-Type'));

if (trim($contentType) === 'application/json') {
$bodyData = json_decode((string)$response->getBody(), true);
$collection = new static($bodyData);
} else {
Expand Down
60 changes: 60 additions & 0 deletions tests/Response/PaymentDetailsCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Consilience\Starling\Payments\Response;

/**
*
*/

use PHPUnit\Framework\TestCase;
use GuzzleHttp\Psr7\Response as Psr7Response;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Currencies\ISOCurrencies;
use Money\Money;
use Carbon\Carbon;

class PaymentDetailsCollectionTest extends TestCase
{
/**
* Read and return the contents of a test JSON file.
*/
protected function readDataFile($filename)
{
$dataJson = file_get_contents(__DIR__ . '/../data/' . $filename);
return $dataJson;
}

/**
* @return void
* @dataProvider providerFromPSR7
*/
public function testFromPSR7($contentType)
{
$payload = $this->readDataFile('paymentDetailsCollection.json');

$paymentDetailsCollectionResponse = new Psr7Response(
200,
[
'Content-Type' => $contentType,
],
// Squeeze out the white space.
json_encode(json_decode($payload))
);

$paymentDetailsCollection = PaymentDetailsCollection::fromResponse(
$paymentDetailsCollectionResponse
);

$this->assertSame(100, $paymentDetailsCollection->count());
}

public function providerFromPSR7()
{
return [
['application/json'],
['application/json;'],
['application/json; utf-8'],
[' application/json ; utf-8 '],
];
}
}
Loading

0 comments on commit 42578be

Please sign in to comment.