Multiple changes and improvements

* Base and NicotineBase separated
* Importer bugfixed
* Mixer improved
This commit is contained in:
2019-07-09 02:10:56 +02:00
parent 6e662f0263
commit 922d9f09dd
14 changed files with 302 additions and 44 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace NoccyLabs\Juicer\Ingredient;
class NicotineBaseTest extends \PhpUnit\Framework\TestCase
{
public function getNicotineAsgData()
{
return [
[ 'PG100', 100, 1.035 ],
[ 'PG100', 60, 1.03632 ],
[ 'PG100', 50, 1.0366 ],
[ 'PG100', 48, 1.036656 ],
[ 'PG100', 36, 1.036992 ],
[ 'PG100', 24, 1.037328 ],
[ 'VG100', 100, 1.235 ],
[ 'VG100', 60, 1.245 ],
[ 'VG100', 50, 1.2475 ],
[ 'VG100', 48, 1.248 ],
[ 'VG100', 36, 1.251 ],
[ 'VG100', 24, 1.254 ],
];
}
/**
* @dataProvider getNicotineAsgData
*/
public function testThatTheApparentSpecificGravityIsCorect($base, $strength, $expected)
{
$nic = new NicotineBase(new Base($base), $strength);
$this->assertEquals(round($expected,3), round($nic->getSpecificGravity(),3));
}
}

View File

@ -5,6 +5,7 @@ namespace NoccyLabs\Juicer\Recipe\Importer;
use NoccyLabs\Juicer\Recipe\Recipe;
use NoccyLabs\Juicer\Ingredient\Ingredient;
use NoccyLabs\Juicer\Recipe\Exporter\JsonExporter;
use NoccyLabs\Juicer\Recipe\RecipeInterface;
class JsonImporterTest extends \PhpUnit\Framework\TestCase
{
@ -41,5 +42,14 @@ class JsonImporterTest extends \PhpUnit\Framework\TestCase
$this->assertEquals($recipe, $importedRecipe);
}
public function testThatRecipesCanBeImportedFromFile()
{
$importer = new JsonImporter();
$importedRecipe = $importer->readFromFile(__DIR__."/../../data/recipe1.json");
$this->assertInstanceOf(RecipeInterface::class, $importedRecipe);
$this->assertEquals("Recipe 1", $importedRecipe->getRecipeName());
}
}

View File

@ -5,55 +5,74 @@ 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 MixerTest extends \PhpUnit\Framework\TestCase
{
public function testMixingEmptyRecipesWithVg()
public function getDataForEmptyRecipes()
{
$recipe = new Recipe();
$mixer = new Mixer();
$base = new Base("VG100");
$mixed = $mixer->mixRecipe($recipe, 10, $base, 0);
$this->assertCount(1, $mixed);
$mixedVg = reset($mixed);
$this->assertEquals(10, $mixedVg->getVolume());
$this->assertEquals("VG", $mixedVg->getFlavorName());
return [
[ 'VG100', 10, [ 'VG' => 10 ], 10 ],
[ 'PG100', 10, [ 'PG' => 10 ], 10 ],
[ 'VG70', 10, [ 'VG' => 7, 'PG' => 3 ], 10 ],
[ 'VG50', 10, [ 'VG' => 5, 'PG' => 5 ], 10 ]
];
}
public function testMixingEmptyRecipesWithPg()
/**
* @dataProvider getDataForEmptyRecipes
*/
public function testMixingEmptyRecipes($base, $amount, array $assertBases, $assertAmount)
{
$recipe = new Recipe();
$mixer = new Mixer();
$base = new Base("PG100");
$mixed = $mixer->mixRecipe($recipe, 10, $base, 0);
$base = new Base($base);
$mixed = $mixer->mixRecipe($recipe, $amount, $base, 0);
$this->assertCount(1, $mixed);
$mixedPg = reset($mixed);
$this->assertEquals(10, $mixedPg->getVolume());
$this->assertEquals("PG", $mixedPg->getFlavorName());
$this->assertCount(count($assertBases), $mixed);
$mixedAmount = 0;
foreach ($mixed as $measured) {
$name = $measured->getFlavorName();
if (!array_key_exists($name, $assertBases)) {
$this->assertFail("Mixed contains unexpected ingredients");
}
$mixedAmount += $measured->getVolume();
}
$this->assertEquals($assertAmount, $mixedAmount);
}
public function testMixingEmptyRecipesWith70Vg30Pg()
public function getDataForDummyRecipes()
{
$ingredientA1 = new Ingredient("Ingredient A1", null, 2);
$ingredientA2 = new Ingredient("Ingredient A2", null, 3);
$recipeA = new Recipe();
$recipeA->setRecipeName("Recipe A");
$recipeA->addIngredient($ingredientA1);
$recipeA->addIngredient($ingredientA2);
return [
[ $recipeA, 'VG70', 10 ],
];
}
/**
* @dataProvider getDataForDummyRecipes
*/
public function testMixingDummyRecipes(RecipeInterface $recipe, $base, $amount)
{
$recipe = new Recipe();
$mixer = new Mixer();
$base = new Base($base);
$mixed = $mixer->mixRecipe($recipe, $amount, $base, 0);
$base = new Base("VG70");
$mixed = $mixer->mixRecipe($recipe, 10, $base, 0);
$expectedCount = count($base->getComponents()) + count($recipe->getIngredients());
$this->assertCount(2, $mixed);
$mixedVg = array_shift($mixed);
$this->assertEquals(10, $mixedVg->getVolume());
$this->assertEquals("VG", $mixedVg->getFlavorName());
$mixedPg = array_shift($mixed);
$this->assertEquals(10, $mixedPg->getVolume());
$this->assertEquals("PG", $mixedPg->getFlavorName());
$this->assertCount($expectedCount, $mixed);
$mixedAmount = 0;
foreach ($mixed as $measured) {
$mixedAmount += $measured->getVolume();
}
$this->assertEquals($amount, $mixedAmount);
}
}

9
tests/data/recipe1.json Normal file
View File

@ -0,0 +1,9 @@
{
"recipe": "Recipe 1",
"author": "Noccy",
"tags": [ "foo", "bar" ],
"ingredients": [
{ "flavor":"Flavor A", "brand":"Brand A", "percent":3 },
{ "flavor":"Flavor B", "brand":"Brand B", "percent":2 }
]
}