Upload
eric-hogue
View
107
Download
5
Embed Size (px)
Citation preview
Tests unitaires
est une procédure permettant de vérifier le bon fonctionnement d'une partie précise d'un logiciel ou d'une portion d'un programme (appelée « unité » ou « module »).
http://fr.wikipedia.org/wiki/Test_unitaire
5
Installation - Phar
$ wget
https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ mv phpunit.phar /usr/local/bin/phpunit
9
Installation - Composer
# composer.json
{
"require-dev": {
"phpunit/phpunit": "4.5.*"
}
}
$ composer install
10
Act
/** @test */
public function factOf1() {
$factorial = new Factorial;
$result = $factorial->fact(1);
}
15
Assert
/** @test */
public function factOf1() {
$factorial = new Factorial;
$result = $factorial->fact(1);
$this->assertSame(1, $result);
}
16
PHPUnit Assertions
● $this->assertTrue();● $this->assertEquals();● $this->assertSame();● $this->assertContains();● $this->assertNull();● $this->assertRegExp();● ...
17
Preparing For Your Tests
setup() -> Avant chaque testteardown() -> Après chaque test
setUpBeforeClass() + tearDownAfterClass()Une fois par test
18
3 règles du TDD - Uncle Bob
● You are not allowed to write any production code unless it is to make a failing unit test pass.
● You are not allowed to write any more of a unit test than is sufficient to fail; ○ and compilation failures are failures.
● You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd21
/** @test */
public function factOf1() {
$facto = new Factorial;
$this->assertSame(1,
$facto->fact(1));
}
27
Duplication
public function create() {
$this->assertNotNull(new Factorial);
}
public function factOf1() {
$facto = new Factorial;
...
29
public function setup() {
$this->facto = new Factorial;
}
/** @test */
public function factOf1() {
$this->assertSame(1,
$this->facto->fact(1));
}
30
Plus de duplication/** @test */
public function factOf1() {
$this->assertSame(1,
$this->facto->fact(1));
}
/** @test */
public function factOf2() {
$this->assertSame(2,
$this->facto->fact(2));
} 33
/**
* @test
* @dataProvider factDataProvider
*/
public function factorial($number,
$expected) {
...
35
public function fact($number) {
if ($number < 2) return 1;
return $number *
$this->fact($number - 1);
}
38
Setter Injection
class Foo {
public function setBar(Bar $bar) {
$this->bar = $bar;
}
}
public function doSomething() {
// Use $this->bar
}
44
Constructor Injection
class Foo {
public function __construct(
Bar $bar) {
$this->bar = $bar;
}
...
}
45
Passer la dépendance aux méthodes
class Foo {
public function doSomething(
Bar $bar) {
// Use $bar
}
}
46
Vérifier la création d’un répertoire
$root = vfsStream::setup('dir');
$parentDir = $root->url('dir');
//Code creating sub folder
$SUT->createDir($parentDir, 'test');
$this->assertTrue(
$root->hasChild('test'));
49
Lire un fichier
$struct = [
'subDir' => ['test.txt'
=> 'content']
];
$root = vfsStream::setup('root',
null, $struct);
$parentDir = $root->url('root');
...50
Lire un fichier
…$content = file_get_contents(
$parentDir . '/subDir/test.txt');
$this->assertSame('content',
$content);
51
Création
$mock = $this->getMock('\NS\Class');
$mock = $this->getMockBuilder
('\Namespace\Class')
->disableOriginalConstructor()
->getMock();
54
$mock->expects($this->once())
->method('methodName')
->with(1, 'aa', $this->anything())
->will($this->returnValue(10));
57
Mocking PDO
$statement = $this->getMockBuilder
('\PDOStatement')
->getMock();
$statement->expects($this->once())
->method('execute')
->will($this->returnValue(true));
...
58
...
$statement->expects($this->once())
->method('fetchAll')
->will(
$this->returnValue(
[['id' => 123]]
)
);
...59
…$pdo = $this->getMockBuilder(
'\stdClass')
->setMethods(['prepare'])
->getMock();
$pdo->expects($this->once())
->method('prepare')
->will(
$this->returnValue($statement));61
class PDOMock extends \PDO {
public function __construct() {}
}
$pdo = $this->getMockBuilder
('\PDOMock')
->getMock();
62
DbUnit
extends PHPUnit_Extensions_Database_TestCase
public function getConnection() {
$pdo = new PDO('sqlite::memory:');
return $this->
createDefaultDBConnection(
$pdo, ':memory:');
}64
Commentaires: https://joind.in/13312twitter: @ehogue
PHP Mentoring: http://phpmentoring.org/
Questions?
76
Credits● Paul - http://www.flickr.com/photos/pauldc/4626637600/in/photostream/● JaseMan - http://www.flickr.com/photos/bargas/3695903512/● mt 23 - http://www.flickr.com/photos/32961941@N03/3166085824/● Adam Melancon - http://www.flickr.com/photos/melancon/348974082/● Zhent_ - http://www.flickr.com/photos/zhent/574472488/in/faves-96579472@N07/● Ryan Vettese - http://www.flickr.com/photos/rvettese/383453435/● shindoverse - http://www.flickr.com/photos/shindotv/3835363999/● Eliot Phillips - http://www.flickr.com/photos/hackaday/5553713944/● World Bank Photo Collection - http://www.flickr.com/photos/worldbank/8262750458/● Steven Depolo - http://www.flickr.com/photos/stevendepolo/3021193208/● Deborah Austin - http://www.flickr.com/photos/littledebbie11/4687828358/● tec_estromberg - http://www.flickr.com/photos/92334668@N07/11122773785/● nyuhuhuu - http://www.flickr.com/photos/nyuhuhuu/4442144329/● Damián Navas - http://www.flickr.com/photos/wingedwolf/5471047557/● Improve It - http://www.flickr.com/photos/improveit/1573943815/● PolliceVersoThumbsUp.jpg - keimevo - https://www.flickr.com/photos/keimevo/14093768567/in/photostream/● PolliceVersoThumbsDown.jpg - keimevo - https://www.flickr.com/photos/keimevo/14093716370/in/photostream/● CILights.jpg - Jan Krutisch - https://www.flickr.com/photos/jankrutisch/4272142306/in/photostream/
77