From 7a26255739b8b7b129a21888561078372ac6eceb Mon Sep 17 00:00:00 2001 From: meus Date: Wed, 12 Sep 2007 20:26:59 +0000 Subject: [PATCH] addded tests for template. added optimization for __call and calling template functions in Record --- lib/Doctrine/Record.php | 15 ++++++++++++++- tests/TemplateTestCase.php | 12 ++++++++++++ tests/run.php | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 20a4b84fd..2e107fc0e 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -119,6 +119,12 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count */ private $_oid; + /** + * @var array $_methods array that contains methods that are already checked for + */ + + protected $_methods; + /** * constructor * @param Doctrine_Table|null $table a Doctrine_Table object or null, @@ -213,7 +219,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count { } /** * construct - * Empty tempalte method to provide concrete Record classes with the possibility + * Empty template method to provide concrete Record classes with the possibility * to hook into the constructor procedure * * @return void @@ -1511,9 +1517,16 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count */ public function __call($method, $args) { + if ( isset( $this->_methods[$method])) { + $methodArray = $this->_methods[$method]; + $template = $methodArray["template"]; + $template->setInvoker($this); + return call_user_func_array( array( $template, $method ), $methodArray["args"]); + } foreach ($this->_table->getTemplates() as $template) { if (method_exists($template, $method)) { $template->setInvoker($this); + $this->_methods[$method] = array("template" => $template, "args" => $args); return call_user_func_array(array($template, $method), $args); } diff --git a/tests/TemplateTestCase.php b/tests/TemplateTestCase.php index 4746cd241..6afe9acb5 100644 --- a/tests/TemplateTestCase.php +++ b/tests/TemplateTestCase.php @@ -69,6 +69,13 @@ class Doctrine_Template_TestCase extends Doctrine_UnitTestCase $this->assertTrue($user->Email[0] instanceof ConcreteEmail); } + + public function testShouldCallMethodInTemplate() + { + $user = new ConcreteUser(); + $this->assertEqual("foo", $user->foo()); + } + } // move these to ../templates? @@ -87,6 +94,11 @@ class UserTemplate extends Doctrine_Template $this->hasMany('EmailTemplate as Email', array('local' => 'id', 'foreign' => 'user_id')); } + + public function foo() + { + return "foo"; + } } class EmailTemplate extends Doctrine_Template { diff --git a/tests/run.php b/tests/run.php index 97c55cf6a..06851f333 100644 --- a/tests/run.php +++ b/tests/run.php @@ -310,7 +310,7 @@ $test->addTestCase(new Doctrine_Import_Schema_Xml_TestCase()); $test->addTestCase(new Doctrine_Export_Schema_Yml_TestCase()); $test->addTestCase(new Doctrine_Export_Schema_Xml_TestCase()); -//$test->addTestCase(new Doctrine_Template_TestCase()); +$test->addTestCase(new Doctrine_Template_TestCase()); //$test->addTestCase(new Doctrine_Import_Builder_TestCase());