Improved MixedRecipe

This commit is contained in:
Chris 2019-07-11 00:19:45 +02:00
parent 6cffe68da5
commit 796bd626c9
3 changed files with 105 additions and 7 deletions

View File

@ -7,15 +7,41 @@ use NoccyLabs\Juicer\Recipe\RecipeInterface;
class MixedRecipe class MixedRecipe
{ {
/** @var RecipeInterface The recipe that was mixed */
protected $recipe; protected $recipe;
protected $mixedIngredients = []; protected $mixedIngredients = [];
public function __construct(RecipeInterface $recipe, array $mixedIngredients) protected $base;
protected $volume;
protected $nicotine;
protected $totalFlavorMl;
protected $totalFlavorPercent;
protected $totalFlavorGrams;
protected $specificGravity;
public function __construct(RecipeInterface $recipe, array $mixedIngredients, Base $base, int $volume, int $nicotine)
{ {
$this->recipe = $recipe; $this->recipe = $recipe;
$this->mixedIngredients = $mixedIngredients; $this->mixedIngredients = $mixedIngredients;
$this->base = $base;
$this->volume = $volume;
$this->nicotine = $nicotine;
foreach ($mixedIngredients as $mixedIngredient) {
if (!in_array($mixedIngredient->getFlavorName(), ['PG','VG'])) {
$this->totalFlavorMl += $mixedIngredient->getVolume();
$this->totalFlavorGrams += $mixedIngredient->getWeight();
$this->totalFlavorPercent += $mixedIngredient->getPercent();
}
$this->specificGravity += $mixedIngredient->getSpecificGravity() * ($mixedIngredient->getPercent() / 100);
}
} }
public function getIngredients(): array public function getIngredients(): array
@ -23,6 +49,16 @@ class MixedRecipe
return $this->recipe->getIngredients(); return $this->recipe->getIngredients();
} }
public function getBase(): Base
{
return $this->base;
}
public function getVolume(): int
{
return $this->volume;
}
public function getMeasuredIngredients(): array public function getMeasuredIngredients(): array
{ {
return $this->mixedIngredients; return $this->mixedIngredients;
@ -30,21 +66,21 @@ class MixedRecipe
public function getTotalFlavorMl(): float public function getTotalFlavorMl(): float
{ {
return $this->totalFlavorMl;
} }
public function getTotalFlavorPercent(): float public function getTotalFlavorPercent(): float
{ {
return $this->totalFlavorPercent;
} }
public function getTotalFlavorGrams(): float public function getTotalFlavorGrams(): float
{ {
return $this->totalFlavorGrams;
} }
public function getSpecificGravity(): float public function getSpecificGravity(): float
{ {
return $this->specificGravity;
} }
} }

View File

@ -55,7 +55,7 @@ class Mixer
$mixed[] = new MeasuredIngredient($ingredient, $ingredientPercent, $volume * $floatPercent); $mixed[] = new MeasuredIngredient($ingredient, $ingredientPercent, $volume * $floatPercent);
} }
$mixedRecipe = new MixedRecipe($recipe, $mixed); $mixedRecipe = new MixedRecipe($recipe, $mixed, $base, $volume, $nicotineStrength);
return $mixedRecipe; return $mixedRecipe;
} }

View File

@ -0,0 +1,62 @@
<?php
namespace NoccyLabs\Juicer\Recipe\Mixer;
use NoccyLabs\Juicer\Recipe\Recipe;
use NoccyLabs\Juicer\Ingredient\Ingredient;
use NoccyLabs\Juicer\Ingredient\Base;
use NoccyLabs\Juicer\Recipe\RecipeInterface;
class MixedRecipeTest extends \PhpUnit\Framework\TestCase
{
public function getValuesForPropertyTest()
{
$ingredientA = new Ingredient("Ingredient A", null, 2);
$ingredientB = new Ingredient("Ingredient B", null, 3);
$ingredientC = new Ingredient("Ingredient B", null, 4);
$recipe1 = new Recipe();
$recipe1->setRecipeName("Recipe 1");
$recipe1->addIngredient($ingredientA);
$recipe1->addIngredient($ingredientB);
$percent1 = 5;
$recipe2 = new Recipe();
$recipe2->setRecipeName("Recipe 2");
$recipe2->addIngredient($ingredientB);
$recipe2->addIngredient($ingredientC);
$percent2 = 7;
$base1 = new Base("VG70");
$base2 = new Base("VG50");
$ml1 = 60;
$ml2 = 30;
$g1_1 = 0;
$g1_2 = 0;
$g2_1 = 0;
$g2_2 = 0;
return [
[ $recipe1, $base1, $percent1, $ml1, $g1_1 ],
[ $recipe1, $base2, $percent1, $ml1, $g1_2 ],
[ $recipe2, $base1, $percent2, $ml2, $g2_1 ],
[ $recipe2, $base2, $percent2, $ml2, $g2_2 ],
];
}
/**
* @dataProvider getValuesForPropertyTest
*/
public function testCalculatedPropertiesOfMixedRecipe(Recipe $recipe, $base, $expectedPercent, $expectedMl, $expectedG)
{
$mixer = new Mixer();
$mixed = $mixer->mixRecipe($recipe, $expectedMl, $base, 0, null);
$this->assertEquals($expectedMl, $mixed->getVolume());
$this->assertEquals($expectedPercent, $mixed->getTotalFlavorPercent());
}
}