====== 12. Cvičení - Řešení ====== Řešení ke stažení: {{:courses:b6b39zwa:tutorials:solutions:12:cart-reseni.zip|ZIP}} Řešení je velmi jednoduché - část je již obsažena ve cvičení, zbytek jsou jen následující dva soubory: assertIsA($cart, 'SerializableCart'); // test na singletona $this->assertReference($cart, SerializableCart::getACart()); $this->dump($cart); } /** * Metoda testuje, zda je mozne ziskat instanci OrdinaryCart */ function testGetOrdinaryCart() { $this->expectError(); // POZOR, tohle skonci vyjimkou - OrdinaryCart ma spatne implementovanou metodu getACart! $cart = OrdinaryCart::getACart(); // test, zda vznikla spravna instance $this->assertIsA($cart, 'OrdinaryCart'); // test na singletona $this->assertReference($cart, SerializableCart::getACart()); } } ?> cart = SerializableCart::getACart(); // mock objekt pro knihu Mock::generate('Book'); $this->book = new MockBook(); $this->book->setReturnValue('getId', 1); // mock objekt pro vzducholod Mock::generate('Zeppelin'); $this->zeppelin = new MockZeppelin(); $this->zeppelin->setReturnValue('getId', 2); // mock objekt pro naradi Mock::generate('Tool'); $this->tool = new MockTool(); $this->tool->setReturnValue('getId', 3); } // uklid prostredi function tearDown() { $this->book = null; $this->zeppelin = null; $this->cart = null; } function testAddToCart() { $this->cart->addToCart($this->book, 10); $this->cart->addToCart($this->zeppelin, 55); $this->cart->addToCart($this->zeppelin, 45); $this->cart->addToCart($this->book, 90); $this->dump($this->cart); $this->assertEqual($this->cart->getItemQty($this->book), 100); $this->assertEqual($this->cart->getItemQty($this->zeppelin), 100); $this->assertEqual($this->cart->getItemQty($this->tool), 0); } function testRemoveFromCart() { $this->cart->removeFromCart($this->book, 30); $this->cart->removeFromCart($this->zeppelin, 55); $this->cart->removeFromCart($this->book, 20); $this->cart->removeFromCart($this->tool, 1000); $this->dump($this->cart); $this->assertEqual($this->cart->getItemQty($this->book), 50); $this->assertEqual($this->cart->getItemQty($this->zeppelin), 45); $this->assertEqual($this->cart->getItemQty($this->tool), 0); } function testMovingItemsBackAndForth() { $this->cart->addToCart($this->book, 10); $this->cart->removeFromCart($this->book, 30); $this->cart->removeFromCart($this->zeppelin, 55); $this->cart->addToCart($this->tool, 1000); $this->cart->removeFromCart($this->tool, 1000); $this->cart->addToCart($this->zeppelin, 55); $this->cart->addToCart($this->zeppelin, 45); $this->cart->removeFromCart($this->book, 20); $this->cart->addToCart($this->book, 90); $this->dump($this->cart); $this->assertEqual($this->cart->getItemQty($this->book), 100); $this->assertEqual($this->cart->getItemQty($this->zeppelin), 100); $this->assertEqual($this->cart->getItemQty($this->tool), 0); } } ?>