Permalink
Browse files

Tests: Added tests for relations and updated some issues in the base …

…mapper
  • Loading branch information...
1 parent a1a8cf7 commit cf72defa0cfbb3468d6f91c645b8854ae990dee1 @vlucas committed Feb 5, 2010
Showing with 249 additions and 174 deletions.
  1. +16 −18 Base.php
  2. +0 −40 tests/AllTests.php
  3. +0 −116 tests/Blog/BasicTest.php
  4. +66 −0 tests/CRUDTest.php
  5. +15 −0 tests/Fixture/Blog/Comments/Mapper.php
  6. +21 −0 tests/Fixture/Blog/Mapper.php
  7. +94 −0 tests/RelationsTest.php
  8. +37 −0 tests/init.php
View
@@ -476,7 +476,7 @@ protected function saveRelatedRowsFor($entity, array $fillData = array())
// Associative array
} elseif(is_array($relatedRow)) {
- $relatedRowObj = new $this->entityClass($relatedRow);
+ $relatedRowObj = new $this->_entityClass($relatedRow);
}
// Set column values on row only if other data has been updated (prevents queries for unchanged existing rows)
@@ -595,7 +595,11 @@ public function update(phpDataMapper_Entity $entity)
}
// Handle with adapter
- $result = $this->adapter()->update($this->source(), $binds, array($this->primaryKeyField() => $this->primaryKey($entity)));
+ if(count($binds) > 0) {
+ $result = $this->adapter()->update($this->source(), $binds, array($this->primaryKeyField() => $this->primaryKey($entity)));
+ } else {
+ $result = true;
+ }
// Save related rows
if($result) {
@@ -657,7 +661,7 @@ public function validate(phpDataMapper_Entity $entity)
if(isset($fieldAttrs['required']) && true === $fieldAttrs['required']) {
// Required field
if(empty($entity->$field)) {
- $this->addError("Required field '" . $field . "' was left blank");
+ $this->error("Required field '" . $field . "' was left blank");
}
}
}
@@ -704,38 +708,32 @@ public function hasErrors()
/**
- * Get array of error messages
+ * Get array of error messages
*
* @return array
*/
- public function getErrors()
+ public function errors($msgs = null)
{
+ if(null !== $msgs) {
+ foreach($msgs as $msg) {
+ $this->error($msg);
+ }
+ }
return $this->_errors;
}
/**
- * Add an error to error messages array
+ * Add an error to error messages array
*/
- public function addError($msg)
+ public function error($msg)
{
// Add to error array
$this->_errors[] = $msg;
}
/**
- * Add an array of errors all at once
- */
- public function addErrors(array $msgs)
- {
- foreach($msgs as $msg) {
- $this->addError($msg);
- }
- }
-
-
- /**
* Attempt to load class file based on phpDataMapper naming conventions
*/
public static function loadClass($className)
View
@@ -1,40 +0,0 @@
-<?php
-// Init / Setup
-require dirname(__FILE__) . '/init.php';
-
-// Require Blog tests
-require dirname(__FILE__) . '/Blog/AllTests.php';
-
-
-/**
- * Run all tests
- */
-class AllTests
-{
- public static function suite()
- {
- $suite = new MapperTestSuite('phpDataMapper Tests');
-
- $blogSuite = new Blog_AllTests('Blog Tests');
- $suite->addTest($blogSuite);
-
- return $suite;
- }
-}
-
-
-/**
- * Mapper test suite to maintain connections for all tests
- */
-class MapperTestSuite extends PHPUnit_Framework_TestSuite
-{
- protected function setUp()
- {
-
- }
-
- protected function tearDown()
- {
- $this->adapter = null;
- }
-}
View
@@ -1,116 +0,0 @@
-<?php
-require_once dirname(dirname(__FILE__)) . '/init.php';
-
-
-/**
- *
- */
-class TestMapper extends phpDataMapper_Base
-{
- // Auto-migrate upon instantiation
- public function init()
- {
- $this->migrate();
- }
-}
-
-/**
- * Blog Mapper
- *
- * @todo Organize this a little better...
- */
-class BlogMapper extends TestMapper {
- protected $source = 'test_blog';
-
- public $id = array('type' => 'int', 'primary' => true);
- public $title = array('type' => 'string', 'required' => true);
- public $body = array('type' => 'text', 'required' => true);
- public $date_created = array('type' => 'datetime');
-
- // Each post entity 'hasMany' comment entites
- public $comments = array(
- 'type' => 'relation',
- 'relation' => 'HasMany',
- 'mapper' => 'BlogCommentsMapper',
- 'where' => array('post_id' => 'entity.id'),
- 'order' => array('date_created' => 'ASC')
- );
-}
-/**
- * Blog Comments Mapper
- * @todo implement 'BelongsTo' relation for linking back to blog post object
- */
-class BlogCommentsMapper extends TestMapper {
- protected $source = 'test_blog_comments';
-
- public $id = array('type' => 'int', 'primary' => true);
- public $post_id = array('type' => 'int', 'index' => true);
- public $name = array('type' => 'string', 'required' => true);
- public $email = array('type' => 'string', 'required' => true);
- public $body = array('type' => 'text', 'required' => true);
- public $date_created = array('type' => 'datetime');
-}
-
-
-/**
- * Blog basic tests
- */
-class Blog_BasicTest extends PHPUnit_Framework_TestCase
-{
- protected $blogMapper;
-
- /**
- * Setup/fixtures for each test
- */
- public function setUp()
- {
- // New mapper instance
- $this->blogMapper = new BlogMapper(fixture_adapter());
- }
- public function tearDown() {}
-
-
- public function testAdapterInstance()
- {
- $this->assertTrue(fixture_adapter() instanceof phpDataMapper_Adapter_Interface);
- }
-
- public function testMapperInstance()
- {
- $this->assertTrue($this->blogMapper instanceof phpDataMapper_Base);
- }
-
- public function testSampleNewsInsert()
- {
- $mapper = $this->blogMapper;
- $post = $mapper->get();
- $post->title = "Test Post";
- $post->body = "<p>This is a really awesome super-duper post.</p><p>It's really quite lovely.</p>";
- $post->date_created = date($mapper->adapter()->dateTimeFormat());
- $result = $mapper->insert($post); // returns an id
-
- $this->assertTrue(is_numeric($result));
- }
-
- public function testSampleNewsUpdate()
- {
- $mapper = $this->blogMapper;
- $post = $mapper->first(array('title' => "Test Post"));
-
- $this->assertTrue($post instanceof phpDataMapper_Entity);
-
- $post->title = "Test Post Modified";
- $result = $mapper->update($post); // returns boolean
-
- $this->assertTrue($result);
- }
-
- public function testSampleNewsDelete()
- {
- $mapper = $this->blogMapper;
- $post = $mapper->first(array('title' => "Test Post Modified"));
- $result = $mapper->delete($post);
-
- $this->assertTrue($result);
- }
-}
View
@@ -0,0 +1,66 @@
+<?php
+require_once dirname(__FILE__) . '/init.php';
+
+/**
+ * Basic CRUD Tests
+ * Create, Read, Update, Delete
+ */
+class CRUDTest extends PHPUnit_Framework_TestCase
+{
+ protected $blogMapper;
+
+ /**
+ * Setup/fixtures for each test
+ */
+ public function setUp()
+ {
+ // New mapper instance
+ $this->blogMapper = fixture_mapper('Blog');
+ }
+ public function tearDown() {}
+
+
+ public function testAdapterInstance()
+ {
+ $this->assertTrue(fixture_adapter() instanceof phpDataMapper_Adapter_Interface);
+ }
+
+ public function testMapperInstance()
+ {
+ $this->assertTrue($this->blogMapper instanceof phpDataMapper_Base);
+ }
+
+ public function testSampleNewsInsert()
+ {
+ $mapper = $this->blogMapper;
+ $post = $mapper->get();
+ $post->title = "Test Post";
+ $post->body = "<p>This is a really awesome super-duper post.</p><p>It's really quite lovely.</p>";
+ $post->date_created = date($mapper->adapter()->dateTimeFormat());
+ $result = $mapper->insert($post); // returns an id
+
+ $this->assertTrue(is_numeric($result));
+ }
+
+ public function testSampleNewsUpdate()
+ {
+ $mapper = $this->blogMapper;
+ $post = $mapper->first(array('title' => "Test Post"));
+
+ $this->assertTrue($post instanceof phpDataMapper_Entity);
+
+ $post->title = "Test Post Modified";
+ $result = $mapper->update($post); // returns boolean
+
+ $this->assertTrue($result);
+ }
+
+ public function testSampleNewsDelete()
+ {
+ $mapper = $this->blogMapper;
+ $post = $mapper->first(array('title' => "Test Post Modified"));
+ $result = $mapper->delete($post);
+
+ $this->assertTrue($result);
+ }
+}
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Blog Comments Mapper
+ * @todo implement 'BelongsTo' relation for linking back to blog post object
+ */
+class Fixture_Blog_Comments_Mapper extends TestMapper {
+ protected $source = 'test_blog_comments';
+
+ public $id = array('type' => 'int', 'primary' => 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);
+ public $body = array('type' => 'text', 'required' => true);
+ public $date_created = array('type' => 'datetime');
+}
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Blog Mapper
+ */
+class Fixture_Blog_Mapper extends TestMapper {
+ protected $source = 'test_blog';
+
+ public $id = array('type' => 'int', 'primary' => true);
+ public $title = array('type' => 'string', 'required' => true);
+ public $body = array('type' => 'text', 'required' => true);
+ public $date_created = array('type' => 'datetime');
+
+ // Each post entity 'hasMany' comment entites
+ public $comments = array(
+ 'type' => 'relation',
+ 'relation' => 'HasMany',
+ 'mapper' => 'Fixture_Blog_Comments_Mapper',
+ 'where' => array('post_id' => 'entity.id'),
+ 'order' => array('date_created' => 'ASC')
+ );
+}
Oops, something went wrong.

0 comments on commit cf72def

Please sign in to comment.