2017-01-31 23:09:17 +02:00
< ? php
2021-02-09 14:30:06 +00:00
namespace Illuminate\Tests\Testing ;
2017-01-31 23:09:17 +02:00
2024-06-07 00:18:24 +10:00
use Exception ;
2020-12-14 15:30:57 -05:00
use Illuminate\Container\Container ;
2017-03-01 18:28:20 -05:00
use Illuminate\Contracts\View\View ;
2020-12-14 15:30:57 -05:00
use Illuminate\Cookie\CookieValuePrefix ;
2022-07-05 04:50:48 +02:00
use Illuminate\Database\Eloquent\Collection as EloquentCollection ;
2018-10-09 09:01:37 -04:00
use Illuminate\Database\Eloquent\Model ;
2020-12-14 15:30:57 -05:00
use Illuminate\Encryption\Encrypter ;
2019-09-10 17:16:05 +02:00
use Illuminate\Filesystem\Filesystem ;
2024-06-07 00:18:24 +10:00
use Illuminate\Http\JsonResponse ;
2021-07-21 22:24:16 +10:00
use Illuminate\Http\RedirectResponse ;
2022-04-04 20:07:23 +04:30
use Illuminate\Http\Request ;
2019-09-10 17:16:05 +02:00
use Illuminate\Http\Response ;
2022-04-04 20:07:23 +04:30
use Illuminate\Routing\RouteCollection ;
use Illuminate\Routing\UrlGenerator ;
2021-07-21 22:24:16 +10:00
use Illuminate\Session\ArraySessionHandler ;
2024-06-07 00:18:24 +10:00
use Illuminate\Session\NullSessionHandler ;
2021-07-21 22:24:16 +10:00
use Illuminate\Session\Store ;
2022-05-15 18:55:52 +03:00
use Illuminate\Support\Collection ;
2021-08-16 08:32:35 -05:00
use Illuminate\Support\MessageBag ;
use Illuminate\Support\ViewErrorBag ;
2021-03-08 11:49:04 -06:00
use Illuminate\Testing\Fluent\AssertableJson ;
2020-01-08 23:03:00 +08:00
use Illuminate\Testing\TestResponse ;
2019-09-10 17:16:05 +02:00
use JsonSerializable ;
use Mockery as m ;
use PHPUnit\Framework\AssertionFailedError ;
2025-05-21 21:11:04 +08:00
use PHPUnit\Framework\Attributes\TestWith ;
2019-02-07 17:04:40 +01:00
use PHPUnit\Framework\ExpectationFailedException ;
2019-09-10 17:16:05 +02:00
use PHPUnit\Framework\TestCase ;
2017-02-23 21:22:44 -05:00
use Symfony\Component\HttpFoundation\BinaryFileResponse ;
2020-12-14 15:30:57 -05:00
use Symfony\Component\HttpFoundation\Cookie ;
2024-01-29 15:55:52 +01:00
use Symfony\Component\HttpFoundation\StreamedJsonResponse ;
2022-12-14 14:50:55 +00:00
use Symfony\Component\HttpFoundation\StreamedResponse ;
2017-01-31 23:09:17 +02:00
2020-01-08 23:03:00 +08:00
class TestResponseTest extends TestCase
2017-01-31 23:09:17 +02:00
{
2025-09-02 01:45:37 +03:00
public function testAssertViewIs () : void
2017-05-20 18:46:03 -07:00
{
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'getData' => [ 'foo' => 'bar' ],
2018-12-05 17:59:41 +01:00
'name' => 'dir.my-view' ,
2018-08-17 15:43:03 +02:00
]);
2017-05-20 18:46:03 -07:00
$response -> assertViewIs ( 'dir.my-view' );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHas () : void
2017-03-01 18:28:20 -05:00
{
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
2019-06-19 09:34:26 -05:00
'gatherData' => [ 'foo' => 'bar' ],
2018-08-17 15:43:03 +02:00
]);
2017-03-01 18:28:20 -05:00
$response -> assertViewHas ( 'foo' );
2017-04-06 15:03:51 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasModel () : void
2018-10-09 09:01:37 -04:00
{
2022-05-19 10:31:04 -04:00
$model = new TestModel ([ 'id' => 1 ]);
2018-10-09 09:01:37 -04:00
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
2019-06-19 09:34:26 -05:00
'gatherData' => [ 'foo' => $model ],
2018-10-09 09:01:37 -04:00
]);
$response -> assertViewHas ( 'foo' , $model );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasWithClosure () : void
2019-12-16 08:27:39 -07:00
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foo' => 'bar' ],
]);
$response -> assertViewHas ( 'foo' , function ( $value ) {
return $value === 'bar' ;
});
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasWithValue () : void
2019-12-16 08:27:39 -07:00
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foo' => 'bar' ],
]);
$response -> assertViewHas ( 'foo' , 'bar' );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasNested () : void
2020-07-17 15:18:51 +02:00
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [
'foo' => [
'nested' => 'bar' ,
],
],
]);
$response -> assertViewHas ( 'foo.nested' );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasWithNestedValue () : void
2019-12-16 08:27:39 -07:00
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [
'foo' => [
'nested' => 'bar' ,
],
],
]);
$response -> assertViewHas ( 'foo.nested' , 'bar' );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasEloquentCollection () : void
2022-05-19 10:31:04 -04:00
{
2022-07-05 04:50:48 +02:00
$collection = new EloquentCollection ([
2022-05-19 10:31:04 -04:00
new TestModel ([ 'id' => 1 ]),
new TestModel ([ 'id' => 2 ]),
new TestModel ([ 'id' => 3 ]),
]);
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foos' => $collection ],
]);
$response -> assertViewHas ( 'foos' , $collection );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasEloquentCollectionRespectsOrder () : void
2022-05-19 10:31:04 -04:00
{
2022-07-05 04:50:48 +02:00
$collection = new EloquentCollection ([
2022-05-19 10:31:04 -04:00
new TestModel ([ 'id' => 3 ]),
new TestModel ([ 'id' => 2 ]),
new TestModel ([ 'id' => 1 ]),
]);
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foos' => $collection ],
]);
$this -> expectException ( AssertionFailedError :: class );
$response -> assertViewHas ( 'foos' , $collection -> reverse () -> values ());
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasEloquentCollectionRespectsType () : void
2022-05-19 10:31:04 -04:00
{
2022-07-05 04:50:48 +02:00
$actual = new EloquentCollection ([
2022-05-19 10:31:04 -04:00
new TestModel ([ 'id' => 1 ]),
new TestModel ([ 'id' => 2 ]),
]);
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foos' => $actual ],
]);
2022-07-05 04:50:48 +02:00
$expected = new EloquentCollection ([
2022-05-19 10:31:04 -04:00
new AnotherTestModel ([ 'id' => 1 ]),
new AnotherTestModel ([ 'id' => 2 ]),
]);
$this -> expectException ( AssertionFailedError :: class );
$response -> assertViewHas ( 'foos' , $expected );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasEloquentCollectionRespectsSize () : void
2022-05-19 10:31:04 -04:00
{
2022-07-05 04:50:48 +02:00
$actual = new EloquentCollection ([
2022-05-19 10:31:04 -04:00
new TestModel ([ 'id' => 1 ]),
new TestModel ([ 'id' => 2 ]),
]);
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foos' => $actual ],
]);
$this -> expectException ( AssertionFailedError :: class );
$response -> assertViewHas ( 'foos' , $actual -> concat ([ new TestModel ([ 'id' => 3 ])]));
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasWithArray () : void
2023-06-03 00:27:23 +03:30
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foo' => 'bar' ],
]);
$response -> assertViewHas ([ 'foo' => 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertViewHasAll () : void
2023-06-07 16:51:51 +03:30
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foo' => 'bar' ],
]);
$response -> assertViewHasAll ([
'foo' => 'bar' ,
]);
}
2025-09-02 01:45:37 +03:00
public function testAssertViewMissing () : void
2020-07-17 15:18:51 +02:00
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foo' => 'bar' ],
]);
$response -> assertViewMissing ( 'baz' );
}
2025-09-02 01:45:37 +03:00
public function testAssertViewMissingNested () : void
2020-07-17 15:18:51 +02:00
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [
'foo' => [
'nested' => 'bar' ,
],
],
]);
$response -> assertViewMissing ( 'foo.baz' );
}
2026-02-09 21:27:42 +08:00
public function testViewData () : void
{
$response = $this -> makeMockResponse ([
'render' => 'hello world' ,
'gatherData' => [ 'foo' => 'bar' , 'baz' => 'qux' ],
]);
$this -> assertEquals ( 'bar' , $response -> viewData ( 'foo' ));
$this -> assertEquals ([ 'foo' => 'bar' , 'baz' => 'qux' ], $response -> viewData ());
}
2025-09-02 01:45:37 +03:00
public function testAssertContent () : void
2022-10-15 00:34:00 +11:00
{
$response = $this -> makeMockResponse ([
'render' => 'expected response data' ,
]);
$response -> assertContent ( 'expected response data' );
try {
$response -> assertContent ( 'expected' );
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
try {
$response -> assertContent ( 'expected response data with extra' );
2022-12-14 14:50:55 +00:00
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
}
2025-09-02 01:45:37 +03:00
public function testAssertStreamedAndAssertNotStreamed () : void
2025-02-12 17:20:48 +01:00
{
$notStreamedResponse = $this -> makeMockResponse ([
'render' => 'expected response data' ,
]);
$streamedResponse = TestResponse :: fromBaseResponse (
new StreamedResponse ( function () {
$stream = fopen ( 'php://memory' , 'r+' );
fwrite ( $stream , 'expected response data' );
rewind ( $stream );
fpassthru ( $stream );
})
);
$notStreamedResponse -> assertNotStreamed ();
$streamedResponse -> assertStreamed ();
try {
$notStreamedResponse -> assertStreamed ();
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( " Expected the response to be streamed, but it wasn't. \n Failed asserting that false is true. " , $e -> getMessage ());
}
try {
$streamedResponse -> assertNotStreamed ();
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( " Response was unexpectedly streamed. \n Failed asserting that false is true. " , $e -> getMessage ());
}
}
2025-09-02 01:45:37 +03:00
public function testAssertStreamedContent () : void
2022-12-14 14:50:55 +00:00
{
$response = TestResponse :: fromBaseResponse (
new StreamedResponse ( function () {
$stream = fopen ( 'php://memory' , 'r+' );
fwrite ( $stream , 'expected response data' );
rewind ( $stream );
fpassthru ( $stream );
})
);
$response -> assertStreamedContent ( 'expected response data' );
try {
$response -> assertStreamedContent ( 'expected' );
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
try {
$response -> assertStreamedContent ( 'expected response data with extra' );
2022-10-15 00:34:00 +11:00
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
}
2025-09-02 01:45:37 +03:00
public function testAssertStreamedJsonContent () : void
2024-01-29 15:55:52 +01:00
{
$response = TestResponse :: fromBaseResponse (
new StreamedJsonResponse ([
'data' => $this -> yieldTestModels (),
])
);
$response -> assertStreamedJsonContent ([
'data' => [
[ 'id' => 1 ],
[ 'id' => 2 ],
[ 'id' => 3 ],
],
]);
try {
$response -> assertStreamedJsonContent ([
'data' => [
[ 'id' => 1 ],
[ 'id' => 2 ],
],
]);
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
try {
$response -> assertStreamedContent ( 'not expected response string' );
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
}
2026-02-27 15:04:50 +01:00
public function testAssertStreamedBinaryFile () : void
{
$response = TestResponse :: fromBaseResponse (
new BinaryFileResponse ( __DIR__ . '/Fixtures/file.json' )
);
$response -> assertStreamedContent ( '{"foo":"bar"}' );
try {
$response -> assertStreamedContent ( 'not expected response string' );
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
}
public function testAssertStreamedJsonFile () : void
{
$response = TestResponse :: fromBaseResponse (
new BinaryFileResponse ( __DIR__ . '/Fixtures/file.json' )
);
$response -> assertStreamedJsonContent ([ 'foo' => 'bar' ]);
try {
$response -> assertStreamedJsonContent ([
'data' => [
[ 'id' => 1 ],
[ 'id' => 2 ],
],
]);
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two strings are identical.' , $e -> getMessage ());
}
}
2025-09-02 01:45:37 +03:00
public function testJsonAssertionsOnStreamedJsonContent () : void
2025-02-11 18:15:16 +01:00
{
$response = TestResponse :: fromBaseResponse (
new StreamedJsonResponse ([
'data' => $this -> yieldTestModels (),
])
);
$response -> assertJsonPath ( 'data' , [
[ 'id' => 1 ],
[ 'id' => 2 ],
[ 'id' => 3 ],
]);
try {
$response -> assertJsonPath ( 'data' , [
[ 'id' => 1 ],
[ 'id' => 2 ],
]);
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( 'Failed asserting that two arrays are identical.' , $e -> getMessage ());
}
$response -> assertJsonCount ( 3 , 'data' );
try {
$response -> assertJsonCount ( 2 , 'data' );
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$this -> assertSame ( " Failed to assert that the response count matched the expected 2 \n Failed asserting that actual size 3 matches expected size 2. " , $e -> getMessage ());
}
}
2024-01-29 15:55:52 +01:00
public function yieldTestModels ()
{
yield new TestModel ([ 'id' => 1 ]);
yield new TestModel ([ 'id' => 2 ]);
yield new TestModel ([ 'id' => 3 ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSee () : void
2020-10-27 15:50:30 +01:00
{
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSee ( 'foo' );
$response -> assertSee ([ 'baz' , 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSee ( 'item' );
$response -> assertSee ([ 'not' , 'found' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeEscaped () : void
2020-10-27 15:50:30 +01:00
{
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertSee ( 'laravel & php' );
$response -> assertSee ([ 'php & friends' , 'laravel & php' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeEscapedCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertSee ( 'foo & bar' );
$response -> assertSee ([ 'bar & baz' , 'baz & qux' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeHtml () : void
2024-07-29 09:26:48 +02:00
{
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeHtml ( '<li>foo</li>' );
$response -> assertSeeHtml ([ '<li>baz</li>' , '<li>bar</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeHtmlCanFail () : void
2024-07-29 09:26:48 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeHtml ( '<li>item</li>' );
$response -> assertSeeHtml ([ '<li>not</li>' , '<li>found</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeInOrder () : void
2018-01-27 00:37:19 +11:00
{
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
2018-01-27 00:37:19 +11:00
$response -> assertSeeInOrder ([ 'foo' , 'bar' , 'baz' ]);
2018-08-17 15:43:03 +02:00
2018-02-05 23:48:24 +01:00
$response -> assertSeeInOrder ([ 'foo' , 'bar' , 'baz' , 'foo' ]);
2018-08-17 15:43:03 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeInOrderCanFail () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
2018-01-27 00:37:19 +11:00
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeInOrder ([ 'baz' , 'bar' , 'foo' ]);
2018-01-27 00:37:19 +11:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeInOrderCanFail2 () : void
2017-04-06 15:03:51 +02:00
{
2018-08-17 15:43:03 +02:00
$this -> expectException ( AssertionFailedError :: class );
2017-04-06 15:03:51 +02:00
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeInOrder ([ 'foo' , 'qux' , 'bar' , 'baz' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeHtmlInOrder () : void
2024-07-29 09:26:48 +02:00
{
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeHtmlInOrder ([ '<li>foo</li>' , '<li>bar</li>' , '<li>baz</li>' ]);
$response -> assertSeeHtmlInOrder ([ '<li>foo</li>' , '<li>bar</li>' , '<li>baz</li>' , '<li>foo</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeHtmlInOrderCanFail () : void
2024-07-29 09:26:48 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeHtmlInOrder ([ '<li>baz</li>' , '<li>bar</li>' , '<li>foo</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeHtmlInOrderCanFail2 () : void
2024-07-29 09:26:48 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertSeeHtmlInOrder ([ '<li>foo</li>' , '<li>qux</li>' , '<li>bar</li>' , '<li>baz</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeText () : void
2018-08-17 15:43:03 +02:00
{
$response = $this -> makeMockResponse ([
2020-10-27 15:50:30 +01:00
'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>' ,
2018-08-17 15:43:03 +02:00
]);
2017-04-06 15:03:51 +02:00
$response -> assertSeeText ( 'foobar' );
2020-10-27 15:50:30 +01:00
$response -> assertSeeText ([ 'bazqux' , 'foobar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
2026-03-17 16:23:57 -04:00
$this -> expectExceptionMessage ( 'Failed asserting that \'foo<strong>bar</strong>\' contains "bazfoo".' );
2020-10-27 15:50:30 +01:00
$response = $this -> makeMockResponse ([
'render' => 'foo<strong>bar</strong>' ,
]);
$response -> assertSeeText ( 'bazfoo' );
$response -> assertSeeText ([ 'bazfoo' , 'barqux' ]);
2017-03-01 18:28:20 -05:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextEscaped () : void
2020-01-22 10:46:35 -06:00
{
$response = $this -> makeMockResponse ([
2020-10-27 15:50:30 +01:00
'render' => 'laravel & php & friends' ,
2020-01-22 10:46:35 -06:00
]);
$response -> assertSeeText ( 'laravel & php' );
2020-10-27 15:50:30 +01:00
$response -> assertSeeText ([ 'php & friends' , 'laravel & php' ]);
}
2026-03-17 16:23:57 -04:00
public function testAssertSeeTextWhitespace () : void
{
$response = $this -> makeMockResponse ([
'render' => <<< ' EOT '
<p>
Hello,
laravel & php & friends
</p>,
EOT
]);
$response -> assertSeeText ( 'Hello, laravel & php & friends' );
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextEscapedCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertSeeText ( 'foo & bar' );
$response -> assertSeeText ([ 'foo & bar' , 'bar & baz' ]);
2020-01-22 10:46:35 -06:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextInOrder () : void
2018-01-27 00:37:19 +11:00
{
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>' ,
]);
2018-01-27 00:37:19 +11:00
$response -> assertSeeTextInOrder ([ 'foobar' , 'baz' ]);
2018-08-17 15:43:03 +02:00
2018-02-05 23:48:24 +01:00
$response -> assertSeeTextInOrder ([ 'foobar' , 'baz' , 'foo' ]);
2018-08-17 15:43:03 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextInOrderEscaped () : void
2020-02-03 09:33:21 -06:00
{
$response = $this -> makeMockResponse ([
'render' => '<strong>laravel & php</strong> <i>phpstorm > sublime</i>' ,
]);
$response -> assertSeeTextInOrder ([ 'laravel & php' , 'phpstorm > sublime' ]);
}
2026-03-17 16:23:57 -04:00
public function testAssertSeeTextInOrderWhitespace () : void
{
$response = $this -> makeMockResponse ([
'render' => <<< ' EOT '
<p>
Hello,
laravel & php & friends
</p>,
EOT
]);
$response -> assertSeeTextInOrder ([ 'Hello' , 'laravel & php' , 'friends' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextInOrderCanFail () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
2026-03-17 16:23:57 -04:00
$this -> expectExceptionMessage ( 'Failed asserting that \'foo<strong>bar</strong> baz <strong>foo</strong>\' contains "foobar" in specified order.' );
2018-08-17 15:43:03 +02:00
$response = $this -> makeMockResponse ([
'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>' ,
]);
$response -> assertSeeTextInOrder ([ 'baz' , 'foobar' ]);
}
2018-01-27 00:37:19 +11:00
2025-09-02 01:45:37 +03:00
public function testAssertSeeTextInOrderCanFail2 () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>' ,
]);
$response -> assertSeeTextInOrder ([ 'foobar' , 'qux' , 'baz' ]);
2018-01-27 00:37:19 +11:00
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSee () : void
2020-10-27 15:50:30 +01:00
{
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertDontSee ( 'laravel' );
$response -> assertDontSee ([ 'php' , 'friends' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertDontSee ( 'foo' );
$response -> assertDontSee ([ 'baz' , 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeEscaped () : void
2020-10-27 15:50:30 +01:00
{
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertDontSee ( 'foo & bar' );
$response -> assertDontSee ([ 'bar & baz' , 'foo & bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeEscapedCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertDontSee ( 'laravel & php' );
$response -> assertDontSee ([ 'php & friends' , 'laravel & php' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeHtml () : void
2024-07-29 09:26:48 +02:00
{
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertDontSeeHtml ( '<li>laravel</li>' );
$response -> assertDontSeeHtml ([ '<li>php</li>' , '<li>friends</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeHtmlCanFail () : void
2024-07-29 09:26:48 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>' ,
]);
$response -> assertDontSeeHtml ( '<li>foo</li>' );
$response -> assertDontSeeHtml ([ '<li>baz</li>' , '<li>bar</li>' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeText () : void
2020-10-27 15:50:30 +01:00
{
$response = $this -> makeMockResponse ([
'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>' ,
]);
$response -> assertDontSeeText ( 'laravelphp' );
$response -> assertDontSeeText ([ 'phpfriends' , 'laravelphp' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeTextCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
2026-03-17 16:23:57 -04:00
$this -> expectExceptionMessage ( 'Failed asserting that \'foo<strong>bar</strong>baz<strong>qux</strong>\' does not contain "foobar".' );
2020-10-27 15:50:30 +01:00
$response = $this -> makeMockResponse ([
'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>' ,
]);
$response -> assertDontSeeText ( 'foobar' );
$response -> assertDontSeeText ([ 'bazqux' , 'foobar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeTextEscaped () : void
2020-10-27 15:50:30 +01:00
{
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertDontSeeText ( 'foo & bar' );
$response -> assertDontSeeText ([ 'bar & baz' , 'foo & bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertDontSeeTextEscapedCanFail () : void
2020-10-27 15:50:30 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = $this -> makeMockResponse ([
'render' => 'laravel & php & friends' ,
]);
$response -> assertDontSeeText ( 'laravel & php' );
$response -> assertDontSeeText ([ 'php & friends' , 'laravel & php' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertOk () : void
2019-06-24 23:04:52 -03:00
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-06-24 23:04:52 -03:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertOk ();
}
2025-09-02 01:45:37 +03:00
public function testAssertCreated () : void
2019-10-21 21:43:50 +03:30
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-10-21 21:43:50 +03:30
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertCreated ();
}
2025-09-02 01:45:37 +03:00
public function testAssertNotFound () : void
2019-06-18 00:01:59 -03:00
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-06-18 00:01:59 -03:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertNotFound ();
}
2025-09-02 01:45:37 +03:00
public function testAssertMethodNotAllowed () : void
2023-05-23 21:08:21 +03:30
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_METHOD_NOT_ALLOWED )
);
$response -> assertMethodNotAllowed ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [405] but received 200. \n Failed asserting that 200 is identical to 405. " );
2023-05-23 21:08:21 +03:30
$response -> assertMethodNotAllowed ();
}
2025-09-02 01:45:37 +03:00
public function testAssertNotAcceptable () : void
2023-05-30 18:19:07 +04:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_NOT_ACCEPTABLE )
);
$response -> assertNotAcceptable ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [406] but received 200. \n Failed asserting that 200 is identical to 406. " );
2023-05-30 18:19:07 +04:00
$response -> assertNotAcceptable ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertForbidden () : void
2019-06-24 23:04:52 -03:00
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-06-24 23:04:52 -03:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertForbidden ();
}
2025-09-02 01:45:37 +03:00
public function testAssertUnauthorized () : void
2019-06-24 23:04:52 -03:00
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-06-24 23:04:52 -03:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertUnauthorized ();
}
2025-09-02 01:45:37 +03:00
public function testAssertBadRequest () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_BAD_REQUEST )
);
$response -> assertBadRequest ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [400] but received 200. \n Failed asserting that 200 is identical to 400. " );
2023-01-26 02:37:14 +11:00
$response -> assertBadRequest ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertRequestTimeout () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_REQUEST_TIMEOUT )
);
$response -> assertRequestTimeout ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [408] but received 200. \n Failed asserting that 200 is identical to 408. " );
2023-01-26 02:37:14 +11:00
$response -> assertRequestTimeout ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertPaymentRequired () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_PAYMENT_REQUIRED )
);
$response -> assertPaymentRequired ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [402] but received 200. \n Failed asserting that 200 is identical to 402. " );
2023-01-26 02:37:14 +11:00
$response -> assertPaymentRequired ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertMovedPermanently () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_MOVED_PERMANENTLY )
);
$response -> assertMovedPermanently ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [301] but received 200. \n Failed asserting that 200 is identical to 301. " );
2023-01-26 02:37:14 +11:00
$response -> assertMovedPermanently ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertFound () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_FOUND )
);
$response -> assertFound ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [302] but received 200. \n Failed asserting that 200 is identical to 302. " );
2023-01-26 02:37:14 +11:00
$response -> assertFound ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertNotModified () : void
2023-05-30 18:19:07 +04:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_NOT_MODIFIED )
);
$response -> assertNotModified ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [304] but received 200. \n Failed asserting that 200 is identical to 304. " );
2023-05-30 18:19:07 +04:00
$response -> assertNotModified ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertTemporaryRedirect () : void
2023-05-30 18:19:07 +04:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_TEMPORARY_REDIRECT )
);
$response -> assertTemporaryRedirect ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [307] but received 200. \n Failed asserting that 200 is identical to 307. " );
2023-05-30 18:19:07 +04:00
$response -> assertTemporaryRedirect ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertPermanentRedirect () : void
2023-05-30 18:19:07 +04:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_PERMANENTLY_REDIRECT )
);
$response -> assertPermanentRedirect ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [308] but received 200. \n Failed asserting that 200 is identical to 308. " );
2023-05-30 18:19:07 +04:00
$response -> assertPermanentRedirect ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertConflict () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_CONFLICT )
);
$response -> assertConflict ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [409] but received 200. \n Failed asserting that 200 is identical to 409. " );
2023-01-26 02:37:14 +11:00
$response -> assertConflict ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertGone () : void
2023-04-20 22:29:37 +06:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_GONE )
);
$response -> assertGone ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [410] but received 200. \n Failed asserting that 200 is identical to 410. " );
2023-04-20 22:29:37 +06:00
$response -> assertGone ();
}
2025-09-02 01:45:37 +03:00
public function testAssertTooManyRequests () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_TOO_MANY_REQUESTS )
);
$response -> assertTooManyRequests ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [429] but received 200. \n Failed asserting that 200 is identical to 429. " );
2023-01-26 02:37:14 +11:00
$response -> assertTooManyRequests ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertAccepted () : void
2023-01-26 02:37:14 +11:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_ACCEPTED )
);
$response -> assertAccepted ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [202] but received 200. \n Failed asserting that 200 is identical to 202. " );
2023-01-26 02:37:14 +11:00
$response -> assertAccepted ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertUnprocessable () : void
2021-08-26 14:33:20 +01:00
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Expected response status code' );
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertUnprocessable ();
}
2025-12-10 18:31:07 +03:00
public function testAssertFailedDependency () : void
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_FAILED_DEPENDENCY )
);
$response -> assertFailedDependency ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( " Expected response status code [424] but received 200. \n Failed asserting that 200 is identical to 424. " );
$response -> assertFailedDependency ();
$this -> fail ();
}
2025-09-02 01:45:37 +03:00
public function testAssertClientError () : void
2025-05-16 22:23:30 +08:00
{
$statusCode = 400 ;
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertClientError ();
}
2025-09-02 01:45:37 +03:00
public function testAssertServerError () : void
2022-10-17 14:59:30 +01:00
{
$statusCode = 500 ;
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertServerError ();
}
2025-09-02 01:45:37 +03:00
public function testAssertInternalServerError () : void
2023-04-20 22:29:37 +06:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_INTERNAL_SERVER_ERROR )
);
$response -> assertInternalServerError ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [500] but received 200. \n Failed asserting that 200 is identical to 500. " );
2023-04-20 22:29:37 +06:00
$response -> assertInternalServerError ();
}
2025-09-02 01:45:37 +03:00
public function testAssertServiceUnavailable () : void
2023-04-20 22:29:37 +06:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_SERVICE_UNAVAILABLE )
);
$response -> assertServiceUnavailable ();
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setStatusCode ( Response :: HTTP_OK )
);
$this -> expectException ( AssertionFailedError :: class );
2023-12-16 16:42:44 +01:00
$this -> expectExceptionMessage ( " Expected response status code [503] but received 200. \n Failed asserting that 200 is identical to 503. " );
2023-04-20 22:29:37 +06:00
$response -> assertServiceUnavailable ();
}
2025-09-02 01:45:37 +03:00
public function testAssertNoContentAsserts204StatusCodeByDefault () : void
2019-09-30 06:57:48 -06:00
{
$statusCode = 500 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-09-30 06:57:48 -06:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertNoContent ();
}
2025-09-02 01:45:37 +03:00
public function testAssertNoContentAssertsExpectedStatusCode () : void
2019-09-30 06:57:48 -06:00
{
$statusCode = 500 ;
$expectedStatusCode = 418 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-09-30 06:57:48 -06:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertNoContent ( $expectedStatusCode );
}
2025-09-02 01:45:37 +03:00
public function testAssertNoContentAssertsEmptyContent () : void
2019-09-30 06:57:48 -06:00
{
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Response content is not empty' );
$baseResponse = tap ( new Response , function ( $response ) {
$response -> setStatusCode ( 204 );
$response -> setContent ( 'non-empty-response-content' );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertNoContent ();
}
2025-09-02 01:45:37 +03:00
public function testAssertStatus () : void
2019-06-24 23:04:52 -03:00
{
$statusCode = 500 ;
$expectedStatusCode = 401 ;
$this -> expectException ( AssertionFailedError :: class );
2021-07-21 22:24:16 +10:00
$this -> expectExceptionMessage ( 'Expected response status code' );
2019-06-24 23:04:52 -03:00
$baseResponse = tap ( new Response , function ( $response ) use ( $statusCode ) {
$response -> setStatusCode ( $statusCode );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertStatus ( $expectedStatusCode );
}
2025-09-02 01:45:37 +03:00
public function testAssertHeader () : void
2017-05-09 05:38:52 +02:00
{
2018-08-17 15:43:03 +02:00
$this -> expectException ( AssertionFailedError :: class );
2017-05-09 05:38:52 +02:00
$baseResponse = tap ( new Response , function ( $response ) {
$response -> header ( 'Location' , '/foo' );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
2018-08-17 15:43:03 +02:00
$response -> assertHeader ( 'Location' , '/bar' );
2017-05-09 05:38:52 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertHeaderMissing () : void
2018-01-20 08:56:03 -06:00
{
2019-02-07 17:04:40 +01:00
$this -> expectException ( ExpectationFailedException :: class );
$this -> expectExceptionMessage ( 'Unexpected header [Location] is present on response.' );
2018-01-20 08:56:03 -06:00
$baseResponse = tap ( new Response , function ( $response ) {
$response -> header ( 'Location' , '/foo' );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
2018-01-21 03:10:22 +01:00
$response -> assertHeaderMissing ( 'Location' );
2018-01-20 08:56:03 -06:00
}
2025-09-02 01:45:37 +03:00
public function testAssertPrecognitionSuccessfulWithMissingHeader () : void
2023-08-30 17:14:34 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Header [Precognition-Success] not present on response.' );
$baseResponse = new Response ( '' , 204 );
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertSuccessfulPrecognition ();
}
2025-09-02 01:45:37 +03:00
public function testAssertPrecognitionSuccessfulWithIncorrectValue () : void
2023-08-30 17:14:34 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'The Precognition-Success header was found, but the value is not `true`.' );
$baseResponse = tap ( new Response ( '' , 204 ), function ( $response ) {
$response -> header ( 'Precognition-Success' , '' );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertSuccessfulPrecognition ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithArray () : void
2017-01-31 23:09:17 +02:00
{
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
2017-01-31 23:09:17 +02:00
$resource = new JsonSerializableSingleResourceStub ;
$response -> assertJson ( $resource -> jsonSerialize ());
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithNull () : void
2018-12-04 20:39:55 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( null ));
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Invalid JSON was returned from the route.' );
$resource = new JsonSerializableSingleResourceStub ;
$response -> assertJson ( $resource -> jsonSerialize ());
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithFluent () : void
2021-03-03 20:07:50 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
2021-03-08 11:49:04 -06:00
$response -> assertJson ( function ( AssertableJson $json ) {
2021-03-03 20:07:50 +01:00
$json -> where ( '0.foo' , 'foo 0' );
2024-10-17 00:25:45 +08:00
$json -> where ( '0.meta' , []);
2021-03-03 20:07:50 +01:00
});
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithFluentFailsWhenNotInteractingWithAllProps () : void
2021-03-03 20:07:50 +01:00
{
2021-03-16 22:41:43 +01:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
2021-03-03 20:07:50 +01:00
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Unexpected properties were found on the root level.' );
2021-03-08 11:49:04 -06:00
$response -> assertJson ( function ( AssertableJson $json ) {
2021-03-16 22:41:43 +01:00
$json -> where ( 'foo' , 'bar' );
});
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithFluentSkipsInteractionWhenTopLevelKeysNonAssociative () : void
2021-03-16 22:41:43 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([
[ 'foo' => 'bar' ],
[ 'foo' => 'baz' ],
]));
$response -> assertJson ( function ( AssertableJson $json ) {
//
});
2021-03-03 20:07:50 +01:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithFluentHasAnyThrows () : void
2021-10-20 16:28:43 +03:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([]));
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'None of properties [data, errors, meta] exist.' );
$response -> assertJson ( function ( AssertableJson $json ) {
$json -> hasAny ( 'data' , 'errors' , 'meta' );
});
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonWithFluentHasAnyPasses () : void
2021-10-20 16:28:43 +03:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([
'data' => [],
]));
$response -> assertJson ( function ( AssertableJson $json ) {
$json -> hasAny ( 'data' , 'errors' , 'meta' );
});
}
2025-09-02 01:45:37 +03:00
public function testAssertSimilarJsonWithMixed () : void
2017-01-31 23:09:17 +02:00
{
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
2017-01-31 23:09:17 +02:00
$resource = new JsonSerializableMixedResourcesStub ;
2020-07-27 18:15:53 +02:00
$expected = $resource -> jsonSerialize ();
2020-07-27 18:19:31 +02:00
$response -> assertSimilarJson ( $expected );
2020-07-27 18:15:53 +02:00
$expected [ 'bars' ][ 0 ] = [ 'bar' => 'foo 2' , 'foo' => 'bar 2' ];
$expected [ 'bars' ][ 2 ] = [ 'bar' => 'foo 0' , 'foo' => 'bar 0' ];
2020-07-27 18:19:31 +02:00
$response -> assertSimilarJson ( $expected );
2017-01-31 23:09:17 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertExactJsonWithMixedWhenDataIsExactlySame () : void
2020-07-27 18:38:04 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$resource = new JsonSerializableMixedResourcesStub ;
$expected = $resource -> jsonSerialize ();
$response -> assertExactJson ( $expected );
}
2025-09-02 01:45:37 +03:00
public function testAssertExactJsonWithMixedWhenDataIsSimilar () : void
2020-07-27 18:38:04 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Failed asserting that two strings are equal.' );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$resource = new JsonSerializableMixedResourcesStub ;
$expected = $resource -> jsonSerialize ();
$expected [ 'bars' ][ 0 ] = [ 'bar' => 'foo 2' , 'foo' => 'bar 2' ];
$expected [ 'bars' ][ 2 ] = [ 'bar' => 'foo 0' , 'foo' => 'bar 0' ];
$response -> assertExactJson ( $expected );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPath () : void
2019-09-13 22:47:08 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$response -> assertJsonPath ( '0.foo' , 'foo 0' );
$response -> assertJsonPath ( '0.foo' , 'foo 0' );
$response -> assertJsonPath ( '0.bar' , 'bar 0' );
$response -> assertJsonPath ( '0.foobar' , 'foobar 0' );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$response -> assertJsonPath ( 'foo' , 'bar' );
$response -> assertJsonPath ( 'foobar.foobar_foo' , 'foo' );
$response -> assertJsonPath ( 'foobar.foobar_bar' , 'bar' );
$response -> assertJsonPath ( 'foobar.foobar_foo' , 'foo' ) -> assertJsonPath ( 'foobar.foobar_bar' , 'bar' );
$response -> assertJsonPath ( 'bars' , [
2019-10-10 15:24:57 +02:00
[ 'bar' => 'foo 0' , 'foo' => 'bar 0' ],
[ 'bar' => 'foo 1' , 'foo' => 'bar 1' ],
[ 'bar' => 'foo 2' , 'foo' => 'bar 2' ],
2019-09-13 22:47:08 +02:00
]);
2019-10-10 15:24:57 +02:00
$response -> assertJsonPath ( 'bars.0' , [ 'bar' => 'foo 0' , 'foo' => 'bar 0' ]);
2019-09-13 22:47:08 +02:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
$response -> assertJsonPath ( '0.id' , 10 );
$response -> assertJsonPath ( '1.id' , 20 );
$response -> assertJsonPath ( '2.id' , 30 );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathCanFail () : void
2019-10-01 14:50:46 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Failed asserting that 10 is identical to \'10\'.' );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
2019-10-10 15:24:57 +02:00
$response -> assertJsonPath ( '0.id' , '10' );
2019-10-01 14:50:46 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathWithClosure () : void
2022-03-10 16:41:31 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([
'data' => [ 'foo' => 'bar' ],
]));
$response -> assertJsonPath ( 'data.foo' , fn ( $value ) => $value === 'bar' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathWithClosureCanFail () : void
2022-03-10 16:41:31 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([
'data' => [ 'foo' => 'bar' ],
]));
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Failed asserting that false is true.' );
$response -> assertJsonPath ( 'data.foo' , fn ( $value ) => $value === null );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathWithEnum () : void
2025-04-23 15:17:11 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([
'data' => [ 'status' => 'booked' ],
]));
$response -> assertJsonPath ( 'data.status' , TestStatus :: Booked );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathWithEnumCanFail () : void
2025-04-23 15:17:11 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ([
'data' => [ 'status' => 'failed' ],
]));
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Failed asserting that two strings are identical.' );
$response -> assertJsonPath ( 'data.status' , TestStatus :: Booked );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathCanonicalizing () : void
2023-08-21 16:06:46 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$response -> assertJsonPathCanonicalizing ( '*.foo' , [ 'foo 0' , 'foo 1' , 'foo 2' , 'foo 3' ]);
$response -> assertJsonPathCanonicalizing ( '*.foo' , [ 'foo 1' , 'foo 0' , 'foo 3' , 'foo 2' ]);
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
$response -> assertJsonPathCanonicalizing ( '*.id' , [ 10 , 20 , 30 ]);
$response -> assertJsonPathCanonicalizing ( '*.id' , [ 30 , 10 , 20 ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonPathCanonicalizingCanFail () : void
2023-08-21 16:06:46 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Failed asserting that two arrays are equal.' );
$response -> assertJsonPathCanonicalizing ( '*.foo' , [ 'foo 0' , 'foo 2' , 'foo 3' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonFragment () : void
2017-02-10 11:15:40 -06:00
{
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
2017-02-10 11:15:40 -06:00
$response -> assertJsonFragment ([ 'foo' => 'foo 0' ]);
$response -> assertJsonFragment ([ 'foo' => 'foo 0' , 'bar' => 'bar 0' , 'foobar' => 'foobar 0' ]);
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
2017-02-10 11:15:40 -06:00
$response -> assertJsonFragment ([ 'foo' => 'bar' ]);
$response -> assertJsonFragment ([ 'foobar_foo' => 'foo' ]);
$response -> assertJsonFragment ([ 'foobar' => [ 'foobar_foo' => 'foo' , 'foobar_bar' => 'bar' ]]);
$response -> assertJsonFragment ([ 'foo' => 'bar 0' , 'bar' => [ 'foo' => 'bar 0' , 'bar' => 'foo 0' ]]);
2018-07-15 19:32:16 +01:00
2018-10-10 22:24:53 +02:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
2018-07-15 19:32:16 +01:00
$response -> assertJsonFragment ([ 'id' => 10 ]);
2018-08-17 15:43:03 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonFragments () : void
2025-02-13 02:21:24 +10:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$response -> assertJsonFragments ([
[ 'foo' => 'foo 0' ],
]);
$response -> assertJsonFragments ([
[ 'foo' => 'foo 0' ],
[ 'foo' => 'foo 1' ],
]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonFragmentCanFail () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
2018-10-10 22:24:53 +02:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
2018-07-15 19:32:16 +01:00
2018-08-17 15:43:03 +02:00
$response -> assertJsonFragment ([ 'id' => 1 ]);
2017-02-10 11:15:40 -06:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonFragmentUnicodeCanFail () : void
2022-10-04 19:43:55 +03:00
{
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessageMatches ( '/Привет|Мир/' );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithUnicodeStub ));
$response -> assertJsonFragment ([ 'id' => 1 ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonStructure () : void
2017-01-31 23:09:17 +02:00
{
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
2017-01-31 23:09:17 +02:00
2017-05-06 21:22:15 +02:00
// Without structure
$response -> assertJsonStructure ();
2017-01-31 23:09:17 +02:00
// At root
$response -> assertJsonStructure ([ 'foo' ]);
// Nested
$response -> assertJsonStructure ([ 'foobar' => [ 'foobar_foo' , 'foobar_bar' ]]);
// Wildcard (repeating structure)
$response -> assertJsonStructure ([ 'bars' => [ '*' => [ 'bar' , 'foo' ]]]);
2018-12-04 20:39:55 +01:00
// Wildcard (numeric keys)
$response -> assertJsonStructure ([ 'numeric_keys' => [ '*' => [ 'bar' , 'foo' ]]]);
2017-01-31 23:09:17 +02:00
// Nested after wildcard
$response -> assertJsonStructure ([ 'baz' => [ '*' => [ 'foo' , 'bar' => [ 'foo' , 'bar' ]]]]);
// Wildcard (repeating structure) at root
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
2017-02-10 11:15:40 -06:00
2017-01-31 23:09:17 +02:00
$response -> assertJsonStructure ([ '*' => [ 'foo' , 'bar' , 'foobar' ]]);
}
2017-02-02 14:33:10 +01:00
2025-09-02 01:45:37 +03:00
public function testAssertExactJsonStructure () : void
2024-07-30 08:56:26 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
// Without structure
$response -> assertExactJsonStructure ();
// At root
try {
$response -> assertExactJsonStructure ([ 'foo' ]);
$failed = false ;
} catch ( AssertionFailedError $e ) {
$failed = true ;
}
$this -> assertTrue ( $failed );
$response -> assertExactJsonStructure ([ 'foo' , 'foobar' , 0 , 'bars' , 'baz' , 'barfoo' , 'numeric_keys' ]);
// Nested
try {
$response -> assertExactJsonStructure ([ 'foobar' => [ 'foobar_foo' ], 'foo' , 0 , 'bars' , 'baz' , 'barfoo' , 'numeric_keys' ]);
$failed = false ;
} catch ( AssertionFailedError $e ) {
$failed = true ;
}
$this -> assertTrue ( $failed );
$response -> assertExactJsonStructure ([ 'foobar' => [ 'foobar_foo' , 'foobar_bar' ], 'foo' , 0 , 'bars' , 'baz' , 'barfoo' , 'numeric_keys' ]);
// Wildcard (repeating structure)
try {
$response -> assertExactJsonStructure ([ 'bars' => [ '*' => [ 'bar' ]], 'foo' , 'foobar' , 0 , 'baz' , 'barfoo' , 'numeric_keys' ]);
$failed = false ;
} catch ( AssertionFailedError $e ) {
$failed = true ;
}
$this -> assertTrue ( $failed );
$response -> assertExactJsonStructure ([ 'bars' => [ '*' => [ 'bar' , 'foo' ]], 'foo' , 'foobar' , 0 , 'baz' , 'barfoo' , 'numeric_keys' ]);
// Wildcard (numeric keys)
try {
$response -> assertExactJsonStructure ([ 'numeric_keys' => [ '*' => [ 'bar' ]], 'foo' , 'foobar' , 0 , 'bars' , 'baz' , 'barfoo' ]);
$failed = false ;
} catch ( AssertionFailedError $e ) {
$failed = true ;
}
$this -> assertTrue ( $failed );
$response -> assertExactJsonStructure ([ 'numeric_keys' => [ '*' => [ 'bar' , 'foo' ]], 'foo' , 'foobar' , 0 , 'bars' , 'baz' , 'barfoo' ]);
// Nested after wildcard
try {
$response -> assertExactJsonStructure ([ 'baz' => [ '*' => [ 'foo' , 'bar' => [ 'foo' ]]], 'foo' , 'foobar' , 0 , 'bars' , 'barfoo' , 'numeric_keys' ]);
$failed = false ;
} catch ( AssertionFailedError $e ) {
$failed = true ;
}
$this -> assertTrue ( $failed );
$response -> assertExactJsonStructure ([ 'baz' => [ '*' => [ 'foo' , 'bar' => [ 'foo' , 'bar' ]]], 'foo' , 'foobar' , 0 , 'bars' , 'barfoo' , 'numeric_keys' ]);
// Wildcard (repeating structure) at root
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
try {
$response -> assertExactJsonStructure ([ '*' => [ 'foo' , 'bar' ]]);
$failed = false ;
} catch ( AssertionFailedError $e ) {
$failed = true ;
}
$this -> assertTrue ( $failed );
2024-10-17 00:25:45 +08:00
$response -> assertExactJsonStructure ([ '*' => [ 'foo' , 'bar' , 'foobar' , 'meta' ]]);
2024-07-30 08:56:26 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonCount () : void
2018-01-11 17:13:54 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
2020-05-04 01:36:25 +10:00
// With falsey key
$response -> assertJsonCount ( 1 , '0' );
2018-01-11 17:13:54 +01:00
// With simple key
$response -> assertJsonCount ( 3 , 'bars' );
// With nested key
2018-01-28 03:26:49 +01:00
$response -> assertJsonCount ( 1 , 'barfoo.0.bar' );
$response -> assertJsonCount ( 3 , 'barfoo.2.bar' );
2018-01-11 17:13:54 +01:00
// Without structure
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$response -> assertJsonCount ( 4 );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissing () : void
2018-07-15 19:32:16 +01:00
{
2018-08-17 15:43:03 +02:00
$this -> expectException ( AssertionFailedError :: class );
2018-07-15 19:32:16 +01:00
2018-08-17 15:43:03 +02:00
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
2018-08-16 16:22:52 +01:00
2018-08-17 15:43:03 +02:00
$response -> assertJsonMissing ([ 'id' => 20 ]);
2018-08-16 16:22:52 +01:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingExact () : void
2018-08-16 16:22:52 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
$response -> assertJsonMissingExact ([ 'id' => 2 ]);
// This is missing because bar has changed to baz
$response -> assertJsonMissingExact ([ 'id' => 20 , 'foo' => 'baz' ]);
2018-07-15 19:32:16 +01:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingExactCanFail () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
$response -> assertJsonMissingExact ([ 'id' => 20 ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingExactCanFail2 () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceWithIntegersStub ));
$response -> assertJsonMissingExact ([ 'id' => 20 , 'foo' => 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingPath () : void
2022-05-12 17:13:43 +02:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
// With simple key
$response -> assertJsonMissingPath ( 'missing' );
// With nested key
$response -> assertJsonMissingPath ( 'foobar.missing' );
$response -> assertJsonMissingPath ( 'numeric_keys.0' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingPathCanFail () : void
2022-05-12 17:13:43 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$response -> assertJsonMissingPath ( 'foo' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingPathCanFail2 () : void
2022-05-12 17:13:43 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$response -> assertJsonMissingPath ( 'foobar.foobar_foo' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingPathCanFail3 () : void
2022-05-12 17:13:43 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$response -> assertJsonMissingPath ( 'numeric_keys.3' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrors () : void
2019-01-17 09:21:58 +01:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ( 'foo' );
}
2025-09-02 01:45:37 +03:00
public function testAssertOnlyJsonValidationErrors () : void
2025-02-19 21:44:24 +01:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'oops' , 'bar' => 'another oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
try {
$testResponse -> assertOnlyJsonValidationErrors ( 'foo' );
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( 'Response has unexpected validation errors: \'bar\'' , $e -> getMessage ());
}
$testResponse -> assertOnlyJsonValidationErrors ([ 'foo' , 'bar' ]);
try {
$testResponse -> assertOnlyJsonValidationErrors ([ 'foo' => 'oops' ]);
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( 'Response has unexpected validation errors: \'bar\'' , $e -> getMessage ());
}
$testResponse -> assertOnlyJsonValidationErrors ([ 'foo' => 'oops' , 'bar' => 'another oops' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsUsingAssertOnlyInvalid () : void
2025-02-19 21:44:24 +01:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'oops' , 'bar' => 'another oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ( '' , 200 , [ 'Content-Type' => 'application/json' ])) -> setContent ( json_encode ( $data ))
);
try {
$testResponse -> assertOnlyInvalid ( 'foo' );
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( 'Response has unexpected validation errors: \'bar\'' , $e -> getMessage ());
}
$testResponse -> assertOnlyInvalid ([ 'foo' , 'bar' ]);
try {
$testResponse -> assertOnlyInvalid ([ 'foo' => 'oops' ]);
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( 'Response has unexpected validation errors: \'bar\'' , $e -> getMessage ());
}
$testResponse -> assertOnlyInvalid ([ 'foo' => 'oops' , 'bar' => 'another oops' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionOnlyValidationErrorsUsingAssertOnlyInvalid () : void
2025-02-19 21:44:24 +01:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'first_name' => [
'Your first name is required' ,
'Your first name must be at least 1 character' ,
],
'last_name' => [
'Your last name is required' ,
],
]));
$testResponse = TestResponse :: fromBaseResponse ( new Response );
try {
$testResponse -> assertOnlyInvalid ( 'first_name' );
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( 'Response has unexpected validation errors: \'last_name\'' , $e -> getMessage ());
}
$testResponse -> assertOnlyInvalid ([ 'first_name' , 'last_name' ]);
try {
$testResponse -> assertOnlyInvalid ([ 'first_name' => 'required' ]);
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( 'Response has unexpected validation errors: \'last_name\'' , $e -> getMessage ());
}
$testResponse -> assertOnlyInvalid ([ 'first_name' => 'required' , 'last_name' => 'required' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsUsingAssertInvalid () : void
2021-08-16 08:32:35 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ( '' , 200 , [ 'Content-Type' => 'application/json' ])) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertInvalid ( 'foo' );
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionValidationErrorsUsingAssertInvalid () : void
2021-08-16 08:32:35 -05:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'first_name' => [
'Your first name is required' ,
'Your first name must be at least 1 character' ,
],
]));
$testResponse = TestResponse :: fromBaseResponse ( new Response );
2021-08-18 23:17:39 +10:00
$testResponse -> assertValid ( 'last_name' );
2021-08-16 08:32:35 -05:00
$testResponse -> assertValid ([ 'last_name' ]);
2021-08-18 23:17:39 +10:00
$testResponse -> assertInvalid ();
$testResponse -> assertInvalid ( 'first_name' );
2021-08-16 08:32:35 -05:00
$testResponse -> assertInvalid ([ 'first_name' ]);
$testResponse -> assertInvalid ([ 'first_name' => 'required' ]);
$testResponse -> assertInvalid ([ 'first_name' => 'character' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionValidationErrorsUsingAssertValid () : void
2021-08-16 08:32:35 -05:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
]));
$testResponse = TestResponse :: fromBaseResponse ( new Response );
$testResponse -> assertValid ();
}
2025-09-02 01:45:37 +03:00
public function testAssertingKeyIsInvalidErrorMessage () : void
2023-12-12 06:22:43 +11:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$testResponse = TestResponse :: fromBaseResponse ( new Response );
try {
$testResponse -> assertInvalid ([ 'input_name' ]);
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( " Failed to find a validation error in session for key: 'input_name' " , $e -> getMessage ());
}
try {
$testResponse -> assertInvalid ([ 'input_name' => 'Expected error message.' ]);
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( " Failed to find a validation error in session for key: 'input_name' " , $e -> getMessage ());
}
}
2025-09-02 01:45:37 +03:00
public function testInvalidWithListOfErrors () : void
2023-12-12 06:22:43 +11:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'first_name' => [
'Your first name is required' ,
'Your first name must be at least 1 character' ,
],
]));
$testResponse = TestResponse :: fromBaseResponse ( new Response );
$testResponse -> assertInvalid ([ 'first_name' => 'Your first name is required' ]);
$testResponse -> assertInvalid ([ 'first_name' => 'Your first name must be at least 1 character' ]);
$testResponse -> assertInvalid ([ 'first_name' => [ 'Your first name is required' , 'Your first name must be at least 1 character' ]]);
try {
$testResponse -> assertInvalid ([ 'first_name' => [ 'Your first name is required' , 'FOO' ]]);
$this -> fail ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringStartsWith ( " Failed to find a validation error for key and message: 'first_name' => 'FOO' " , $e -> getMessage ());
}
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsCustomErrorsName () : void
2019-03-16 15:49:43 +02:00
{
$data = [
'status' => 'ok' ,
'data' => [ 'foo' => 'oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
2019-03-16 16:21:12 +02:00
$testResponse -> assertJsonValidationErrors ( 'foo' , 'data' );
2019-03-16 15:49:43 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsCustomNestedErrorsName () : void
2020-08-24 16:38:17 +01:00
{
$data = [
'status' => 'ok' ,
'data' => [ 'errors' => [ 'foo' => 'oops' ]],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ( 'foo' , 'data.errors' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsCanFail () : void
2019-01-17 09:21:58 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ( 'bar' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsCanFailWhenThereAreNoErrors () : void
2019-01-17 09:21:58 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [ 'status' => 'ok' ];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ( 'bar' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsFailsWhenGivenAnEmptyArray () : void
2019-02-12 10:43:40 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ([ 'errors' => [ 'foo' => 'oops' ]]))
);
$testResponse -> assertJsonValidationErrors ([]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorsWithArray () : void
2019-06-10 13:51:40 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'one' , 'bar' => 'two' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'foo' , 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessages () : void
2019-06-10 13:51:40 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'key' => 'foo' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'key' => 'foo' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorContainsMessages () : void
2019-06-10 13:51:40 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'key' => 'foo bar' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'key' => 'foo' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesCanFail () : void
2019-06-10 13:51:40 -05:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [
'status' => 'ok' ,
'errors' => [ 'key' => 'foo' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'key' => 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessageKeyCanFail () : void
2019-06-10 13:51:40 -05:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [
'status' => 'ok' ,
'errors' => [ 'foo' => 'value' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'bar' => 'value' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesMultipleMessages () : void
2019-06-10 13:51:40 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'one' => 'foo' , 'two' => 'bar' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'one' => 'foo' , 'two' => 'bar' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesMultipleMessagesCanFail () : void
2019-08-02 11:58:55 +09:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [
'status' => 'ok' ,
'errors' => [ 'one' => 'foo' , 'two' => 'bar' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'one' => 'foo' , 'three' => 'baz' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesMixed () : void
2019-06-10 13:51:40 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [ 'one' => 'foo' , 'two' => 'bar' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'one' => 'foo' , 'two' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesMixedCanFail () : void
2019-06-10 13:51:40 -05:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [
'status' => 'ok' ,
'errors' => [ 'one' => 'foo' , 'two' => 'bar' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'one' => 'taylor' , 'otwell' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesMultipleErrors () : void
2021-11-11 09:17:45 -05:00
{
$data = [
'status' => 'ok' ,
'errors' => [
'one' => [
'First error message.' ,
'Second error message.' ,
],
],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'one' => [ 'First error message.' , 'Second error message.' ]]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonValidationErrorMessagesMultipleErrorsCanFail () : void
2021-11-11 09:17:45 -05:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [
'status' => 'ok' ,
'errors' => [
'one' => [
'First error message.' ,
],
],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonValidationErrors ([ 'one' => [ 'First error message.' , 'Second error message.' ]]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrors () : void
2018-06-13 17:51:45 +02:00
{
$baseResponse = tap ( new Response , function ( $response ) {
$response -> setContent ( json_encode ([ 'errors' => [
2019-12-31 00:06:15 +00:00
'foo' => [],
'bar' => [ 'one' , 'two' ],
]]));
2018-06-13 17:51:45 +02:00
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertJsonMissingValidationErrors ( 'baz' );
$baseResponse = tap ( new Response , function ( $response ) {
$response -> setContent ( json_encode ([ 'foo' => 'bar' ]));
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertJsonMissingValidationErrors ( 'foo' );
2018-06-13 17:57:34 +02:00
}
2018-06-13 17:51:45 +02:00
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsCanFail () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$baseResponse = tap ( new Response , function ( $response ) {
$response -> setContent ( json_encode ([ 'errors' => [
2019-12-31 00:06:15 +00:00
'foo' => [],
'bar' => [ 'one' , 'two' ],
]]));
2018-08-17 15:43:03 +02:00
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertJsonMissingValidationErrors ( 'foo' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsCanFail2 () : void
2018-08-17 15:43:03 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$baseResponse = tap ( new Response , function ( $response ) {
$response -> setContent ( json_encode ([ 'errors' => [
2019-12-31 00:06:15 +00:00
'foo' => [],
'bar' => [ 'one' , 'two' ],
]]));
2018-08-17 15:43:03 +02:00
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertJsonMissingValidationErrors ( 'bar' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsCanFail3 () : void
2021-02-10 16:32:46 +02:00
{
$this -> expectException ( AssertionFailedError :: class );
$baseResponse = tap ( new Response , function ( $response ) {
$response -> setContent (
json_encode ([
'data' => [
'errors' => [
'foo' => [ 'one' ],
],
],
]),
);
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertJsonMissingValidationErrors ( 'foo' , 'data.errors' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsWithoutArgument () : void
2019-01-15 16:10:24 +01:00
{
$data = [ 'status' => 'ok' ];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonMissingValidationErrors ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsWithoutArgumentWhenErrorsIsEmpty () : void
2019-01-15 16:10:24 +01:00
{
$data = [ 'status' => 'ok' , 'errors' => []];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonMissingValidationErrors ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsWithoutArgumentCanFail () : void
2019-01-15 16:10:24 +01:00
{
$this -> expectException ( AssertionFailedError :: class );
$data = [ 'errors' => [ 'foo' => []]];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonMissingValidationErrors ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsOnInvalidJson () : void
2019-06-21 17:31:47 +02:00
{
2019-05-23 15:46:55 +01:00
$this -> expectException ( AssertionFailedError :: class );
2019-06-21 17:31:47 +02:00
$this -> expectExceptionMessage ( 'Invalid JSON was returned from the route.' );
2019-05-23 15:46:55 +01:00
2019-06-21 17:33:02 +02:00
$invalidJsonResponse = TestResponse :: fromBaseResponse (
2019-06-21 17:31:47 +02:00
( new Response ) -> setContent ( '~invalid json' )
2019-05-23 15:46:55 +01:00
);
2019-06-21 17:31:47 +02:00
2019-06-21 17:33:02 +02:00
$invalidJsonResponse -> assertJsonMissingValidationErrors ();
2019-05-23 14:20:48 +01:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsCustomErrorsName () : void
2019-03-16 15:49:43 +02:00
{
$data = [
'status' => 'ok' ,
'data' => [ 'foo' => 'oops' ],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
2019-03-16 16:21:12 +02:00
$testResponse -> assertJsonMissingValidationErrors ( 'bar' , 'data' );
2019-03-16 15:49:43 +02:00
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName1 () : void
2021-02-10 16:32:46 +02:00
{
$data = [
'status' => 'ok' ,
'data' => [
'errors' => [ 'foo' => 'oops' ],
],
];
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ( $data ))
);
$testResponse -> assertJsonMissingValidationErrors ( 'bar' , 'data.errors' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName2 () : void
2021-02-10 16:32:46 +02:00
{
$testResponse = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( json_encode ([]))
);
$testResponse -> assertJsonMissingValidationErrors ( 'bar' , 'data.errors' );
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonIsArray () : void
2023-01-23 11:27:10 +07:00
{
$responseArray = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$responseArray -> assertJsonIsArray ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonIsNotArray () : void
2023-01-23 11:27:10 +07:00
{
$this -> expectException ( ExpectationFailedException :: class );
$responseObject = TestResponse :: fromBaseResponse ( new Response ([
'foo' => 'bar' ,
]));
$responseObject -> assertJsonIsArray ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonIsObject () : void
2023-01-23 11:27:10 +07:00
{
$responseObject = TestResponse :: fromBaseResponse ( new Response ([
'foo' => 'bar' ,
]));
$responseObject -> assertJsonIsObject ();
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonIsNotObject () : void
2023-01-23 11:27:10 +07:00
{
$this -> expectException ( ExpectationFailedException :: class );
$responseArray = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableSingleResourceStub ));
$responseArray -> assertJsonIsObject ();
}
2025-09-02 01:45:37 +03:00
public function testAssertDownloadOffered () : void
2021-06-02 02:26:26 +06:00
{
$files = new Filesystem ;
$tempDir = __DIR__ . '/tmp' ;
$files -> makeDirectory ( $tempDir , 0755 , false , true );
$files -> put ( $tempDir . '/file.txt' , 'Hello World' );
$testResponse = TestResponse :: fromBaseResponse ( new Response (
$files -> get ( $tempDir . '/file.txt' ), 200 , [
'Content-Disposition' => 'attachment; filename=file.txt' ,
]
));
$testResponse -> assertDownload ();
$files -> deleteDirectory ( $tempDir );
}
2025-09-02 01:45:37 +03:00
public function testAssertDownloadOfferedWithAFileName () : void
2021-06-02 02:26:26 +06:00
{
$files = new Filesystem ;
$tempDir = __DIR__ . '/tmp' ;
$files -> makeDirectory ( $tempDir , 0755 , false , true );
$files -> put ( $tempDir . '/file.txt' , 'Hello World' );
$testResponse = TestResponse :: fromBaseResponse ( new Response (
$files -> get ( $tempDir . '/file.txt' ), 200 , [
'Content-Disposition' => 'attachment; filename = file.txt' ,
]
));
$testResponse -> assertDownload ( 'file.txt' );
$files -> deleteDirectory ( $tempDir );
}
2025-09-02 01:45:37 +03:00
public function testAssertDownloadOfferedWorksWithBinaryFileResponse () : void
2021-06-02 02:26:26 +06:00
{
$files = new Filesystem ;
$tempDir = __DIR__ . '/tmp' ;
$files -> makeDirectory ( $tempDir , 0755 , false , true );
$files -> put ( $tempDir . '/file.txt' , 'Hello World' );
$testResponse = TestResponse :: fromBaseResponse ( new BinaryFileResponse (
$tempDir . '/file.txt' , 200 , [], true , 'attachment'
));
$testResponse -> assertDownload ( 'file.txt' );
$files -> deleteDirectory ( $tempDir );
}
2025-09-02 01:45:37 +03:00
public function testAssertDownloadOfferedFailsWithInlineContentDisposition () : void
2021-06-02 02:26:26 +06:00
{
$this -> expectException ( AssertionFailedError :: class );
$files = new Filesystem ;
$tempDir = __DIR__ . '/tmp' ;
$files -> makeDirectory ( $tempDir , 0755 , false , true );
$files -> put ( $tempDir . '/file.txt' , 'Hello World' );
$testResponse = TestResponse :: fromBaseResponse ( new BinaryFileResponse (
$tempDir . '/file.txt' , 200 , [], true , 'inline'
));
$testResponse -> assertDownload ();
$files -> deleteDirectory ( $tempDir );
}
2025-09-02 01:45:37 +03:00
public function testAssertDownloadOfferedWithAFileNameWithSpacesInIt () : void
2021-11-08 21:33:03 +01:00
{
$files = new Filesystem ;
$tempDir = __DIR__ . '/tmp' ;
$files -> makeDirectory ( $tempDir , 0755 , false , true );
$files -> put ( $tempDir . '/file.txt' , 'Hello World' );
$testResponse = TestResponse :: fromBaseResponse ( new Response (
$files -> get ( $tempDir . '/file.txt' ), 200 , [
'Content-Disposition' => 'attachment; filename = "test file.txt"' ,
]
));
$testResponse -> assertDownload ( 'test file.txt' );
$files -> deleteDirectory ( $tempDir );
}
2025-09-02 01:45:37 +03:00
public function testMacroable () : void
2017-02-02 14:33:10 +01:00
{
TestResponse :: macro ( 'foo' , function () {
return 'bar' ;
});
2017-02-23 21:22:44 -05:00
$response = TestResponse :: fromBaseResponse ( new Response );
2017-02-02 14:33:10 +01:00
2019-08-27 14:48:17 +02:00
$this -> assertSame (
2017-02-02 14:33:10 +01:00
'bar' , $response -> foo ()
);
}
2017-02-23 21:22:44 -05:00
2025-09-02 01:45:37 +03:00
public function testCanBeCreatedFromBinaryFileResponses () : void
2017-02-23 21:22:44 -05:00
{
2017-06-19 15:34:26 +02:00
$files = new Filesystem ;
2017-02-23 21:22:44 -05:00
$tempDir = __DIR__ . '/tmp' ;
$files -> makeDirectory ( $tempDir , 0755 , false , true );
$files -> put ( $tempDir . '/file.txt' , 'Hello World' );
$response = TestResponse :: fromBaseResponse ( new BinaryFileResponse ( $tempDir . '/file.txt' ));
$this -> assertEquals ( $tempDir . '/file.txt' , $response -> getFile () -> getPathname ());
$files -> deleteDirectory ( $tempDir );
}
2017-12-22 16:03:42 +01:00
2025-09-02 01:45:37 +03:00
public function testJsonHelper () : void
2017-12-22 16:03:42 +01:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'foo' , $response -> json ( 'foobar.foobar_foo' ));
2017-12-22 16:03:42 +01:00
$this -> assertEquals (
json_decode ( $response -> getContent (), true ),
$response -> json ()
);
}
2018-08-17 15:43:03 +02:00
2025-09-02 01:45:37 +03:00
public function testResponseCanBeReturnedAsCollection () : void
2022-05-15 18:55:52 +03:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( new JsonSerializableMixedResourcesStub ));
$this -> assertInstanceOf ( Collection :: class , $response -> collect ());
$this -> assertEquals ( collect ([
'foo' => 'bar' ,
'foobar' => [
'foobar_foo' => 'foo' ,
'foobar_bar' => 'bar' ,
],
'0' => [ 'foo' ],
'bars' => [
[ 'bar' => 'foo 0' , 'foo' => 'bar 0' ],
[ 'bar' => 'foo 1' , 'foo' => 'bar 1' ],
[ 'bar' => 'foo 2' , 'foo' => 'bar 2' ],
],
'baz' => [
[ 'foo' => 'bar 0' , 'bar' => [ 'foo' => 'bar 0' , 'bar' => 'foo 0' ]],
[ 'foo' => 'bar 1' , 'bar' => [ 'foo' => 'bar 1' , 'bar' => 'foo 1' ]],
],
'barfoo' => [
[ 'bar' => [ 'bar' => 'foo 0' ]],
[ 'bar' => [ 'bar' => 'foo 0' , 'foo' => 'foo 0' ]],
[ 'bar' => [ 'foo' => 'bar 0' , 'bar' => 'foo 0' , 'rab' => 'rab 0' ]],
],
'numeric_keys' => [
2 => [ 'bar' => 'foo 0' , 'foo' => 'bar 0' ],
3 => [ 'bar' => 'foo 1' , 'foo' => 'bar 1' ],
4 => [ 'bar' => 'foo 2' , 'foo' => 'bar 2' ],
],
]), $response -> collect ());
$this -> assertEquals ( collect ([ 'foobar_foo' => 'foo' , 'foobar_bar' => 'bar' ]), $response -> collect ( 'foobar' ));
$this -> assertEquals ( collect ([ 'bar' ]), $response -> collect ( 'foobar.foobar_bar' ));
$this -> assertEquals ( collect (), $response -> collect ( 'missing_key' ));
}
2025-09-02 01:45:37 +03:00
public function testItCanBeTapped () : void
2019-07-02 18:53:33 +02:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ) -> setContent ( '' ) -> setStatusCode ( 418 )
);
$response -> tap ( function ( $response ) {
$this -> assertInstanceOf ( TestResponse :: class , $response );
}) -> assertStatus ( 418 );
}
2025-09-02 01:45:37 +03:00
public function testAssertPlainCookie () : void
2020-12-14 15:30:57 -05:00
{
$response = TestResponse :: fromBaseResponse (
2021-03-19 01:05:33 +01:00
( new Response ) -> withCookie ( new Cookie ( 'cookie-name' , 'cookie-value' ))
2020-12-14 15:30:57 -05:00
);
$response -> assertPlainCookie ( 'cookie-name' , 'cookie-value' );
}
2025-09-02 01:45:37 +03:00
public function testAssertCookie () : void
2020-12-14 15:30:57 -05:00
{
$container = Container :: getInstance ();
$encrypter = new Encrypter ( str_repeat ( 'a' , 16 ));
2024-06-14 23:21:59 +07:00
$container -> instance ( 'encrypter' , $encrypter );
2020-12-14 15:30:57 -05:00
$cookieName = 'cookie-name' ;
$cookieValue = 'cookie-value' ;
$encryptedValue = $encrypter -> encrypt ( CookieValuePrefix :: create ( $cookieName , $encrypter -> getKey ()) . $cookieValue , false );
$response = TestResponse :: fromBaseResponse (
2021-03-19 01:05:33 +01:00
( new Response ) -> withCookie ( new Cookie ( $cookieName , $encryptedValue ))
2020-12-14 15:30:57 -05:00
);
$response -> assertCookie ( $cookieName , $cookieValue );
}
2025-09-02 01:45:37 +03:00
public function testAssertCookieExpired () : void
2020-12-14 15:30:57 -05:00
{
$response = TestResponse :: fromBaseResponse (
2021-03-19 01:05:33 +01:00
( new Response ) -> withCookie ( new Cookie ( 'cookie-name' , 'cookie-value' , time () - 5000 ))
2020-12-14 15:30:57 -05:00
);
$response -> assertCookieExpired ( 'cookie-name' );
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionCookieExpiredDoesNotTriggerOnSessionCookies () : void
2020-12-14 16:42:13 -05:00
{
$response = TestResponse :: fromBaseResponse (
2021-03-19 01:05:33 +01:00
( new Response ) -> withCookie ( new Cookie ( 'cookie-name' , 'cookie-value' , 0 ))
2020-12-14 16:42:13 -05:00
);
$this -> expectException ( ExpectationFailedException :: class );
$response -> assertCookieExpired ( 'cookie-name' );
}
2025-09-02 01:45:37 +03:00
public function testAssertCookieNotExpired () : void
2020-12-14 15:30:57 -05:00
{
$response = TestResponse :: fromBaseResponse (
2021-03-19 01:05:33 +01:00
( new Response ) -> withCookie ( new Cookie ( 'cookie-name' , 'cookie-value' , time () + 5000 ))
2020-12-14 15:30:57 -05:00
);
$response -> assertCookieNotExpired ( 'cookie-name' );
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionCookieNotExpired () : void
2020-12-14 16:09:05 -05:00
{
$response = TestResponse :: fromBaseResponse (
2021-03-19 01:05:33 +01:00
( new Response ) -> withCookie ( new Cookie ( 'cookie-name' , 'cookie-value' , 0 ))
2020-12-14 16:09:05 -05:00
);
$response -> assertCookieNotExpired ( 'cookie-name' );
}
2025-09-02 01:45:37 +03:00
public function testAssertCookieMissing () : void
2020-12-14 15:30:57 -05:00
{
2021-03-19 01:05:33 +01:00
$response = TestResponse :: fromBaseResponse ( new Response );
2020-12-14 15:30:57 -05:00
$response -> assertCookieMissing ( 'cookie-name' );
}
2025-09-02 01:45:37 +03:00
public function testAssertLocation () : void
2022-04-04 20:07:23 +04:30
{
app () -> instance ( 'url' , $url = new UrlGenerator ( new RouteCollection , new Request ));
$response = TestResponse :: fromBaseResponse (
2023-03-13 03:00:40 +00:00
new RedirectResponse ( $url -> to ( 'https://foo.com' ))
2022-04-04 20:07:23 +04:30
);
$response -> assertLocation ( 'https://foo.com' );
$this -> expectException ( ExpectationFailedException :: class );
$response -> assertLocation ( 'https://foo.net' );
}
2025-09-02 01:45:37 +03:00
public function testAssertRedirectContains () : void
2021-10-13 22:17:07 +02:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ( '' , 302 )) -> withHeaders ([ 'Location' => 'https://url.com' ])
);
2021-10-17 11:49:12 +02:00
$response -> assertRedirectContains ( 'url.com' );
2021-10-13 22:17:07 +02:00
$this -> expectException ( ExpectationFailedException :: class );
2021-10-17 11:49:12 +02:00
$response -> assertRedirectContains ( 'url.net' );
2021-10-13 22:17:07 +02:00
}
2025-12-23 18:42:06 +03:30
public function testAssertHeaderContainsSuccess () : void
{
$baseResponse = tap ( new Response , function ( $response ) {
$response -> headers -> set ( 'X-Custom-Header' , 'prefix-value-suffix' );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$response -> assertHeaderContains ( 'X-Custom-Header' , 'value' );
}
public function testAssertHeaderContainsFailure () : void
{
$baseResponse = tap ( new Response , function ( $response ) {
$response -> headers -> set ( 'X-Custom-Header' , 'unrelated' );
});
$response = TestResponse :: fromBaseResponse ( $baseResponse );
$this -> expectException ( AssertionFailedError :: class );
$this -> expectExceptionMessage ( 'Header [X-Custom-Header] was found, but [unrelated] does not contain [value].' );
$response -> assertHeaderContains ( 'X-Custom-Header' , 'value' );
}
2025-09-02 01:45:37 +03:00
public function testAssertRedirect () : void
2022-04-04 22:33:09 +04:30
{
$response = TestResponse :: fromBaseResponse (
( new Response ( '' , 302 )) -> withHeaders ([ 'Location' => 'https://url.com' ])
);
$response -> assertRedirect ();
}
2025-09-02 01:45:37 +03:00
public function testAssertRedirectBack () : void
2025-05-05 02:43:35 +01:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> setPreviousUrl ( 'https://url.com' );
app ( 'url' ) -> setSessionResolver ( fn () => app ( 'session.store' ));
$response = TestResponse :: fromBaseResponse (
( new Response ( '' , 302 )) -> withHeaders ([ 'Location' => 'https://url.com' ])
);
$response -> assertRedirectBack ();
$this -> expectException ( ExpectationFailedException :: class );
$store -> setPreviousUrl ( 'https://url.net' );
$response -> assertRedirectBack ();
}
2025-09-02 01:45:37 +03:00
public function testGetDecryptedCookie () : void
2020-12-14 15:30:57 -05:00
{
$response = TestResponse :: fromBaseResponse (
( new Response ()) -> withCookie ( new Cookie ( 'cookie-name' , 'cookie-value' ))
);
2020-12-14 15:33:53 -05:00
$cookie = $response -> getCookie ( 'cookie-name' , false );
2020-12-14 15:30:57 -05:00
$this -> assertInstanceOf ( Cookie :: class , $cookie );
2022-02-22 15:46:19 +01:00
$this -> assertSame ( 'cookie-name' , $cookie -> getName ());
$this -> assertSame ( 'cookie-value' , $cookie -> getValue ());
2020-12-14 15:30:57 -05:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionHasErrors () : void
2022-03-22 21:14:51 +04:30
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'foo' => [
'foo is required' ,
],
]));
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHasErrors ([ 'foo' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertJsonSerializedSessionHasErrors () : void
2022-06-08 16:48:12 +02:00
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 ), null , 'json' ));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'foo' => [
'foo is required' ,
],
]));
$store -> save (); // Required to serialize error bag to JSON
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHasErrors ([ 'foo' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionDoesntHaveErrors () : void
2022-03-26 21:49:02 +04:30
{
$this -> expectException ( AssertionFailedError :: class );
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'foo' => [
'foo is required' ,
],
]));
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionDoesntHaveErrors ([ 'foo' ]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionHasNoErrors () : void
2022-03-29 02:13:12 +04:30
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'errors' , $errorBag = new ViewErrorBag );
$errorBag -> put ( 'default' , new MessageBag ([
'foo' => [
'foo is required' ,
],
]));
2022-11-01 20:47:41 +01:00
$errorBag -> put ( 'some-other-bag' , new MessageBag ([
'bar' => [
'bar is required' ,
],
]));
2022-03-29 02:13:12 +04:30
$response = TestResponse :: fromBaseResponse ( new Response ());
2022-11-01 20:47:41 +01:00
try {
$response -> assertSessionHasNoErrors ();
} catch ( AssertionFailedError $e ) {
$this -> assertStringContainsString ( 'foo is required' , $e -> getMessage ());
$this -> assertStringContainsString ( 'bar is required' , $e -> getMessage ());
}
2022-03-29 02:13:12 +04:30
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionHas () : void
2022-03-23 20:08:53 +04:30
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'value' );
$store -> put ( 'bar' , 'value' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHas ( 'foo' );
$response -> assertSessionHas ( 'bar' );
$response -> assertSessionHas ([ 'foo' , 'bar' ]);
}
2026-02-21 07:44:59 -06:00
public function testAssertSessionHasAllWithValues () : void
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'apple' );
$store -> put ( 'bar' , 'banana' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHasAll ([
'foo' => 'apple' ,
'bar' => 'banana' ,
]);
}
public function testAssertSessionHasAllShowsAllMismatches () : void
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'wrong1' );
$store -> put ( 'bar' , 'wrong2' );
$response = TestResponse :: fromBaseResponse ( new Response ());
try {
$response -> assertSessionHasAll ([
'foo' => 'apple' ,
'bar' => 'banana' ,
]);
$this -> fail ( 'xxxx' );
} catch ( AssertionFailedError $e ) {
$diff = $e -> getComparisonFailure () -> getDiff ();
$this -> assertStringContainsString ( 'wrong1' , $diff );
$this -> assertStringContainsString ( 'wrong2' , $diff );
$this -> assertStringContainsString ( 'apple' , $diff );
$this -> assertStringContainsString ( 'banana' , $diff );
}
}
public function testAssertSessionHasAllWithMixedKeys () : void
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'apple' );
$store -> put ( 'bar' , 'banana' );
$store -> put ( 'baz' , 'cherry' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHasAll ([
'baz' ,
'foo' => 'apple' ,
'bar' => 'banana' ,
]);
}
public function testAssertSessionHasAllWithClosures () : void
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'apple' );
$store -> put ( 'bar' , 'banana' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHasAll ([
'foo' => fn ( $value ) => $value === 'apple' ,
'bar' => 'banana' ,
]);
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionMissing () : void
2022-03-27 20:13:26 +04:30
{
$this -> expectException ( AssertionFailedError :: class );
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'value' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionMissing ( 'foo' );
}
2025-09-22 09:59:39 -05:00
#[TestWith(['foo', 'goodvalue'])]
#[TestWith([['foo', 'bar'], 'goodvalue'])]
public function testAssertSessionMissingValueIsPresent ( array | string $key , mixed $value ) : void
{
$this -> expectException ( AssertionFailedError :: class );
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'goodvalue' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionMissing ( $key , $value );
}
public function testAssertSessionMissingValueIsPresentClosure () : void
2025-05-19 07:11:02 -06:00
{
$this -> expectException ( AssertionFailedError :: class );
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'goodvalue' );
2025-09-22 09:59:39 -05:00
$key = 'foo' ;
$value = function ( $value ) {
return $value === 'goodvalue' ;
};
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionMissing ( $key , $value );
}
#[TestWith(['foo', 'badvalue'])]
public function testAssertSessionMissingValueIsMissing ( array | string $key , mixed $value ) : void
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'goodvalue' );
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionMissing ( $key , $value );
}
public function testAssertSessionMissingValueIsMissingClosure () : void
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( 'foo' , 'goodvalue' );
$key = 'foo' ;
$value = function ( $value ) {
return $value === 'badvalue' ;
};
2025-05-19 07:11:02 -06:00
$response = TestResponse :: fromBaseResponse ( new Response ());
2025-05-21 21:11:04 +08:00
$response -> assertSessionMissing ( $key , $value );
2025-05-19 07:11:02 -06:00
}
2025-09-02 01:45:37 +03:00
public function testAssertSessionHasInput () : void
2022-03-26 20:32:00 +04:30
{
app () -> instance ( 'session.store' , $store = new Store ( 'test-session' , new ArraySessionHandler ( 1 )));
$store -> put ( '_old_input' , [
'foo' => 'value' ,
'bar' => 'value' ,
]);
$response = TestResponse :: fromBaseResponse ( new Response ());
$response -> assertSessionHasInput ( 'foo' );
$response -> assertSessionHasInput ( 'foo' , 'value' );
$response -> assertSessionHasInput ( 'bar' );
$response -> assertSessionHasInput ( 'bar' , 'value' );
$response -> assertSessionHasInput ([ 'foo' , 'bar' ]);
$response -> assertSessionHasInput ( 'foo' , function ( $value ) {
return $value === 'value' ;
});
}
2025-09-02 01:45:37 +03:00
public function testGetEncryptedCookie () : void
2020-12-14 15:30:57 -05:00
{
$container = Container :: getInstance ();
$encrypter = new Encrypter ( str_repeat ( 'a' , 16 ));
2024-06-14 23:21:59 +07:00
$container -> instance ( 'encrypter' , $encrypter );
2020-12-14 15:30:57 -05:00
$cookieName = 'cookie-name' ;
$cookieValue = 'cookie-value' ;
$encryptedValue = $encrypter -> encrypt (
CookieValuePrefix :: create ( $cookieName , $encrypter -> getKey ()) . $cookieValue , false
);
$response = TestResponse :: fromBaseResponse (
( new Response ()) -> withCookie ( new Cookie ( $cookieName , $encryptedValue ))
);
2020-12-14 15:33:53 -05:00
$cookie = $response -> getCookie ( $cookieName );
2020-12-14 15:30:57 -05:00
$this -> assertInstanceOf ( Cookie :: class , $cookie );
$this -> assertEquals ( $cookieName , $cookie -> getName ());
$this -> assertEquals ( $cookieValue , $cookie -> getValue ());
}
2025-09-02 01:45:37 +03:00
public function testHandledExceptionIsIncludedInAssertionFailure () : void
2024-06-07 00:18:24 +10:00
{
$response = TestResponse :: fromBaseResponse ( new Response ( '' , 500 ))
-> withExceptions ( collect ([ new Exception ( 'Unexpected exception.' )]));
$this -> expectException ( ExpectationFailedException :: class );
$this -> expectExceptionMessageMatches ( '/Expected response status code \[200\] but received 500.*Exception: Unexpected exception/s' );
$response -> assertStatus ( 200 );
}
2025-09-02 01:45:37 +03:00
public function testValidationErrorsAreIncludedInAssertionFailure () : void
2024-06-07 00:18:24 +10:00
{
$response = TestResponse :: fromBaseResponse (
tap ( new RedirectResponse ( '/' ))
-> setSession ( new Store ( 'test-session' , new NullSessionHandler ()))
-> withErrors ([
'first_name' => 'The first name field is required.' ,
'last_name' => 'The last name field is required.' ,
])
);
$this -> expectException ( ExpectationFailedException :: class );
$this -> expectExceptionMessageMatches ( '/Expected response status code \[200\] but received 302.*The first name field is required.*The last name field is required/s' );
$response -> assertStatus ( 200 );
}
2025-09-02 01:45:37 +03:00
public function testJsonErrorsAreIncludedInAssertionFailure () : void
2024-06-07 00:18:24 +10:00
{
$response = TestResponse :: fromBaseResponse ( new JsonResponse ([
'errors' => [
'first_name' => [ 'The first name field is required.' ],
'last_name' => [ 'The last name field is required.' ],
],
], 422 ));
$this -> expectException ( ExpectationFailedException :: class );
$this -> expectExceptionMessageMatches ( '/Expected response status code \[200\] but received 422.*The first name field is required.*The last name field is required/s' );
$response -> assertStatus ( 200 );
}
2025-09-02 01:45:37 +03:00
public function testItHandlesFalseJson () : void
2024-06-07 00:18:24 +10:00
{
$response = TestResponse :: fromBaseResponse (
new Response ( false , 422 , [ 'Content-Type' => 'application/json' ])
);
$this -> expectException ( ExpectationFailedException :: class );
$this -> expectExceptionMessage ( 'Expected response status code [200] but received 422.' );
$response -> assertStatus ( 200 );
}
2025-09-02 01:45:37 +03:00
public function testItHandlesEncodedJson () : void
2024-06-07 00:18:24 +10:00
{
$response = TestResponse :: fromBaseResponse (
new Response ( 'b"x£½V*.I,)-V▓R╩¤V¬\x05\x00+ü\x059"' , 422 , [ 'Content-Type' => 'application/json' , 'Content-Encoding' => 'gzip' ])
);
$this -> expectException ( ExpectationFailedException :: class );
$this -> expectExceptionMessage ( 'Expected response status code [200] but received 422.' );
$response -> assertStatus ( 200 );
}
2018-08-17 15:43:03 +02:00
private function makeMockResponse ( $content )
{
$baseResponse = tap ( new Response , function ( $response ) use ( $content ) {
2018-10-05 14:48:10 +02:00
$response -> setContent ( m :: mock ( View :: class , $content ));
2018-08-17 15:43:03 +02:00
});
return TestResponse :: fromBaseResponse ( $baseResponse );
}
2017-01-31 23:09:17 +02:00
}
class JsonSerializableMixedResourcesStub implements JsonSerializable
{
2021-06-28 15:56:26 +02:00
public function jsonSerialize () : array
2017-01-31 23:09:17 +02:00
{
return [
2018-12-04 20:39:55 +01:00
'foo' => 'bar' ,
2017-01-31 23:09:17 +02:00
'foobar' => [
'foobar_foo' => 'foo' ,
'foobar_bar' => 'bar' ,
],
2020-05-04 01:36:25 +10:00
'0' => [ 'foo' ],
2018-12-04 20:39:55 +01:00
'bars' => [
2017-01-31 23:09:17 +02:00
[ 'bar' => 'foo 0' , 'foo' => 'bar 0' ],
[ 'bar' => 'foo 1' , 'foo' => 'bar 1' ],
[ 'bar' => 'foo 2' , 'foo' => 'bar 2' ],
],
2018-12-04 20:39:55 +01:00
'baz' => [
2017-01-31 23:09:17 +02:00
[ 'foo' => 'bar 0' , 'bar' => [ 'foo' => 'bar 0' , 'bar' => 'foo 0' ]],
[ 'foo' => 'bar 1' , 'bar' => [ 'foo' => 'bar 1' , 'bar' => 'foo 1' ]],
],
2018-01-28 03:26:49 +01:00
'barfoo' => [
[ 'bar' => [ 'bar' => 'foo 0' ]],
2020-10-06 15:39:54 +03:00
[ 'bar' => [ 'bar' => 'foo 0' , 'foo' => 'foo 0' ]],
2018-01-28 03:26:49 +01:00
[ 'bar' => [ 'foo' => 'bar 0' , 'bar' => 'foo 0' , 'rab' => 'rab 0' ]],
],
2018-12-04 20:39:55 +01:00
'numeric_keys' => [
2 => [ 'bar' => 'foo 0' , 'foo' => 'bar 0' ],
3 => [ 'bar' => 'foo 1' , 'foo' => 'bar 1' ],
4 => [ 'bar' => 'foo 2' , 'foo' => 'bar 2' ],
],
2017-01-31 23:09:17 +02:00
];
}
}
class JsonSerializableSingleResourceStub implements JsonSerializable
{
2021-06-28 15:56:26 +02:00
public function jsonSerialize () : array
2017-01-31 23:09:17 +02:00
{
return [
2024-10-17 00:25:45 +08:00
[ 'foo' => 'foo 0' , 'bar' => 'bar 0' , 'foobar' => 'foobar 0' , 'meta' => []],
[ 'foo' => 'foo 1' , 'bar' => 'bar 1' , 'foobar' => 'foobar 1' , 'meta' => null ],
[ 'foo' => 'foo 2' , 'bar' => 'bar 2' , 'foobar' => 'foobar 2' , 'meta' => []],
[ 'foo' => 'foo 3' , 'bar' => 'bar 3' , 'foobar' => 'foobar 3' , 'meta' => null ],
2017-01-31 23:09:17 +02:00
];
}
}
2018-07-15 19:32:16 +01:00
class JsonSerializableSingleResourceWithIntegersStub implements JsonSerializable
{
2021-06-28 15:56:26 +02:00
public function jsonSerialize () : array
2018-07-15 19:32:16 +01:00
{
return [
[ 'id' => 10 , 'foo' => 'bar' ],
[ 'id' => 20 , 'foo' => 'bar' ],
[ 'id' => 30 , 'foo' => 'bar' ],
];
}
}
2022-05-19 10:31:04 -04:00
2022-10-04 19:43:55 +03:00
class JsonSerializableSingleResourceWithUnicodeStub implements JsonSerializable
{
public function jsonSerialize () : array
{
return [
[ 'id' => 10 , 'foo' => 'bar' ],
[ 'id' => 20 , 'foo' => 'Привет' ],
[ 'id' => 30 , 'foo' => 'Мир' ],
];
}
}
2022-05-19 10:31:04 -04:00
class TestModel extends Model
{
protected $guarded = [];
}
class AnotherTestModel extends Model
{
protected $guarded = [];
}
2025-04-23 15:17:11 +02:00
enum TestStatus : string
{
case Booked = 'booked' ;
}