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

Laravel Makesure Laravel Package

imanghafoori/laravel-makesure

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require imanghafoori/laravel-makesure --dev
    

    No additional configuration is required—just publish the facade alias if needed (though the package auto-registers).

  2. First Use Case: Replace a basic HTTP assertion in a test:

    // Before (standard Laravel)
    $this->get('/api/users')
         ->assertStatus(200)
         ->assertJson(['count' => 1]);
    
    // After (MakeSure)
    MakeSure::about($this)
        ->sendingGetRequest('/api/users')
        ->isRespondedWith()
        ->statusCode(200)
        ->json(['count' => 1]);
    
  3. Where to Look First:

    • Facade: Imanghafoori\MakeSure\Facades\MakeSure (or alias MakeSure).
    • Chainable Methods: Start with about($this) → HTTP method (e.g., sendingGetRequest()) → assertions (e.g., statusCode(), json()).
    • Documentation: The README lists all available methods (e.g., sendingPostRequest(), isRespondedWith()).

Implementation Patterns

Core Workflow

  1. Test Initialization: Always pass $this (the test case instance) to MakeSure::about($this) to maintain context.

  2. HTTP Request Chaining:

    MakeSure::about($this)
        ->sendingPostRequest('/api/users')
        ->withHeaders(['Accept' => 'application/json'])
        ->withData(['name' => 'John']);
    
  3. Assertion Patterns:

    • Status Codes:
      ->isRespondedWith()
          ->statusCode(201);
      
    • JSON Responses:
      ->isRespondedWith()
          ->json(['key' => 'value']);
      
    • Redirects:
      ->isRedirectedTo('/login');
      
    • Exceptions:
      ->throwsException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
      
  4. Dynamic Data: Use helper methods for reusable assertions:

    // In a trait or helper
    public function assertUserCreatedResponse()
    {
        return MakeSure::about($this)
            ->isRespondedWith()
            ->statusCode(201)
            ->json(['id' => $this->user->id]);
    }
    
  5. Integration with Laravel Features:

    • Authentication:
      $this->actingAs($user);
      MakeSure::about($this)
          ->sendingGetRequest('/profile')
          ->isRespondedWith()
          ->statusCode(200);
      
    • API Testing: Combine with JsonTestingTrait for API-specific assertions:
      MakeSure::about($this)
          ->sendingJsonPostRequest('/api/login')
          ->isRespondedWith()
          ->statusCode(200)
          ->header('Content-Type', 'application/json');
      
  6. Grouping Assertions: Use group() to organize related assertions:

    MakeSure::about($this)
        ->group('User Creation')
            ->sendingPostRequest('/users')
            ->isRespondedWith()
                ->statusCode(201)
                ->json(['name' => 'John']);
    

Gotchas and Tips

Pitfalls

  1. Context Loss:

    • Issue: Forgetting to pass $this to about() will break the chain.
    • Fix: Always use MakeSure::about($this) at the start of every test.
  2. Method Order Sensitivity:

    • Issue: Some methods (e.g., isRespondedWith()) must precede others (e.g., statusCode()).
    • Fix: Follow the documented order: HTTP method → isRespondedWith() → assertions.
  3. Facade Alias Conflicts:

    • Issue: If another package uses MakeSure, manually bind the facade in config/app.php:
      'aliases' => [
          'MakeSure' => Imanghafoori\MakeSure\Facades\MakeSure::class,
      ],
      
  4. JSON Assertions:

    • Issue: Deeply nested JSON assertions may require exact key paths.
    • Fix: Use dot notation for nested keys:
      ->json(['user.profile.name' => 'John']);
      

Debugging Tips

  1. Chain Validation:

    • Use dd() on intermediate steps to verify the response object:
      $response = MakeSure::about($this)
          ->sendingGetRequest('/api/users')
          ->getResponse(); // Debug here
      
  2. Custom Assertions:

    • Extend the package by creating a custom assertion class:
      namespace App\Tests\Assertions;
      use Imanghafoori\MakeSure\Assertions\Assertion;
      
      class CustomAssertion extends Assertion {
          public function customCheck($expected) {
              return $this->response->assertSee($expected);
          }
      }
      
    • Register it in MakeSureServiceProvider (if extending).
  3. Performance:

    • Issue: Overusing MakeSure for simple tests may reduce readability.
    • Tip: Reserve it for complex assertions or teams where consistency is key.

Extension Points

  1. Custom HTTP Methods:

    • Add support for non-standard HTTP methods (e.g., sendingCustomRequest()) by extending the MakeSure facade:
      public function sendingCustomRequest($uri, $method = 'CUSTOM') {
          return $this->newAssertion()
              ->sendingRequest($uri, $method);
      }
      
  2. Database Assertions:

    • Combine with Laravel’s DatabaseMigrations or DatabaseTransactions traits:
      $this->actingAs($admin);
      MakeSure::about($this)
          ->sendingDeleteRequest('/api/users/1')
          ->isRespondedWith()
              ->statusCode(200)
          ->databaseHas('users', ['id' => 1], false); // Custom extension
      
  3. Mocking:

    • Use MakeSure with Mockery or Laravel’s partialMock:
      $mock = $this->partialMock(User::class, ['find']);
      MakeSure::about($this)
          ->sendingGetRequest('/api/users/1')
          ->isRespondedWith()
              ->statusCode(200)
          ->mock($mock); // Hypothetical extension
      
  4. Reporting:

    • Integrate with Laravel Pint or PHPUnit reporters for test output:
      // Example: Custom reporter for MakeSure chains
      public function addFailure(MakeSure $makeSure, $description) {
          $this->fail($description . "\nChain: " . $makeSure->getChain());
      }
      
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.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium