Inheritance: extends Ouzo\Model
 /**
  * @test
  */
 public function shouldReturnDescription()
 {
     //given
     $model = new Product(array('name' => 'product1'));
     $matcher = new ModelAttributesMatcher($model);
     //when
     $description = $matcher->__toString();
     //then
     $attributes = print_r($model->attributes(), true);
     $this->assertEquals("Application\\Model\\Test\\Product with attributes({$attributes})", $description);
 }
Esempio n. 2
0
 /**
  * @test
  */
 public function shouldAddErrorClassIfClassGiven()
 {
     //given
     $product = new Product(array());
     $product->validate();
     $formBuilder = new ModelFormBuilder($product);
     //when
     $html = $formBuilder->textField('name', array('class' => 'class1 class2'));
     //then
     $this->assertContains('class="class1 class2 error"', $html);
 }
Esempio n. 3
0
 /**
  * @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 = ?)');
 }
 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()));
 }
Esempio n. 5
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);
 }
Esempio n. 6
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);
 }
Esempio n. 7
0
 /**
  * @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());
 }
Esempio n. 8
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);
 }
Esempio n. 9
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);
 }
Esempio n. 10
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());
 }
Esempio n. 11
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');
 }
Esempio n. 13
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);
 }