/** * @test */ public function shouldExtractInlineRelation() { //given $root = OrderProduct::metaInstance(); $inlineRelation = new Relation('orderProduct', 'Test\\OrderProduct', 'id', 'id_product', false); //when $relations = ModelQueryBuilderHelper::extractRelations($root, $inlineRelation); //then Assert::thatArray($relations)->containsExactly($inlineRelation); }
/** * @group postgres * @test */ public function shouldBatchInsertWithoutReturning() { //given $product1 = Product::create(array('name' => 'product1')); $order1 = Order::create(array('name' => 'order1')); $product2 = Product::create(array('name' => 'product2')); $order2 = Order::create(array('name' => 'order2')); $inserter = new BatchInserter(); $inserter->add(new OrderProduct(array('id_order' => $order1->getId(), 'id_product' => $product1->getId()))); $inserter->add(new OrderProduct(array('id_order' => $order2->getId(), 'id_product' => $product2->getId()))); //when $inserter->execute(); //then $this->assertNotNull(OrderProduct::where(array('id_order' => $order1->getId()))->fetch()); $this->assertNotNull(OrderProduct::where(array('id_order' => $order2->getId()))->fetch()); }
/** * @test */ public function shouldOptimizeDuplicatedRelationFetches() { //given $category = Category::create(array('name' => 'phones')); $manufacturer = Manufacturer::create(array('name' => 'sony')); $product = Product::create(array('name' => 'sony', 'id_category' => $category->getId(), 'id_manufacturer' => $manufacturer->id)); OrderProduct::create(array('id_product' => $product->getId())); Stats::reset(); //when $orderProduct = OrderProduct::where()->with('product->category')->with('product->manufacturer')->fetch(); //then $this->assertEquals($product->id, $orderProduct->product->id); $this->assertEquals($category, $orderProduct->product->category); $this->assertEquals($manufacturer, $orderProduct->product->manufacturer); $this->assertEquals(4, Stats::getNumberOfQueries()); }
/** * @test */ public function shouldJoinMultipleModels() { //given $category1 = Category::create(array('name' => 'phones')); $product1 = Product::create(array('name' => 'sony', 'id_category' => $category1->getId())); $order1 = Order::create(array('name' => 'order#1')); OrderProduct::create(array('id_order' => $order1->getId(), 'id_product' => $product1->getId())); $category2 = Category::create(array('name' => 'phones')); $product2 = Product::create(array('name' => 'sony', 'id_category' => $category2->getId())); $order2 = Order::create(array('name' => 'order#2')); OrderProduct::create(array('id_order' => $order2->getId(), 'id_product' => $product2->getId())); //when $orderProducts = OrderProduct::join('product')->join('order')->where(array('products.id' => $product1->getId()))->fetchAll(); //then $this->assertCount(1, $orderProducts); $find = Arrays::first($orderProducts); $this->assertEquals('order#1', $find->order->name); $this->assertEquals('sony', $find->product->name); $this->assertEquals('phones', $find->product->category->name); }