major-caiger / munit
There is no license information available for the latest version (0.1.7) of this package.
MUnit testing framework
This package's canonical repository appears to be gone and the package has been frozen as a result.
0.1.7
2015-01-12 13:38 UTC
Requires
- php: >=5.3.3
- mockery/mockery: ~0.9.0
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2021-09-04 00:32:30 UTC
README
MUnit is a small library that wraps PHPUnit. The library allows you to write "Jasmine" style tests for your php applications, while still giving you the power of PHPUnit and Mockery. It contains 2 abstract test cases, 1 that extends PHPUnit_Framework_TestCase and 1 that extends MockeryTestCase.
Installation
Via composer
"require": {
"major-caiger/munit": "~0.1.0"
}
Sample class
<?php
namespace MUnit\Test\Resource;
class Sample
{
public function sampleMethod($object)
{
$object->doSomething();
if ($object->checkSomething()) {
$object->doSomethingPositive();
} else {
$object->doSomethingNegative();
}
}
}
Sample test case (Using the PHPUnit adapter)
<?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);
});
});
});
}
}
Sample test case (Using the Mockery adapter)
<?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);
});
});
});
}
}
Things to remember
- When, declaring a setUp and/or tearDown method in the test case, these methods will only be called for each test method, rather than each "describe" or "it" call.
Known issues
- At the moment you need to declare the beforeEach and afterEach callbacks before any further nested calls to describe
Support
- PHP 5.4+
To do
- Stack describe calls