Skip to content

Commit

Permalink
Tests: Fixed relations tests and did some initial setup for an idea o…
Browse files Browse the repository at this point in the history
…n HasMany:through relations
  • Loading branch information
vlucas committed Mar 12, 2010
1 parent 5b58288 commit f5a133a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
5 changes: 3 additions & 2 deletions tests/Fixture/Blog/Comments/Mapper.php
Expand Up @@ -3,10 +3,11 @@
* Blog Comments Mapper
* @todo implement 'BelongsTo' relation for linking back to blog post object
*/
class Fixture_Blog_Comments_Mapper extends TestMapper {
class Fixture_Blog_Comments_Mapper extends TestMapper
{
protected $source = 'test_blog_comments';

public $id = array('type' => 'int', 'primary' => true);
public $id = array('type' => 'int', 'primary' => true, 'serial' => true);
public $post_id = array('type' => 'int', 'index' => true, 'required' => true);
public $name = array('type' => 'string', 'required' => true);
public $email = array('type' => 'string', 'required' => true);
Expand Down
14 changes: 12 additions & 2 deletions tests/Fixture/Blog/Mapper.php
Expand Up @@ -2,10 +2,11 @@
/**
* Blog Mapper
*/
class Fixture_Blog_Mapper extends TestMapper {
class Fixture_Blog_Mapper extends TestMapper
{
protected $source = 'test_blog';

public $id = array('type' => 'int', 'primary' => true);
public $id = array('type' => 'int', 'primary' => true, 'serial' => true);
public $title = array('type' => 'string', 'required' => true);
public $body = array('type' => 'text', 'required' => true);
public $date_created = array('type' => 'datetime');
Expand All @@ -18,4 +19,13 @@ class Fixture_Blog_Mapper extends TestMapper {
'where' => array('post_id' => 'entity.id'),
'order' => array('date_created' => 'ASC')
);

// Each post entity 'hasMany' tags through a 'post_tags' relationship
public $tags = array(
'type' => 'relation',
'relation' => 'HasMany',
'mapper' => 'Fixture_Blog_Tags_Mapper',
'where' => array('post_id' => 'entity.id'),
'through' => 'post_tags'
);
}
28 changes: 28 additions & 0 deletions tests/Fixture/Blog/Tags/Mapper.php
@@ -0,0 +1,28 @@
<?php
/**
* Blog Tags Mapper
* Relates tags to blog posts through proxy table
*/
class Fixture_Blog_Tags_Mapper extends TestMapper
{
protected $source = 'test_blog_tags';

public $post_id = array('type' => 'int', 'primary' => true, 'serial' => true);
public $tag_id = array('type' => 'int', 'key' => true);

public $post_tags = array(
'type' => 'relation',
'relation' => 'HasMany',
'mapper' => 'Fixture_Tag_Mapper',
'where' => array('tag_id' => 'entity.tag_id')
);

/*
public $post = array(
'type' => 'relation',
'relation' => 'BelongsTo',
'mapper' => 'Fixture_Blog_Mapper',
'where' => array('post_id' => 'entity.post_id')
);
*/
}
11 changes: 11 additions & 0 deletions tests/Fixture/Tags/Mapper.php
@@ -0,0 +1,11 @@
<?php
/**
* Tags Mapper
*/
class Fixture_Tags_Mapper extends TestMapper
{
protected $source = 'test_tags';

public $id = array('type' => 'int', 'primary' => true);
public $name = array('type' => 'string', 'required' => true, 'unique' => true);
}
25 changes: 15 additions & 10 deletions tests/RelationsTest.php
Expand Up @@ -40,28 +40,33 @@ public function testBlogPostInsert()
/**
* @depends testBlogPostInsert
*/
public function testBlogCommentsRelationInsertByArray($postId)
public function testBlogCommentsRelationInsertByObject($postId)
{
$post = $this->blogMapper->get($postId);
$commentMapper = fixture_mapper('Blog_Comments');

// Array will usually come from POST/JSON data or other source
$post->comments = array(
0 => array(
$commentSaved = false;
$comment = $commentMapper->get()
->data(array(
'post_id' => $postId,
'name' => 'Testy McTester',
'email' => 'test@test.com',
'body' => 'This is a test comment. Yay!',
'date_created' => date($this->blogMapper->adapter()->dateTimeFormat())
)
);
'date_created' => date($commentMapper->adapter()->dateTimeFormat())
));
try {
$this->blogMapper->save($post);
$commentSaved = $commentMapper->save($comment);
if(!$commentSaved) {
print_r($commentMapper->errors());
$this->fail("Comment NOT saved");
}
} catch(Exception $e) {
echo $e->getTraceAsString();
$this->blogMapper->debug();
$commentMapper->debug();
exit();
}
$this->assertTrue($post->comments instanceof phpDataMapper_Query);
$this->assertTrue($commentSaved !== false);
}

/**
Expand Down Expand Up @@ -89,6 +94,6 @@ public function testBlogCommentsRelationCanBeModified($postId)
{
$post = $this->blogMapper->get($postId);
$sortedComments = $post->comments->order(array('date_created' => 'DESC'));
$this->assertTrue($post->comments instanceof phpDataMapper_Query);
$this->assertTrue($sortedComments instanceof phpDataMapper_Query);
}
}

0 comments on commit f5a133a

Please sign in to comment.