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

belsym/test-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require belsym/test-bundle
    

    Add the bundle to config/bundles.php (Symfony only):

    return [
        // ...
        Belsym\TestBundle\BelsymTestBundle::class => ['all' => true],
    ];
    
  2. First Use Case Extend MockedUpTestCase for basic mocking:

    use Belsym\TestBundle\TestCase\MockedUpTestCase;
    
    class MyTest extends MockedUpTestCase
    {
        public function testMockExample()
        {
            $mock = $this->mock('MyService');
            $mock->shouldReceive('doSomething')->once();
            // Test logic...
        }
    }
    
  3. Key Files

    • src/TestCase/MockedUpTestCase.php (Base mocking)
    • src/TestCase/MockContainerAwareTestCase.php (Mocked container + Doctrine)
    • src/TestCase/ContainerAwareTestCase.php (Real Symfony container)

Implementation Patterns

Core Workflows

  1. Mocking Services

    // In MockedUpTestCase
    $mock = $this->mock('App\Service\MyService');
    $mock->shouldReceive('process')->withArgs([1, 2])->andReturn(true);
    
  2. Mocking Doctrine EntityManager

    // In MockContainerAwareTestCase
    $em = $this->createMockEntityManager();
    $em->expects($this->once())->method('find')->with('User', 1)->willReturn(new User());
    
  3. Real Container Integration (Symfony)

    // In ContainerAwareTestCase
    $service = $this->getContainer()->get('my.service');
    

Integration Tips

  • For Laravel: Use MockedUpTestCase directly (Symfony-specific classes won’t work).
  • Mocking Facades: Extend MockedUpTestCase and override createApplication() to bind facades:
    protected function createApplication()
    {
        $app = parent::createApplication();
        $app->bind('App\Services\MyService', function () {
            return $this->mock('App\Services\MyService');
        });
    }
    
  • Test Isolation: Prefer MockedUpTestCase for unit tests; reserve ContainerAwareTestCase for integration tests.

Gotchas and Tips

Pitfalls

  1. Symfony Dependency

    • ContainerAwareTestCase only works in Symfony. Throws RuntimeException if AppKernel is missing.
    • Laravel Workaround: Use MockContainerAwareTestCase for container-like behavior (but manually mock ContainerInterface).
  2. Mockery Initialization

    • MockedUpTestCase auto-loads Mockery, but ensure mockery/mockery is installed:
      composer require --dev mockery/mockery
      
    • Conflict Risk: Avoid mixing with Laravel’s native mocking (e.g., createMock()).
  3. Doctrine Mocking Quirks

    • createMockEntityManager() returns a partial mock. Override methods like persist() explicitly:
      $em->expects($this->once())->method('persist')->with($user);
      

Debugging Tips

  • Mockery Errors: Use Mockery::close() in tearDown() to avoid memory leaks:
    protected function tearDown(): void
    {
        Mockery::close();
        parent::tearDown();
    }
    
  • Container Issues: For ContainerAwareTestCase, ensure APP_ENV=test and CACHE_DRIVER=array in .env.

Extension Points

  1. Custom Mock Factories Override createMock() in MockedUpTestCase:
    protected function createMock($originalClassName)
    {
        return Mockery::mock($originalClassName . '[doSomething]');
    }
    
  2. Adding New Test Cases Extend MockedUpTestCase and add helper methods:
    class ApiTestCase extends MockedUpTestCase
    {
        protected function mockHttpClient()
        {
            return $this->mock('GuzzleHttp\Client', [
                'request' => $this->mockResponse(200, ['data' => []])
            ]);
        }
    }
    
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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
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