This document provides an introduction to using PhpSpec for test-driven development (TDD). It explains what TDD is, the benefits of TDD, and why PhpSpec is a good choice for TDD in PHP projects. It then walks through setting up a PhpSpec project, describing a sample Blog class, writing examples to test the Blog class, running the tests, refactoring code, and using PhpSpec matchers. The document concludes with tips on using PhpSpec to design object interactions and collaboration.
4. Contents
● Part 1: TDD in 60 seconds
● Part 2: PhpSpec setup
● Part 3: Using PhpSpec
5. What is TDD?
1) Write a failing test (red)
2) Write the minimum amount of code to pass the test
(green)
3) Refactor (restructure code, without changing its
behaviour)
6. Why TDD?
● Don't write code that isn't needed
● Write fewer bugs
● Write more maintainable code
● Be more productive
12. $ spec list
...
Available commands:
describe Creates a specification for a
class
help Displays help for a command
list Lists commands
run Runs specifications
Features
40. Pro tips
●
Use interfaces if you want to talk to collaborators
● Refactor the SUS and the specs
●
Don't be afraid to delete specs when they're not helpful
● Painful to spec means you have a bad design
●
Use PhpSpec to design how objects talk to each other (internal
behaviour)
●
Use PHPUnit to write tests (eg integration)
●
Use Behat to specify external behaviour