Coder Social home page Coder Social logo

speciphy's Introduction

speciphy

Simple context/specification style testing for PHP, inspired by mspec and the wonderful contexts

Does it work yet?

Yes! Packages are coming soon, but right now you can build your own phar using box.

What does it do?

Speciphy gives you simple given/when/then style testing in PHP. It uses Hamcrest-php for matching, so that you get useful error messages.

How do I use it?

class When_I_write_a_test_class_starting_with_when
{
    function given_some_setup_code()
    {
       $this-> calculator = new Calculator(1);
    }
    
    function when_the_action_is_run()
    {
       $this->calculator->add(2);
    }
    
    function it_will_execute_assertions()
    {
        expect($this->calculator->result, 
          is(equalTo(3)));
    }
}

How does it know what my methods are intended to do?

Speciphy uses a simple keyword-based approach to finding contexts and assertions, with annotations for those hard-to-reach areas:

  • A class starting with the word "when" (or annotated with @context) is a test.
  • A function starting with the word "given" (or annotated with @arrange) is a setup method.
  • A function containing the word "when", or "because" (or annotated @act) is an action method.
  • A function containing the word "should", or "will" (or annotated @assert) is an assertion.

Can I change the matching?

You can change the behaviour of the runner by replacing the rules it uses for matching tests. You can introduce your own assertion matchers by implementing a class that's compatible with Hamcrest's BaseMatcher

StackTest.php example

class When_adding_an_element_to_an_empty_stack
{
    function given_an_empty_stack()
    {
        $this->stack = new Stack();
    }

    function because_we_push_an_item()
    {
        $this->stack.Push("foo");
    }

    function it_should_have_length_1()
    {
        expect($this->stack.length, equal_to(1));
    }

    function it_should_have_the_item_as_its_topmost_element()
    {
        expect($this->stack.Peek(), equal_to("foo"));
    }
}


class When_popping_an_empty_stack
{

    function given_an_empty_stack()
    {
        $this->stack = new Stack();
    }

    function popping_the_stack_should_throw
    {
        expect(
            function(){
                $this->stack.Pop()
            },
           throws()    
        );
    }
}

class When_popping_from_a_non_empty_stack
{
    function given_a_non_empty_stack()
    {
        $this->stack = new Stack(["foo"]);
    }

    function because_we_pop_the_item()
    {
        $this->result = $this->stack.Pop();
    }

    function it_should_return_the_popped_item()
    {
        expect($this->result, equal_to("foo"));
    }

    function it_should_decrement_the_length()
    {
        expect($this->stack->length, equal_to(0));
    }
}

speciphy's People

Contributors

bobthemighty avatar

Stargazers

Gil Gonçalves avatar Benjamin Hodgson avatar

Watchers

 avatar James Cloos 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.