1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
<?php
namespace Tests\Foundation\Testing;
use Exception;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Session\Store;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase as BaseTestCase;
class TestCaseTest extends BaseTestCase
{
public function test_it_includes_response_exceptions_on_test_failures()
{
$testCase = new ExampleTestCase('foo');
$testCase::$latestResponse = TestResponse::fromBaseResponse(new Response())
->withExceptions(collect([new Exception('Unexpected exception.')]));
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessageMatches('/Assertion message.*Unexpected exception/s');
$testCase::$latestResponse->transformNotSuccessfulException(
$exception = new ExpectationFailedException('Assertion message.'),
);
throw $exception;
}
public function test_it_includes_validation_errors_on_test_failures()
{
$testCase = new ExampleTestCase('foo');
$testCase::$latestResponse = TestResponse::fromBaseResponse(
tap(new RedirectResponse('/'))
->setSession(new Store('test-session', new NullSessionHandler()))
->withErrors([
'first_name' => 'The first name field is required.',
])
);
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessageMatches('/Assertion message.*The first name field is required/s');
$testCase::$latestResponse->transformNotSuccessfulException(
$exception = new ExpectationFailedException('Assertion message.'),
);
throw $exception;
}
public function test_it_includes_json_validation_errors_on_test_failures()
{
$testCase = new ExampleTestCase('foo');
$testCase::$latestResponse = TestResponse::fromBaseResponse(
new Response(['errors' => ['first_name' => 'The first name field is required.']])
);
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessageMatches('/Assertion message.*The first name field is required/s');
$testCase::$latestResponse->transformNotSuccessfulException(
$exception = new ExpectationFailedException('Assertion message.'),
);
throw $exception;
}
public function test_it_doesnt_fail_with_false_json()
{
$testCase = new ExampleTestCase('foo');
$testCase::$latestResponse = TestResponse::fromBaseResponse(
new Response(false, 200, ['Content-Type' => 'application/json'])
);
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessageMatches('/Assertion message/s');
$testCase::$latestResponse->transformNotSuccessfulException(
$exception = new ExpectationFailedException('Assertion message.'),
);
throw $exception;
}
public function test_it_doesnt_fail_with_encoded_json()
{
$testCase = new ExampleTestCase('foo');
$testCase::$latestResponse = TestResponse::fromBaseResponse(
tap(new Response, function ($response) {
$response->header('Content-Type', 'application/json');
$response->header('Content-Encoding', 'gzip');
$response->setContent('b"x£½V*.I,)-V▓R╩¤V¬\x05\x00+ü\x059"');
})
);
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessageMatches('/Assertion message/s');
$testCase::$latestResponse->transformNotSuccessfulException(
$exception = new ExpectationFailedException('Assertion message.'),
);
throw $exception;
}
protected function tearDown(): void
{
ExampleTestCase::$latestResponse = null;
parent::tearDown();
}
}
class ExampleTestCase extends TestCase
{
public function createApplication()
{
//
}
}
|