Skip to content

Commit

Permalink
Résolution et tests unitaire du challenge COLLECTION_1
Browse files Browse the repository at this point in the history
2 objets créés :
- Figurine
- Collection

Tests réalisés avec PEST et PHPUnit

Mise à jour du readme
  • Loading branch information
TainixCode committed Nov 20, 2022
1 parent 395c5d6 commit 53e880d
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
@@ -1,7 +1,7 @@
# PHP-Sandbox
Corrigés des challenges, avec tests unitaires réalisés avec PHPUnit.
Corrigés des challenges, avec tests unitaires réalisés avec PHPUnit ou Pest PHP.

### Challenges corrigés
### Challenges corrigés PHPUnit
[Découverte de PHPUnit #1 : Pierre Feuille Ciseaux](https://tainix.fr/code/Tests-unitaires-en-PHP-1-prendre-en-main-PHPUnit).

[Découverte de PHPUnit #2 : FOOTBALL 3](https://tainix.fr/code/Tests-unitaires-en-PHP-2-prendre-en-main-PHPUnit).
Expand All @@ -10,4 +10,7 @@ Corrigés des challenges, avec tests unitaires réalisés avec PHPUnit.

[Refacto en appliquant le S de SOLID : WALL_E V2 ](https://tainix.fr/code/Refactoring-de-code-PHP-en-appliquant-un-principe-de-SOLID).

[Expérimentation des enums : TRAIN_1](https://tainix.fr/code/Utilisation-des-enums-avec-PHP-8-1).
[Expérimentation des enums : TRAIN_1](https://tainix.fr/code/Utilisation-des-enums-avec-PHP-8-1).

### Challenges corrigés Pest
[Découverte de Pest #1 : Collectionneur de figurines](https://tainix.fr/code/Tests-unitaires-en-PHP-1-prendre-en-main-Pest).
61 changes: 61 additions & 0 deletions challenges/COLLECTION_1/Collection.php
@@ -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();
}
}
41 changes: 41 additions & 0 deletions challenges/COLLECTION_1/Figurine.php
@@ -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;
}
}
52 changes: 52 additions & 0 deletions pest/COLLECTION_1/CollectionTest.php
@@ -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()
);
});
68 changes: 68 additions & 0 deletions pest/COLLECTION_1/FigurineTest.php
@@ -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()
);
});
57 changes: 57 additions & 0 deletions phpunit/COLLECTION_1/CollectionTest.php
@@ -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()
);
}
}

0 comments on commit 53e880d

Please sign in to comment.