PhpSpec 2.0 ilustrated by examples

11,549 views

Published on

An quick introduction to those wanting to get started with phpspec

Published in: Technology

PhpSpec 2.0 ilustrated by examples

  1. 1. Illustrated by examples 2.0.0-BETA1 http://phpspec.net http://github.com/phpspec/phpspec Marcello Duarte | @_md Konstantin Kudryashov | @everzet
  2. 2. generate or edit specs run the specs edit code generate code
  3. 3. Installation Create a composer.json file:
  4. 4. Installation Get composer, if you don’t have it already
  5. 5. Installation Install
  6. 6. Generate Specs $
  7. 7. Generated Specs
  8. 8. Edit the Spec
  9. 9. Run Specs $
  10. 10. Generate Code
  11. 11. Generated Code
  12. 12. Edit Code
  13. 13. Run Specs $
  14. 14. :)
  15. 15. Matchers object expectation matcher $result ->should ->shouldNot ->should ->shouldNot Be...()Be...()$result
  16. 16. Types of Matchers Identity Comparison Throw Type Object State Inline
  17. 17. Identity $this->greet()->shouldReturn('Hello, World!'); $this->greet()->shouldBe('Hello, World!'); $this->greet()->shouldBeEqualTo('Hello, World!'); $this->greet()->shouldEqual('Hello, World!'); ===
  18. 18. Comparison $this->greet()->shouldBeLike('Hello, World!'); ==
  19. 19. 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'));
  20. 20. Type $this->greet()->shouldBeAnInstanceOf('Greeting'); $this->greet()->returnAnInstanceOf('Greeting'); $this->greet()->haveType('Greeting');
  21. 21. Object State class ShoppingCartSpec extends ObjectBehavior { function it_is_created_empty() { $this->shouldNotHaveItems(); } } class ShoppingCart { public function hasItems() {} } has -> have
  22. 22. Object State class LifeSpec extends ObjectBehavior { function it_is_simple() { $this->shouldBeSimple(); } } class Life { public function isSimple() {} } is -> be
  23. 23. Inline class NeoSpec extends ObjectBehavior { function it_should_be_the_one() { $this->shouldBeTheOne(); } function getMatchers() { return [ 'beTheOne' => function($actual) { return $actual instanceof TheOne; } ]; } }
  24. 24. Formatters
  25. 25. Progress $
  26. 26. Dot $ -fdot
  27. 27. Pretty $ -fpretty
  28. 28. Nyan $ -fnyan
  29. 29. Let & Let go
  30. 30. Constructors
  31. 31. Stubbing
  32. 32. Mocking
  33. 33. More github.com/phpspec/phpspec phpspec.net groups.google.com/forum/#!forum/phpspec-dev twitter.com/phpspec

×