PHP Code:
require_once(LIMB_DIR . '/tests/cases/orm/AppPersistenceTest.class.php');
require_once(COCKTAIL_DIR . '/Cocktail.class.php');
require_once(COCKTAIL_DIR . '/Unit.class.php');
require_once(COCKTAIL_DIR . '/Ingredient.class.php');
class CocktailAndRecipeTest extends AppPersistenceTest
{
function _defineMapperName()
{
return 'CocktailMapper';
}
function _defineCleanupTables()
{
return array('sys_uid',
'cocktail',
'ingredient',
'unit',
'recipe_item');
}
function testCocktailRecipe()
{
$unit = new Unit();
$unit->setName("kg");
$unit->setFullName("Kilogram");
$ingr1 = new Ingredient();
$ingr1->setName("A Frog");
$ingr1->setUnit($unit);
$ingr2 = new Ingredient();
$ingr2->setName("A Fox");
$ingr2->setUnit($unit);
$cocktail = new Cocktail();
$cocktail->addIngredient($ingr1, $amount1 = 0.25, $order1 = 15);
$cocktail->addIngredient($ingr2, $amount2 = 1.15, $order2 = 10);
$this->_checkCocktailRecipe($cocktail,
array(array($ingr1, $amount1, $order1),
array($ingr2, $amount2, $order2)),
__LINE__);
$this->uow->register($cocktail);
$this->_commitAndReset();
$this->_checkDbRecordsAmount(1, 2, 2, __LINE__);
$cocktail_loaded = $this->mapper->findById($cocktail->getID());
$this->_checkCocktailRecipe($cocktail_loaded,
array(array($ingr1, $amount1, $order1),
array($ingr2, $amount2, $order2)),
__LINE__);
$cocktail_loaded->removeIngredient($ingr2);
$this->_commitAndReset();
$this->_checkDbRecordsAmount(1, 1, 2, __LINE__);
$cocktail_reloaded = $this->mapper->findById($cocktail->getID());
$this->_checkCocktailRecipe($cocktail_reloaded,array(array($ingr1, $amount1, $order1)),__LINE__);
$this->uow->delete($cocktail_reloaded);
$this->uow->commit();
$this->_checkDbRecordsAmount(0, 0, 2, __LINE__);
$this->assertFalse($this->uow->isRegistered($cocktail_reloaded));
}
function _checkCocktailRecipe($cocktail, $ingredients_array, $line)
{
$cocktail = ProxyResolver :: resolve($cocktail);
$recipe = $cocktail->getRecipe();
$i = 0;
foreach($recipe as $recipe_item)
{
$this->assertEqual($recipe_item->getIngredient()->getId(), $ingredients_array[$i][0]->getId(), '%s ' . $line);
$this->assertEqual($recipe_item->getAmount(), $ingredients_array[$i][1], '%s ' . $line);
$this->assertEqual($recipe_item->getPriority(), $ingredients_array[$i][2], '%s ' . $line);
$i++;
}
$this->assertEqual($i, sizeof($ingredients_array), '%s' . $line);
}
function _checkDbRecordsAmount($cocktails, $recipe_items, $ingredients, $line)
{
$result = $this->db->select('cocktail');
$this->assertEqual($result->getTotalRowCount(), $cocktails, '%s' . $line);
$result = $this->db->select('recipe_item');
$this->assertEqual($result->getTotalRowCount(), $recipe_items, '%s' . $line);
$result = $this->db->select('ingredient');
$this->assertEqual($result->getTotalRowCount(), $ingredients, '%s' . $line);
}
}
?>
We try to stick to vertical tests(using Marcus' terminology) as much as possible when developing end applications based on our own simple ORM solution. At the same time we have very detailed horizontal tests for base datamapping classes(UnitOfWork, AbstractDataMapper, etc). However sometimes we mix high level checks with low level ones, e.g we check UnitOfWork for registered objects and at the same time we check raw amount of inserted db records just to be on the safe side.
Bookmarks