Giter Site home page Giter Site logo

phpspec-cheat-sheet's Introduction

PHPSpec Cheat Sheet

A cheat sheet for phpspec (>= v2.4.0)

Console

  • Generate a new specification: bin/phpspec desc Full/Class/Name
  • Run the tests: bin/phpspec run

Matchers

Same (===)

$this->getMethodName()->shouldReturn('value');
$this->getMethodName()->shouldBe('value');
$this->getMethodName()->shouldEqual('value');
$this->getMethodName()->shouldBeEqualTo('value');

Equals (==)

$this->getMethodName()->shouldBeLike('value');

Contain (array, string)

$this->getArrayValue()->shouldContain('Jane Smith'); // Element in array (===)
$this->getArrayValue()->shouldHaveKeyWithValue('key', 'value');
$this->getArrayValue()->shouldHaveKey('key');

$this->getStringValue()->shouldContain('value'); // String contain exact value (===)
$this->getStringValue()->shouldStartWith('value');
$this->getStringValue()->shouldEndWith('value');
$this->getStringValue()->shouldMatch('/pattern/'); // Regex

Object type (instanceof)

$this->getMethodName()->shouldHaveType('\Full\Class\Name');
$this->getMethodName()->shouldReturnAnInstanceOf('\Full\Class\Name');
$this->getMethodName()->shouldBeAnInstanceOf('\Full\Class\Name');
$this->getMethodName()->shouldImplement('\Full\Interface\Name');

Bool

For methods named is* or has*.

$this->shouldBeActive(); // isActive() method should return true
$this->shouldHaveSomething(); // hasSomething() method should return true

$this->shouldNotBeActive(); // isActive() method should return false
$this->shouldNotHaveSomething(); // hasSomething() method should return false

Countable

$this->getCountableObject()->shouldHaveCount(1);
$this->getArrayValue()->shouldHaveCount(1);

Exception

$this->shouldThrow('\Exception')->duringMethodName('value');
$this->shouldThrow(new \Exception('Message'))->duringMethodName('value');

$this->shouldThrow('\Exception')->during('methodName', array('value'));
$this->shouldThrow(new \Exception('Message'))->during('methodName', array('value'));

$this->shouldThrow('\Exception')->duringInstantiation();
$this->shouldThrow(new \Exception('Message'))->duringInstantiation();

Scalar

$this->getBool()->shouldBeBool();
$this->getObject()->shouldBeObject();
$this->getString()->shouldBeString();
$this->getInteger()->shouldBeInteger();
$this->getDecimal()->shouldBeDecimal();
$this->getCollection()->shouldBeArray();

Object construct

$this->beConstructedWith(-3);
$this->beConstructedThrough('methodName', array(-3));

Custom matcher

Method should return a value that match the custom matcher.

function it_should_have_some_specific_options_by_default()
{
    $this->getOptions()->shouldHaveKey('username');
    $this->getOptions()->shouldHaveValue('diegoholiveira');
}

public function getMatchers()
{
    return [
        'haveKey' => function($subject, $key) {
            return array_key_exists($key, $subject);
        },
        'haveValue' => function($subject, $value) {
            return in_array($value, $subject);
        },
    ];
}

Stubs

Collaborator (Composed object) should call a method that could return a value.

/**
 * @param Full\Class\Name $stub
 */
function it_adds_a_end_of_list_to_markup($stub)
{
    $stub->getExpectedMethod()->willReturn("Some value");
    $this->testMethod("value", $stub)->shouldReturn("other value");
}

Mocks

Expecting a method to be called on an object.

/**
 * @param SomeMock $expectedMock
 * @param SomeStub $expectedStub
 */
function it_should_call_a_method_on_subscriber($expectedMock, $expectedStub)
{
    $expectedMock->methodName($expectedStub)->shouldBeCalled();

    // when
    $this->addStub($expectedStub);
    $this->doMethod($expectedMock);
}

let/letgo

Construct expectations before and after each tests.

    function let($object)
    {
        $object->beADoubleOf('Full\Class\Name');
        $this->beConstructedWith($object);
    }

    function it_live_and_let_die($object)
    {
        $this->getSomeObject()->shouldReturn($object);
    }

    function letgo()
    {
        // release any resource
        // put the system back into the state it was before the example
    }

phpspec-cheat-sheet's People

Contributors

fabiensalles avatar marktinsley avatar yvoyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpspec-cheat-sheet's Issues

Typo: comparaison

There's a small typo. I'd like to proof read for you and make a PR with any spelling fixes I find. Nice resource, thanks.

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.