如何测试Magento块
我想测试一个Magento块函数。我不知道如何在.phtml文件外部调用函数。有人知道像块的getModel这样的函数吗 我发现 getBlockSingleton如何测试Magento块,magento,singleton,phpunit,Magento,Singleton,Phpunit,我想测试一个Magento块函数。我不知道如何在.phtml文件外部调用函数。有人知道像块的getModel这样的函数吗 我发现 getBlockSingleton 但是,它已被弃用,我无法让它工作。假设您的Magento根目录是您的web根目录。在Magento根目录中,创建一个test.php文件。您可以在http://base_url/test.php. ini_set('display_errors',true); //PHP has such friendly errors, show
但是,它已被弃用,我无法让它工作。假设您的Magento根目录是您的web根目录。在Magento根目录中,创建一个test.php文件。您可以在http://base_url/test.php.
ini_set('display_errors',true); //PHP has such friendly errors, show them!
include 'app/Mage.php'; //include the helper class/bootstrap file
Mage::setIsDeveloperMode(true); //flag to render Magento's traces
Mage::app();
/**
Instantiate the app. Note that this is different from Mage::run()! This can
be skipped given the Mage::app() call below.
*/
//block "type"
$class = 'core/bar';
//block instance
$block = Mage::app()->getLayout()->createBlock($class);
if (is_object($block)) die("Okay! ".get_class($block));
/**
* If script execution reaches this point, there is one of
* two problems:
*
* 1) bad/missing config
* 2) bad path based on filename
*/
//the xpath which is used
$xpath = 'global/blocks/'.strstr($class,'/',true).'/class';
//a node from config XML (we hope)
$node = Mage::getConfig()->getNode($xpath);
//error condition 1:
if (!$node) die("Bad xpath, check configuration: ".$xpath);
//error condition 2:
$name = uc_words((string) $node . '_' . substr(strrchr($class, '/'), 1));
$file = str_replace('_', DIRECTORY_SEPARATOR, $name.'.php');
$issue = '<br /><br />';
if (!is_readable($file)) {
//no file matching classname
$issue .= "No file found for $file, tried:<pre> - ";
$issue .= str_replace(PATH_SEPARATOR,'/'.$file.'<br /> - ',get_include_path()).$xpath.'</pre>';
} else {
$issue .= "Wrong class name in $file";
}
echo sprintf('Xpath ok, looking for class <span style="font-family: Courier New">%s</span>%s',$name,$issue);
假设您的Magento根是web根。在Magento根目录中,创建一个test.php文件。您可以在http://base_url/test.php.
ini_set('display_errors',true); //PHP has such friendly errors, show them!
include 'app/Mage.php'; //include the helper class/bootstrap file
Mage::setIsDeveloperMode(true); //flag to render Magento's traces
Mage::app();
/**
Instantiate the app. Note that this is different from Mage::run()! This can
be skipped given the Mage::app() call below.
*/
//block "type"
$class = 'core/bar';
//block instance
$block = Mage::app()->getLayout()->createBlock($class);
if (is_object($block)) die("Okay! ".get_class($block));
/**
* If script execution reaches this point, there is one of
* two problems:
*
* 1) bad/missing config
* 2) bad path based on filename
*/
//the xpath which is used
$xpath = 'global/blocks/'.strstr($class,'/',true).'/class';
//a node from config XML (we hope)
$node = Mage::getConfig()->getNode($xpath);
//error condition 1:
if (!$node) die("Bad xpath, check configuration: ".$xpath);
//error condition 2:
$name = uc_words((string) $node . '_' . substr(strrchr($class, '/'), 1));
$file = str_replace('_', DIRECTORY_SEPARATOR, $name.'.php');
$issue = '<br /><br />';
if (!is_readable($file)) {
//no file matching classname
$issue .= "No file found for $file, tried:<pre> - ";
$issue .= str_replace(PATH_SEPARATOR,'/'.$file.'<br /> - ',get_include_path()).$xpath.'</pre>';
} else {
$issue .= "Wrong class name in $file";
}
echo sprintf('Xpath ok, looking for class <span style="font-family: Courier New">%s</span>%s',$name,$issue);
如果只需要块实例本身来测试其上的方法,则可以使用以下函数:
如果只需要块实例本身来测试其上的方法,则可以使用以下函数: