Skip to content

Instantly share code, notes, and snippets.

@avalanche123
Created May 21, 2010 20:18
Show Gist options
  • Save avalanche123/409365 to your computer and use it in GitHub Desktop.
Save avalanche123/409365 to your computer and use it in GitHub Desktop.
<?php
public function testProductWithOptions() {
$product = new $this->entities['product']();
$product->setName($productName = 'Configurable T-Shirt');
$stock = array(
'small' => new StockItem('t-shirt S', 9.99, 15),
'medium' => new StockItem('t-shirt M', 12.99, 15),
'large' => new StockItem('t-shirt L', 14.99, 15),
);
foreach ($stock as $name => $item) {
$product->addOption($name, $item->getCost() + 3, $item);
}
$this->dm->persist($product);
$this->dm->flush();
$productId = $product->getId();
unset ($product);
$loadedProduct = $this->dm->find($this->entities['product'], $productId);
$this->assertEquals(3, count($loadedProduct->getOptions()));
$loadedProduct->selectOption('small');
$this->assertEquals(12.99, $loadedProduct->getPrice());
$loadedProduct->selectOption('medium');
$this->assertEquals(15.99, $loadedProduct->getPrice());
$loadedProduct->selectOption('large');
$this->assertEquals(17.99, $loadedProduct->getPrice(), '', 0.00000000000001); // if we remove the last parameter from the function, it fails
$stockItems = $this->dm->find($this->entities['stock_item']);
$this->assertEquals(3, $stockItems->count());
unset ($loadedProduct);
$stock['small']->setInventory(10);
$this->dm->flush();
unset ($stock);
$loadedProduct = $this->dm->find($this->entities['product'], $productId);
$this->assertSame(10, $loadedProduct->getOption('small')->getStockItem()->getInventory());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment