2017 © Pedro Peláez
 

library factory

Small library for helping create testable code

image

mooti/factory

Small library for helping create testable code

  • Thursday, May 12, 2016
  • by lalobo
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,633 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 6 % Grown

The README.md

Mooti Factory

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

A small repo to aid in creating simple clean factory code without the need to use a dependancy injection container or create numerous factory classes. It replaces the new keyword with a method call enabling you to easily mock objects., (*2)

Installation

You can install this through packagist:, (*3)

$ composer require mooti/factory

Run the tests

If you would like to run the tests. Use the following:, (*4)

$ ./vendor/bin/phpunit -c config/phpunit.xml

Usage

Say you have a class Foo that will be used within another class Bar. Given you have the following Foo.php., (*5)

<?php

namespace My;

class Foo
{
    private $firstName;
    private $lastName;

    public function __construct($firstName, $lastName) {
        $this->firstName = $firstName;
        $this->lastName  = $lastName;
    }

    public function hello()
    {
        return 'hello '.$this->firstName. ' ' . $this->lastName;
    }
}

You will create Bar.php. You can then add the Factory trait and use the createNew method to instantiate a new object. The first argument is the name of the class and subsequent arguments are the classes constructor arguments., (*6)

<?php

namespace Your;

use Mooti\Factory\Factory;
use My\Foo;

class Bar
{
    use Factory;

    public function speak($firstName, $lastName)
    {
        $foo = $this->createNew(Foo::class, $firstName, $lastName);
        return $foo->hello();
    }
}

So if you have the following script called run.php in you bin directory (assuming you are using composer's autoload), (*7)

<?php
require_once('../vendor/autoload.php');

$bar = new \Your\Bar();
echo $bar->speak('Ken', 'Lalobo');

and we run it, we should see:, (*8)

$ php bin/run.php
$ Hello Ken Lalobo

Now for tests. There are two ways in which we can write our tests:, (*9)

Partial Mock

We can now create a partial mock of Bar and override the createNew method to return a mocked version of the Foo class. We can then set our expectations as normal., (*10)

<?php
require_once('../vendor/autoload.php');

use My\Foo;
use Your\Bar;

class BarTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    public function speakSucceeds()
    {
        $firstName = 'Ken';
        $lastName  = 'Lalobo';
        $greeting  = 'Hello Ken Lalobo';

        $foo = $this->getMockBuilder(Foo::class)
            ->disableOriginalConstructor()
            ->getMock();

        $foo->expects(self::once())
            ->method('hello')
            ->will(self::returnValue($greeting));

        $bar = $this->getMockBuilder(Bar::class)
            ->disableOriginalConstructor()
            ->setMethods(['createNew'])
            ->getMock();

        $bar->expects(self::once())
            ->method('createNew')
            ->with(
                self::equalTo(Foo::class),
                self::equalTo($firstName),
                self::equalTo($lastName)
            )
            ->will(self::returnValue($foo));

        self::assertSame($greeting, $bar->speak($firstName, $lastName));
    }
}

Injection

Alternativley we can inject the mock of Foo into Bar using the addInstance method. When you call createNew it will return the mocked version of the Foo class. We can then set our expectations as normal., (*11)

<?php
require_once('../vendor/autoload.php');

use My\Foo;
use Your\Bar;

class BarTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    public function speakSucceeds()
    {
        $firstName = 'Ken';
        $lastName  = 'Lalobo';
        $greeting  = 'Hello Ken Lalobo';

        $foo = $this->getMockBuilder(Foo::class)
            ->disableOriginalConstructor()
            ->getMock();

        $foo->expects(self::once())
            ->method('hello')
            ->will(self::returnValue($greeting));

        $bar = new Bar;

        $bar->addInstance(Foo::class, $foo);

        self::assertSame($greeting, $bar->speak($firstName, $lastName));
    }
}

If you need multiple objects of the same class you can still add them and set their individual expectations. They will be returned back in the order they weere added, (*12)

The Versions

12/05 2016

dev-master

9999999-dev

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

12/05 2016

0.0.6

0.0.6.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

12/05 2016

1.0.0

1.0.0.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

11/05 2016

0.0.5

0.0.5.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

04/05 2016

0.0.4

0.0.4.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

27/04 2016

0.0.3

0.0.3.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

20/04 2016

0.0.2

0.0.2.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable

20/04 2016

0.0.1

0.0.1.0

Small library for helping create testable code

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

mock testing factory testable