PHPUnit and Drupal 8 PDF
PHPUnit and Drupal 8 PDF
PHPUnit and Drupal 8 PDF
Paul Mitchum
Mile23 on Drupal.org
paul-m on Github
from Seattle
G3T C0D3Z
Support page on my site:
http://mile23.com/phpunit-talk
Keep In Mind:
To Know.
Stuff.
For Sure.
What Do We Want To Know?
That Our Code Works.
Development Process
Regression Testing
We want to know what we know.
Code-level
- Class Unit
- Method/Function Unit
(demo)
How To Run PHPUnit Tests: Fail
0. At the command line...
1. Obtain Drupal 8.
2. cd to core/
3. ./vendor/bin/phpunit
4. Wait 8 seconds.
5. Know………………………………….(all/all)
(demo)
Coverage Report
How do you know if you can trust the tests?
Requires XDebug.
2) Missing Coverage:
Compare Directory
w/ Coverage
Coverage report won’t reflect
classes untouched by tests.
Demo of coverage report
Coverage Report 1: Overview
Coverage Report 2: Single Class
CRAP: Change Risk Anti-Pattern
4
Recap
- Run tests in order to know things.
Unit - PHPUnit
Functional - SimpleTest
Behavioral - Behat
Behavioral Testing
Behat
Functional Testing (SimpleTest)
What Are You Testing?
How (sub)systems interact.
- System isolation:
No database, no server
- Language isolation:
Pick out extensions and libraries
- Code isolation:
Dataproviders, test doubles, reflection, hard.
U R DOING IT RONG!1!!
IF U R….
- Requiring a database
- Making a module
- Subclassing anything
Isolation Anti-Patterns
Patterns For Isolation
- Data Providers
- @expectedException
- Dependency Injection
/**
* @dataProvider providerTestSomething
*/
public function testSomething($expected, $data)
{ // Your Logic Here }
Pattern: DataProvider Isolation
Once a unit test is written, it becomes:
The Test™
Class_A::
fooMethod(\Interface_B $b);
$mock = $this->getMock
(‘\Interface_b’);
Pattern: Test Doubles
Class_A::fooMethod(\Interface_B $b);
$mock = $this->getMock(‘\Interface_b’);
$mock->expects($this->any())
->method(‘stubThisMethod’)
->will($this->returnValue(‘expected’));
$this->assertEquals(
‘expected’,
$a->fooMethod($mock)
);
Dependency Injection
of Mocked Object
(Modern PHP)
Winding Up: Recap
PHPUnit: ./vendor/bin/phpunit
http://mile23.com/phpunit-talk