Skip to content

Commit

Permalink
add shortcut to return json data tester from TestResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Mar 16, 2018
1 parent 12062be commit cb9ffcc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"phpunit/phpunit": "^6.0|^5.7",
"guzzlehttp/psr7": "^1.4",
"pdeans/http": "^1.1"
},
"require-dev": {
"draw/data-tester": "*"
}
}
15 changes: 15 additions & 0 deletions src/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Draw\HttpTester;

use Draw\DataTester\Tester;
use Draw\HttpTester\Cookie\Cookie;
use Draw\HttpTester\Cookie\CookieJar;
use PHPUnit\Framework\TestCase as PHPUnit;
Expand Down Expand Up @@ -196,4 +197,18 @@ private function getCookies()
}
return $cookies;
}

/**
* @return Tester
*/
public function toJsonDataTester()
{
if(!class_exists('Draw\DataTester\Tester')) {
throw new \RuntimeException('Draw\\DataTester\\Tester class not found. It require package draw/data-tester.');
}

return (new Tester($this->getResponseBodyContents()))
->assertJson()
->transform('json_decode');
}
}
13 changes: 13 additions & 0 deletions test/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Draw\HttpTester;

use Draw\DataTester\Tester;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\ExpectationFailedException;
Expand Down Expand Up @@ -343,4 +344,16 @@ public function testAssertCookieValueNotMatch()

$testResponse->assertCookie('name', 'other-value');
}

public function testCreateJsonDataTester()
{
$testResponse = $this->createTestResponse(
new Response(200, [], '{}')
);

$this->assertInstanceOf(
Tester::class,
$testResponse->toJsonDataTester()
);
}
}

0 comments on commit cb9ffcc

Please sign in to comment.