bilan jeux tdd php

27
Bilan du jeux de coding C. Brun <christophe.brun.c Introduction Etape1 : Sujet Classe + Ajouter Etape 2 : Exception Etape 3 : Ajouter plusieurs valeurs Etape 4 : Soustraction Etape 5 : InitResultat Etape 6 : Test Résultat Après Init Ultimate Bilan Bilan du jeux de coding C. Brun <[email protected]> Perso Septembre 2013

Upload: christophe-brun

Post on 14-Apr-2017

295 views

Category:

Software


0 download

TRANSCRIPT

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Bilan du jeux de coding

C. Brun <[email protected]>

Perso

Septembre 2013

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Sommaire

Introduction

Etape1 : Sujet Classe + Ajouter

Etape 2 : Exception

Etape 3 : Ajouter plusieurs valeurs

Etape 4 : Soustraction

Etape 5 : InitResultat

Etape 6 : Test Résultat Après Init

Ultimate

Bilan

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Introduction I

I But du jeuxI Introduction au TDDI Voir votre comportement sans spécificationsI Voir votre façon de coder

I BilanI Confronter les résultats de chacun (anonyme)I RéagirI Evoluer

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Sujet Classe + Ajouter

class CalculatriceTest extends PHPUnit_Framework_TestCase { public function testInstances() { new Calculatrice; } public function testResultatDefaultsZero() {

$calc = new Calculatrice; $this->assertSame(0,$calc->getResultat()); } public function testAjouterNombre() { $calc = new Calculatrice; $calc->ajouter(2); $this->assertEquals(2,$calc->getResultat()); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Reponse 1

class Calculatrice { private $resultat = 0; public function getResultat() { return $this->resultat; }

public function ajouter($nombre) { $this->resultat=2; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Reponse 2

class Calculatrice { private $resultats = 0; function __construct() { $this->resultats = 0; } function ajouter($nb) {

$this->resultats += $nb; } function getResultat() { return $this->resultats; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Reponse 3

class Calculatrice { protected $_result = 0; public function __construct() { } public function ajouter( $nbr ) {

$this->_result += intval( $nbr ); } public function getResultat() { return $this->_result; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Reponse 4

class Calculatrice { var $resultat; function Calculatrice() { $this->resultat = 0; } public function ajouter( $num ) {

if (!is_numeric($num)) { throw new Exception(’"’ . $num . ’" n\’est pas de type numérique.’); } $this->resultat += $num; } public function getResultat() { return $this->resultat; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Reponse 5 class Calculatrice { public function __construct() { $this->Result = 0; } public function getResultat() { return $this->Result; }

/* * PRIVATE */ private function calculer( $signe, $nb ) { $this->Result = $this->Result . $signe . $nb; } /* * OPERATION */ public function ajouter( $nb ) { $nb = isset( $nb ) ? $nb : $this->Result; self::calculer( (int)’+’, $nb ); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 1 : Reponse 6

Class Calculatrice { private $vResultat; private $vInitial; /** * Constructeur */ function __construct() {

$this->setDefaut(); } /** * Méthode pour faire l’addition * @param $valeur */ public function ajouter($valeur) { $this->vResultat = $this->vInitial + $valeur; } /** * Méthode pour faire la soustraction * @param $valeur */ public function suprimer($valeur) { $this->vResultat = $this->vInitial - $valeur;

} /** * Méthode pour faire la multiplication * @param $valeur */ public function multiplier($valeur) { $this->vResultat = $this->vInitial * $valeur; } /** * Méthode pour faire la division * @param $valeur */ public function diviser($valeur) { $this->vResultat = $this->vInitial / $valeur; } /** * Méthode pour faire la puissance 2 * @param $valeur */ public function exposant2($valeur) { $this->vResultat = $valeur * $valeur; }

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 2 : Exception

public function testAjouterNombre() { $calc = new Calculatrice; $calc->ajouter(2); $this->assertEquals(2,$calc->getResultat()); } /** * @expectedException InvalidArgumentException */ public function testValeurNumerique() { $calc = new Calculatrice; $calc->ajouter(’un chiffre’); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 2 : Reponse 1

class Calculatrice { private $resultat = 0; public function getResultat() { return $this->resultat; }

public function ajouter($nombre) { if ($nombre == ’un chiffre’) throw new InvalidArgumentException; $this->resultat=2; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 2 : Response 2

class Calculatrice { private $resultats; function __construct() { $this->resultats = 0; } function ajouter($nb) {

if (!is_numeric($nb)) { throw new InvalidArgumentException(’You must give a numeric.’); } $this->resultats += $nb; } function getResultat() { return $this->resultats; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 2 : Response 3

*/ Class Calculatrice { private $vResultat; private $vInitial; /** * Constructeur */ function __construct() { $this->setDefaut(); } /** * Méthode pour faire l’addition * @param $valeur */ public function ajouter($valeur) { if( !is_numeric($valeur) )

throw new InvalidArgumentException(’Argument non valide : ’.$valeur); $this->vResultat = $this->vInitial + $valeur; }

public function getResultat() { $this->vInitial = $this->vResultat; return $this->vResultat; } /** * Méthode Mettre tous les valeur par defaut */ public function setDefaut() { $this->vResultat = $this->vInitial = 0; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 3 : Ajouter plusieurs valeurs

/** * @expectedException InvalidArgumentException */ public function testValeurNumerique() { $calc = new Calculatrice; $calc->ajouter(’un chiffre’); } public function testAjouterPlusieursValeurs() { $calc = new Calculatrice; $calc->ajouter(1,2,3,4,5); $this->assertEquals(15,$calc->getResultat()); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 3 : Response 1

class Calculatrice { private $resultats; function __construct() { $this->resultats = 0; } function ajouter() {

$numberList = func_get_args(); foreach ($numberList as $key => $number) { if (!is_numeric($number)) { throw new InvalidArgumentException(’You must provide a number for the argument #’.($key+1)); } $this->resultats += $number; } } function getResultat() { return $this->resultats; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 3 : Response 2

class Calculatrice { protected $_result = 0; public function __construct() { } public function ajouter( ) {

$numargs = func_num_args(); if( $numargs == 0 ) { throw new InvalidArgumentException(); } $arg_list = func_get_args(); foreach( $arg_list as $nbr ) { if( !is_numeric( $nbr ) ) { throw new InvalidArgumentException(); } $this->_result += $nbr; } } public function getResultat() { return $this->_result; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 3 : Response 3

*/ Class Calculatrice { private $vResultat; private $vInitial; /** * Constructeur */ function __construct() { $this->setDefaut(); } /** * Méthode pour faire l’addition * @param $valeur */ public function ajouter() { $args = func_get_args(); $valeur = 0; foreach($args as $arg) {

if( !is_numeric($arg) ) { throw new InvalidArgumentException(’Argument non valide : ’.$arg); } $valeur += $arg; } $this->vResultat = $this->vInitial + $valeur; } public function getResultat() { $this->vInitial = $this->vResultat; return $this->vResultat; } /** * Méthode Mettre tous les valeur par defaut */ public function setDefaut() { $this->vResultat = $this->vInitial = 0; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 4 : soustraire

public function testSoustraire() { $this->calc->soustraire(4); $this->assertEquals(-4,$this->calc->getResultat()); } /** * @expectedException InvalidArgumentException */ public function testValeurNumeriqueSoustraction() { $this->calc->soustraire(’bonjour’); } public function testSoustrairePlusieursValeurs() { $this->calc->soustraire(4,2); $this->assertEquals(-6,$this->calc->getResultat()); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 4 : Response 1

class Calculatrice { const ERROR_VAL_NOT_NUMERIC = "La valeur à ajouter n’est pas numérique"; private $resultat; function __construct () {

$this->resultat = 0; } public function getResultat() { return $this->resultat; } public function ajouter() { foreach(func_get_args() as $nombre){ if (!is_numeric($nombre)) { throw new InvalidArgumentException(self::ERROR_VAL_NOT_NUMERIC);

} else { $this->resultat += $nombre; } } } public function soustraire() { foreach(func_get_args() as $nombre){ if (!is_numeric($nombre)) { throw new InvalidArgumentException(self::ERROR_VAL_NOT_NUMERIC); } else { $this->resultat -= $nombre; } } } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 4 : Response 2

class Calculatrice { private $resultats; function __construct() { $this->resultats = 0; }

function getResultat() { return $this->resultats; } function operate($numberList, $operation) { foreach ($numberList as $key => $number) { if (!is_numeric($number)) { throw new InvalidArgumentException(’You must provide a number for the argument #’.($key+1)); } switch ($operation) { case ’add’:

$this->resultats += $number; break; case ’rem’: $this->resultats -= $number; break; } } } function ajouter() { call_user_func_array(array($this, "operate"), array(func_get_args(), ’add’)); } function soustraire() { call_user_func_array(array($this, "operate"), array(func_get_args(), ’rem’)); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 4 : Response 3

class Calculatrice { private $resultat; function __construct() { $this->resultat = 0; }

// Effectue l’opération $operation pour chacun des arguments $args private function effectuerOperation( $operation, $args ) { foreach( $args as $num ) { if (!is_numeric($num)) { throw new InvalidArgumentException(’"’ . $num . ’" n\’est pas de type numérique.’); } $this->$operation($num); } } /************************************************* * OPERATIONS *************************************************/ private function addition( $num ) { $this->resultat += $num; }

private function soustraction( $num ) { $this->resultat -= $num; } /************************************************* * INTERFACE *************************************************/ public function ajouter() { $this->effectuerOperation( ’addition’, func_get_args() ); } public function soustraire() { $this->effectuerOperation( ’soustraction’, func_get_args() ); } public function getResultat() { return $this->resultat; } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 5 : InitResultat

{ $this->calc->soustraire(’bonjour’); } public function testSoustrairePlusieursValeurs() { $this->calc->soustraire(4,2); $this->assertEquals(-6,$this->calc->getResultat()); } public function testInitResultat() { $this->assertEquals(4,$this->calc->initResultat(4)); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Etape 6 : Test Resultat Apres Init

} public function testResultatApresInit() { $this->calc->initResultat(6); $this->assertEquals(6,$this->calc->getResultat()); } } ?>

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Niveau Bonus

I Toujours TDDI Refactoring ?

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Un bilan ? ? ?

I TDD c’est bien ?I TDD c’est facile ?I Mauvais / Bon coding ? ? ? ?I Il ne faut pas aller trop et/ou pas assez loin (pas

toujours)I Le refactoring ?I CodeToTrash ?I BabyTask ?I guard ? qui ?

Bilan du jeux decoding

C. Brun<[email protected]>

Introduction

Etape1 : SujetClasse + Ajouter

Etape 2 :Exception

Etape 3 :Ajouter plusieursvaleurs

Etape 4 :Soustraction

Etape 5 :InitResultat

Etape 6 : TestRésultat AprèsInit

Ultimate

Bilan

Et Maintenant