Commit 66fb71ac authored by romanb's avatar romanb

Started playing with isolated hydration tests.

parent 234253ed
......@@ -7,7 +7,10 @@ require_once 'lib/DoctrineTestInit.php';
// Suites
require_once 'Orm/Component/AllTests.php';
require_once 'Orm/Hydration/AllTests.php';
require_once 'Orm/Ticket/AllTests.php';
// Tests
require_once 'Orm/UnitOfWorkTestCase.php';
class Orm_AllTests
......@@ -25,6 +28,7 @@ class Orm_AllTests
//$suite->addTestSuite('Orm_ConfigurableTestCase');
$suite->addTest(Orm_Component_AllTests::suite());
$suite->addTest(Orm_Hydration_AllTests::suite());
$suite->addTest(Orm_Ticket_AllTests::suite());
return $suite;
......
<?php
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Dbal_Component_AllTests::main');
define('PHPUnit_MAIN_METHOD', 'Orm_Component_AllTests::main');
}
require_once 'lib/DoctrineTestInit.php';
......@@ -29,6 +29,6 @@ class Orm_Component_AllTests
}
}
if (PHPUnit_MAIN_METHOD == 'Dbal_Component_AllTests::main') {
if (PHPUnit_MAIN_METHOD == 'Orm_Component_AllTests::main') {
Dbal_Component_AllTests::main();
}
<?php
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Hydration_AllTests::main');
}
require_once 'lib/DoctrineTestInit.php';
// Tests
require_once 'Orm/Hydration/BasicHydrationTest.php';
class Orm_Hydration_AllTests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new Doctrine_TestSuite('Doctrine Orm Hydration');
$suite->addTestSuite('Orm_Hydration_BasicHydrationTest');
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Hydration_AllTests::main') {
Orm_Hydration_AllTests::main();
}
<?php
require_once 'lib/DoctrineTestInit.php';
require_once 'lib/mocks/Doctrine_HydratorMockStatement.php';
class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
{
protected function setUp()
{
parent::setUp();
}
/**
* Fakes the DQL query: select u.id, u.name from CmsUser u
*
*/
public function testBasic()
{
// Faked query components
$queryComponents = array(
'u' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'),
'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'),
'parent' => null,
'relation' => null,
'map' => null
)
);
// Faked table alias map
$tableAliasMap = array(
'u' => 'u'
);
// Faked result set
$resultSet = array(
//row1
array(
'u__id' => '1',
'u__name' => 'romanb'
),
//row2
array(
'u__id' => '2',
'u__name' => 'jwage'
)
);
$stmt = new Doctrine_HydratorMockStatement($resultSet);
$hydrator = new Doctrine_Hydrator();
$hydrator->setQueryComponents($queryComponents);
$arrayResult = $hydrator->hydrateResultSet($stmt, $tableAliasMap, Doctrine::HYDRATE_ARRAY);
$this->assertTrue(is_array($arrayResult));
$this->assertEquals(2, count($arrayResult));
$this->assertEquals(1, $arrayResult[0]['id']);
$this->assertEquals('romanb', $arrayResult[0]['name']);
$this->assertEquals(2, $arrayResult[1]['id']);
$this->assertEquals('jwage', $arrayResult[1]['name']);
$stmt->setResultSet($resultSet);
$objectResult = $hydrator->hydrateResultSet($stmt, $tableAliasMap, Doctrine::HYDRATE_RECORD);
$this->assertTrue($objectResult instanceof Doctrine_Collection);
$this->assertEquals(2, count($objectResult));
$this->assertTrue($objectResult[0] instanceof Doctrine_Record);
$this->assertEquals(1, $objectResult[0]->id);
$this->assertEquals('romanb', $objectResult[0]->name);
$this->assertTrue($objectResult[1] instanceof Doctrine_Record);
$this->assertEquals(2, $objectResult[1]->id);
$this->assertEquals('jwage', $objectResult[1]->name);
//Doctrine::dump($res);
$this->assertEquals(0, 0);
}
}
\ No newline at end of file
<?php
/**
* This class is a mock of the PDOStatement class that can be passed in to the Hydrator
* to test the hydration standalone with faked result sets.
*
* @author Roman Borschel <roman@code-factory.org>
*/
class Doctrine_HydratorMockStatement
{
private $_resultSet;
/**
* Creates a new mock statement that will serve the provided fake result set to clients.
*
* @param array $resultSet The faked SQL result set.
*/
public function __construct(array $resultSet)
{
$this->_resultSet = $resultSet;
}
/**
* Fetches all rows from the result set.
*
* NOTE: Must adhere to the PDOStatement::fetchAll() signature that looks as follows:
* array fetchAll ([ int $fetch_style [, int $column_index [, array $ctor_args ]]] )
*
* @return array
*/
public function fetchAll($fetchStyle = null, $columnIndex = null, array $ctorArgs = null)
{
return $this->_resultSet;
}
/**
* Fetches the next row in the result set.
*
* NOTE: Must adhere to the PDOStatement::fetch() signature that looks as follows:
* mixed fetch ([ int $fetch_style [, int $cursor_orientation [, int $cursor_offset ]]] )
*
*/
public function fetch($fetchStyle = null, $cursorOrientation = null, $cursorOffset = null)
{
return array_shift($this->_resultSet);
}
/**
* Closes the cursor, enabling the statement to be executed again.
*
* @return boolean
*/
public function closeCursor()
{
return true;
}
public function setResultSet(array $resultSet)
{
$this->_resultSet = $resultSet;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment