/**
  * Tests the default relationship information provided by the backend data source.
  *
  * @return void
  */
 public function testDefaultRelationshipInfo()
 {
     $connection = $this->_connection;
     $message = "Relationships are not supported by this adapter.";
     $this->skipIf(!$connection::enabled('relationships'), $message);
     $this->assertEqual(array('Employeess'), array_keys(Companies::relations()));
     $this->assertEqual(array('Companies'), array_keys(Employees::relations()));
     $this->assertEqual(array('Employeess'), Companies::relations('hasMany'));
     $this->assertEqual(array('Companies'), Employees::relations('belongsTo'));
     $this->assertFalse(Companies::relations('belongsTo'));
     $this->assertFalse(Companies::relations('hasOne'));
     $this->assertFalse(Employees::relations('hasMany'));
     $this->assertFalse(Employees::relations('hasOne'));
     $result = Companies::relations('Employeess');
     $this->assertEqual('hasMany', $result->data('type'));
     $this->assertEqual($this->_classes['employees'], $result->data('to'));
 }
 public function testResultSet()
 {
     Employees::config(array('meta' => array('connection' => 'test')));
     Companies::config(array('meta' => array('connection' => 'test')));
     Connections::get('test')->read('DROP TABLE IF EXISTS employees;');
     $sql = "CREATE TABLE employees (id int,title varchar(100))";
     Connections::get('test')->read($sql);
     for ($i = 1; $i < 9; $i++) {
         $sql = "INSERT INTO employees (id, title) VALUES ({$i}, 'Title {$i}')";
         Connections::get('test')->read($sql);
     }
     $employees = Employees::all();
     $cpt = 0;
     foreach ($employees as $employee) {
         $cpt++;
         $this->assertEqual($cpt, $employee->id);
     }
     $this->assertEqual(8, $cpt);
     $this->assertEqual(8, count($employees));
     Employees::reset();
     Companies::reset();
     $base = Libraries::get(true, 'resources') . '/tmp/tests';
     $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
     Connections::add('sqlite_file', array('type' => 'database', 'adapter' => 'Sqlite3', 'database' => "{$base}/sqlite_file.sq3", 'database' => ':memory:', 'encoding' => 'UTF-8'));
     Employees::config(array('meta' => array('connection' => 'sqlite_file')));
     Companies::config(array('meta' => array('connection' => 'sqlite_file')));
     Connections::get('sqlite_file')->read('DROP TABLE IF EXISTS employees;');
     $sql = "CREATE TABLE employees (id int,title varchar(100))";
     Connections::get('sqlite_file')->read($sql);
     for ($i = 1; $i < 9; $i++) {
         $sql = "INSERT INTO employees (id, title) VALUES ({$i}, 'Title {$i}')";
         Connections::get('sqlite_file')->read($sql);
     }
     $employees = Employees::all();
     $cpt = 0;
     foreach ($employees as $employee) {
         $cpt++;
         $this->assertEqual($cpt, $employee->id);
     }
     $this->assertEqual(8, $cpt);
     $this->assertEqual(8, count($employees));
     $this->_cleanUp();
     Connections::get('test')->read('DROP TABLE employees;');
     Connections::get('sqlite_file')->read('DROP TABLE employees;');
 }
Example #3
0
 public function testRelationshipQuerying()
 {
     $connection = $this->_connection;
     $message = "Relationships are not supported by this adapter.";
     $this->skipIf(!$connection::enabled('relationships'), $message);
     foreach ($this->companiesData as $data) {
         Companies::create($data)->save();
     }
     $stuffMart = Companies::findFirstByName('StuffMart');
     $maAndPas = Companies::findFirstByName('Ma \'n Pa\'s Data Warehousing & Bait Shop');
     $this->assertEqual($this->_classes['employees'], $stuffMart->employees->model());
     $this->assertEqual($this->_classes['employees'], $maAndPas->employees->model());
     foreach (array('Mr. Smith', 'Mr. Jones', 'Mr. Brown') as $name) {
         $stuffMart->employees[] = Employees::create(compact('name'));
     }
     $expected = Companies::key($stuffMart) + array('name' => 'StuffMart', 'active' => true, 'employees' => array(array('name' => 'Mr. Smith'), array('name' => 'Mr. Jones'), array('name' => 'Mr. Brown')));
     $this->assertEqual($expected, $stuffMart->data());
     $this->assertTrue($stuffMart->save());
     $this->assertEqual('Smith', $stuffMart->employees[0]->lastName());
     $stuffMartReloaded = Companies::findFirstByName('StuffMart');
     $this->assertEqual('Smith', $stuffMartReloaded->employees[0]->lastName());
     foreach (array('Ma', 'Pa') as $name) {
         $maAndPas->employees[] = Employees::create(compact('name'));
     }
     $maAndPas->save();
 }