summaryrefslogtreecommitdiff
path: root/tests/simple_unit/ActiveRecord
diff options
context:
space:
mode:
Diffstat (limited to 'tests/simple_unit/ActiveRecord')
-rw-r--r--tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php83
-rw-r--r--tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php15
-rw-r--r--tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php211
-rw-r--r--tests/simple_unit/ActiveRecord/records/ItemRecord.php49
4 files changed, 316 insertions, 42 deletions
diff --git a/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php b/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php
index 5ab89206..fbae5a4b 100644
--- a/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php
+++ b/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php
@@ -1,5 +1,4 @@
-<?php
-
+<?php
Prado::using('System.Data.ActiveRecord.TActiveRecordStateRegistry');
Prado::using('System.Data.ActiveRecord.Exceptions.*');
@@ -16,10 +15,10 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$obj = new StateTestObject();
$registry = new TActiveRecordStateRegistry();
- $this->assertTrue($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertTrue($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
}
function test_clean_object_registers_without_error()
@@ -28,10 +27,10 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$registry = new TActiveRecordStateRegistry();
$registry->registerClean($obj);
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertTrue($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertTrue($registry->isCleanObject($obj));
}
function test_clean_object_becomes_dirty_when_changed()
@@ -43,10 +42,10 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$obj->propB='b';
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertTrue($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertTrue($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
}
function test_removed_object_must_register_as_clean_first()
@@ -73,10 +72,10 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$registry->registerRemoved($obj);
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertTrue($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertTrue($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
}
@@ -98,10 +97,10 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$this->pass();
}
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertTrue($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertTrue($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
}
function test_remove_dirty_object()
@@ -113,17 +112,17 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$obj->propB='b';
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertTrue($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertTrue($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
$registry->registerRemoved($obj);
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertTrue($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertTrue($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
try
{
@@ -143,24 +142,24 @@ class ActiveRecordRegistryTestCase extends UnitTestCase
$registry->registerClean($obj);
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertTrue($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertTrue($registry->isCleanObject($obj));
$obj->propB='b';
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertTrue($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertFalse($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertTrue($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertFalse($registry->isCleanObject($obj));
$registry->registerClean($obj);
- $this->assertFalse($registry->getIsNewObject($obj));
- $this->assertFalse($registry->getIsDirtyObject($obj));
- $this->assertFalse($registry->getIsRemovedObject($obj));
- $this->assertTrue($registry->getIsCleanObject($obj));
+ $this->assertFalse($registry->isNewObject($obj));
+ $this->assertFalse($registry->isDirtyObject($obj));
+ $this->assertFalse($registry->isRemovedObject($obj));
+ $this->assertTrue($registry->isCleanObject($obj));
}
}
diff --git a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
index 8c39a797..0c1da5d2 100644
--- a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
+++ b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
@@ -1,6 +1,7 @@
<?php
Prado::using('System.Data.ActiveRecord.TActiveRecord');
+require_once(dirname(__FILE__).'/records/ItemRecord.php');
abstract class SqliteRecord extends TActiveRecord
{
@@ -153,6 +154,20 @@ class ForeignKeyTestCase extends UnitTestCase
$this->assertEqual($album->cover->content, 'lalala');
}
+
+ function test_self_reference_fk()
+ {
+ $item = ItemRecord::finder()->withRelated_Items()->findByPk(1);
+ $this->assertNotNull($item);
+ $this->assertEqual($item->name, "Professional Work Attire");
+
+ $this->assertEqual(count($item->related_items),2);
+ $this->assertEqual($item->related_items[0]->name, "Nametags");
+ $this->assertEqual($item->related_items[0]->item_id, 2);
+
+ $this->assertEqual($item->related_items[1]->name, "Grooming and Hygiene");
+ $this->assertEqual($item->related_items[1]->item_id, 3);
+ }
}
?> \ No newline at end of file
diff --git a/tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php b/tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php
new file mode 100644
index 00000000..77b9cdf3
--- /dev/null
+++ b/tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php
@@ -0,0 +1,211 @@
+<?php
+Prado::using('System.Data.ActiveRecord.TActiveRecord');
+
+class BaseFkRecord extends TActiveRecord
+{
+ public function getDbConnection()
+ {
+ static $conn;
+ if($conn===null)
+ {
+ $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test');
+ //$this->OnExecuteCommand[] = array($this,'logger');
+ }
+ return $conn;
+ }
+
+ function logger($sender,$param)
+ {
+ }
+}
+
+class TeamRecord extends BaseFkRecord
+{
+ const TABLE='teams';
+ public $name;
+ public $location;
+
+ public $players=array();
+
+ //define the $player member having has many relationship with PlayerRecord
+ protected static $RELATIONS=array
+ (
+ 'players' => array(self::HAS_MANY, 'PlayerRecord'),
+ );
+
+ public static function finder($className=__CLASS__)
+ {
+ return parent::finder($className);
+ }
+}
+
+class PlayerRecord extends BaseFkRecord
+{
+ const TABLE='players';
+ public $player_id;
+ public $age;
+ public $team_name;
+
+ public $team;
+ public $skills=array();
+ public $profile;
+
+ protected static $RELATIONS=array
+ (
+ 'skills' => array(self::HAS_MANY, 'SkillRecord', 'player_skills'),
+ 'team' => array(self::BELONGS_TO, 'TeamRecord'),
+ 'profile' => array(self::HAS_ONE, 'ProfileRecord'),
+ );
+
+ public static function finder($className=__CLASS__)
+ {
+ return parent::finder($className);
+ }
+}
+
+class ProfileRecord extends BaseFkRecord
+{
+ const TABLE='profiles';
+ public $fk_player_id;
+ public $salary;
+
+ public $player;
+
+ protected static $RELATIONS=array
+ (
+ 'player' => array(self::BELONGS_TO, 'PlayerRecord'),
+ );
+
+ public static function finder($className=__CLASS__)
+ {
+ return parent::finder($className);
+ }
+}
+
+class SkillRecord extends BaseFkRecord
+{
+ const TABLE='skills';
+ public $skill_id;
+ public $name;
+
+ public $players=array();
+
+ protected static $RELATIONS=array
+ (
+ 'players' => array(self::HAS_MANY, 'PlayerRecord', 'player_skills'),
+ );
+
+ public static function finder($className=__CLASS__)
+ {
+ return parent::finder($className);
+ }
+}
+
+class ForeignObjectUpdateTest extends UnitTestCase
+{
+ function test_add_has_one()
+ {
+ ProfileRecord::finder()->deleteByPk(3);
+
+ $player = PlayerRecord::finder()->findByPk(3);
+ $player->profile = new ProfileRecord(array('salary'=>50000));
+ $player->save();
+
+ //test insert
+ $player2 = PlayerRecord::finder()->withProfile()->findByPk(3);
+ $this->assertEqual($player2->profile->salary,50000);
+
+ $player2->profile->salary = 45000;
+ $player2->save();
+ $this->assertEqual($player2->profile->salary,45000);
+
+ //test update
+ $player3 = PlayerRecord::finder()->withProfile()->findByPk(3);
+ $this->assertEqual($player3->profile->salary,45000);
+ }
+
+ function test_add_many()
+ {
+ PlayerRecord::finder()->deleteAll("player_id > ?", 3);
+
+ $team = TeamRecord::finder()->findByPk('Team b');
+ $team->players[] = new PlayerRecord(array('age'=>20));
+ $team->players[] = new PlayerRecord(array('age'=>25));
+ $team->save();
+
+ //test insert
+ $team1 = TeamRecord::finder()->withPlayers()->findByPk('Team b');
+ $this->assertEqual(count($team1->players),3);
+ $this->assertEqual($team1->players[0]->age, 18);
+ $this->assertEqual($team1->players[1]->age, 20);
+ $this->assertEqual($team1->players[2]->age, 25);
+
+ //test update
+ $team1->players[1]->age = 55;
+ $team1->save();
+
+ $this->assertEqual($team1->players[0]->age, 18);
+ $this->assertEqual($team1->players[1]->age, 55);
+ $this->assertEqual($team1->players[2]->age, 25);
+
+ $criteria = new TActiveRecordCriteria();
+ $criteria->OrdersBy['age'] = 'desc';
+ $team2 = TeamRecord::finder()->withPlayers($criteria)->findByPk('Team b');
+ $this->assertEqual(count($team2->players),3);
+ //ordered by age
+ $this->assertEqual($team2->players[0]->age, 55);
+ $this->assertEqual($team2->players[1]->age, 25);
+ $this->assertEqual($team2->players[2]->age, 18);
+ }
+
+ function test_add_belongs_to()
+ {
+ TeamRecord::finder()->deleteByPk('Team c');
+ PlayerRecord::finder()->deleteAll("player_id > ?", 3);
+
+ $player = new PlayerRecord(array('age'=>27));
+ $player->team = new TeamRecord(array('name'=>'Team c', 'location'=>'Sydney'));
+ $player->save();
+
+ //test insert
+ $player1 = PlayerRecord::finder()->withTeam()->findByAge(27);
+ $this->assertNotNull($player1);
+ $this->assertNotNull($player1->team);
+ $this->assertEqual($player1->team->name, 'Team c');
+ $this->assertEqual($player1->team->location, 'Sydney');
+ }
+
+ function test_add_many_via_association()
+ {
+ PlayerRecord::finder()->deleteAll("player_id > ?", 3);
+ SkillRecord::finder()->deleteAll("skill_id > ?", 3);
+
+ $player = new PlayerRecord(array('age'=>37));
+ $player->skills[] = new SkillRecord(array('name'=>'Bash'));
+ $player->skills[] = new SkillRecord(array('name'=>'Jump'));
+ $player->save();
+
+ //test insert
+ $player2 = PlayerRecord::finder()->withSkills()->findByAge(37);
+ $this->assertNotNull($player2);
+ $this->assertEqual(count($player2->skills), 2);
+ $this->assertEqual($player2->skills[0]->name, 'Bash');
+ $this->assertEqual($player2->skills[1]->name, 'Jump');
+
+ //test update
+ $player2->skills[1]->name = "Skip";
+ $player2->skills[] = new SkillRecord(array('name'=>'Push'));
+ $player2->save();
+
+ $criteria = new TActiveRecordCriteria();
+ $criteria->OrdersBy['name'] = 'asc';
+ $player3 = PlayerRecord::finder()->withSkills($criteria)->findByAge(37);
+ $this->assertNotNull($player3);
+ $this->assertEqual(count($player3->skills), 3);
+ $this->assertEqual($player3->skills[0]->name, 'Bash');
+ $this->assertEqual($player3->skills[1]->name, 'Push');
+ $this->assertEqual($player3->skills[2]->name, 'Skip');
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/simple_unit/ActiveRecord/records/ItemRecord.php b/tests/simple_unit/ActiveRecord/records/ItemRecord.php
new file mode 100644
index 00000000..189d22c9
--- /dev/null
+++ b/tests/simple_unit/ActiveRecord/records/ItemRecord.php
@@ -0,0 +1,49 @@
+<?php
+
+class ItemRecord extends TActiveRecord
+{
+ const TABLE='items';
+ public $item_id;
+ public $name;
+ public $brand_specific;
+ public $description;
+ public $meta;
+ public $active;
+ public $need_review;
+ public $category_id;
+ public $type_id;
+ public $content;
+ public $standard_id;
+ public $timestamp;
+
+ public $related_items = array();
+ public $related_item_id;
+
+ protected static $RELATIONS=array
+ (
+ 'related_items' => array(self::HAS_MANY, 'ItemRecord', 'related_items.(related_item_id)'),
+ );
+
+ public function getDbConnection()
+ {
+ static $conn;
+ if($conn===null)
+ {
+ $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test');
+ $this->OnExecuteCommand[] = array($this,'logger');
+ }
+ return $conn;
+ }
+
+ public function logger($sender,$param)
+ {
+ //var_dump($param->Command->Text);
+ }
+
+ public static function finder($className=__CLASS__)
+ {
+ return parent::finder($className);
+ }
+}
+
+?> \ No newline at end of file