承接 mooti/factory 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mooti/factory

最新稳定版本:1.0.0

Composer 安装命令:

composer require --dev mooti/factory

包简介

Small library for helping create testable code

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

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.

Installation

You can install this through packagist:

$ composer require mooti/factory

Run the tests

If you would like to run the tests. Use the following:

$ ./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.

<?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.

<?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)

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

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

and we run it, we should see:

$ php bin/run.php
$ Hello Ken Lalobo

Now for tests. There are two ways in which we can write our tests:

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.

<?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.

<?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.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2016-04-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固