Skip to content

Commit

Permalink
Résolution et tests unitaire du challenge RUGBY_1 en utilisant le pat…
Browse files Browse the repository at this point in the history
…tern Strategy

7 objets créés :
En lien avec le pattern Strategy :
- ImpactStrategy (interface)
- FirstLineStrategy
- SecondLineStrategy
- ThirdLineStrategy

Pour le programme principal :
- Scrum
- Line
- Player

Les tests sont rangés dans 3 fichiers :
- PlayerTest
- ScrumTest
- StrategyTest

+readme
  • Loading branch information
TainixCode committed Feb 3, 2024
1 parent d9834e5 commit 7f168f4
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -21,5 +21,7 @@ Corrigés des challenges, avec tests unitaires réalisés avec PHPUnit ou Pest P

[Design Pattern en PHP : Factory](https://tainix.fr/code/Design-Pattern-en-PHP-Factory).

[Design Pattern en PHP : Strategy](https://tainix.fr/code/POO-en-PHP-design-Pattern-Strategy).

### Autres
[Interface ou héritage avec méthode abstraite ?](https://tainix.fr/code/POO-interfaces-ou-methodes-abstraites).
12 changes: 12 additions & 0 deletions challenges/RUGBY_1/FirstLineStrategy.php
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Challenges\RUGBY_1;

class FirstLineStrategy implements ImpactStrategy
{
public function calculateImpact(Player $player): int
{
return (int) floor($player->impact() * 1.5);
}
}
9 changes: 9 additions & 0 deletions challenges/RUGBY_1/ImpactStrategy.php
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Challenges\RUGBY_1;

interface ImpactStrategy
{
public function calculateImpact(Player $player): int;
}
36 changes: 36 additions & 0 deletions challenges/RUGBY_1/Line.php
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace Challenges\RUGBY_1;

class Line
{
/**
* @var Player[] $players
*/
private array $players = [];
private ImpactStrategy $strategy;

/**
* @param string[] $playersInformations
*/
public function __construct(array $playersInformations, ImpactStrategy $strategy)
{
foreach ($playersInformations as $playerInformations) {
$this->players[] = Player::createFromText($playerInformations);
}

$this->strategy = $strategy;
}

public function impact(): int
{
$impact = 0;

foreach ($this->players as $player) {
$impact += $this->strategy->calculateImpact($player);
}

return $impact;
}
}
26 changes: 26 additions & 0 deletions challenges/RUGBY_1/Player.php
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Challenges\RUGBY_1;

class Player
{
public function __construct(
public readonly int $poids,
public readonly int $force
) {}

public static function createFromText(string $informations): Player
{
$values = explode(':', $informations);
return new self(
(int) $values[0],
(int) $values[1]
);
}

public function impact(): int
{
return $this->poids * $this->force;
}
}
19 changes: 19 additions & 0 deletions challenges/RUGBY_1/Scrum.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Challenges\RUGBY_1;

class Scrum
{
public function __construct(
private Line $firstLine,
private Line $secondLine,
private Line $thirdLine,
) {}

public function impact(): int
{
return $this->firstLine->impact() + $this->secondLine->impact() + $this->thirdLine->impact();
}
}
12 changes: 12 additions & 0 deletions challenges/RUGBY_1/SecondLineStrategy.php
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Challenges\RUGBY_1;

class SecondLineStrategy implements ImpactStrategy
{
public function calculateImpact(Player $player): int
{
return $player->impact();
}
}
12 changes: 12 additions & 0 deletions challenges/RUGBY_1/ThirdLineStrategy.php
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Challenges\RUGBY_1;

class ThirdLineStrategy implements ImpactStrategy
{
public function calculateImpact(Player $player): int
{
return (int) floor($player->impact() * 0.75);
}
}
19 changes: 19 additions & 0 deletions pest/RUGBY_1/PlayerTest.php
@@ -0,0 +1,19 @@
<?php

use Challenges\RUGBY_1\Player;

test('parsing', function() {

$informations = '1:2';

$player = Player::createFromText($informations);

$this->assertEquals(1, $player->poids);
$this->assertEquals(2, $player->force);
});

test('impact', function() {

$player = new Player(2, 3);
$this->assertEquals(6, $player->impact());
});
47 changes: 47 additions & 0 deletions pest/RUGBY_1/ScrumTest.php
@@ -0,0 +1,47 @@
<?php

use Challenges\RUGBY_1\Line;
use Challenges\RUGBY_1\Scrum;
use Challenges\RUGBY_1\FirstLineStrategy;
use Challenges\RUGBY_1\ThirdLineStrategy;
use Challenges\RUGBY_1\SecondLineStrategy;

test('Première ligne', function() {

$lineInformations = ['5:20', '10:10'];
$line = new Line($lineInformations, new FirstLineStrategy);

$this->assertEquals(300, $line->impact());
});

test('Deuxième ligne', function() {

$lineInformations = ['5:20', '10:10'];
$line = new Line($lineInformations, new SecondLineStrategy);

$this->assertEquals(200, $line->impact());
});

test('Troisième ligne', function() {

$lineInformations = ['5:20', '10:10'];
$line = new Line($lineInformations, new ThirdLineStrategy);

$this->assertEquals(150, $line->impact());
});

test('Mélée complète', function() {

// Données issues de Tainix
$line1 = ['102:23', '100:35', '117:33'];
$line2 = ['96:57', '110:58', '92:42', '90:15'];
$line3 = ['75:93'];

$scrum = new Scrum(
new Line($line1, new FirstLineStrategy),
new Line($line2, new SecondLineStrategy),
new Line($line3, new ThirdLineStrategy),
);

$this->assertEquals(36857, $scrum->impact());
});
36 changes: 36 additions & 0 deletions pest/RUGBY_1/StrategyTest.php
@@ -0,0 +1,36 @@
<?php

use Challenges\RUGBY_1\FirstLineStrategy;
use Challenges\RUGBY_1\Player;
use Challenges\RUGBY_1\SecondLineStrategy;
use Challenges\RUGBY_1\ThirdLineStrategy;

test('Première ligne', function() {

$player = new Player(5, 20);

$strategy = new FirstLineStrategy;
$impact = $strategy->calculateImpact($player);

$this->assertEquals(150, $impact);
});

test('Deuxième ligne', function() {

$player = new Player(5, 20);

$strategy = new SecondLineStrategy;
$impact = $strategy->calculateImpact($player);

$this->assertEquals(100, $impact);
});

test('Troisième ligne', function() {

$player = new Player(5, 20);

$strategy = new ThirdLineStrategy;
$impact = $strategy->calculateImpact($player);

$this->assertEquals(75, $impact);
});

0 comments on commit 7f168f4

Please sign in to comment.