[patch] bringing Smarty unit tests to PHPunit 3

[email protected] (Sebastian Mendel) Wed, 13 Jun 2007 10:27:01 +0200
Newsgroups php.smarty.dev
Message-ID <[email protected]>
--------------010009000509040906090105
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

Hi,

this will update your unit testing suite to work with PHPunit 3

i also added some trim around Smarty returned values to remove trailing line
breaks -  as i don't think that this will do any harm

this makes smarty_unit_test.php and smarty_unit_test_gui.php obsolete


-- 
Sebastian Mendel

www.sebastianmendel.de

--------------010009000509040906090105
Content-Type: text/plain;
 name="smarty_unit_test_phpunit3.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="smarty_unit_test_phpunit3.patch"

Index: unit_test/test_cases.php
===================================================================
RCS file: /repository/smarty/unit_test/test_cases.php,v
retrieving revision 1.12
diff -u -r1.12 test_cases.php
--- unit_test/test_cases.php	21 Sep 2004 14:40:53 -0000	1.12
+++ unit_test/test_cases.php	13 Jun 2007 08:20:06 -0000
@@ -1,13 +1,49 @@
 <?php
-
+/**
+ * 
+ */
+
+/**
+ *
+ */
 require_once './config.php';
 require_once SMARTY_DIR . 'Smarty.class.php';
-require_once 'PHPUnit.php';
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+//require_once 'PHPUnit/Util/TestDox/ResultPrinter.php.';
+
+/**
+ *
+ */
+if (! defined('SMARTY_MAIN_METHOD')) {
+    define('SMARTY_MAIN_METHOD', 'test_cases');
+}
+
+class SmartyTestCases
+{
+    public static function main()
+    {
+        $parameters = array();
+        //$parameters['testdoxHTMLFile'] = true;
+        //$parameters['printer'] = PHPUnit_Util_TestDox_ResultPrinter::factory('HTML');
+        //$parameters['printer'] = PHPUnit_Util_TestDox_ResultPrinter::factory('Text');
+        PHPUnit_TextUI_TestRunner::run(self::suite(), $parameters);
+    }
+
+    public static function suite()
+    {
+        $suite = new PHPUnit_Framework_TestSuite('Smarty');
+
+        //$suite->addTestSuite('FailTest');
+        $suite->addTestSuite('SmartyTest');
+        return $suite;
+    }
+}
 
 class Obj {
-    var $val = 'val';
-    var $arr = array('one' => 'one', 'two' => 2);
-    var $ten = 10;
+    public $val = 'val';
+    public $arr = array('one' => 'one', 'two' => 2);
+    public $ten = 10;
 
     function meth($a="a", $b="b") {
         return "$a:$b";
@@ -19,16 +55,14 @@
 }
 
     
-class SmartyTest extends PHPUnit_TestCase {   
+class SmartyTest extends PHPUnit_Framework_TestCase
+{
     // contains the object handle of the string class
-    var $abc;
+    public $abc;
     // contains the last triggered error's errorlevel
-    var $errorlevel;
-
-    // constructor of the test suite
-    function SmartyTest($name) {
-       $this->PHPUnit_TestCase($name);
-    }
+    public $errorlevel;
+    
+    public $smarty = null;
 
     // called before the test functions will be executed    
     // this function is defined in PHPUnit_TestCase and overwritten 
@@ -55,183 +89,183 @@
     
     // test that template_dir exists
     function test_template_dir_exists() {
-        $this->assertTrue(file_exists($this->smarty->template_dir));                       
+        $this->assertTrue(true, file_exists($this->smarty->template_dir));                       
     }
     // test that template_dir is a directory
     function test_template_dir_is_dir() {
-        $this->assertTrue(is_dir($this->smarty->template_dir));                       
+        $this->assertTrue(true, is_dir($this->smarty->template_dir));                       
     }
     // test that template_dir is readable
     function test_template_dir_is_readable() {
-        $this->assertTrue(is_readable($this->smarty->template_dir));                       
+        $this->assertTrue(true, is_readable($this->smarty->template_dir));                       
     }
     // test that config_dir exists
     function test_config_dir_exists() {
-        $this->assertTrue(file_exists($this->smarty->config_dir));                       
+        $this->assertTrue(true, file_exists($this->smarty->config_dir));                       
     }
     // test that config_dir is a directory
     function test_config_dir_is_dir() {
-        $this->assertTrue(is_dir($this->smarty->config_dir));                       
+        $this->assertTrue(true, is_dir($this->smarty->config_dir));                       
     }
     // test that config_dir is readable
     function test_config_dir_is_readable() {
-        $this->assertTrue(is_readable($this->smarty->config_dir));                       
+        $this->assertTrue(true, is_readable($this->smarty->config_dir));                       
     }
     // test that compile_dir exists
     function test_compile_dir_exists() {
-        $this->assertTrue(file_exists($this->smarty->compile_dir));                       
+        $this->assertTrue(true, file_exists($this->smarty->compile_dir));                       
     }
     // test that compile_dir is a directory
     function test_compile_dir_is_dir() {
-        $this->assertTrue(is_dir($this->smarty->compile_dir));                       
+        $this->assertTrue(true, is_dir($this->smarty->compile_dir));                       
     }
     // test that compile_dir is readable
     function test_compile_dir_is_readable() {
-        $this->assertTrue(is_readable($this->smarty->compile_dir));                       
+        $this->assertTrue(true, is_readable($this->smarty->compile_dir));                       
     }
     // test that compile_dir is writable
     function test_compile_dir_is_writable() {
-        $this->assertTrue(is_writable($this->smarty->compile_dir));                       
+        $this->assertTrue(true, is_writable($this->smarty->compile_dir));                       
     }
     // test that cache_dir exists
     function test_cache_dir_exists() {
-        $this->assertTrue(file_exists($this->smarty->cache_dir));                       
+        $this->assertTrue(true, file_exists($this->smarty->cache_dir));                       
     }
     // test that cache_dir is a directory
     function test_cache_dir_is_dir() {
-        $this->assertTrue(is_dir($this->smarty->cache_dir));                       
+        $this->assertTrue(true, is_dir($this->smarty->cache_dir));                       
     }
     // test that cache_dir is readable
     function test_cache_dir_is_readable() {
-        $this->assertTrue(is_readable($this->smarty->cache_dir));                       
+        $this->assertTrue(true, is_readable($this->smarty->cache_dir));                       
     }
     // test that cache_dir is writable
     function test_cache_dir_is_writable() {
-        $this->assertTrue(is_writable($this->smarty->cache_dir));                       
+        $this->assertTrue(true, is_writable($this->smarty->cache_dir));                       
     }
 
     /* METHOD EXISTS TESTS */
     function test_assign_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'assign'));
+        $this->assertTrue(true, method_exists($this->smarty, 'assign'));
     }
     function test_assign_by_ref_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'assign_by_ref'));
+        $this->assertTrue(true, method_exists($this->smarty, 'assign_by_ref'));
     }
     function test_append_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'append'));
+        $this->assertTrue(true, method_exists($this->smarty, 'append'));
     }
     function test_append_by_ref_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'append_by_ref'));
+        $this->assertTrue(true, method_exists($this->smarty, 'append_by_ref'));
     }
     function test_clear_assign_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'clear_assign'));
+        $this->assertTrue(true, method_exists($this->smarty, 'clear_assign'));
     }
     function test_register_function_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_function'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_function'));
     }
     function test_unregister_function_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_function'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_function'));
     }
     function test_register_object_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_object'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_object'));
     }
     function test_unregister_object_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_object'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_object'));
     }
     function test_register_block_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_block'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_block'));
     }
     function test_unregister_block_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_block'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_block'));
     }
     function test_register_compiler_function_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_compiler_function'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_compiler_function'));
     }
     function test_unregister_compiler_function_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_compiler_function'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_compiler_function'));
     }
     function test_register_modifier_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_modifier'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_modifier'));
     }
     function test_unregister_modifier_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_modifier'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_modifier'));
     }
     function test_register_resource_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_resource'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_resource'));
     }
     function test_unregister_resource_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_resource'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_resource'));
     }
     function test_register_prefilter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_prefilter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_prefilter'));
     }
     function test_unregister_prefilter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_prefilter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_prefilter'));
     }
     function test_register_postfilter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_postfilter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_postfilter'));
     }
     function test_unregister_postfilter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_postfilter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_postfilter'));
     }
     function test_register_outputfilter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'register_outputfilter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'register_outputfilter'));
     }
     function test_unregister_outputfilter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'unregister_outputfilter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'unregister_outputfilter'));
     }
     function test_load_filter_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'load_filter'));
+        $this->assertTrue(true, method_exists($this->smarty, 'load_filter'));
     }
     function test_clear_cache_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'clear_cache'));
+        $this->assertTrue(true, method_exists($this->smarty, 'clear_cache'));
     }
     function test_clear_all_cache_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'clear_all_cache'));
+        $this->assertTrue(true, method_exists($this->smarty, 'clear_all_cache'));
     }
     function test_is_cached_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'is_cached'));
+        $this->assertTrue(true, method_exists($this->smarty, 'is_cached'));
     }
     function test_clear_all_assign_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'clear_all_assign'));
+        $this->assertTrue(true, method_exists($this->smarty, 'clear_all_assign'));
     }
     function test_clear_compiled_tpl_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'clear_compiled_tpl'));
+        $this->assertTrue(true, method_exists($this->smarty, 'clear_compiled_tpl'));
     }
     function test_template_exists_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'template_exists'));
+        $this->assertTrue(true, method_exists($this->smarty, 'template_exists'));
     }
     function test_get_template_vars_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'get_template_vars'));
+        $this->assertTrue(true, method_exists($this->smarty, 'get_template_vars'));
     }
     function test_get_config_vars_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'get_config_vars'));
+        $this->assertTrue(true, method_exists($this->smarty, 'get_config_vars'));
     }
     function test_trigger_error_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'trigger_error'));
+        $this->assertTrue(true, method_exists($this->smarty, 'trigger_error'));
     }
     function test_display_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'display'));
+        $this->assertTrue(true, method_exists($this->smarty, 'display'));
     }
     function test_fetch_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'fetch'));
+        $this->assertTrue(true, method_exists($this->smarty, 'fetch'));
     }
     function test_config_load_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'config_load'));
+        $this->assertTrue(true, method_exists($this->smarty, 'config_load'));
     }
     function test_get_registered_object_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'get_registered_object'));
+        $this->assertTrue(true, method_exists($this->smarty, 'get_registered_object'));
     }
     function test_clear_config_method_exists() {
-        $this->assertTrue(method_exists($this->smarty, 'clear_config'));
+        $this->assertTrue(true, method_exists($this->smarty, 'clear_config'));
     }
     function test_get_plugin_filepath() {
-        $this->assertTrue(method_exists($this->smarty, '_get_plugin_filepath'));
+        $this->assertTrue(true, method_exists($this->smarty, '_get_plugin_filepath'));
     }
 
     
     function test_clear_compiled_tpl() {
-        $this->assertTrue($this->smarty->clear_compiled_tpl());
+        $this->assertTrue(true, $this->smarty->clear_compiled_tpl());
     }
     
     /* DISPLAY TESTS */
@@ -240,8 +274,7 @@
     function test_call_to_display() {
         ob_start();
         $this->smarty->display('index.tpl');
-        $output = ob_get_contents();
-        ob_end_clean();
+        $output = trim(ob_get_clean());
         $this->assertEquals($output, 'TEST STRING');
     }
 
@@ -249,7 +282,7 @@
 
     // test that fetch() executes properly
     function test_call_to_fetch() {
-        $this->assertEquals($this->smarty->fetch('index.tpl'), 'TEST STRING');
+        $this->assertEquals(trim($this->smarty->fetch('index.tpl')), 'TEST STRING');
     }
     
     /* ASSIGN TESTS */
@@ -257,7 +290,7 @@
     // test assigning a simple template variable
     function test_assign_var() {
         $this->smarty->assign('foo', 'bar');
-        $this->assertEquals($this->smarty->fetch('assign_var.tpl'), 'bar');
+        $this->assertEquals(trim($this->smarty->fetch('assign_var.tpl')), 'bar');
     }
 
     /* PARSING TESTS */
@@ -274,7 +307,7 @@
 foo:val
 foo:foo
 one:2
-foo:foo:b', $this->smarty->fetch('parse_obj_meth.tpl'));
+foo:foo:b', trim($this->smarty->fetch('parse_obj_meth.tpl')));
     }
     
     // test assigning and calling an object
@@ -296,7 +329,7 @@
 16
 20
 8.5
-7', $this->smarty->fetch('parse_math.tpl'));
+7', trim($this->smarty->fetch('parse_math.tpl')));
     }
     
     /* CONFIG FILE TESTS */
@@ -320,16 +353,16 @@
     // test loading and running modifier.escape.php
     function test_escape_modifier_get_plugins_filepath() {
         $filepath = $this->smarty->_get_plugin_filepath('modifier', 'escape');
-        $this->assertTrue($filepath);
+        $this->assertTrue(true, $filepath);
     }
 
     function test_escape_modifier_include_file() {
         $filepath = $this->smarty->_get_plugin_filepath('modifier', 'escape');
-        $this->assertTrue(include($filepath));
+        $this->assertTrue(true, include($filepath));
     }
 
     function test_escape_modifier_function_exists() {
-        $this->assertTrue(function_exists('smarty_modifier_escape'));
+        $this->assertTrue(true, function_exists('smarty_modifier_escape'));
     }
 
     function test_escape_modifier_escape_default() {
@@ -378,16 +411,16 @@
 
     function test_core_is_secure_file_exists() {
         $file = SMARTY_CORE_DIR . 'core.is_secure.php';
-        $this->assertTrue(file_exists($file));
+        $this->assertTrue(true, file_exists($file));
     }
 
     function test_core_is_secure_file_include() {
         $file = SMARTY_CORE_DIR . 'core.is_secure.php';
-        $this->assertTrue(include($file));
+        $this->assertTrue(true, include($file));
     }
 
     function test_core_is_secure_function_exists() {
-        $this->assertTrue(function_exists('smarty_core_is_secure'));
+        $this->assertTrue(true, function_exists('smarty_core_is_secure'));
     }
 
     function test_core_is_secure_function_is_secure_true() {
@@ -398,7 +431,7 @@
         $params = array('resource_type' => 'file',
                         'resource_base_path' => dirname(__FILE__) . '/templates',
                         'resource_name' => dirname(__FILE__) . '/templates/index.tpl');
-        $this->assertTrue(smarty_core_is_secure($params, $this->smarty));
+        $this->assertTrue(true, smarty_core_is_secure($params, $this->smarty));
         $this->smarty->security = $security;
     }
 
@@ -417,8 +450,8 @@
     // test constants and security
     function test_core_is_secure_function_smarty_var_const() {
         define('TEST_CONSTANT', 'test constant');
-        $this->assertEquals('test constant', $this->smarty->fetch('constant.tpl',
-                                                             null, 'var_const'));
+        $this->assertEquals('test constant',
+            trim($this->smarty->fetch('constant.tpl', null, 'var_const')));
     }
 
     function test_core_is_secure_function_smarty_var_const_allowed() {
@@ -426,8 +459,8 @@
         $security_settings = $this->smarty->security_settings;
         $this->smarty->security_settings['ALLOW_CONSTANTS'] = true;
         $this->smarty->security = true;
-        $this->assertEquals('test constant', $this->smarty->fetch('constant.tpl',
-                                                     null, 'var_const_allowed'));
+        $this->assertEquals('test constant',
+            trim($this->smarty->fetch('constant.tpl', null, 'var_const_allowed')));
         $this->smarty->security_settings = $security_settings;
         $this->smarty->security = $security;   
     }
@@ -444,7 +477,12 @@
         $this->assertEquals( $this->errorlevel, E_USER_WARNING);
         $this->smarty->security = $security;
     }
+}
 
+if (SMARTY_MAIN_METHOD == 'test_cases') {
+    echo '<pre>';
+    SmartyTestCases::main();
+    echo '</pre>';
 }
 
 ?>


--------------010009000509040906090105--