Skip to content

Automated Testing

lpachecob edited this page Mar 23, 2026 · 1 revision

🧪 Automated Testing

BOUNDLY embraces a robust Domain-Driven Design testing strategy by moving away from traditional frameworks' tests/Feature monolithic directories and bringing tests directly to the Domain layer where the logic lives.

To achieve this, the framework generates tests automatically based on your Entity definitions.


🏃 Generating Tests

To generate test stubs for all discovered Domain entities, simply run:

php artisan core:make:test

What does it do?

  1. Intelligent Payload Mocking: BOUNDLY inspects every #[Column] defined in your Entity. It parses the types (integer, boolean, date, email) and creates a valid mock payload automatically in the test file.
  2. Path Generation: It reads your Entity namespace and places the generated test file right next to it in a Tests/ directory:
    • Example: Domain/Users/Entities/User.phpDomain/Users/Tests/UserApiTest.php

🔬 Running Tests

Since the tests are generated in Domain/ (and potentially Application/), BOUNDLY updates your root phpunit.xml out-of-the-box to detect these directories.

To run the full suite:

php artisan test

The BoundlyTestCase Magic

Standard Laravel tests rely on checking standard migration classes to recreate the test database (often using :memory: SQLite databases). Because BOUNDLY uses PHP Attributes instead of migration files, standard Laravel testing traits are useless.

This is solved by having all tests extend Infrastructure\FrameworkCore\Testing\BoundlyTestCase.

When you use BoundlyTestCase, before each test runs:

  1. It connects to your testing database connection (like sqlite :memory:).
  2. It internally invokes php artisan core:migrate behind the scenes.
  3. BOUNDLY dynamically translates all your #[Entity] and #[Column] metadata into real SQLite tables in milliseconds.
  4. Your endpoints execute using a fully configured database schema perfectly synchronized to your entity code.

🔒 Testing Secured Routes

If your entity uses the #[Authorize] attribute, requests made in test environments will naturally return 401 Unauthorized or 403 Forbidden if there is no user authenticated.

To bypass authorization during unit tests that only focus on the generic entity CRUD, the auto-generated test stub includes commented-out code to disable the middleware:

// Uncomment to disable RBAC during CRUD tests:
// $this->withoutMiddleware(\Infrastructure\FrameworkCore\Http\Middleware\ResourceAuthorize::class);

Alternatively, you can authenticate a dummy user using Laravel's standard standard helper:

$user = \Domain\Users\Entities\User::first();
$this->actingAs($user);

Next Step: Roadmap 🚀

Clone this wiki locally