/** * @test */ public function shouldBuildNotExistsClause() { // when $result = WhereClause::notExists(Product::where(array('name' => 'phone'))); // then $this->assertEquals(array('phone'), $result->getParameters()); Assert::thatString($result->toSql())->startsWith('NOT EXISTS (SELECT')->endsWith('FROM products WHERE name = ?)'); }
/** * @test */ public function shouldTreatEmptyWhereClauseAsNothingWasGivenAsParameter() { // given Product::create(array('name' => 'one')); Product::create(array('name' => 'two')); // when $products = Product::where(new EmptyWhereClause())->fetchAll(); // then Assert::thatArray($products)->hasSize(2); }
/** * @test * @group postgres */ public function shouldBatchInsertWhenModelHasFetchedRelation() { //given $product1 = new Product(array('name' => 'product1')); $product2 = new Product(array('name' => 'product2')); $product1->category; $inserter = new BatchInserter(); $inserter->add($product1); $inserter->add($product2); //when $inserter->execute(); //then $this->assertNotNull(Product::where(array('name' => 'product1'))->fetch()); $this->assertNotNull(Product::where(array('name' => 'product2'))->fetch()); }
/** * @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()); }
/** * @group non-sqlite3 * @test */ public function shouldReturnModelUsingRegexpRestriction() { //given $product = Product::create(array('name' => 'name1', 'description' => 'desc')); Product::create(array('name' => 'other product', 'description' => 'desc')); //when $loadedProduct = Product::where(array('name' => Restrictions::regexp('ame')))->fetch(); //then Assert::thatModel($loadedProduct)->isEqualTo($product); }
/** * @group non-sqlite3 * @test */ public function shouldSelectForUpdate() { // given Product::create(array('name' => 'Sport', 'price' => '123')); // when $products = Product::where()->lockForUpdate()->fetchAll(); // then $this->assertCount(1, $products); }