public function setUp() { parent::setUp(); $this->_category = Category::create(array('name' => 'sony')); Product::create(array('name' => 'a', 'id_category' => $this->_category->getId())); Product::create(array('name' => 'c', 'id_category' => $this->_category->getId())); Product::create(array('name' => 'b', 'id_category' => $this->_category->getId())); }
/** * @test */ public function shouldCallDbFunction() { //given $category = Category::create(array('name' => 'test')); //when $name = $category->getName('test'); //then $this->assertEquals('test', $name); }
/** * @test */ public function shouldNotCompareRelations() { $cars = Category::create(array('name' => 'phones')); $product = Product::create(array('name' => 'Reno', 'id_category' => $cars->getId())); $productWithoutLoadedCategory = Product::findById($product->getId()); // when relation is loaded $product->category; //then Assert::thatModel($product)->hasSameAttributesAs($productWithoutLoadedCategory); }
/** * @test */ public function shouldFetchIteratorAndFetchRelationsInBatches() { //given Product::create(array('name' => '1', 'id_category' => Category::create(array('name' => 'cat1'))->getId())); Product::create(array('name' => '2', 'id_category' => Category::create(array('name' => 'cat2'))->getId())); Product::create(array('name' => '3', 'id_category' => Category::create(array('name' => 'cat3'))->getId())); Stats::reset(); //when $results = Product::where()->with('category')->fetchIterator(2); //then Assert::thatArray(iterator_to_array($results))->extracting('name')->containsExactly('1', '2', '3'); $this->assertEquals(3, Stats::getNumberOfQueries()); }
/** * @test */ public function shouldFetchHasManyJoinWithArrayCondition() { //given $category = Category::create(array('name' => 'samsung')); Product::create(array('name' => 'cris', 'id_category' => $category->getId())); //when $searchCategory = Category::innerJoin('products_name_bob')->fetchAll(); //then Assert::thatArray($searchCategory)->hasSize(1)->onProperty('name')->containsOnly('sony'); }
/** * @test */ public function shouldAcceptSingleParamInFindBySql() { //given Category::create(array('name' => 'phones1')); Category::create(array('name' => 'phones2')); //when $found = Category::findBySql("SELECT * FROM categories where name = ?", 'phones1'); //then Assert::thatArray($found)->onProperty('name')->containsOnly('phones1'); }