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

Aitestbundle Laravel Package

antonio/aitestbundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Add the bundle in development mode:

    composer require antonio/aitestbundle --dev
    

    Ensure your Symfony project is on PHP 8.2+ and Symfony 7.0+.

  2. First Use Case Generate and run tests for a controller (e.g., App\Controller\Api\UserController):

    symfony console ai:test-controller App\Controller\Api\UserController
    
    • The command auto-generates a PHPUnit test file in tests/Controller/ (or a configured path).
    • Executes the test suite immediately, showing assertions for HTTP statuses, responses, and routes.
  3. Where to Look First

    • Generated Tests: Check tests/Controller/ for the auto-generated test file (e.g., UserControllerTest.php).
    • Configuration: Override defaults in config/packages/aitestbundle.yaml (if needed).
    • Logs: Use --verbose flag for debugging:
      symfony console ai:test-controller --verbose App\Controller\Api\UserController
      

Implementation Patterns

Workflows

  1. Incremental Testing

    • Run tests for individual controllers during development:
      symfony console ai:test-controller App\Controller\ProductController
      
    • Batch mode: Test all controllers in a namespace (e.g., App\Controller\Api):
      symfony console ai:test-controller App\Controller\Api
      
      Note: Requires --batch flag (if supported in future versions).
  2. Integration with CI/CD

    • Add to phpunit.xml.dist to run auto-generated tests alongside manual tests:
      <testsuites>
          <testsuite name="Controller Tests">
              <directory>./tests/Controller</directory>
          </testsuite>
      </testsuite>
      
    • Use in GitHub Actions or GitLab CI to validate controllers post-merge:
      - run: symfony console ai:test-controller App\Controller
      
  3. Custom Test Templates

    • Extend the default test template by overriding the bundle’s templates/test.twig (if supported).
    • Example: Add custom assertions for JWT authentication:
      // In a custom template (e.g., config/aitestbundle/test_template.php)
      public function assertJwtTokenExists(Response $response) {
          $this->assertStringContainsString('Bearer ', $response->headers->get('Authorization'));
      }
      
  4. Mocking Dependencies

    • The bundle auto-generates tests for public methods but may miss private/protected logic.
    • Manually add mocks for services injected into controllers:
      // In UserControllerTest.php (manually edited)
      protected function setUp(): void {
          $this->mockService = $this->createMock(UserService::class);
          $this->controller->setUserService($this->mockService);
      }
      

Integration Tips

  • Symfony Flex: Works seamlessly with Symfony’s autoloader.
  • Doctrine ORM: Auto-generated tests validate database interactions (e.g., assertDatabaseHas()).
  • API Platform: Test serializers and routes by default (no extra config needed).
  • Event Dispatchers: Tests verify events are dispatched (e.g., KernelEvents::REQUEST).

Gotchas and Tips

Pitfalls

  1. False Positives in Assertions

    • The bundle assumes 200 OK for successful responses. Override for custom statuses:
      # config/packages/aitestbundle.yaml
      ai_test_bundle:
          default_status: 201  # For POST routes
      
    • Debug: Check generated tests for hardcoded assertions (e.g., assertEquals(200, $response->getStatusCode())).
  2. Dynamic Routes

    • Tests may fail if routes use dynamic parameters (e.g., /users/{id}).
    • Solution: Mock the router or use ->withParameter():
      // In generated test
      $this->client->request('GET', '/users/1');
      
  3. Service Container Issues

    • If the controller uses private services, the auto-generated test may fail.
    • Fix: Manually inject services in setUp() (see Implementation Patterns).
  4. Environment-Specific Config

    • Tests run in the test environment by default. Override kernel parameters:
      symfony console ai:test-controller --env=dev App\Controller\DashboardController
      

Debugging

  • Verbose Mode: Reveal hidden errors:

    symfony console ai:test-controller --verbose App\Controller\PaymentController
    
  • Dry Run: Generate tests without executing them:

    symfony console ai:test-controller --generate-only App\Controller\ReportController
    

    Output: Test file path printed to console.

  • Log Generation: Enable Symfony’s profiler to inspect test execution:

    symfony console ai:test-controller --debug App\Controller\AuthController
    

Extension Points

  1. Custom Assertions

    • Extend the test class to add domain-specific assertions:
      // src/Test/AiTestBundle/Extension/CustomAssertions.php
      namespace App\Test\AiTestBundle\Extension;
      
      use PHPUnit\Framework\Assert;
      
      class CustomAssertions {
          public static function assertValidPagination(Response $response) {
              $data = json_decode($response->getContent(), true);
              Assert::assertArrayHasKey('data', $data);
              Assert::assertArrayHasKey('meta', $data);
          }
      }
      
    • Hook: Override the bundle’s TestGenerator service to include your assertions.
  2. Pre/Post-Test Hooks

    • Use Symfony’s event system to run logic before/after test generation:
      # config/services.yaml
      services:
          App\EventListener\AiTestListener:
              tags:
                  - { name: kernel.event_listener, event: ai_test.pre_generate, method: onPreGenerate }
      
    • Example: Clear cache or seed test data:
      public function onPreGenerate(PreGenerateTestEvent $event) {
          $this->container->get('cache_clearer')->clear();
      }
      
  3. Exclude Controllers

    • Skip tests for specific controllers (e.g., legacy or CLI commands):
      ai_test_bundle:
          excluded_controllers:
              - App\Controller\LegacyController
              - App\Command\*
      

Config Quirks

  • Test Namespace: Defaults to tests/Controller/. Change via:
    ai_test_bundle:
        test_namespace: tests\Functional
    
  • PHPUnit Configuration: The bundle uses phpunit.xml.dist by default. Override with:
    ai_test_bundle:
        phpunit_config: phpunit.custom.xml
    
  • Parallel Testing: Not supported natively. Use PHPUnit’s --parallel flag manually.
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.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony