Skip to content

Commit

Permalink
Move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Aug 31, 2022
1 parent 4f088b0 commit 288583b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ trait MakesHttpRequests
*
* @var TestResponse
*/
protected $latestResponse;
public $latestResponse;

/**
* Define additional headers to be sent with the request.
Expand Down
92 changes: 92 additions & 0 deletions tests/Foundation/Testing/TestCaseTest.php
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()
{
//
}
}
85 changes: 0 additions & 85 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Tests\Testing;

use Exception;
use Illuminate\Container\Container;
use Illuminate\Contracts\View\View;
use Illuminate\Cookie\CookieValuePrefix;
Expand Down Expand Up @@ -623,90 +622,6 @@ public function testAssertStatus()
$response->assertStatus($expectedStatusCode);
}

public function testAssertStatusShowsExceptionOnUnexpected500()
{
$statusCode = 500;
$expectedStatusCode = 200;

$this->expectException(AssertionFailedError::class);

$this->expectExceptionMessage('Test exception message');

$baseResponse = tap(new Response, function ($response) use ($statusCode) {
$response->setStatusCode($statusCode);
});
$exceptions = collect([new Exception('Test exception message')]);

$response = TestResponse::fromBaseResponse($baseResponse)
->withExceptions($exceptions);
$response->assertStatus($expectedStatusCode);
}

public function testAssertStatusShowsErrorsOnUnexpectedErrorRedirect()
{
$statusCode = 302;
$expectedStatusCode = 200;

$this->expectException(AssertionFailedError::class);

$this->expectExceptionMessage('The first name field is required.');

$baseResponse = tap(new RedirectResponse('/', $statusCode), function ($response) {
$response->setSession(new Store('test-session', new ArraySessionHandler(1)));
$response->withErrors([
'first_name' => 'The first name field is required.',
'last_name' => 'The last name field is required.',
]);
});

$response = TestResponse::fromBaseResponse($baseResponse);
$response->assertStatus($expectedStatusCode);
}

public function testAssertStatusShowsJsonErrorsOnUnexpected422()
{
$statusCode = 422;
$expectedStatusCode = 200;

$this->expectException(AssertionFailedError::class);

$this->expectExceptionMessage('"The first name field is required."');

$baseResponse = new Response(
[
'message' => 'The given data was invalid.',
'errors' => [
'first_name' => 'The first name field is required.',
'last_name' => 'The last name field is required.',
],
],
$statusCode
);

$response = TestResponse::fromBaseResponse($baseResponse);
$response->assertStatus($expectedStatusCode);
}

public function testAssertStatusWhenJsonIsFalse()
{
$baseResponse = new Response('false', 200, ['Content-Type' => 'application/json']);

$response = TestResponse::fromBaseResponse($baseResponse);
$response->assertStatus(200);
}

public function testAssertStatusWhenJsonIsEncoded()
{
$baseResponse = 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"');
});

$response = TestResponse::fromBaseResponse($baseResponse);
$response->assertStatus(200);
}

public function testAssertHeader()
{
$this->expectException(AssertionFailedError::class);
Expand Down

0 comments on commit 288583b

Please sign in to comment.