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

Test Bundle Laravel Package

beelab/test-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle to your composer.json:

    composer require beelab/test-bundle
    

    Register it in config/bundles.php:

    return [
        // ...
        Beelab\TestBundle\BeelabTestBundle::class => ['test' => true],
    ];
    
  2. First Test Case Extend BeelabTestBundle\TestCase instead of Symfony’s WebTestCase:

    use Beelab\TestBundle\TestCase;
    
    class MyTest extends TestCase
    {
        public function testHomepage(): void
        {
            $this->client->request('GET', '/');
            $this->assertResponseStatusCodeSame(200);
        }
    }
    
  3. Key Features to Explore

    • Assertions: Check src/Assertions/ for custom helpers (e.g., assertJsonStructure()).
    • Fixtures: Use loadFixtures() in TestCase for database seeding.
    • Mocking: Leverage createMock() with built-in trait Mockable.

Implementation Patterns

Common Workflows

  1. Database Testing

    public function testUserCreation(): void
    {
        $this->loadFixtures(['UserFixture']);
        $this->client->request('POST', '/api/users', [
            'json' => ['name' => 'Test User']
        ]);
        $this->assertDatabaseHas('users', ['name' => 'Test User']);
    }
    
  2. API Response Validation

    public function testApiResponse(): void
    {
        $this->client->request('GET', '/api/data');
        $this->assertJsonStructure([
            'data' => [
                '*' => [
                    'id',
                    'name'
                ]
            ]
        ]);
    }
    
  3. Mocking Services

    use Beelab\TestBundle\Traits\Mockable;
    
    class MyTest extends TestCase
    {
        use Mockable;
    
        public function testMockedService(): void
        {
            $mock = $this->createMock(MyService::class);
            $mock->method('doSomething')->willReturn('mocked');
            $this->container->set(MyService::class, $mock);
        }
    }
    

Integration Tips

  • Kernel Bootstrapping: Override getKernel() in TestCase for custom environments:
    protected function getKernel(): KernelInterface
    {
        return new Kernel('test', true);
    }
    
  • Event Dispatching: Use dispatchEvent() for testing event listeners:
    $this->dispatchEvent(new UserRegisteredEvent($user));
    $this->assertEventDispatched(UserRegisteredEvent::class);
    
  • Caching: Clear cache in setUp() if needed:
    $this->container->get('cache')->clear();
    

Gotchas and Tips

Pitfalls

  1. Fixture Loading

    • Ensure fixtures are in tests/Fixtures/ and follow the FixtureInterface.
    • Error: FixtureNotFoundException → Verify namespace/class names.
  2. Assertion Overrides

    • Custom assertions may conflict with Symfony’s. Prefix them (e.g., assertJsonStructure() vs. assertJson()).
  3. Container Mocking

    • Avoid mocking services that rely on the container itself (e.g., ContainerAware traits).

Debugging

  • Enable Debug Mode: Set APP_ENV=test and APP_DEBUG=1 in .env.test.
  • Log Output: Use dump() from symfony/var-dumper for complex objects:
    $this->dump($this->client->getResponse()->getContent());
    

Extension Points

  1. Custom Assertions Add assertions to src/Assertions/ and autoload them in composer.json:

    "autoload": {
        "psr-4": {
            "Beelab\\TestBundle\\Assertions\\": "src/Assertions/"
        }
    }
    
  2. Hooks Override setUp()/tearDown() in TestCase for pre/post-test logic:

    protected function setUp(): void
    {
        parent::setUp();
        $this->enableProfiler();
    }
    
  3. Configuration Override bundle config in config/packages/test.yaml:

    beelab_test:
        default_locale: 'en'
        fixtures_dir: '%kernel.project_dir%/tests/Fixtures/Custom'
    
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.
calliostro/spotify-bundle
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle