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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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()); } } ?>
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; } } ?>
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; }
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’); } } ?>
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; } } ?>
*/ 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; } } ?>
/** * @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()); } } ?>
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; } } ?>
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()); } } ?>
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; } } } } ?>
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->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)); } } ?>