lastdragon-ru/lara-asp-testing
Testing utilities for integrating Lara ASP into Laravel apps. Provides helpers, fakes, and assertions to simplify writing automated tests around ASP policies, decisions, and request/response flows in your application.
Installation Add the package via Composer:
composer require lastdragon-ru/lara-asp-testing
Publish the config (if needed) with:
php artisan vendor:publish --provider="LastDragon\LaraAspTesting\LaraAspTestingServiceProvider"
First Use Case Test a simple HTTP response in a PHPUnit test:
use LastDragon\LaraAspTesting\Assertions\AspAssertions;
public function test_homepage_returns_success()
{
$response = $this->get('/');
AspAssertions::assertOk($response); // Asserts HTTP 200
AspAssertions::assertSee('Welcome'); // Asserts response contains text
}
Key Entry Points
AspAssertions class for HTTP-specific assertions.TestResponse with additional methods.config/lara-asp-testing.php for customization (e.g., default assertions).Response Validation Use fluent assertions for readability:
$response = $this->post('/login', ['email' => 'test@example.com']);
AspAssertions::assertCreated($response)
->assertJsonStructure(['data' => ['token']]);
API Testing Test JSON responses with structured assertions:
$response = $this->getJson('/api/users/1');
AspAssertions::assertJson($response)
->assertEquals('John Doe', 'data.name')
->assertArrayHasKey('email', 'data');
Authentication Flows Test protected routes with session/token handling:
$this->actingAs(User::find(1));
$response = $this->get('/dashboard');
AspAssertions::assertOk($response)->assertSeeInOrder(['Dashboard', 'Welcome']);
Error Handling Validate error responses:
$response = $this->get('/invalid-route');
AspAssertions::assertNotFound($response)
->assertSeeInResponse('Not Found');
AspAssertions for project-specific rules:
use LastDragon\LaraAspTesting\Assertions\AspAssertions;
class CustomAssertions extends AspAssertions {
public static function assertCustomRule($response) {
return self::assertOk($response)->assertSee('Custom Rule Passed');
}
}
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/profile');
$this->mock('GET', '/external-api', ['status' => 'success']);
$response = $this->get('/route-that-calls-api');
AspAssertions::assertOk($response);
Assertion Order Matters
Chaining assertions (e.g., assertOk()->assertSee()) fails fast if the first assertion fails. Use try-catch or separate assertions for granular debugging:
try {
AspAssertions::assertOk($response)->assertSee('Missing Text');
} catch (AssertionFailedError $e) {
$this->fail('Assertion failed: ' . $e->getMessage());
}
Case Sensitivity in assertSee
Text assertions (assertSee, assertSeeInResponse) are case-sensitive by default. Use assertSeeIgnoreCase for flexibility:
AspAssertions::assertSeeIgnoreCase('welcome'); // Matches "Welcome", "WELCOME", etc.
JSON Parsing Quirks Ensure JSON responses are valid before asserting structure:
$response = $this->getJson('/api/data');
$this->assertJson($response); // Laravel’s built-in check
AspAssertions::assertJson($response)->assertEquals(1, 'data.id');
Session/Token Expiry
Tests using actingAs may fail if tokens/sessions expire. Refresh credentials or use:
$this->actingAs(User::find(1), 'api'); // For API token auth
$this->assertTrue($response->ok());
$this->dump($response->original); // Dump raw response
AspAssertions::assertOk($response, 'Expected HTTP 200, got ' . $response->status());
phpunit.xml:
<env name="APP_DEBUG" value="true"/>
<env name="APP_LOG_LEVEL" value="debug"/>
Custom Assertion Macros
Add reusable assertions to the TestResponse class:
use Illuminate\Foundation\Testing\TestResponse;
TestResponse::macro('assertCustomHeader', function ($header, $value) {
$this->assertHeader($header, $value);
return $this;
});
Hook into Assertions Override assertions globally in a test trait:
trait CustomAssertionTrait {
protected function assertResponse($response, $method, $args) {
if ($method === 'assertSee' && in_array('ignoreCase', $args)) {
// Custom logic
}
parent::assertResponse($response, $method, $args);
}
}
Configuration Overrides
Customize default behaviors in config/lara-asp-testing.php:
'default_assertions' => [
'assertJson' => true, // Auto-assert JSON for API tests
],
How can I help you explore Laravel packages today?