Top Banner
Illustrated by examples 2.0.0-BETA1 http://phpspec.net http://github.com/phpspec/phpspec Marcello Duarte | @_md Konstantin Kudryashov | @everzet
33

PhpSpec 2.0 ilustrated by examples

May 10, 2015

Download

Technology

Marcello Duarte

An quick introduction to those wanting to get started with phpspec
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PhpSpec 2.0 ilustrated by examples

Illustrated by examples2.0.0-BETA1

http://phpspec.net

http://github.com/phpspec/phpspecMarcello Duarte | @_md

Konstantin Kudryashov | @everzet

Page 2: PhpSpec 2.0 ilustrated by examples

generate or edit specs

run the specs

edit code generate code

Page 3: PhpSpec 2.0 ilustrated by examples

Installation

Create a composer.json file:

Page 4: PhpSpec 2.0 ilustrated by examples

Installation

Get composer, if you don’t have it already

Page 5: PhpSpec 2.0 ilustrated by examples

Installation

Install

Page 6: PhpSpec 2.0 ilustrated by examples

Generate Specs

$

Page 7: PhpSpec 2.0 ilustrated by examples

Generated Specs

Page 8: PhpSpec 2.0 ilustrated by examples

Edit the Spec

Page 9: PhpSpec 2.0 ilustrated by examples

Run Specs

$

Page 10: PhpSpec 2.0 ilustrated by examples

Generate Code

Page 11: PhpSpec 2.0 ilustrated by examples

Generated Code

Page 12: PhpSpec 2.0 ilustrated by examples

Edit Code

Page 13: PhpSpec 2.0 ilustrated by examples

Run Specs

$

Page 14: PhpSpec 2.0 ilustrated by examples

:)

Page 15: PhpSpec 2.0 ilustrated by examples

Matchers

object expectation matcher

$result ->should->shouldNot->should

->shouldNot Be...()Be...()$result

Page 16: PhpSpec 2.0 ilustrated by examples

Types of Matchers

Identity

Comparison

Throw

Type

Object State

Inline

Page 17: PhpSpec 2.0 ilustrated by examples

Identity

$this->greet()->shouldReturn('Hello, World!');

$this->greet()->shouldBe('Hello, World!');

$this->greet()->shouldBeEqualTo('Hello, World!');

$this->greet()->shouldEqual('Hello, World!');

===

Page 18: PhpSpec 2.0 ilustrated by examples

Comparison

$this->greet()->shouldBeLike('Hello, World!');

==

Page 19: PhpSpec 2.0 ilustrated by examples

Throw

$this->shouldThrow('EndOfTheWorld')->duringGreet();

$this->shouldThrow('EndOfTheWorld')->during('greet');

$this->greet() ->shouldThrow(new \Exception('the end')) ->duringGreet('some argument');

$this->greet()->shouldThrow('EndOfTheWorld') ->during('greet', array('some argument'));

Page 20: PhpSpec 2.0 ilustrated by examples

Type

$this->greet()->shouldBeAnInstanceOf('Greeting');

$this->greet()->returnAnInstanceOf('Greeting');

$this->greet()->haveType('Greeting');

Page 21: PhpSpec 2.0 ilustrated by examples

Object State

class ShoppingCartSpec extends ObjectBehavior{ function it_is_created_empty() { $this->shouldNotHaveItems(); }}

class ShoppingCart{ public function hasItems() {}}

has -> have

Page 22: PhpSpec 2.0 ilustrated by examples

Object State

class LifeSpec extends ObjectBehavior{ function it_is_simple() { $this->shouldBeSimple(); }}

class Life{ public function isSimple() {}}

is -> be

Page 23: PhpSpec 2.0 ilustrated by examples

Inline

class NeoSpec extends ObjectBehavior { function it_should_be_the_one() { $this->shouldBeTheOne(); }

function getMatchers() { return [ 'beTheOne' => function($actual) { return $actual instanceof TheOne; } ]; }}

Page 24: PhpSpec 2.0 ilustrated by examples

Formatters

Page 25: PhpSpec 2.0 ilustrated by examples

Progress

$

Page 26: PhpSpec 2.0 ilustrated by examples

Dot

$ -fdot

Page 27: PhpSpec 2.0 ilustrated by examples

Pretty

$ -fpretty

Page 28: PhpSpec 2.0 ilustrated by examples

Nyan

$ -fnyan

Page 29: PhpSpec 2.0 ilustrated by examples

Let & Let go

Page 30: PhpSpec 2.0 ilustrated by examples

Constructors

Page 31: PhpSpec 2.0 ilustrated by examples

Stubbing

Page 32: PhpSpec 2.0 ilustrated by examples

Mocking