/** * @test */ public function shouldHandleRightExclusiveMode() { //given $restriction = Restrictions::between(1, 3, Between::RIGHT_EXCLUSIVE); //when $sql = $restriction->toSql('key'); //then $this->assertEquals('(key >= ? AND key < ?)', $sql); }
/** * @test */ public function shouldCreateSql() { //given $restriction = Restrictions::isNotNull(); //when $sql = $restriction->toSql('category_id'); //then $this->assertEquals('category_id IS NOT NULL', $sql); }
/** * @test */ public function shouldReturnNullForEmptyArray() { //given $restriction = Restrictions::isNotIn(array()); //when $sql = $restriction->toSql('category_id'); //then $this->assertNull($sql); }
/** * @test */ public function shouldCreateProperSql() { //given $restriction = Restrictions::greaterOrEqualTo(5); //when $sql = $restriction->toSql('key'); //then $this->assertEquals('key >= ?', $sql); $this->assertEquals(array(5), $restriction->getValues()); }
/** * @test */ public function shouldCreateProperSql() { //given $restriction = Restrictions::like('value'); //when $sql = $restriction->toSql('key'); //then $this->assertEquals('key LIKE ?', $sql); $this->assertEquals(array('value'), $restriction->getValues()); }
/** * @test */ public function shouldCreateProperSqlForEmptyString() { //given $restriction = Restrictions::notEqualTo(''); //when $sql = $restriction->toSql('key'); //then $this->assertEquals('key <> ?', $sql); $this->assertEquals(array(''), $restriction->getValues()); }
/** * @test */ public function shouldCreateProperSqlForSqlite() { //given Config::overrideProperty('sql_dialect')->with('\\Ouzo\\Db\\Dialect\\Sqlite3Dialect'); $restriction = Restrictions::regexp('value'); //when $sql = $restriction->toSql('key'); //then $this->assertEquals('key REGEXP ?', $sql); $this->assertEquals(array('value'), $restriction->getValues()); }
public function __construct($where, $operator = 'AND') { foreach ($where as $column => $value) { if ($value === null) { $where[$column] = Restrictions::isNull(); } } $this->where = $where; $this->values = Arrays::flatten(array_values($where)); $this->operator = $operator; }
/** * @return Event[] */ public static function loadNew() { $lastEventId = Session::get('last_event_id'); if (!$lastEventId) { //do not load events that we triggered before this session was started /** @var Event $lastEvent */ $lastEvent = Event::queryBuilder()->limit(1)->order('id desc')->fetch(); Session::set('last_event_id', $lastEvent ? $lastEvent->id : 0); } $events = Event::where(['id' => Restrictions::greaterThan($lastEventId)])->order('id asc')->fetchAll(); if ($events) { Session::set('last_event_id', Arrays::last($events)->id); } return $events; }
/** * @test */ public function shouldSearchAnyOfAndWhereValues() { //given $category = Category::create(array('name' => 'shop')); $product1 = Product::create(array('name' => 'notebook', 'description' => 'notebook desc', 'id_category' => $category->getId())); $product2 = Product::create(array('name' => 'tablet', 'description' => 'tablet desc', 'id_category' => $category->getId())); Product::create(array('name' => 'pc', 'description' => 'pc desc')); //when $products = Product::where(Any::of(array('name' => 'tablet', 'description' => Restrictions::like('%desc'))))->where(array('id_category' => $category->getId()))->fetchAll(); //then Assert::thatArray($products)->hasSize(2)->onProperty('id')->containsExactly($product1->getId(), $product2->getId()); }
/** * @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 shouldNotAddParenthesisToSingleOfRestriction() { // when $result = WhereClause::create(array('a' => Restrictions::equalTo('b'))); // then $this->assertInstanceOf('\\Ouzo\\Db\\WhereClause\\ArrayWhereClause', $result); $this->assertEquals('a = ?', $result->toSql()); }