/** * DB\Model::belongs_to */ public function test_belongs_to() { // find by primary key $library = CCUnit\Model_Library::assign(array('name' => 'Sci-Fi'))->save(); $person = CCUnit\Model_DBPerson::assign(array('name' => 'Mario', 'age' => '20', 'library_id' => $library->id))->save(); $library_from_person = $person->library(); $this->assertTrue($library_from_person instanceof DB\Model_Relation_BelongsTo); $library_from_person = $library_from_person->run(); $this->assertTrue($library_from_person instanceof CCUnit\Model_Library); $this->assertEquals($library_from_person, $library); }
/** * DB\Model::find */ public function test_find() { // find by primary key $model = CCUnit\Model_DBPerson::assign(array('name' => 'peter_fox', 'age' => '30', 'library_id' => 0))->save(); $this->assertEquals($model->name, CCUnit\Model_DBPerson::find($model->id)->name); // find by diffrent key $this->assertEquals($model->id, CCUnit\Model_DBPerson::find('name', $model->name)->id); // find with callback $person = CCUnit\Model_DBPerson::find(function ($q) { $q->where('name', 'peter_fox'); $q->where('age', 30); $q->limit(1); }); $this->assertEquals($model->id, $person->id); // find no limit $person = CCUnit\Model_DBPerson::find(function ($q) { $q->where('name', 'peter_fox'); $q->where('age', 30); }); $this->assertEquals($model->id, reset($person)->id); // find all $people = CCUnit\Model_DBPerson::find(); $this->assertTrue(reset($people) instanceof CCUnit\Model_DBPerson); // did we got the primary key foreach ($people as $id => $person) { $this->assertEquals($id, $person->id); } }