Skip to content

Commit

Permalink
Merged PR #177.
Browse files Browse the repository at this point in the history
Fixes #90
  • Loading branch information
willdurand committed Jan 16, 2012
2 parents a77a4cf + 1acaf0c commit 6984ed0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
10 changes: 6 additions & 4 deletions runtime/lib/formatter/PropelFormatter.php
Expand Up @@ -169,12 +169,14 @@ protected function isWithOneToMany()
*/
protected function getWorkerObject($col, $class)
{
if(isset($this->currentObjects[$col])) {
$this->currentObjects[$col]->clear();
$key = $col . '_' . $class;

if (isset($this->currentObjects[$key])) {
$this->currentObjects[$key]->clear();
} else {
$this->currentObjects[$col] = new $class();
$this->currentObjects[$key] = new $class();
}
return $this->currentObjects[$col];
return $this->currentObjects[$key];
}

/**
Expand Down
65 changes: 65 additions & 0 deletions test/testsuite/runtime/formatter/PropelFormatterTest.php
@@ -0,0 +1,65 @@
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreEmptyTestBase.php';

/**
* Test class for PropelObjectFormatter.
*
* @author Francois Zaninotto
* @version $Id$
* @package runtime.formatter
*/
class PropelFormatterTest extends BookstoreEmptyTestBase
{
protected function setUp()
{
parent::setUp();
BookstoreDataPopulator::populate();
}

public function testGetWorkerObjectReturnsRightClass()
{
$formatter = $this->getMockForAbstractClass('PropelFormatter');

$method = new ReflectionMethod('PropelFormatter', 'getWorkerObject');
$method->setAccessible(true);

$classNames = array(
'Bookstore',
'BookReader',
'BookClubList',
);

$col = 0;
foreach ($classNames as $className) {
// getWorkerObject() should always return an instance of the requested class, regardless of the value of $col
$result = $method->invoke($formatter, $col, $className);

$this->assertEquals($className, get_class($result), 'getWorkerObject did not return an instance of the requested class');
}
}

public function testGetWorkerObjectCachedInstance()
{
$formatter = $this->getMockForAbstractClass('PropelFormatter');

$method = new ReflectionMethod('PropelFormatter', 'getWorkerObject');
$method->setAccessible(true);

$className = 'Bookstore';
$col = 0;

$result1 = $method->invoke($formatter, $col, $className);
$result2 = $method->invoke($formatter, $col, $className);

$this->assertEquals(spl_object_hash($result1), spl_object_hash($result2), 'getWorkerObject should return a cached instance of a class at the same col index');
}
}
21 changes: 21 additions & 0 deletions test/testsuite/runtime/formatter/PropelOnDemandFormatterTest.php
Expand Up @@ -148,4 +148,25 @@ public function testFormatOneManyResults()
$this->assertTrue($book instanceof Book, 'PropelOnDemandFormatter::formatOne() returns a model object');
}

public function testFormatSingleTableInheritanceManyResults()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
BookstoreDataPopulator::populate($con);

$stmt = $con->query('SELECT * FROM bookstore_employee');
$formatter = new PropelOnDemandFormatter();
$formatter->init(new ModelCriteria('bookstore', 'BookstoreEmployee'));

$employees = $formatter->format($stmt);

foreach ($employees as $employee) {
$row = array();
$row[1] = $employee->getClassKey();

$omClass = BookstoreEmployeePeer::getOMClass($row, 0, false);
$actualClass = get_class($employee);

$this->assertEquals($omClass, $actualClass, 'PropelOnDemandFormatter::format() should handle single table inheritance');
}
}
}
5 changes: 5 additions & 0 deletions test/tools/helpers/bookstore/BookstoreDataPopulator.php
Expand Up @@ -167,6 +167,11 @@ public static function populate($con = null)
$bemp2->setSupervisor($bemp1);
$bemp2->save($con);

$bemp3 = new BookstoreCashier();
$bemp3->setName("Tim");
$bemp3->setJobTitle("Cashier");
$bemp3->save($con);

$role = new AcctAccessRole();
$role->setName("Admin");

Expand Down

0 comments on commit 6984ed0

Please sign in to comment.