Last active
October 15, 2017 14:08
-
-
Save JulienRAVIA/96e189a534c26a63a91ce09c4681fb28 to your computer and use it in GitHub Desktop.
Démonstration de tests unitaires
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- testsUnitaires/phpunit.xml --> | |
<?xml version="1.0" encoding="utf-8" ?> | |
<phpunit colors="true"> | |
<testsuite name="Mes tests"> | |
<directory>./tests</directory> | |
</testsuite> | |
<filter> | |
<whitelist proceessUncoveredFromWhitelist="true"> | |
<directory suffic=".php">src/</directory> | |
</whitelist> | |
</filter> | |
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// testsUnitaires/src/User.php | |
namespace TestsUnitaires; | |
/** | |
* Classe Utilisateur | |
*/ | |
class User | |
{ | |
private $_prenom; | |
private $_age; | |
private $_eleve; | |
private $_toulonnais; | |
private $_cours; | |
private $_adresse; | |
public function __construct(string $prenom, int $age, bool $eleve, bool $toulonnais, array $cours = null) | |
{ | |
$this->_prenom = $prenom; | |
$this->_age = $age; | |
$this->_eleve = $eleve; | |
$this->_toulonnais = $toulonnais; | |
if($this->_toulonnais) { | |
$this->_adresse = 'Toulon'; | |
} else { | |
$this->_adresse = null; | |
} | |
$this->_cours = $cours; | |
} | |
public function ajouteCours($matiere) | |
{ | |
$this->_cours['matieres'][] = $matiere; | |
} | |
public function recupereStatut() | |
{ | |
return $this->_eleve; | |
} | |
public function recupereAge() | |
{ | |
return $this->_age; | |
} | |
public function recuperePrenom() | |
{ | |
return $this->_prenom; | |
} | |
public function recupereVille() | |
{ | |
return $this->_toulonnais; | |
} | |
public function recupereCours() | |
{ | |
return $this->_cours; | |
} | |
public function recupereAdresse() | |
{ | |
return $this->_adresse; | |
} | |
public function disBonjour() | |
{ | |
return 'Bonjour '.$this->_prenom; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//testsUnitaires/tests/UserTest.php | |
include_once './src/User.php'; | |
use TestsUnitaires\User as User; | |
/** | |
IMPORTANT | |
**/ | |
use PHPUnit\Framework\TestCase; | |
/** | |
* Test de la classe User | |
*/ | |
class UserTest extends TestCase | |
{ | |
private $user; | |
/** | |
* Cette méthode sert à instancier l'objet, le constructeur ne marche pas | |
*/ | |
public function setUp() | |
{ | |
$cours = array('Français', 'Maths', 'SLAM3', 'SLAM4', 'SLAM4'); | |
$this->user = new User('Julien', 19, true, false, $cours); | |
} | |
/** | |
* On va tester que le le prénom renseigné est différent de celui voulu | |
* @return [type] [description] | |
*/ | |
public function testPrenomsDifferents() | |
{ | |
$this->assertNotEquals("Kevun", $this->user->recuperePrenom()); | |
} | |
/** | |
* On va tester que les prénoms renseignés et voulus soient les mêmes | |
* @return [type] [description] | |
*/ | |
public function testMemesPrenoms() | |
{ | |
$this->assertEquals("Julien", $this->user->recuperePrenom()); | |
} | |
/** | |
* On va tester qu'il ai cinq cours | |
* @return [type] [description] | |
*/ | |
public function testACinqCours() { | |
$this->assertCount(5, $this->user->recupereCours()); | |
} | |
/** | |
* On va tester qu'il n'ai pas zéro cours | |
* @return [type] [description] | |
*/ | |
public function testNaPasZeroCours() | |
{ | |
$this->assertNotCount(0, $this->user->recupereCours()); | |
} | |
/** | |
* On va vérifier que son statut est bien un éléve | |
* @return [type] [description] | |
*/ | |
public function testEstEleve() { | |
$this->assertTrue($this->user->recupereStatut()); | |
} | |
/** | |
* On va tester qu'il n'est pas toulonnais, étant donné qu'il habite au luc | |
* @return [type] [description] | |
*/ | |
public function testEstPasToulonnais() { | |
$this->assertFalse($this->user->recupereVille()); | |
} | |
/** | |
* Si il n'est pas toulonnais, son adresse n'est pas renseignée, donc son adresse est nulle | |
* @return [type] [description] | |
*/ | |
public function testAdresseRenseignee() { | |
$this->assertNull($this->user->recupereAdresse()); | |
} | |
/** | |
* On vérifie qu'il ai bien plus de 18 ans | |
* @return [type] [description] | |
*/ | |
public function testEstMajeur() | |
{ | |
$this->assertGreaterThanOrEqual(18, $this->user->recupereAge()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment