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

Bridge Mockery Laravel Package

testo/bridge-mockery

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require --dev testo/bridge-mockery
    

    Ensure testo/testo is also installed (core dependency).

  2. First Use Case: Mock a class in a Testo test file:

    use Testo\Bridge\Mockery\Mockery;
    use Testo\Test;
    
    class MyTest extends Test
    {
        public function testSomething()
        {
            $mock = Mockery::mock('MyClass');
            $mock->shouldReceive('methodName')->once()->andReturn('foo');
    
            $result = $mock->methodName();
            $this->assertEquals('foo', $result);
        }
    }
    
  3. Key Files:


Implementation Patterns

Workflow Integration

  1. Mocking Dependencies:

    // In a Testo test
    $mock = Mockery::mock('MyDependency');
    $mock->shouldReceive('fetchData')->andReturn(['data']);
    $service = new MyService($mock);
    
  2. Partial Mocking:

    $partialMock = Mockery::mock('MyClass[methodToMock]');
    $partialMock->methodToMock()->andReturn('partial');
    
  3. Testo Assertions with Mockery:

    $mock = Mockery::mock('MyClass');
    $mock->shouldReceive('save')->once();
    $this->assertTrue($mock->save()); // Testo assertion
    
  4. Mockery Verification:

    $mock->shouldHaveReceived('save')->once();
    $this->assertTrue($mock->save()); // Optional: Combine with Testo assertions
    

Common Patterns

  • Service Layer Testing: Mock external APIs, databases, or other services to isolate unit tests.

    $apiMock = Mockery::mock('GuzzleHttp\Client');
    $apiMock->shouldReceive('request')->andReturn(new Response(200, [], '{}'));
    
  • Event Listeners/Observers:

    $listener = Mockery::mock('App\Listeners\SendEmail');
    $listener->shouldReceive('handle')->once();
    
  • Laravel-Specific: If using Laravel, pair with laravel-bridge (if available) or manually mock Facades:

    $mock = Mockery::mock('Illuminate\Support\Facades\Cache');
    $mock->shouldReceive('get')->andReturn('cached_value');
    

Gotchas and Tips

Pitfalls

  1. Mockery vs. Testo Assertions:

    • Avoid mixing Mockery::mock() with Testo’s native mocking (if any). Stick to one.
    • Example of conflict:
      // ❌ Avoid: Mixing frameworks
      $this->mock('MyClass'); // Testo syntax (if exists)
      Mockery::mock('MyClass'); // Bridge syntax
      
  2. Autoloading Issues:

    • Ensure composer dump-autoload runs after installation.
    • If using namespaces, verify the bridge is autoloaded:
      composer dump-autoload --optimize
      
  3. Static Method Mocking:

    • Mockery’s static mocking works, but Testo may not handle static assertions natively.
    $mock = Mockery::mock('MyClass')->makePartial();
    $mock->shouldReceive('staticMethod')->andReturn('static');
    
  4. Lifetime Management:

    • Mockery mocks do not persist between tests. Recreate them in each test method.
    • Example of leaking mocks:
      // ❌ Bad: Mock persists across tests
      $this->onceBeforeAll(function() {
          $this->mock = Mockery::mock('MyClass');
      });
      

Debugging Tips

  1. Verify Mockery is Loaded:

    $this->assertTrue(class_exists(Mockery::class), 'Mockery bridge not loaded');
    
  2. Check for Double Mocking:

    • If a mock behaves unexpectedly, ensure it’s not being redefined:
      $mock = Mockery::mock('MyClass');
      $mock->shouldReceive('method')->andReturn('value');
      // Later in the same test:
      $mock->method(); // Should return 'value'
      
  3. Testo-Specific Quirks:

    • If Testo has a setUp()/tearDown(), avoid mocking in setUp() (see Pitfall #4).
    • Use Mockery::close() in tearDown() to clean up:
      protected function tearDown(): void
      {
          Mockery::close();
          parent::tearDown();
      }
      

Extension Points

  1. Custom Matchers:

    • Extend Mockery’s matchers for domain-specific logic:
      $mock->shouldReceive('validate')
           ->with(Mockery::on(function($input) {
               return is_array($input) && isset($input['id']);
           }))
           ->andReturn(true);
      
  2. Mockery Configuration:

    • Override Mockery’s global settings (e.g., strict mode) in a test trait:
      use Mockery\Mockery as M;
      
      trait MockeryConfig
      {
          public function setUp(): void
          {
              M::getConfiguration()->setStrictMode(true);
              parent::setUp();
          }
      }
      
  3. Integration with Testo Plugins:

    • If Testo supports plugins, check for compatibility:
      // Example: Hypothetical Testo-Mockery plugin
      $this->plugin('testo/mockery')->enable();
      
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