Transcript

4.
Test ﬁrst, code later
Write tests for all your code
• New features come with a test
• Bugﬁxes come with a test
Write tests before your code
• Adding a feature means adding a test ﬁrst
• Fixing a bug means writing a test ﬁrst
Inspiring people to
share

5.
Unit tests
Unit tests are small programs that test your code
They check
• the result of methods against expected values
• whether methods are (not) called as expected
A test should
• be well-named
• check only one assertion
Inspiring people to
share

11.
They make you feel good
The tests make you
• focus on your task
• code exactly what you need
• think from the outside
• a better programmer*
So what the tests do for you is the key
* decoupling helps with testing,
decoupled code is easier to maintain,
easier is better - q.e.d.
Inspiring people to
share

17.
More testing
Sometimes you need to test interaction with external systems,
like database access
A test should be small, encapsulated, stand-alone
So how do you test database access?
Inspiring people to
share

18.
Mocks & stubs
Mock objects allow you to use fake objects instead of real
implementations
Stubs can be used to return hard-coded results
Using them is actually (somewhat) easy with PHPUnit
Inspiring people to
share

19.
Saving in TYPO3CR
Assumptions / Background
• The Session object has a save() method
• It uses a Storage_Backend implementation to do the work
• That implementation has methods like addNode(),
updateNode() and deleteNode()
You need to make sure those are used when calling save()
Inspiring people to
share

24.
Dependencies
Unit Testing: You want to test a small unit
You don't want to test
• The Simple File Logger
• The Card Repository
Inspiring people to
share

25.
Dependencies
Unit Testing: You want to test a small unit
You want to test
• if the action returns a string representation of the
random card it gets from the repository
Inspiring people to
share

26.
Dependency Injection
A class doesn't ask for the instance of another class but gets it
injected
This methodology is referred to as the quot;Hollywood Principlequot;:
quot;Don't call us, we'll call youquot;
Enforces loose coupling and high cohesion
Allows you to mock collaborators
Makes you a better programmer
Inspiring people to
share