Exemplo n.º 1
0
 public function set_up($connection_name = null)
 {
     ActiveRecord\Table::clear_cache();
     $config = ActiveRecord\Config::instance();
     $this->original_default_connection = $config->get_default_connection();
     $this->original_date_class = $config->get_date_class();
     if ($connection_name) {
         $config->set_default_connection($connection_name);
     }
     if ($connection_name == 'sqlite' || $config->get_default_connection() == 'sqlite') {
         // need to create the db. the adapter specifically does not create it for us.
         static::$db = substr(ActiveRecord\Config::instance()->get_connection('sqlite'), 9);
         new SQLite3(static::$db);
     }
     $this->connection_name = $connection_name;
     try {
         $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name);
     } catch (ActiveRecord\DatabaseException $e) {
         $this->mark_test_skipped($connection_name . ' failed to connect. ' . $e->getMessage());
     }
     $GLOBALS['ACTIVERECORD_LOG'] = false;
     $loader = new DatabaseLoader($this->conn);
     $loader->reset_table_data();
     if (self::$log) {
         $GLOBALS['ACTIVERECORD_LOG'] = true;
     }
 }
Exemplo n.º 2
0
 public function set_up($connection_name = null)
 {
     ActiveRecord\Table::clear_cache();
     $config = ActiveRecord\Config::instance();
     $this->original_default_connection = $config->get_default_connection();
     if ($connection_name) {
         $config->set_default_connection($connection_name);
     }
     if ($connection_name == 'sqlite' || $config->get_default_connection() == 'sqlite') {
         // need to create the db. the adapter specifically does not create it for us.
         $this->db = substr(ActiveRecord\Config::instance()->get_connection('sqlite'), 9);
         new SQLite3($this->db);
     }
     $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name);
     $GLOBALS['ACTIVERECORD_LOG'] = false;
     $loader = new DatabaseLoader($this->conn);
     $loader->reset_table_data();
     if (self::$log) {
         $GLOBALS['ACTIVERECORD_LOG'] = true;
     }
 }
 public function test_gh_23_relationships_with_joins_to_same_table_should_alias_table_name()
 {
     $old = Book::$belongs_to;
     Book::$belongs_to = array(array('from_', 'class_name' => 'Author', 'foreign_key' => 'author_id'), array('to', 'class_name' => 'Author', 'foreign_key' => 'secondary_author_id'), array('another', 'class_name' => 'Author', 'foreign_key' => 'secondary_author_id'));
     $c = ActiveRecord\Table::load('Book')->conn;
     $select = "books.*, authors.name as to_author_name, {$c->quote_name('from_')}.name as from_author_name, {$c->quote_name('another')}.name as another_author_name";
     $book = Book::find(2, array('joins' => array('to', 'from_', 'another'), 'select' => $select));
     $this->assert_not_null($book->from_author_name);
     $this->assert_not_null($book->to_author_name);
     $this->assert_not_null($book->another_author_name);
     Book::$belongs_to = $old;
 }
 public function testGroup()
 {
     $venues = Venue::all(array('select' => 'state', 'group' => 'state'));
     $this->assertTrue(count($venues) > 0);
     $this->assertSqlHas('GROUP BY state', ActiveRecord\Table::load('Venue')->lastSql);
 }
 public function test_group_with_order_and_limit_and_having()
 {
     $venues = Venue::all(array('select' => 'state', 'group' => 'state', 'having' => 'length(state) = 2', 'order' => 'state', 'limit' => 2));
     $this->assert_true(count($venues) > 0);
     $this->assert_true(strpos(ActiveRecord\Table::load('Venue')->last_sql, 'GROUP BY state HAVING length(state) = 2 ORDER BY state LIMIT 0,2') !== false);
 }
Exemplo n.º 6
0
 public function test_clear_cache_for_specific_class()
 {
     $book_table1 = ActiveRecord\Table::load('Book');
     $book_table2 = ActiveRecord\Table::load('Book');
     ActiveRecord\Table::clear_cache('Book');
     $book_table3 = ActiveRecord\Table::load('Book');
     $this->assert_true($book_table1 === $book_table2);
     $this->assert_true($book_table1 !== $book_table3);
 }
Exemplo n.º 7
0
 public function test_before_validation_returned_false_halts_execution()
 {
     VenueCB::$before_validation = array('before_validation_halt_execution');
     ActiveRecord\Table::clear_cache('VenueCB');
     $table = ActiveRecord\Table::load('VenueCB');
     $v = VenueCB::find(1);
     $v->name .= 'test';
     $ret = $v->save();
     $this->assert_false($ret);
     $this->assert_true(strpos(ActiveRecord\Table::load('VenueCB')->last_sql, 'UPDATE') === false);
 }
 public function testGh23RelationshipsWithJoinsToSameTableShouldAliasTableName()
 {
     $old = Book::$belongsTo;
     Book::$belongsTo = array(array('from_', 'className' => 'Author', 'foreignKey' => 'author_id'), array('to', 'className' => 'Author', 'foreignKey' => 'secondary_author_id'), array('another', 'className' => 'Author', 'foreignKey' => 'secondary_author_id'));
     $c = ActiveRecord\Table::load('Book')->conn;
     $select = "books.*, authors.name as to_author_name, {$c->quoteName('from_')}.name as from_author_name, {$c->quoteName('another')}.name as another_author_name";
     $book = Book::find(2, array('joins' => array('to', 'from_', 'another'), 'select' => $select));
     $this->assertNotNull($book->from_author_name);
     $this->assertNotNull($book->to_author_name);
     $this->assertNotNull($book->another_author_name);
     Book::$belongsTo = $old;
 }
 public function test_has_many_through_with_conditions()
 {
     Event::$belongs_to = array(array('host'));
     Venue::$has_many[1] = array('hosts', 'through' => 'events', 'conditions' => array('events.title != ?', 'Love Overboard'));
     $venue = $this->get_relationship();
     $this->assert_true(count($venue->hosts) === 1);
     $this->assert_true(strpos(ActiveRecord\Table::load('Host')->last_sql, "events.title !=") !== false);
 }
Exemplo n.º 10
0
 public function test_gh_28_after_create_should_be_invoked_after_auto_incrementing_pk_is_set()
 {
     $that = $this;
     VenueCB::$after_create = function ($model) use($that) {
         $that->assert_not_null($model->id);
     };
     ActiveRecord\Table::clear_cache('VenueCB');
     $venue = VenueCB::find(1);
     $venue = new VenueCB($venue->attributes());
     $venue->id = null;
     $venue->name = 'alksdjfs';
     $venue->save();
 }
 public function testClearCacheForSpecificClass()
 {
     $bookTable1 = ActiveRecord\Table::load('Book');
     $bookTable2 = ActiveRecord\Table::load('Book');
     ActiveRecord\Table::clearCache('Book');
     $bookTable3 = ActiveRecord\Table::load('Book');
     $this->assertTrue($bookTable1 === $bookTable2);
     $this->assertTrue($bookTable1 !== $bookTable3);
 }
 public function testBeforeValidationReturnedFalseHaltsExecution()
 {
     VenueCB::$beforeValidation = array('beforeValidationHaltExecution');
     ActiveRecord\Table::clearCache('VenueCB');
     $table = ActiveRecord\Table::load('VenueCB');
     $v = VenueCB::find(1);
     $v->name .= 'test';
     $ret = $v->save();
     $this->assertFalse($ret);
     $this->assertTrue(strpos(ActiveRecord\Table::load('VenueCB')->lastSql, 'UPDATE') === false);
 }