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

Mockista Laravel Package

janmarek/mockista

Mockista is a lightweight mocking library for PHP/Laravel that helps you create and configure test doubles quickly. Define expectations, stub methods, and verify calls with a simple, fluent API to keep unit tests fast, readable, and maintainable.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Mocking Paradigm: Mockista’s fluent, declarative API aligns with modern PHP practices (PHP 8.1+) and reduces cognitive overhead compared to PHPUnit’s getMockBuilder() verbosity. Its type-safe expectations (e.g., when()->thenReturn()) improve test reliability by enforcing contracts, which is critical for TDD/BDD workflows in Laravel applications.
  • Laravel Synergy: While not natively integrated with Laravel’s ecosystem (e.g., Eloquent, Service Container), Mockista’s interface/class mocking complements Laravel’s dependency injection and repository patterns. It excels in unit testing services, DTOs, and API clients where interfaces are well-defined.
  • Test Isolation: The package enforces strict mocking boundaries, reducing unintended side effects in tests. However, its lack of support for static methods, magic methods, or Laravel-specific traits (e.g., HasFactory) may require custom adapters for monolithic apps.
  • Performance: Minimal runtime impact, but no benchmarks exist for Laravel-specific use cases (e.g., Eloquent queries, queue jobs). Reflection-based mocking (like PHPUnit’s) may still outperform Mockista in micro-optimized scenarios.

Integration Feasibility

  • Laravel Ecosystem:
    • Pros:
      • Seamless integration with PHPUnit 9.5+ and PestPHP (via PHPUnit).
      • Supports modern PHP features (e.g., named arguments, union types) that Laravel leverages.
      • No framework lock-in: Works with any PHP project, not just Laravel.
    • Cons:
      • No native Laravel helpers: Requires manual migration from createMock(), Mockery, or partialMock().
      • Eloquent/Database Testing: Limited utility; prefer Laravel’s DatabaseMigrations or RefreshDatabase traits.
      • Service Container: Mockista doesn’t integrate with Laravel’s bindWhen() or AppServiceProvider mocking.
  • Testing Layers:
    • Unit Tests: Ideal for services, repositories, and API clients with clear interfaces.
    • Integration Tests: Poor fit (use Laravel’s test helpers or Http::fake()).
    • Feature Tests: Not recommended (use Laravel’s built-in test doubles).

Technical Risk

  • API Instability: Mockista is pre-1.0 (as of 2023), risking breaking changes in method signatures (e.g., when()->then() vs. expects()->willReturn()).
  • Debugging Challenges:
    • Dynamic proxies may obscure stack traces in complex mock interactions.
    • No IDE support: Lack of autocompletion or refactoring tools compared to Mockery.
  • Edge Cases:
    • Static methods or magic methods (__call) require manual workarounds.
    • Closure-based expectations (e.g., thenCallback()) may conflict with Laravel’s event system or queues.
  • Maintenance Risk: Low activity (29 stars, 0 dependents) raises concerns about long-term viability.

Key Questions

  1. Adoption Justification:
    • Does the team’s pain point (e.g., PHPUnit mock verbosity, legacy code) outweigh the risks of migration?
    • Is there a specific use case (e.g., API testing, TDD) where Mockista provides measurable benefits?
  2. Laravel-Specific Gaps:
    • Can Mockista be extended to support Eloquent models or Service Container bindings?
    • Are there plans to add Laravel-specific adapters (e.g., Mockista\Eloquent)?
  3. Performance:
    • Has Mockista been benchmarked against Mockery or PHPUnit’s getMockBuilder() in Laravel apps?
  4. Long-Term Viability:
    • Is the maintainer (@janmarek) actively developing it, or is it a niche solution?
    • Are there alternatives (e.g., Mockery, Laravel’s createMock()) with better Laravel integration?

Integration Approach

Stack Fit

  • Best For:
    • Greenfield Laravel projects using PHP 8.1+ and PHPUnit/PestPHP.
    • Teams prioritizing clean, maintainable mocks over PHPUnit’s verbosity.
    • Microservices/API layers with well-defined interfaces (e.g., repositories, DTOs).
  • Poor Fit:
    • Legacy Laravel apps heavily using Mockery or createMock().
    • Database-heavy tests (use Laravel’s RefreshDatabase or DatabaseTransactions).
    • Teams relying on IDE-generated mocks (e.g., PHPStorm’s MockBuilder).

Migration Path

  1. Pilot Phase:
    • Replace 1–2 complex mocks in unit tests (e.g., a UserRepository interface).
    • Compare test readability, maintenance effort, and CI/CD impact vs. PHPUnit/Mockery.
  2. Incremental Adoption:
    • Step 1: Use Mockista for new unit tests only.
    • Step 2: Gradually migrate existing mocks to Mockista’s syntax.
    • Step 3: Build custom adapters for Laravel-specific classes (e.g., Mockista\Eloquent).
  3. Tooling Setup:
    • Configure PHPUnit to autoload Mockista:
      <!-- phpunit.xml -->
      <php>
          <autoload>
              <classmap prefix="Mockista"/>
          </autoload>
      </php>
      
    • For PestPHP, extend the test case:
      use Mockista\Mockista;
      
      beforeEach(function () {
          $this->mockista = new Mockista();
      });
      

Compatibility

Feature Mockista Support Laravel Workaround
Interface Mocking ✅ Yes None needed
Class Mocking ✅ Yes Use Mockista::partialMock()
Static Methods ❌ No Manual __callStatic stubbing
Eloquent Models ❌ No Create a mock interface + adapter
Service Container ❌ No Use Laravel’s createMock() for bindings
PestPHP Integration ⚠️ Partial Custom traits/helpers
Mockery Interop ❌ No None (incompatible APIs)

Sequencing

  1. Phase 1: Unit Tests (High Priority)
    • Replace getMockBuilder() with Mockista’s Mockista::mock().
    • Example:
      // Before (PHPUnit)
      $mock = $this->getMockBuilder(UserRepository::class)
          ->disableOriginalConstructor()
          ->onlyMethods(['find'])
          ->getMock();
      $mock->method('find')->willReturn($user);
      
      // After (Mockista)
      $mock = Mockista::mock(UserRepository::class)
          ->when('find', $userId)->thenReturn($user);
      
  2. Phase 2: Service Layer (Medium Priority)
    • Mock DTOs, commands, or event handlers with Mockista.
  3. Phase 3: Laravel-Specific (Low Priority)
    • Build custom mocks for Illuminate\Contracts or Eloquent.
    • Example:
      // Custom Eloquent mock adapter
      $mock = Mockista::mock(User::class)
          ->partialMock()
          ->when('findOrFail', 1)->thenThrow(new ModelNotFoundException());
      

Operational Impact

Maintenance

  • Pros:
    • Reduced boilerplate → Easier to maintain complex mocks.
    • Declarative syntax → Lower cognitive load for new developers.
    • No external dependencies → No Composer conflicts or versioning issues.
  • Cons:
    • No official Laravel documentation → Self-service troubleshooting required.
    • Limited community support (29 stars, 0 dependents) → Risk of unanswered questions.
    • Potential for API drift if the maintainer abandons the project.

Support

  • Debugging:
    • Stack traces may be harder to follow due to dynamic proxies.
    • No built-in assertion helpers (e.g., verify() like Mockery).
    • Workaround: Use PHPUnit’s assertObjectHasAttribute() or assertMethodCalled().
  • Onboarding:
    • Steep learning curve for teams unfamiliar with Mockista’s syntax.
    • Recommendation: Create an internal style guide with examples and migration tips.
  • Tooling Gaps:
    • No VSCode/PHPStorm plugins → Manual mock generation.
    • CI/CD: Add a test suite to validate Mockista’s output (e.g.,
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
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