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 COLLECTION_1
2 objets créés : - Figurine - Collection Tests réalisés avec PEST et PHPUnit Mise à jour du readme
- Loading branch information
1 parent
395c5d6
commit 53e880d
Showing
7 changed files
with
380 additions
and
3 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,61 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Challenges\COLLECTION_1; | ||
|
||
class Collection | ||
{ | ||
/** | ||
* @var array<int, Figurine> $figurines | ||
*/ | ||
private array $figurines = []; | ||
|
||
/** | ||
* @param array<int, int> $informationsExemplaires | ||
* @param array<int, float> $informationsCotes | ||
*/ | ||
public function __construct(array $informationsExemplaires, array $informationsCotes) | ||
{ | ||
foreach ($informationsExemplaires as $key => $exemplaires) { | ||
$this->figurines[] = new Figurine( | ||
exemplaires: $exemplaires, | ||
cote: $informationsCotes[$key] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @return array<int, Figurine> | ||
*/ | ||
public function getFigurines(): array | ||
{ | ||
return $this->figurines; | ||
} | ||
|
||
public function getTotalPriceBuy(): int | ||
{ | ||
$total = 0; | ||
|
||
foreach ($this->figurines as $figurine) { | ||
$total += $figurine->getPriceBuy(); | ||
} | ||
|
||
return $total; | ||
} | ||
|
||
public function getTotalPriceSell(): float | ||
{ | ||
$total = 0; | ||
|
||
foreach ($this->figurines as $figurine) { | ||
$total += $figurine->getPriceSell(); | ||
} | ||
|
||
return $total; | ||
} | ||
|
||
public function getTotalDifference(): float | ||
{ | ||
return $this->getTotalPriceSell() - $this->getTotalPriceBuy(); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Challenges\COLLECTION_1; | ||
|
||
final class Figurine | ||
{ | ||
private const LIMIT_RARE = 2000; | ||
private const PRICE_BUY_RARE = 30; | ||
private const PRICE_BUY_NOT_RARE = 15; | ||
|
||
private int $priceBuy; | ||
|
||
public function __construct( | ||
private int $exemplaires, | ||
private float $cote | ||
) | ||
{ | ||
$this->calculPriceBuy(); | ||
} | ||
|
||
private function calculPriceBuy(): void | ||
{ | ||
if ($this->exemplaires < self::LIMIT_RARE) { | ||
$this->priceBuy = self::PRICE_BUY_RARE; | ||
return; | ||
} | ||
|
||
$this->priceBuy = self::PRICE_BUY_NOT_RARE; | ||
} | ||
|
||
public function getPriceBuy(): int | ||
{ | ||
return $this->priceBuy; | ||
} | ||
|
||
public function getPriceSell(): float | ||
{ | ||
return $this->priceBuy * $this->cote; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Challenges\COLLECTION_1\Collection; | ||
use Challenges\COLLECTION_1\Figurine; | ||
|
||
/** | ||
* Jeux de données | ||
*/ | ||
$exemplaires = [50, 5000]; | ||
$cotes = [1.5, 2.5]; | ||
$collection = new Collection($exemplaires, $cotes); | ||
|
||
test('on instancie bien des Figurines', function() use ($collection) { | ||
|
||
$this->assertInstanceOf( | ||
Figurine::class, | ||
$collection->getFigurines()[0] | ||
); | ||
}); | ||
|
||
test('total d\'achat', function() use ($collection) { | ||
|
||
$this->assertEquals( | ||
30 + 15, | ||
$collection->getTotalPriceBuy() | ||
); | ||
}); | ||
|
||
test('total du prix de vente', function() use ($collection) { | ||
|
||
$this->assertEquals( | ||
30 * 1.5 + 15 * 2.5, | ||
$collection->getTotalPriceSell() | ||
); | ||
}); | ||
|
||
/** | ||
* Jeux de données complexe issu de Tainix diretement | ||
*/ | ||
test('jeu de données complet', function() { | ||
|
||
$exemplaires = [50, 50, 50000, 2000, 50000, 2000, 2000, 2000, 50000, 2000, 2000, 50000, 50000, 2000, 2000, 2000, 50000]; | ||
$cotes = [2, 8, 1, 0.6, 1, 1.2, 1, 0.6, 0.6, 1, 1, 1, 0.8, 1.2, 1, 1, 0.6]; | ||
|
||
$collection = new Collection($exemplaires, $cotes); | ||
|
||
$this->assertEquals( | ||
219, // Réponse fournie par Tainix | ||
$collection->getTotalDifference() | ||
); | ||
}); |
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,68 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Challenges\COLLECTION_1\Figurine; | ||
|
||
test('un prix d\'achat à 15€', function() { | ||
|
||
$figurine = new Figurine(exemplaires: 5000, cote: 1); | ||
|
||
$this->assertEquals( | ||
15, | ||
$figurine->getPriceBuy() | ||
); | ||
}); | ||
|
||
test('un prix d\'achat à 30€', function() { | ||
|
||
$figurine = new Figurine(exemplaires: 500, cote: 1); | ||
|
||
$this->assertEquals( | ||
30, | ||
$figurine->getPriceBuy() | ||
); | ||
}); | ||
|
||
test('un prix d\'achat à 15€ pour plein de nombres d\'exemplaires', function($exemplaires) { | ||
|
||
$figurine = new Figurine(exemplaires: $exemplaires, cote: 1); | ||
|
||
$this->assertEquals( | ||
15, | ||
$figurine->getPriceBuy() | ||
); | ||
})->with( | ||
[2000, 2001, 2500, 5000, 10000, 50000] | ||
); | ||
|
||
test('un prix d\'achat à 30€ pour plein de nombres d\'exemplaires', function($exemplaires) { | ||
|
||
$figurine = new Figurine(exemplaires: $exemplaires, cote: 1); | ||
|
||
$this->assertEquals( | ||
30, | ||
$figurine->getPriceBuy() | ||
); | ||
})->with( | ||
[1, 20, 50, 500, 1999] | ||
); | ||
|
||
test('un prix de vente à partir d\'une figurine non rare', function() { | ||
|
||
$figurine = new Figurine(exemplaires: 5000, cote: 2.5); | ||
|
||
$this->assertEquals( | ||
15 * 2.5, | ||
$figurine->getPriceSell() | ||
); | ||
}); | ||
|
||
test('un prix de vente à partir d\'une figurine rare', function() { | ||
|
||
$figurine = new Figurine(exemplaires: 500, cote: 1.5); | ||
|
||
$this->assertEquals( | ||
30 * 1.5, | ||
$figurine->getPriceSell() | ||
); | ||
}); |
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,57 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Challenges\COLLECTION_1\Collection; | ||
use Challenges\COLLECTION_1\Figurine; | ||
|
||
final class CollectionTest extends TestCase | ||
{ | ||
private Collection $collection; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$exemplaires = [50, 5000]; | ||
$cotes = [1.5, 2.5]; | ||
$this->collection = new Collection($exemplaires, $cotes); | ||
} | ||
|
||
public function test_instanciation_figurine(): void | ||
{ | ||
$this->assertInstanceOf( | ||
Figurine::class, | ||
$this->collection->getFigurines()[0] | ||
); | ||
} | ||
|
||
public function test_total_achat(): void | ||
{ | ||
$this->assertEquals( | ||
30 + 15, | ||
$this->collection->getTotalPriceBuy() | ||
); | ||
} | ||
|
||
public function test_total_vente(): void | ||
{ | ||
$this->assertEquals( | ||
30 * 1.5 + 15 * 2.5, | ||
$this->collection->getTotalPriceSell() | ||
); | ||
} | ||
|
||
public function test_jeu_donnees_complet(): void | ||
{ | ||
$exemplaires = [50, 50, 50000, 2000, 50000, 2000, 2000, 2000, 50000, 2000, 2000, 50000, 50000, 2000, 2000, 2000, 50000]; | ||
$cotes = [2, 8, 1, 0.6, 1, 1.2, 1, 0.6, 0.6, 1, 1, 1, 0.8, 1.2, 1, 1, 0.6]; | ||
|
||
$collection = new Collection($exemplaires, $cotes); | ||
|
||
$this->assertEquals( | ||
219, // Réponse fournie par Tainix | ||
$collection->getTotalDifference() | ||
); | ||
} | ||
} |
Oops, something went wrong.