此包的最新版本(0.1.7)没有可用的许可信息。

MUnit 测试框架

0.1.7 2015-01-12 13:38 UTC

This package is not auto-updated.

Last update: 2021-09-04 00:32:30 UTC


README

MUnit 是一个包装 PHPUnit 的小型库。该库允许您为您的 PHP 应用程序编写“Jasmine”风格的测试,同时仍然提供 PHPUnit 和 Mockery 的功能。它包含 2 个抽象测试用例,1 个扩展 PHPUnit_Framework_TestCase 的,1 个扩展 MockeryTestCase 的。

安装

通过 composer

"require": {
    "major-caiger/munit": "~0.1.0"
}

示例类

<?php

namespace MUnit\Test\Resource;

class Sample
{
    public function sampleMethod($object)
    {
        $object->doSomething();

        if ($object->checkSomething()) {
            $object->doSomethingPositive();
        } else {
            $object->doSomethingNegative();
        }
    }
}

示例测试用例(使用 PHPUnit 适配器)

<?php

namespace MUnit\Test\Adapter\PhpUnit;

use MUnit\Adapter\PhpUnit\TestCase;

class TestCaseTest extends TestCase
{
    public function testSomeMethod()
    {
        $this->describe('someMethod', function() {
            $this->beforeEach(function() {
                $this->sample = new \MUnit\Test\Resource\Sample();
                $this->mockObject = $this->getMock(
                    '\stdClass',
                    array(
                        'doSomething',
                        'checkSomething',
                        'doSomethingPositive',
                        'doSomethingNegative'
                    )
                );
                $this->mockObject->expects($this->once())
                    ->method('doSomething');
            });

            $this->afterEach(function() {
                unset($this->sample);
                unset($this->mockObject);
            });

            $this->describe('when given a positive mock object', function() {
                $this->beforeEach(function() {
                    $this->mockObject->expects($this->once())
                        ->method('checkSomething')
                        ->willReturn(true);
                    $this->mockObject->expects($this->once())
                        ->method('doSomethingPositive');
                });

                $this->it('will doSomethingPositive with the mock object', function() {
                    $this->sample->sampleMethod($this->mockObject);
                });
            });

            $this->describe('when given a negative mock object', function() {
                $this->beforeEach(function() {
                    $this->mockObject->expects($this->once())
                        ->method('checkSomething')
                        ->willReturn(false);
                    $this->mockObject->expects($this->once())
                        ->method('doSomethingNegative');
                });

                $this->it('will doSomethingNegative with the mock object', function() {
                    $this->sample->sampleMethod($this->mockObject);
                });
            });
        });
    }
}

示例测试用例(使用 Mockery 适配器)

<?php

namespace MUnit\Test\Adapter\Mockery;

use Mockery as m;
use MUnit\Adapter\Mockery\TestCase;

class TestCaseTest extends TestCase
{
    public function testSomeMethod()
    {
        $this->describe('someMethod', function() {
            $this->beforeEach(function() {
                $this->sample = new \MUnit\Test\Resource\Sample();
                $this->mockObject = m::mock();
                $this->mockObject->shouldReceive('doSomething')->once();
            });

            $this->afterEach(function() {
                unset($this->sample);
                unset($this->mockObject);
            });

            $this->describe('when given a positive mock object', function() {
                $this->beforeEach(function() {
                    $this->mockObject->shouldReceive('checkSomething')->once()
                        ->andReturn(true);
                    $this->mockObject->shouldReceive('doSomethingPositive');
                });

                $this->it('will doSomethingPositive with the mock object', function() {
                    $this->sample->sampleMethod($this->mockObject);
                });
            });

            $this->describe('when given a negative mock object', function() {
                $this->beforeEach(function() {
                    $this->mockObject->shouldReceive('checkSomething')->once()
                        ->andReturn(false);
                    $this->mockObject->shouldReceive('doSomethingNegative');
                });

                $this->it('will doSomethingNegative with the mock object', function() {
                    $this->sample->sampleMethod($this->mockObject);
                });
            });
        });
    }
}

注意事项

  • 当在测试用例中声明 setUp 和/或 tearDown 方法时,这些方法将仅对每个测试方法调用,而不是对每个 "describe" 或 "it" 调用。

已知问题

  • 目前您需要在嵌套调用 describe 之前声明 beforeEachafterEach 回调。

支持

  • PHP 5.4+

待办事项

  • 堆叠 describe 调用