Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Lara Asp Testing Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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"
    
  2. 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
    }
    
  3. Key Entry Points

    • Assertions: AspAssertions class for HTTP-specific assertions.
    • Test Helpers: Extends Laravel’s built-in TestResponse with additional methods.
    • Configuration: Check config/lara-asp-testing.php for customization (e.g., default assertions).

Implementation Patterns

Common Workflows

  1. Response Validation Use fluent assertions for readability:

    $response = $this->post('/login', ['email' => 'test@example.com']);
    AspAssertions::assertCreated($response)
                 ->assertJsonStructure(['data' => ['token']]);
    
  2. 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');
    
  3. Authentication Flows Test protected routes with session/token handling:

    $this->actingAs(User::find(1));
    $response = $this->get('/dashboard');
    AspAssertions::assertOk($response)->assertSeeInOrder(['Dashboard', 'Welcome']);
    
  4. Error Handling Validate error responses:

    $response = $this->get('/invalid-route');
    AspAssertions::assertNotFound($response)
                 ->assertSeeInResponse('Not Found');
    

Integration Tips

  • Custom Assertions: Extend 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');
        }
    }
    
  • Data Factories: Combine with Laravel’s factories for robust test data:
    $user = User::factory()->create();
    $response = $this->actingAs($user)->get('/profile');
    
  • Mocking: Use Laravel’s HTTP test mocking alongside assertions:
    $this->mock('GET', '/external-api', ['status' => 'success']);
    $response = $this->get('/route-that-calls-api');
    AspAssertions::assertOk($response);
    

Gotchas and Tips

Pitfalls

  1. 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());
    }
    
  2. Case Sensitivity in assertSee Text assertions (assertSee, assertSeeInResponse) are case-sensitive by default. Use assertSeeIgnoreCase for flexibility:

    AspAssertions::assertSeeIgnoreCase('welcome'); // Matches "Welcome", "WELCOME", etc.
    
  3. 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');
    
  4. 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
    

Debugging Tips

  • Dump Response: Use Laravel’s built-in helpers for debugging:
    $this->assertTrue($response->ok());
    $this->dump($response->original); // Dump raw response
    
  • Custom Error Messages: Override default assertion messages:
    AspAssertions::assertOk($response, 'Expected HTTP 200, got ' . $response->status());
    
  • Log Assertions: Enable Laravel’s test logging in phpunit.xml:
    <env name="APP_DEBUG" value="true"/>
    <env name="APP_LOG_LEVEL" value="debug"/>
    

Extension Points

  1. 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;
    });
    
  2. 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);
        }
    }
    
  3. Configuration Overrides Customize default behaviors in config/lara-asp-testing.php:

    'default_assertions' => [
        'assertJson' => true, // Auto-assert JSON for API tests
    ],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky