2017 © Pedro Peláez
 

library mockingbird

PHP Mocking Utilities

image

chromabits/mockingbird

PHP Mocking Utilities

  • Thursday, February 23, 2017
  • by etcinit
  • Repository
  • 0 Watchers
  • 0 Stars
  • 81 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 16 % Grown

The README.md

mockingbird

A DSL for mocking dependencies on PHP unit tests, (*1)

Features:

  • Acts like a mini IoC container for your unit tests.
  • Automatically creates mocks for your class' dependencies.
  • Supports injecting dependencies through the constructor and methods.
  • Provides a DSL on top of Mockery for quickly mocking dependencies.
  • You can provide real/non-mock instances and scalar arguments.
  • Works with Laravel!

Requirements

  • PHP 7 or higher.
  • Composer

Installation

Run composer require chromabits/mockingbird on a Composer project., (*2)

Quick Example:

<?php

include_once __DIR__ . '/../vendor/autoload.php';

// Mockingbird is available as a set of functions, which you can import into
// your current scope.
use function Mockingbird\{ stage, on };

// We begin by defining the classes which will act as our dependencies for this
// example.
class DependencyA {
    private $prefix;

    public function __construct(string $prefix)
    {
        $this->prefix = $prefix;
    }

    public function getPrefix(): string { return $this->prefix; }
};
class DependencyB {};
class DependencyC {
    public function sayWorld(string $postfix): string {
        return 'world' . $postfix;
    }
}

// We also define our service class which will consume these dependencies
// through constructor-based and method-based dependency injection.
class Service {
    /**
     * @var DependencyA
     */
    private $a;

    public function __construct(DependencyA $a, DependencyB $b) {
        $this->a = $a;
    }

    public function targetMethod(DependencyC $c): string
    {
        return $this->a->getPrefix() . 'hello ' . $c->sayWorld('!');
    }
};

// Our Service class has three dependencies, two services injected through the
// constructor and one passed on the called method. We will build a stage that
// provides them for us:
//
// - DependencyA: We will pass down a real instance (not a mock).
// - DependencyB: We will let Stage auto-mock it for us.
// - DependencyC: We will manually create our own mock.
//
$result = stage()
    ->provide(new DependencyA('>>> '))
    ->mock(DependencyC::class, [
        on('sayWorld', ['!'], 'worlds!!!'),
    ])
    ->makeAndCall(Service::class, 'targetMethod');

// Should output ">>> hello worlds!!!"
echo $result;

The Versions

23/02 2017

dev-master

9999999-dev

PHP Mocking Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Trujillo

23/02 2017

v1.0.0

1.0.0.0

PHP Mocking Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Trujillo

19/01 2017

dev-etcinit/scopes

dev-etcinit/scopes

PHP Mocking Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Trujillo