Ejemplo n.º 1
0
 public function test_products_inherit_discounts_from_their_categories()
 {
     $parent = Factory::create(new Category(), ['is_inheriting' => true]);
     $first = Factory::create(new Discount(), ['is_percentage' => true, 'amount' => 50]);
     $first->categories()->attach($parent);
     $child = Factory::create(new Category(), ['parent_id' => $parent->id]);
     $second = Factory::create(new Discount(), ['is_percentage' => true, 'amount' => 50]);
     $second->categories()->attach($child);
     $orphan = Factory::create(new Category());
     $third = Factory::create(new Discount(), ['is_percentage' => true, 'amount' => 50]);
     $third->categories()->attach($orphan);
     $product = Factory::create(new Product());
     $product->categories()->sync([$parent->id, $child->id]);
     $product->save();
     $this->assertEquals(1, Price::whereProductId($product->id)->whereDiscountId($first->id)->count());
     $this->assertEquals(1, Price::whereProductId($product->id)->whereDiscountId($second->id)->count());
     $this->assertEquals(0, Price::whereProductId($product->id)->whereDiscountId($third->id)->count());
     $product->discounts()->attach($third);
     $product->save();
     $this->assertEquals(1, Price::whereProductId($product->id)->whereDiscountId($third->id)->count());
 }