Introduction to PHPSpec

Start moving from TDD to BDD

Agenda

  • What’s PHPSpec?
  • What offers me PHPSpec?
  • Matchers
  • Special Methods
  • Prophecy
  • Templates
  • References

What’s PHPSpec

  • BDD Framework
  • DSL based on RSpec
  • Created by Pádraic Brady

What offers me PHPSpec?

  • Code generation
  • $this
  • Natural language matchers
  • Mocks/Stubs

Matchers

method()->should…(Be, Return, Equal)

method()->shouldNot…(Be, Return, Equal)

Matchers

      
        class HelloWorldSpec extends ObjectBehavior
        {
          public function it_should_say_hello()
          {
            $this->helloWorld()->shouldBeEqualTo('Hello World!');
          }
        }
      
    

Matchers: Identity

  • shouldReturn
  • shouldBe
  • shouldBeEqualTo
  • shouldEqual

Matchers: Comparison

  • shouldBeLike

Matchers: Exceptions

  • shouldThrow()->during('method_name')
  • shouldThrow()->duringMethodName()
  • fail()

Matchers: Type

  • shouldBeAnInstanceOf
  • haveType
  • returnAnInstanceOf

Matchers: Count

  • shouldHaveCount

Matchers: Scalar

  • shouldBeString
  • shouldBeArray
  • shouldBeBoolean
  • shouldBeInteger
  • shouldBeDecimal

Matchers: Object State

      
        namespace Spec;

        use PhPSpec\ObjectBehavior;

        class ObjectStateSpec extends ObjectBehavior
        {
          public function it_should_has_a_valid_state()
          {
            $this->shouldBeAValidState();
          }
        }
      
    
      
        class ObjectState
        {
          public function isAValidState()
          {
            return true;
          }
        }
      
    

Matchers: Custom Matchers

      
        class CustomMatcherSpec extends ObjectBehavior
        {
          public function it_should_say_hello()
          {
            $this->helloWorld()->shouldSayHello();
          }

          public function getMatchers()
          {
            return array(
              'sayHello'  => function($actual) {
                return $actual == 'Hello World!';
              }
            );
          }
        }
      
    

More methods...

  • let
    • beConstructedWith
  • letGo
  • it

Stubs

      
        class HelloWorldStubsSpec extends ObjectBehavior
        {
          public function it_should_say_hello(Greeter $greeter)
          {
            $greeter->sayHello()->willReturn('');

            $this->helloWorld($greeter)->shouldSayHello();
          }
        }
      
    

Mocks

      
        class HelloWorldMockSpec extends ObjectBehavior
        {
          public function it_should_say_hello(Greeter $greeter)
          {
            $greeter->sayHello()->shouldBeCalled();

            $this->helloWorld($greeter)->shouldSayHello();
          }
        }
      
    

Templates

  • Location:
    • {project}/.phpspec/{template}.tpl
    • {home}/.phpspec/{template}.tpl
  • Types:
    • specification
    • class
    • method

Templates: Variables

  • specification:
    • %filepath%
    • %name%
    • %namespace%
    • %subject%
  • class:
    • %filepath%
    • %name%
    • %namespace%
    • %namespace_block%
  • method:
    • %name%
    • %arguments%

Reference

www.phpspec.net

code.tutsplus.com/tutorials/getting-started-with-phpspec--cms-20919