Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created November 16, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/13a0aa121bb49ca4df7729ee381315d9 to your computer and use it in GitHub Desktop.
Save martinbean/13a0aa121bb49ca4df7729ee381315d9 to your computer and use it in GitHub Desktop.
TestResponse fromJsonString() macro
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\TestResponse;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testExample()
{
$response = TestResponse::fromJsonString(
file_get_contents('http://example.com/test.json')
);
$response->assertJsonFragment(
//
);
}
}
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp()
{
parent::setUp();
TestResponse::macro('fromJsonString', function ($string) {
return TestResponse::fromBaseResponse(
(new Response)->setContent($string)
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment