summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorwei <>2007-10-08 03:24:07 +0000
committerwei <>2007-10-08 03:24:07 +0000
commit4ceba82b9863f2c6323cbe00407e4bfbedbfc1cd (patch)
treebc93498b5bf77b25c5a2d5a87ffaa4f8a9b70728 /tests
parentaedbfe04daaaa0baa8e97c4d7d13e8a1f9d867cb (diff)
Allow active records to have multiple foreign key references to the same table. Add TXCache.
Diffstat (limited to 'tests')
-rw-r--r--tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php9
-rw-r--r--tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php5
-rw-r--r--tests/simple_unit/ActiveRecord/MultipleForeignKeyTestCase.php179
-rw-r--r--tests/simple_unit/ActiveRecord/records/ItemRecord.php2
-rw-r--r--tests/simple_unit/ActiveRecord/test1.sqlitebin0 -> 6144 bytes
-rw-r--r--tests/test_tools/simpletest/test_case.php3
6 files changed, 191 insertions, 7 deletions
diff --git a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
index cbde7fa0..2e4bee2d 100644
--- a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
+++ b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
@@ -26,7 +26,7 @@ class Album extends SqliteRecord
public static $RELATIONS = array(
'Tracks' => array(self::HAS_MANY, 'Track'),
- 'Artists' => array(self::HAS_MANY, 'Artist', 'album_artists'),
+ 'Artists' => array(self::MANY_TO_MANY, 'Artist', 'album_artists'),
'cover' => array(self::HAS_ONE, 'Cover')
);
@@ -42,8 +42,9 @@ class Artist extends SqliteRecord
public $Albums = array();
- public static $RELATIONS=array(
- 'Albums' => array(self::HAS_MANY, 'Album', 'album_artists')
+ public static $RELATIONS=array
+ (
+ 'Albums' => array(self::MANY_TO_MANY, 'Album', 'album_artists')
);
public static function finder($class=__CLASS__)
@@ -158,6 +159,7 @@ class ForeignKeyTestCase extends UnitTestCase
function test_self_reference_fk()
{
$item = ItemRecord::finder()->withRelated_Items()->findByPk(1);
+
$this->assertNotNull($item);
$this->assertEqual($item->name, "Professional Work Attire");
@@ -168,6 +170,7 @@ class ForeignKeyTestCase extends UnitTestCase
$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
index 36864f77..026efb4d 100644
--- a/tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php
+++ b/tests/simple_unit/ActiveRecord/ForeignObjectUpdateTest.php
@@ -52,7 +52,7 @@ class PlayerRecord extends BaseFkRecord
public static $RELATIONS=array
(
- 'skills' => array(self::HAS_MANY, 'SkillRecord', 'player_skills'),
+ 'skills' => array(self::MANY_TO_MANY, 'SkillRecord', 'player_skills'),
'team' => array(self::BELONGS_TO, 'TeamRecord'),
'profile' => array(self::HAS_ONE, 'ProfileRecord'),
);
@@ -112,7 +112,7 @@ class SkillRecord extends BaseFkRecord
public static $RELATIONS=array
(
- 'players' => array(self::HAS_MANY, 'PlayerRecord', 'player_skills'),
+ 'players' => array(self::MANY_TO_MANY, 'PlayerRecord', 'player_skills'),
);
public static function finder($className=__CLASS__)
@@ -236,6 +236,7 @@ class ForeignObjectUpdateTest extends UnitTestCase
$this->assertEqual($player4->skills[1]->name, 'Skip');
$this->assertEqual($player4->skills[2]->name, 'Push');
}
+//*/
}
?> \ No newline at end of file
diff --git a/tests/simple_unit/ActiveRecord/MultipleForeignKeyTestCase.php b/tests/simple_unit/ActiveRecord/MultipleForeignKeyTestCase.php
new file mode 100644
index 00000000..16036b9f
--- /dev/null
+++ b/tests/simple_unit/ActiveRecord/MultipleForeignKeyTestCase.php
@@ -0,0 +1,179 @@
+<?php
+
+Prado::using('System.Data.ActiveRecord.TActiveRecord');
+
+abstract class MultipleFKSqliteRecord extends TActiveRecord
+{
+ protected static $conn;
+
+ public function getDbConnection()
+ {
+ if(self::$conn===null)
+ self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/test1.sqlite');
+ return self::$conn;
+ }
+}
+
+/**
+ *
+CREATE TABLE table1 (
+id integer PRIMARY KEY AUTOINCREMENT,
+field1 varchar,
+fk1 integer CONSTRAINT fk_id1 REFERENCES table2(id) ON DELETE CASCADE,
+fk2 integer CONSTRAINT fk_id2 REFERENCES table2(id) ON DELETE CASCADE,
+fk3 integer CONSTRAINT fk_id3 REFERENCES table2(id) ON DELETE CASCADE)
+ */
+class Table1 extends MultipleFKSqliteRecord
+{
+ public $id;
+ public $field1;
+ public $fk1;
+ public $fk2;
+ public $fk3;
+
+ public $object1;
+ public $object2;
+ public $object3;
+
+ public static $RELATIONS = array
+ (
+ 'object1' => array(self::BELONGS_TO, 'Table2', 'fk1'),
+ 'object2' => array(self::BELONGS_TO, 'Table2', 'fk2'),
+ 'object3' => array(self::BELONGS_TO, 'Table2', 'fk3'),
+ );
+
+ public static function finder($class=__CLASS__)
+ {
+ return parent::finder($class);
+ }
+}
+
+/**
+ * CREATE TABLE table2 (id integer PRIMARY KEY AUTOINCREMENT,field1 varchar)
+ */
+class Table2 extends MultipleFKSqliteRecord
+{
+ public $id;
+ public $field1;
+
+ private $_state1;
+ public $state2;
+ public $state3;
+
+ public static $RELATIONS = array
+ (
+ 'state1' => array(self::HAS_MANY, 'Table1', 'fk1'),
+ 'state2' => array(self::HAS_MANY, 'Table1', 'fk2'),
+ 'state3' => array(self::HAS_ONE, 'Table1', 'fk3'),
+ );
+
+ public function setState1($obj)
+ {
+ $this->_state1 = $obj;
+ }
+
+ public function getState1()
+ {
+ if(is_null($this->_state1))
+ $this->fetchResultsFor('state1');
+ return $this->_state1;
+ }
+
+ public static function finder($class=__CLASS__)
+ {
+ return parent::finder($class);
+ }
+}
+
+
+class Category extends MultipleFKSqliteRecord
+{
+ public $cat_id;
+ public $category_name;
+ public $parent_cat;
+
+ public $parent_category;
+ public $child_categories=array();
+
+ public static $RELATIONS=array
+ (
+ 'parent_category' => array(self::BELONGS_TO, 'Category'),
+ 'child_categories' => array(self::HAS_MANY, 'Category'),
+ );
+
+ public static function finder($class=__CLASS__)
+ {
+ return parent::finder($class);
+ }
+}
+
+class MultipleForeignKeyTestCase extends UnitTestCase
+{
+ function testBelongsTo()
+ {
+ $obj = Table1::finder()->withObject1()->findAll();
+ $this->assertEqual(count($obj), 3);
+ $this->assertEqual($obj[0]->id, '1');
+ $this->assertEqual($obj[1]->id, '2');
+ $this->assertEqual($obj[2]->id, '3');
+
+ $this->assertEqual($obj[0]->object1->id, '1');
+ $this->assertEqual($obj[1]->object1->id, '2');
+ $this->assertEqual($obj[2]->object1->id, '2');
+ }
+
+ function testHasMany()
+ {
+ $obj = Table2::finder()->withState1()->findAll();
+ $this->assertEqual(count($obj), 5);
+
+ $this->assertEqual(count($obj[0]->state1), 1);
+ $this->assertEqual($obj[0]->state1[0]->id, '1');
+
+ $this->assertEqual(count($obj[1]->state1), 2);
+ $this->assertEqual($obj[1]->state1[0]->id, '2');
+ $this->assertEqual($obj[1]->state1[1]->id, '3');
+
+ $this->assertEqual(count($obj[2]->state1), 0);
+ $this->assertEqual($obj[2]->id, '3');
+
+ $this->assertEqual(count($obj[3]->state1), 0);
+ $this->assertEqual($obj[3]->id, '4');
+ }
+
+ function testHasOne()
+ {
+ $obj = Table2::finder()->withState3('id = 3')->findAll();
+
+ $this->assertEqual(count($obj), 5);
+
+ $this->assertEqual($obj[0]->id, '1');
+ $this->assertNull($obj[0]->state3);
+
+ $this->assertEqual($obj[1]->id, '2');
+ $this->assertNull($obj[1]->state3);
+
+ $this->assertEqual($obj[2]->id, '3');
+ $this->assertNotNull($obj[2]->state3);
+ $this->assertEqual($obj[2]->state3->id, '3');
+
+ $this->assertEqual($obj[3]->id, '4');
+ $this->assertNull($obj[3]->state3);
+ }
+
+ function testParentChild()
+ {
+ $obj = Category::finder()->withChild_Categories()->withParent_Category()->findByPk(2);
+
+ $this->assertEqual($obj->cat_id, '2');
+ $this->assertEqual(count($obj->child_categories), 2);
+ $this->assertNotNull($obj->parent_category);
+
+ $this->assertEqual($obj->child_categories[0]->cat_id, 3);
+ $this->assertEqual($obj->child_categories[1]->cat_id, 4);
+
+ $this->assertEqual($obj->parent_category->cat_id, 1);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/simple_unit/ActiveRecord/records/ItemRecord.php b/tests/simple_unit/ActiveRecord/records/ItemRecord.php
index 45d15427..52a1a658 100644
--- a/tests/simple_unit/ActiveRecord/records/ItemRecord.php
+++ b/tests/simple_unit/ActiveRecord/records/ItemRecord.php
@@ -21,7 +21,7 @@ class ItemRecord extends TActiveRecord
public static $RELATIONS=array
(
- 'related_items' => array(self::HAS_MANY, 'ItemRecord', 'related_items.(related_item_id)'),
+ 'related_items' => array(self::MANY_TO_MANY, 'ItemRecord', 'related_items.related_item_id'),
);
public function getDbConnection()
diff --git a/tests/simple_unit/ActiveRecord/test1.sqlite b/tests/simple_unit/ActiveRecord/test1.sqlite
new file mode 100644
index 00000000..1e056b52
--- /dev/null
+++ b/tests/simple_unit/ActiveRecord/test1.sqlite
Binary files differ
diff --git a/tests/test_tools/simpletest/test_case.php b/tests/test_tools/simpletest/test_case.php
index bc215640..bc4f0c42 100644
--- a/tests/test_tools/simpletest/test_case.php
+++ b/tests/test_tools/simpletest/test_case.php
@@ -240,7 +240,8 @@
'Unexpected exception of type [' . get_class($exception) .
'] with message ['. $exception->getMessage() .
'] in ['. $exception->getFile() .
- '] line [' . $exception->getLine() . ']');
+ '] line [' . $exception->getLine() .
+ '] stack [' . $exception->getTraceAsString() .']');
}
/**