Skip to content

Commit

Permalink
Unit tests for adding, removing and checking many 2 many relations
Browse files Browse the repository at this point in the history
More coverage for the other types is needed
  • Loading branch information
ralflang committed Apr 3, 2012
1 parent cbcf21a commit fd138c0
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Objects/ManyToManyA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class Horde_Rdo_Test_Objects_ManyToManyA extends Horde_Rdo_Base
{
}
13 changes: 13 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Objects/ManyToManyAMapper.php
Original file line number Diff line number Diff line change
@@ -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'),
);
}
5 changes: 5 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Objects/ManyToManyB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class Horde_Rdo_Test_Objects_ManyToManyB extends Horde_Rdo_Base
{
}
9 changes: 9 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Objects/ManyToManyBMapper.php
Original file line number Diff line number Diff line change
@@ -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();
}
78 changes: 78 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Sql/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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) {
}

Expand All @@ -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');
}

Expand Down Expand 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();
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
}
}
15 changes: 15 additions & 0 deletions framework/Rdo/test/Horde/Rdo/fixtures/unit_tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.