Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Résolution et tests unitaire du challenge RUGBY_1 en utilisant le pat…
…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
1 parent
d9834e5
commit 7f168f4
Showing
11 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Challenges\RUGBY_1; | ||
|
||
interface ImpactStrategy | ||
{ | ||
public function calculateImpact(Player $player): int; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |