Permalink
Browse files

Unit tests for adding, removing and checking many 2 many relations

More coverage for the other types is needed
  • Loading branch information...
1 parent cbcf21a commit fd138c0ef29b781425737f44c611679e42e110b5 @ralflang ralflang committed Apr 3, 2012
View
4 framework/Rdo/test/Horde/Rdo/Autoload.php
@@ -28,3 +28,7 @@
require_once __DIR__ . '/Objects/SomeEagerBaseObject.php';
require_once __DIR__ . '/Objects/RelatedThingMapper.php';
require_once __DIR__ . '/Objects/RelatedThing.php';
+require_once __DIR__ . '/Objects/ManyToManyA.php';
+require_once __DIR__ . '/Objects/ManyToManyB.php';
+require_once __DIR__ . '/Objects/ManyToManyAMapper.php';
+require_once __DIR__ . '/Objects/ManyToManyBMapper.php';
View
5 framework/Rdo/test/Horde/Rdo/Objects/ManyToManyA.php
@@ -0,0 +1,5 @@
+<?php
+
+class Horde_Rdo_Test_Objects_ManyToManyA extends Horde_Rdo_Base
+{
+}
View
13 framework/Rdo/test/Horde/Rdo/Objects/ManyToManyAMapper.php
@@ -0,0 +1,13 @@
+<?php
+class Horde_Rdo_Test_Objects_ManyToManyAMapper extends Horde_Rdo_Mapper
+{
+ /**
+ * Inflector doesn't support Horde-style tables yet
+ */
+ protected $_table = 'test_manytomanya';
+ protected $_lazyRelationships = array(
+ 'manybs' => array('type' => Horde_Rdo::MANY_TO_MANY,
+ 'through' => 'test_manythrough',
+ 'mapper' => 'Horde_Rdo_Test_Objects_ManyToManyBMapper'),
+ );
+}
View
5 framework/Rdo/test/Horde/Rdo/Objects/ManyToManyB.php
@@ -0,0 +1,5 @@
+<?php
+
+class Horde_Rdo_Test_Objects_ManyToManyB extends Horde_Rdo_Base
+{
+}
View
9 framework/Rdo/test/Horde/Rdo/Objects/ManyToManyBMapper.php
@@ -0,0 +1,9 @@
+<?php
+class Horde_Rdo_Test_Objects_ManyToManyBMapper extends Horde_Rdo_Mapper
+{
+ /**
+ * Inflector doesn't support Horde-style tables yet
+ */
+ protected $_table = 'test_manytomanyb';
+ protected $_lazyRelationships = array();
+}
View
78 framework/Rdo/test/Horde/Rdo/Sql/Base.php
@@ -19,6 +19,8 @@ class Horde_Rdo_Test_Sql_Base extends Horde_Rdo_Test_Base
protected static $EagerBaseObjectMapper;
protected static $LazyBaseObjectMapper;
protected static $RelatedThingMapper;
+ protected static $MtmaMapper;
+ protected static $MtmbMapper;
protected static function _migrate_sql_rdo($db)
{
@@ -29,6 +31,9 @@ protected static function _migrate_sql_rdo($db)
$migration->dropTable('test_someeagerbaseobjects');
$migration->dropTable('test_somelazybaseobjects');
$migration->dropTable('test_relatedthings');
+ $migration->dropTable('test_manytomanya');
+ $migration->dropTable('test_manytomanyb');
+ $migration->dropTable('test_manythrough');
} catch (Horde_Db_Exception $e) {
}
@@ -47,6 +52,20 @@ protected static function _migrate_sql_rdo($db)
$t->column('relatedthing_intproperty', 'integer', array('null' => false));
$t->end();
+ $t = $migration->createTable('test_manytomanya', array('autoincrementKey' => 'a_id'));
+ $t->column('a_intproperty', 'integer', array('null' => false));
+ $t->end();
+
+ $t = $migration->createTable('test_manytomanyb', array('autoincrementKey' => 'b_id'));
+ $t->column('b_intproperty', 'integer', array('null' => false));
+ $t->end();
+
+
+ $t = $migration->createTable('test_manythrough');
+ $t->column('a_id', 'integer');
+ $t->column('b_id', 'integer');
+ $t->end();
+
$migration->migrate('up');
}
@@ -92,6 +111,60 @@ public function testListOffsetExistsReturnFalseFor0inemptylist()
$this->assertFalse(isset($list[0]), "return false for first index in empty list");
}
+ public function testHasRelationManyToManyAny()
+ {
+ $objectA = self::$MtmaMapper->findOne(2);
+ $this->assertTrue($objectA->hasRelation('manybs'));
+ }
+
+ public function testHasRelationManyToManyAnyButEmpty()
+ {
+ $objectA = self::$MtmaMapper->findOne(1);
+ $this->assertFalse($objectA->hasRelation('manybs'));
+ }
+
+ public function testHasRelationManyToManyWrongPeer()
+ {
+ $objectA = self::$MtmaMapper->findOne(2);
+ $objectB = self::$MtmbMapper->findOne(11);
+ $this->assertFalse($objectA->hasRelation('manybs', $objectB));
+ }
+
+ public function testHasRelationManyToManyRightPeer()
+ {
+ $objectA = self::$MtmaMapper->findOne(2);
+ $objectB = self::$MtmbMapper->findOne(12);
+ $this->assertTrue($objectA->hasRelation('manybs', $objectB));
+ }
+
+ public function testAddRelationManyToMany()
+ {
+ $objectA = self::$MtmaMapper->findOne(1);
+ $objectB = self::$MtmbMapper->findOne(11);
+ $objectA->addRelation('manybs', $objectB);
+ $this->assertTrue($objectA->hasRelation('manybs', $objectB));
+ }
+
+ public function testRemoveRelationManyToManyOne()
+ {
+ $objectA = self::$MtmaMapper->findOne(2);
+ $objectB1 = self::$MtmbMapper->findOne(12);
+ $objectB2 = self::$MtmbMapper->findOne(14);
+ $objectA->removeRelation('manybs', $objectB1);
+ $this->assertFalse($objectA->hasRelation('manybs', $objectB1));
+ $this->assertTrue($objectA->hasRelation('manybs', $objectB2));
+ }
+
+ public function testRemoveRelationManyToManyAll()
+ {
+ $objectA = self::$MtmaMapper->findOne(2);
+ $objectB1 = self::$MtmbMapper->findOne(12);
+ $objectB2 = self::$MtmbMapper->findOne(14);
+ $objectA->removeRelation('manybs');
+ $this->assertFalse($objectA->hasRelation('manybs', $objectB1));
+ $this->assertFalse($objectA->hasRelation('manybs', $objectB2));
+ }
+
public function testListOffsetExistsReturnTrueForFirst()
{
$list = self::$LazyBaseObjectMapper->find();
@@ -205,6 +278,9 @@ public static function tearDownAfterClass()
$migration->dropTable('test_someeagerbaseobjects');
$migration->dropTable('test_somelazybaseobjects');
$migration->dropTable('test_relatedthings');
+ $migration->dropTable('test_manytomanya');
+ $migration->dropTable('test_manytomanyb');
+ $migration->dropTable('test_manythrough');
self::$db = null;
}
}
@@ -217,5 +293,7 @@ public function setUp()
self::$LazyBaseObjectMapper = new Horde_Rdo_Test_Objects_SomeLazyBaseObjectMapper(self::$db);
self::$EagerBaseObjectMapper = new Horde_Rdo_Test_Objects_SomeEagerBaseObjectMapper(self::$db);
+ self::$MtmaMapper = new Horde_Rdo_Test_Objects_ManyToManyAMapper(self::$db);
+ self::$MtmbMapper = new Horde_Rdo_Test_Objects_ManyToManyBMapper(self::$db);
}
}
View
15 framework/Rdo/test/Horde/Rdo/fixtures/unit_tests.sql
@@ -16,3 +16,18 @@ INSERT INTO test_someeagerbaseobjects (baseobject_id, relatedthing_id, atextprop
INSERT INTO test_someeagerbaseobjects (baseobject_id, relatedthing_id, atextproperty) VALUES ('4', '', 'Fourth Base Thing with empty string relation');
INSERT INTO test_relatedthings (relatedthing_id, relatedthing_textproperty, relatedthing_intproperty) VALUES ('1', 'First Related Thing', '100');
INSERT INTO test_relatedthings (relatedthing_id, relatedthing_textproperty, relatedthing_intproperty) VALUES ('2', 'Second Related Thing', '200');
+
+INSERT INTO test_manytomanya (a_id, a_intproperty) VALUES ('1', '200');
+INSERT INTO test_manytomanya (a_id, a_intproperty) VALUES ('2', '220');
+INSERT INTO test_manytomanya (a_id, a_intproperty) VALUES ('3', '230');
+INSERT INTO test_manytomanya (a_id, a_intproperty) VALUES ('4', '240');
+INSERT INTO test_manytomanya (a_id, a_intproperty) VALUES ('5', '250');
+
+INSERT INTO test_manytomanyb (b_id, b_intproperty) VALUES ('11', '400');
+INSERT INTO test_manytomanyb (b_id, b_intproperty) VALUES ('12', '420');
+INSERT INTO test_manytomanyb (b_id, b_intproperty) VALUES ('13', '430');
+INSERT INTO test_manytomanyb (b_id, b_intproperty) VALUES ('14', '440');
+INSERT INTO test_manytomanyb (b_id, b_intproperty) VALUES ('15', '450');
+
+INSERT INTO test_manythrough (a_id, b_id) VALUES (2, 12);
+INSERT INTO test_manythrough (a_id, b_id) VALUES (2, 14);

0 comments on commit fd138c0

Please sign in to comment.