summaryrefslogtreecommitdiff
path: root/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
diff options
context:
space:
mode:
authorwei <>2007-04-24 06:14:56 +0000
committerwei <>2007-04-24 06:14:56 +0000
commitd5eb713888715e8f18d2ccf508a8eb0b1a483ad1 (patch)
tree3752f12f0a9379681e13171df805e8f5760c53ec /tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
parent1c74ee3c07cd2b25670826d44f7a1b1959302ce3 (diff)
add active record Relations
Diffstat (limited to 'tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php')
-rw-r--r--tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php144
1 files changed, 94 insertions, 50 deletions
diff --git a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
index 6c240b2f..8c39a797 100644
--- a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
+++ b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
@@ -2,24 +2,32 @@
Prado::using('System.Data.ActiveRecord.TActiveRecord');
-abstract class Mysql4Record extends TActiveRecord
+abstract class SqliteRecord extends TActiveRecord
{
protected static $conn;
public function getDbConnection()
{
if(self::$conn===null)
- self::$conn = new TDbConnection('mysql:host=localhost;port=3306;dbname=tests', 'test4', 'test4');
+ self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/fk_tests.db');
return self::$conn;
}
}
-class Album extends Mysql4Record
+class Album extends SqliteRecord
{
public $title;
- public $Tracks = array(self::HAS_MANY, 'Track');
- public $Artists = array(self::HAS_MANY, 'Artist', 'album_artist');
+ public $Tracks = array();
+ public $Artists = array();
+
+ public $cover;
+
+ protected static $RELATIONS = array(
+ 'Tracks' => array(self::HAS_MANY, 'Track'),
+ 'Artists' => array(self::HAS_MANY, 'Artist', 'album_artists'),
+ 'cover' => array(self::HAS_ONE, 'Cover')
+ );
public static function finder($class=__CLASS__)
{
@@ -27,11 +35,15 @@ class Album extends Mysql4Record
}
}
-class Artist extends Mysql4Record
+class Artist extends SqliteRecord
{
public $name;
- public $Albums = array(self::HAS_MANY, 'Album', 'album_artist');
+ public $Albums = array();
+
+ protected static $RELATIONS=array(
+ 'Albums' => array(self::HAS_MANY, 'Album', 'album_artists')
+ );
public static function finder($class=__CLASS__)
{
@@ -39,13 +51,17 @@ class Artist extends Mysql4Record
}
}
-class Track extends Mysql4Record
+class Track extends SqliteRecord
{
public $id;
public $song_name;
public $album_id; //FK -> Album.id
- public $Album = array(self::BELONGS_TO, 'Album');
+ public $Album;
+
+ protected static $RELATIONS = array(
+ 'Album' => array(self::BELONGS_TO, 'Album'),
+ );
public static function finder($class=__CLASS__)
{
@@ -53,61 +69,89 @@ class Track extends Mysql4Record
}
}
-abstract class SqliteRecord extends TActiveRecord
+class Cover extends SqliteRecord
{
- protected static $conn;
-
- public function getDbConnection()
- {
- if(self::$conn===null)
- self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/blog.db');
- return self::$conn;
- }
+ public $album;
+ public $content;
}
-class PostRecord extends SqliteRecord
+class ForeignKeyTestCase extends UnitTestCase
{
- const TABLE='posts';
- public $post_id;
- public $author;
- public $create_time;
- public $title;
- public $content;
- public $status;
+ function test_has_many()
+ {
+ $albums = Album::finder()->withTracks()->findAll();
+ $this->assertEqual(count($albums), 2);
+
+ $this->assertEqual($albums[0]->title, 'Album 1');
+ $this->assertEqual($albums[1]->title, 'Album 2');
+
+ $this->assertEqual(count($albums[0]->Artists), 0);
+ $this->assertEqual(count($albums[1]->Artists), 0);
- public $authorRecord = array(self::HAS_ONE, 'BlogUserRecord');
+ $this->assertEqual(count($albums[0]->Tracks), 3);
+ $this->assertEqual(count($albums[1]->Tracks), 2);
- public static function finder($className=__CLASS__)
+ $this->assertEqual($albums[0]->Tracks[0]->song_name, 'Track 1');
+ $this->assertEqual($albums[0]->Tracks[1]->song_name, 'Song 2');
+ $this->assertEqual($albums[0]->Tracks[2]->song_name, 'Song 3');
+
+ $this->assertEqual($albums[1]->Tracks[0]->song_name, 'Track A');
+ $this->assertEqual($albums[1]->Tracks[1]->song_name, 'Track B');
+ }
+
+ function test_has_one()
{
- return parent::finder($className);
+ $albums = Album::finder()->with_cover()->findAll();
+ $this->assertEqual(count($albums), 2);
+
+ $this->assertEqual($albums[0]->title, 'Album 1');
+ $this->assertEqual($albums[1]->title, 'Album 2');
+
+ $this->assertEqual($albums[0]->cover->content, 'lalala');
+ $this->assertEqual($albums[1]->cover->content, 'conver content');
+
+ $this->assertEqual(count($albums[0]->Artists), 0);
+ $this->assertEqual(count($albums[1]->Artists), 0);
+
+ $this->assertEqual(count($albums[0]->Tracks), 0);
+ $this->assertEqual(count($albums[1]->Tracks), 0);
}
-}
-class BlogUserRecord extends SqliteRecord
-{
- const TABLE='users';
- public $username;
- public $email;
- public $password;
- public $role;
- public $first_name;
- public $last_name;
- public $posts = array(self::HAS_MANY, 'PostRecord');
+ function test_belongs_to()
+ {
+ $track = Track::finder()->withAlbum()->find('id = ?', 1);
+
+ $this->assertEqual($track->id, "1");
+ $this->assertEqual($track->song_name, "Track 1");
+ $this->assertEqual($track->Album->title, "Album 1");
+ }
- public static function finder($className=__CLASS__)
+ function test_has_many_associate()
{
- return parent::finder($className);
+ $album = Album::finder()->withArtists()->find('title = ?', 'Album 2');
+ $this->assertEqual($album->title, 'Album 2');
+ $this->assertEqual(count($album->Artists), 3);
+
+ $this->assertEqual($album->Artists[0]->name, 'Dan');
+ $this->assertEqual($album->Artists[1]->name, 'Karl');
+ $this->assertEqual($album->Artists[2]->name, 'Tom');
}
-}
-class ForeignKeyTestCase extends UnitTestCase
-{
- function test()
+ function test_multiple_fk()
{
- $album = Album::finder()->withTracks()->findAll();
- //print_r($album);
- //print_r(PostRecord::finder()->findAll());
- //print_r(BlogUserRecord::finder()->with_posts()->findAll());
+ $album = Album::finder()->withArtists()->withTracks()->with_cover()->find('title = ?', 'Album 1');
+
+ $this->assertEqual($album->title, 'Album 1');
+ $this->assertEqual(count($album->Artists), 2);
+
+ $this->assertEqual($album->Artists[0]->name, 'Dan');
+ $this->assertEqual($album->Artists[1]->name, 'Jenny');
+
+ $this->assertEqual($album->Tracks[0]->song_name, 'Track 1');
+ $this->assertEqual($album->Tracks[1]->song_name, 'Song 2');
+ $this->assertEqual($album->Tracks[2]->song_name, 'Song 3');
+
+ $this->assertEqual($album->cover->content, 'lalala');
}
}