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()));
 }
Example #2
0
 /**
  * @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);
 }
Example #3
0
 /**
  * @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);
 }
Example #4
0
 /**
  * @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());
 }
Example #5
0
 /**
  * @test
  */
 public function shouldFetchIteratorWrappedWithBatching()
 {
     // given
     $numberOfItems = 100;
     $chunkSize = 20;
     for ($i = 0; $i < $numberOfItems; $i++) {
         Product::create(array('name' => sprintf('p%03d', $i)));
     }
     // when
     $iterator = new BatchingIterator(Db::getInstance()->query('SELECT * FROM products ORDER BY name ASC')->fetchIterator(), $chunkSize);
     // then
     $batches = 0;
     $items = 0;
     foreach ($iterator as $products) {
         foreach ($products as $product) {
             $this->assertEquals(sprintf('p%03d', $items), $product['name'], "Product name {$i} does not match");
             $items++;
         }
         $batches++;
     }
     $this->assertEquals($numberOfItems, $items);
     $this->assertEquals($numberOfItems, $chunkSize * $batches);
 }
Example #6
0
 /**
  * @test
  */
 public function shouldHandleSubQueries()
 {
     //given
     Product::create(array('name' => 'prod1', 'description' => 'd'));
     Product::create(array('name' => 'prod1', 'description' => 'd'));
     Product::create(array('name' => 'prod2', 'description' => 'd'));
     $query = Query::select(array('count(*) AS c'))->from(Query::select(array('name', 'count(*) c'))->from('products')->groupBy('name')->where(array('description' => 'd')), 'sub')->where(array('c' => 2));
     $executor = QueryExecutor::prepare(Db::getInstance(), $query);
     //when
     $result = $executor->fetch();
     //then
     $this->assertEquals(array('c' => 1), $result);
 }
Example #7
0
 /**
  * @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());
 }
Example #8
0
 /**
  * @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);
 }
 /**
  * @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');
 }
Example #10
0
 /**
  * @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);
 }