assoc + tests

This commit is contained in:
Simon Vieille 2015-04-07 21:25:31 +02:00
parent 72f7d52013
commit 2a06e950c5
8 changed files with 380 additions and 39 deletions

View file

@ -91,10 +91,10 @@ class Categorie
return $this;
}
$video->addCategorie($this);
$this->videos[] = $video;
$video->addCategorie($this);
return $this;
}
@ -106,7 +106,7 @@ class Categorie
{
$this->videos = [];
foreach ($this->getVideos() as $v) {
foreach ($videos as $v) {
$this->addVideo($v);
}

View file

@ -17,6 +17,11 @@ class Format
*/
protected $normalise;
/**
* @var mixed[] $videos Vidéos
*/
protected $videos = [];
/**
* @param string $lib Lib du format
*/
@ -110,10 +115,10 @@ class Format
return $this;
}
$video->addFormat($this);
$this->videos[] = $video;
$video->addFormat($this);
return $this;
}
@ -125,7 +130,7 @@ class Format
{
$this->videos = [];
foreach ($this->getVideos() as $v) {
foreach ($videos as $v) {
$this->addVideo($v);
}

View file

@ -12,6 +12,16 @@ class Participant extends Personne
*/
protected $pays;
/**
* @var mixed(] $videosRealisees Vidéos réalisées
*/
protected $videosRealisees = [];
/**
* @var mixed(] $videosJouees Vidéos jouées (acteur)
*/
protected $videosJouees = [];
/**
* @param string $image
* @return Participant
@ -54,4 +64,114 @@ class Participant extends Personne
{
return $this->pays;
}
/**
* @param Video $vidéo
* @return boolean La vidéo est associée à l'acteur
*/
public function hasVideoJouee(Video $video)
{
foreach ($this->getVideoJouees() as $p) {
if ($p === $video) {
return true;
}
}
return false;
}
/**
* @param Video $video
* @return Participant
*/
public function addVideoJouee(Video $video)
{
if ($this->hasVideoJouee($video)) {
return $this;
}
$this->videosJouees[] = $video;
$video->addActeur($this);
return $this;
}
/**
* @param mixed[] $videosJouees
* @return Participant
*/
public function setVideoJouees(array $videosJouees)
{
$this->videosJouees = [];
foreach ($videosJouees as $v) {
$this->addVideoJouee($v);
}
return $this;
}
/**
* @return mixed[] $realisateurs
*/
public function getVideoJouees()
{
return $this->videosJouees;
}
/**
* @param Video $vidéo
* @return boolean La vidéo est associée au realisateur
*/
public function hasVideoRealisee(Video $video)
{
foreach ($this->getVideoRealisees() as $p) {
if ($p === $video) {
return true;
}
}
return false;
}
/**
* @param Video $video
* @return Participant
*/
public function addVideoRealisee(Video $video)
{
if ($this->hasVideoRealisee($video)) {
return $this;
}
$this->videosRealisees[] = $video;
$video->addRealisateur($this);
return $this;
}
/**
* @param mixed[] $videosRealisees
* @return Participant
*/
public function setVideoRealisees(array $videosRealisees)
{
$this->videosRealisees = [];
foreach ($videosRealisees as $v) {
$this->addVideoRealisee($v);
}
return $this;
}
/**
* @return mixed[] $videosRealisees
*/
public function getVideoRealisees()
{
return $this->videosRealisees;
}
}

View file

@ -116,12 +116,12 @@ class Pays
}
}
$this->participants[] = $participant;
if ($this !== $participant->getPays()) {
$participant->setPays($this);
}
$this->participants[] = $participant;
return $this;
}
@ -169,10 +169,10 @@ class Pays
return $this;
}
$video->addPays($this);
$this->videos[] = $video;
$video->addPays($this);
return $this;
}
@ -184,7 +184,7 @@ class Pays
{
$this->videos = [];
foreach ($this->getVideos() as $v) {
foreach ($videos as $v) {
$this->addVideo($v);
}

View file

@ -91,10 +91,10 @@ class Type
return $this;
}
$video->addType($this);
$this->videos[] = $video;
$video->addType($this);
return $this;
}
@ -106,7 +106,7 @@ class Type
{
$this->videos = [];
foreach ($this->getVideos() as $v) {
foreach ($videos as $v) {
$this->addVideo($v);
}

View file

@ -13,7 +13,7 @@ class Video
protected $titre;
/**
* @var string $duree Durée de la vidéo en secondes
* @var string $duree Durée de la vidéo en minutes
*/
protected $duree;
@ -40,27 +40,32 @@ class Video
/**
* @var mixed[] $types Types
*/
protected $types;
protected $types = [];
/**
* @var mixed[] $formats Formats
*/
protected $formats;
protected $formats = [];
/**
* @var mixed[] $categories Catégories
*/
protected $categories;
protected $categories = [];
/**
* @var mixed[] $acteurs Acteurs
*/
protected $acteurs;
protected $acteurs = [];
/**
* @var mixed[] $realisateurs Réalisateurs
*/
protected $realisateurs;
protected $realisateurs = [];
/**
* @var mixed[] $pays Pays
*/
protected $pays = [];
/**
* @param integer|null $id
@ -232,10 +237,10 @@ class Video
return $this;
}
$type->addVideo($this);
$this->types[] = $type;
$type->addVideo($this);
return $this;
}
@ -287,10 +292,10 @@ class Video
return $this;
}
$format->addVideo($this);
$this->formats[] = $format;
$format->addVideo($this);
return $this;
}
@ -302,7 +307,7 @@ class Video
{
$this->formats = [];
foreach ($this->getFormats() as $v) {
foreach ($formats as $v) {
$this->addFormat($v);
}
@ -342,10 +347,10 @@ class Video
return $this;
}
$categorie->addVideo($this);
$this->categories[] = $categorie;
$categorie->addVideo($this);
return $this;
}
@ -357,7 +362,7 @@ class Video
{
$this->categories = [];
foreach ($this->getCategories() as $v) {
foreach ($categories as $v) {
$this->addCategorie($v);
}
@ -397,9 +402,9 @@ class Video
return $this;
}
$pays->addVideo($this);
$this->pays[] = $pays;
$this->payss[] = $pays;
$pays->addVideo($this);
return $this;
}
@ -408,9 +413,9 @@ class Video
* @param mixed[] $pays
* @return Video
*/
public function setPays(array $payss)
public function setPayss(array $pays)
{
$this->payss = [];
$this->pays = [];
foreach ($this->getPays() as $v) {
$this->addPays($v);
@ -424,7 +429,7 @@ class Video
*/
public function getPays()
{
return $this->payss;
return $this->pays;
}
/**
@ -452,10 +457,10 @@ class Video
return $this;
}
$acteur->addVideoActeur($this);
$this->acteurs[] = $acteur;
$acteur->addVideoJouee($this);
return $this;
}
@ -467,7 +472,7 @@ class Video
{
$this->acteurs = [];
foreach ($this->getActeurs() as $v) {
foreach ($acteurs as $v) {
$this->addActeur($v);
}
@ -507,10 +512,10 @@ class Video
return $this;
}
$realisateur->addVideoRealisateur($this);
$this->realisateurs[] = $realisateur;
$realisateur->addVideoRealisee($this);
return $this;
}
@ -522,7 +527,7 @@ class Video
{
$this->realisateurs = [];
foreach ($this->getRealisateurs() as $v) {
foreach ($realisateurs as $v) {
$this->addRealisateur($v);
}

12
tests/autoload.php Normal file
View file

@ -0,0 +1,12 @@
<?php
function __autoload($name)
{
static $classes = [];
if (isset($classes[$name])) {
return;
}
require __DIR__.'/../classes/class.'.$name.'.php';
}

199
tests/test.php Normal file
View file

@ -0,0 +1,199 @@
<?php
require __DIR__.'/autoload.php';
/**
* Catégories de films
*/
$categorieAction = new Categorie('Action');
$categorieScienceFiction = new Categorie('Science Fiction');
$categorieHumour = new Categorie('Humour');
/**
* Format de films
*/
$formatDvd = new Format('DVD');
$formatBlueray = new Format('Blue Ray');
$formatVHS = new Format('VHS');
/**
* Pays
*/
$paysUSA = new Pays('USA');
/**
* Films
*/
$filmMatrix = new Video();
$filmMatrix
->setTitre('The Matrix')
->setDuree(2 * 60 + 15) // 2h15 en minutes
->setSynopsis("Programmeur anonyme dans un service administratif le jour, Thomas Anderson devient Neo la nuit venue. Sous ce pseudonyme, il est l'un des pirates les plus recherchés du cyber-espace. A cheval entre deux mondes, Neo est assailli par d'étranges songes et des messages cryptés provenant d'un certain Morpheus. Celui-ci l'exhorte à aller au-delà des apparences et à trouver la réponse à la question qui hante constamment ses pensées : qu'est-ce que la Matrice ? Nul ne le sait, et aucun homme n'est encore parvenu à en percer les defenses. Mais Morpheus est persuadé que Neo est l'Elu, le libérateur mythique de l'humanité annoncé selon la prophétie. Ensemble, ils se lancent dans une lutte sans retour contre la Matrice et ses terribles agents...");
$paysUSA->addVideo($filmMatrix);
$filmScaryMovie = new Video();
$filmScaryMovie->setTitre('Scary Movie');
$filmScaryMovie->setDuree(1 * 60 + 24); // 1h24
$filmScaryMovie->addPays($paysUSA);
$filmScaryMovie->setSynopsis("Un soir, Drew Becker recoit un appel anonyme d'un maniaque. Traquée dans sa maison, puis dans son jardin, elle finit par se faire tuer. Sa mort plonge ses camarades de lycée en plein cauchemar, d'autant qu'ils doivent désormais faire face à un tueur en série, caché parmi eux. Flairant le scoop, la journaliste Gail Hailstorn débarque en ville, bien décidée à harceler Cindy Campbell et ses amis à propos de cette histoire...");
/**
* Réalisateurs
*/
$realisateurScaryMovie = new Participant('Wayans', 'Keenen Ivory');
$realisateurMatrix1 = new Participant('Wachowski', 'Lana');
$realisateurMatrix2 = new Participant('Wachowski', 'Andy');
/**
* Acteurs
*/
$acteurScaryMovie1 = new Participant('Anna', 'Faris');
$acteurScaryMovie2 = new Participant('Shawn', 'Wayans');
$acteurScaryMovie3 = new Participant('Marlon', 'Wayans');
$acteurMatrix1 = new Participant('Keanu', 'Reeves');
$acteurMatrix2 = new Participant('Laurence', 'Wayans');
$acteurMatrix3 = new Participant('Carrie-Anne', 'Wayans');
/**
* Et on associe tout le monde...
*/
// Films <-> Categorie
$filmMatrix->addCategorie($categorieAction);
$filmMatrix->addCategorie($categorieScienceFiction);
$categorieHumour->addVideo($filmScaryMovie);
// Participants <-> Pays
$acteurMatrix1->setPays($paysUSA);
$acteurMatrix2->setPays($paysUSA);
// une autre manière de la faire
$paysUSA->addParticipant($acteurMatrix3);
$acteurScaryMovie1->setPays($paysUSA);
$acteurScaryMovie2->setPays($paysUSA);
$acteurScaryMovie3->setPays($paysUSA);
// Acteurs <-> films
$acteurMatrix1->addVideoJouee($filmMatrix);
$filmMatrix->addActeur($acteurMatrix2);
$filmMatrix->addActeur($acteurMatrix3);
$filmMatrix
->addRealisateur($realisateurMatrix1)
->addRealisateur($realisateurMatrix2);
$filmScaryMovie
->setActeurs([
$acteurScaryMovie1,
$acteurScaryMovie2,
$acteurScaryMovie3,
]);
$realisateurScaryMovie->addVideoRealisee($filmScaryMovie);
// Les formats
$formatBlueray->addVideo($filmMatrix);
$filmMatrix->addFormat($formatVHS);
$filmMatrix->addFormat($formatDvd);
$formatDvd->addVideo($filmScaryMovie);
$formatVHS->addVideo($filmScaryMovie);
/**
* On affiche les résultats par différents moyens...
*/
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>J'aime les pommes</title>
</head>
<body>
<h1>Pays</h1>
<?php foreach([$paysUSA] as $pays): ?>
<h2><?php echo $pays->getNom(); ?></h2>
<h3>Participants</h3>
<ul>
<?php foreach($pays->getParticipants() as $participant): ?>
<li>
<?php echo $participant->getPrenom(); ?> <?php echo $participant->getNom(); ?>
</li>
<?php endforeach; ?>
</ul>
<h3>Vidéos</h3>
<ul>
<?php foreach($pays->getVideos() as $video): ?>
<li>
<h4><?php echo $video->getTitre(); ?> (Durée : <?php echo $video->getDuree(); ?> minutes)</h4>
<h5>Catégorie(s)</h5>
<ul>
<?php foreach($video->getCategories() as $categorie): ?>
<li><?php echo $categorie->getLib(); ?></li>
<?php endforeach; ?>
</ul>
<h5>Type(s)</h5>
<ul>
<?php foreach($video->getTypes() as $type): ?>
<li><?php echo $type->getLib(); ?></li>
<?php endforeach; ?>
</ul>
<blockquote><?php echo $video->getSynopsis(); ?></blockquote>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<h1>Vidéos</h1>
<?php foreach([$filmScaryMovie, $filmMatrix] as $video): ?>
<h2><?php echo $video->getTitre(); ?></h2>
<p><?php echo $video->getSynopsis(); ?></p>
<p><em><?php echo $video->getDuree(); ?> minutes</em></p>
<h3>Réalisateur(s)</h3>
<ul>
<?php foreach($video->getRealisateurs() as $realisateur): ?>
<li>
<?php echo $realisateur->getPrenom(); ?> <?php echo $realisateur->getNom(); ?>
</li>
<?php endforeach; ?>
</ul>
<h3>Acteurs</h3>
<ul>
<?php foreach($video->getActeurs() as $acteur): ?>
<li>
<?php echo $acteur->getPrenom(); ?> <?php echo $acteur->getNom(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<h1>Formats</h1>
<?php foreach([$formatVHS, $formatDvd, $formatBlueray] as $format): ?>
<h2><?php echo $format->getLib(); ?></h2>
<h3>Films(s)</h3>
<ul>
<?php foreach($format->getVideos() as $video): ?>
<li>
<?php echo $video->getTitre(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</body>
</html>