Giter Site home page Giter Site logo

twizanex / munit Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 380 KB

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.

License: MIT License

PHP 100.00%

munit's Introduction

MUnit

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

munit's People

Contributors

majorcaiger avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.