-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Improve test failure output (#43943)
* Append exceptions and errors on all test failures * Move tests * formatting * Uses FQN * Unsets `latestResponse` on setup * Uses `is_null` * formatting Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
f955686
commit b7e096a
Showing
5 changed files
with
224 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?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(); | ||
$testCase->latestResponse = TestResponse::fromBaseResponse(new Response()) | ||
->withExceptions(collect([new Exception('Unexpected exception.')])); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessageMatches('/Assertion message.*Unexpected exception/s'); | ||
|
||
$testCase->onNotSuccessfulTest(new ExpectationFailedException('Assertion message.')); | ||
} | ||
|
||
public function test_it_includes_validation_errors_on_test_failures() | ||
{ | ||
$testCase = new ExampleTestCase(); | ||
$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->onNotSuccessfulTest(new ExpectationFailedException('Assertion message.')); | ||
} | ||
|
||
public function test_it_includes_json_validation_errors_on_test_failures() | ||
{ | ||
$testCase = new ExampleTestCase(); | ||
$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->onNotSuccessfulTest(new ExpectationFailedException('Assertion message.')); | ||
} | ||
|
||
public function test_it_doesnt_fail_with_false_json() | ||
{ | ||
$testCase = new ExampleTestCase(); | ||
$testCase->latestResponse = TestResponse::fromBaseResponse( | ||
new Response(false, 200, ['Content-Type' => 'application/json']) | ||
); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessageMatches('/Assertion message/s'); | ||
$testCase->onNotSuccessfulTest(new ExpectationFailedException('Assertion message.')); | ||
} | ||
|
||
public function test_it_doesnt_fail_with_encoded_json() | ||
{ | ||
$testCase = new ExampleTestCase(); | ||
$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->onNotSuccessfulTest(new ExpectationFailedException('Assertion message.')); | ||
} | ||
} | ||
|
||
class ExampleTestCase extends TestCase | ||
{ | ||
public function createApplication() | ||
{ | ||
// | ||
} | ||
} |
Oops, something went wrong.