Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Created November 27, 2023 20:23
Show Gist options
  • Save medeirosinacio/1292e2acbbddd50072f9eba57fff46eb to your computer and use it in GitHub Desktop.
Save medeirosinacio/1292e2acbbddd50072f9eba57fff46eb to your computer and use it in GitHub Desktop.
MockeryExpectations Trait

Overview

The MockeryExpectations trait provides a convenient method to assert Mockery expectations in PHPUnit tests. It is particularly useful when working with Mockery for mocking and stubbing objects.

Usage

To use the MockeryExpectations trait in your PHPUnit test class, follow these steps:

  1. Include the trait in your test class:
use Tests\Util\MockeryExpectations;

class YourTestClass extends \PHPUnit\Framework\TestCase
{
    use MockeryExpectations;

    // ... your test methods
}
  1. Call the assertMockeryExpectations() method in your test class after you have finished your assertions:
public function testYourFeature(): void
{
    // ... your test logic and assertions

    // Call the assertMockeryExpectations() method to ensure Mockery expectations are met
    $this->assertMockeryExpectations();
}

Notes

  • The trait includes a method named assertMockeryExpectations, which calls Mockery's getExpectationCount method and asserts it using PHPUnit's addToAssertionCount.
  • This trait is particularly handy when using Mockery in PHPUnit tests.
<?php
declare(strict_types=1);
namespace Tests\Common;
use Mockery;
/**
* @method void addToAssertionCount(int $count)
*
* @see \PHPUnit\Framework\TestCase::addToAssertionCount
*/
trait MockeryExpectations
{
protected function assertMockeryExpectations(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment