[mapguide-commits] r8225 - in sandbox/jng/v30: . UnitTest/WebTier/Php

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Sun Jun 15 10:30:25 PDT 2014


Author: jng
Date: 2014-06-15 10:30:25 -0700 (Sun, 15 Jun 2014)
New Revision: 8225

Added:
   sandbox/jng/v30/UnitTest/WebTier/Php/SQLiteEngine.php
Modified:
   sandbox/jng/v30/UnitTest/WebTier/Php/HtmlPrinter.php
   sandbox/jng/v30/UnitTest/WebTier/Php/Run.php
   sandbox/jng/v30/UnitTest/WebTier/Php/RunTests.php
   sandbox/jng/v30/UnitTest/WebTier/Php/Utils.php
   sandbox/jng/v30/UnitTest/WebTier/Php/Validate.php
   sandbox/jng/v30/run_tests.bat
Log:
PHP test runner improvements:
 - Fix PHP warnings in HtmlPrinter
 - Add a PHP implementation of SqliteDB and SqliteVM (based on PDO). This is used by run_tests.bat to avoid random access violations that seem to be happening in my test runs. The existing SWIG PHP wrapper is still required for dump file generation/updates.

Modified: sandbox/jng/v30/UnitTest/WebTier/Php/HtmlPrinter.php
===================================================================
--- sandbox/jng/v30/UnitTest/WebTier/Php/HtmlPrinter.php	2014-06-15 11:14:08 UTC (rev 8224)
+++ sandbox/jng/v30/UnitTest/WebTier/Php/HtmlPrinter.php	2014-06-15 17:30:25 UTC (rev 8225)
@@ -19,10 +19,6 @@
 
 require_once("Utils.php");
 
-if (!defined('SQLITE_ROW')) {
-    define('SQLITE_ROW', 100);
-}
-
 //This is a helper class that contains methods that output data in an Html format.
 
 class HtmlPrinter
@@ -149,6 +145,7 @@
     {
         print("<table scroll=\"no\" border=\"1\" cellpadding=\"5\">\n");
         print("<h3><b>$caption</b></h3>");
+        print("<col width=10><col width=20><col width=40><col width=20><col width=180><col width=180><col>");
         print("<tr>\n");
         print("<td>&nbsp</td><th>Param Set</th><th>Operation</th>\n<th>Outcome</th>\n<th>Actual Result</th>\n<th>Expected Result</th>\n");
         print("</tr>\n");

Modified: sandbox/jng/v30/UnitTest/WebTier/Php/Run.php
===================================================================
--- sandbox/jng/v30/UnitTest/WebTier/Php/Run.php	2014-06-15 11:14:08 UTC (rev 8224)
+++ sandbox/jng/v30/UnitTest/WebTier/Php/Run.php	2014-06-15 17:30:25 UTC (rev 8225)
@@ -118,7 +118,7 @@
         }
         catch (MgException $e)
         {
-            print $e->GetExceptionMessage('en');
+            print $e->GetDetails();
             return false;
         }
         catch (SqliteException $s)

Modified: sandbox/jng/v30/UnitTest/WebTier/Php/RunTests.php
===================================================================
--- sandbox/jng/v30/UnitTest/WebTier/Php/RunTests.php	2014-06-15 11:14:08 UTC (rev 8224)
+++ sandbox/jng/v30/UnitTest/WebTier/Php/RunTests.php	2014-06-15 17:30:25 UTC (rev 8225)
@@ -163,6 +163,7 @@
     catch (SqliteException $s)
     {
         print $s->GetExceptionMessage();
+        $s->Dispose();
         return 1;
     }
 }

Added: sandbox/jng/v30/UnitTest/WebTier/Php/SQLiteEngine.php
===================================================================
--- sandbox/jng/v30/UnitTest/WebTier/Php/SQLiteEngine.php	                        (rev 0)
+++ sandbox/jng/v30/UnitTest/WebTier/Php/SQLiteEngine.php	2014-06-15 17:30:25 UTC (rev 8225)
@@ -0,0 +1,214 @@
+<?php
+
+// SQLiteEngine.php
+//
+// A PHP-based implementation of SqliteDB and SqliteVM class using PDO instead
+// of the internal SWIG-wrapped SQLite classes in Oem
+//
+// This avoids any access violation issues should our test runner happen to
+// be mis-using any classes in the SWIG wrapper.
+
+if (!defined('SQLITE_ROW')) {
+    define('SQLITE_ROW', 100);
+}
+
+if (!extension_loaded("SQLitePhpApi"))
+{
+    //echo "SQLitePhpApi not loaded. Using PHP implementation";
+
+    class SqliteException extends Exception
+    {
+    }
+
+    class CantOpenDbException extends SqliteException
+    {
+    }
+    
+    class SqliteDB
+    {
+        private $dbh;
+        private $dbName;
+
+        public function __construct()
+        {
+            $this->dbh = NULL;
+            $this->dbName = "";
+        }
+        
+        public function __destruct()
+        {
+            $this->Close();
+        }
+
+        public function Close()
+        {
+            if ($this->dbh != NULL)
+            {
+                $this->dbh = NULL;
+            }
+        }
+        
+        public function GetHandle() { return $this->dbh; }
+        
+        public function Open($dbName)
+        {
+            $this->Close();
+            $this->dbh = new PDO("sqlite:$dbName");
+            $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+            $this->dbName = $dbName;
+        }
+        
+        public function GetName() { return $this->dbName; }
+        
+        public function GenerateDatabase($dumpFilePath, $dbPath)
+        {
+            if (php_sapi_name() == 'cli')
+                echo "Generating database from dump: $dumpFilePath\n";
+            else
+                echo "Generating database from dump: $dumpFilePath<br/>";
+            $pdo = new PDO("sqlite:$dbPath");
+            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+            $sql = file_get_contents($dumpFilePath);
+            $pdo->exec($sql);
+            $pdo = NULL;
+        }
+        
+        public function DumpDatabase($path)
+        {
+            throw new Exception("Creating database dumps not implemented. Please run this script with the php_SQLitePhpApi extension loaded");
+        }
+    }
+    
+    class SqliteVM
+    {
+        private $db;
+        private $currentStmt;
+        private $currentRow;
+        private $resultLOB;
+        
+        private $nameIndexMap;
+
+        public function __construct($db)
+        {
+            $this->db = $db->GetHandle();
+            $this->currentStmt = NULL;
+            $this->currentRow = NULL;
+            $this->nameIndexMap = array();
+        }
+        
+        public function Execute($query)
+        {
+            /*
+            if (php_sapi_name() == 'cli')
+                echo "Executing: $query\n";
+            else
+                echo "Executing: $query<br/>";
+            */
+            if ($this->currentStmt != NULL)
+            {
+                $this->currentStmt->closeCursor();
+                $this->currentStmt = NULL;
+            }
+            $this->nameIndexMap = array();
+            $this->currentStmt = $this->db->prepare($query);
+            try
+            {
+                if ($this->currentStmt === FALSE)
+                {
+                    if (php_sapi_name() == 'cli')
+                        echo "ERROR: Failed to prepare query: $query\n";
+                    else
+                        echo "ERROR: Failed to prepare query: $query<br/>";
+                    return -1;
+                }
+                if (!$this->currentStmt->execute())
+                {
+                    if (php_sapi_name() == 'cli')
+                        echo "ERROR: Failed to execute query: $query\n";
+                    else
+                        echo "ERROR: Failed to execute query: $query<br/>";
+                    return -1;
+                }
+            }
+            catch (PDOException $e)
+            {
+                return -1;
+            }
+            //Compile name index map for the purposes of column binding below
+            for ($i = 0; $i < $this->currentStmt->columnCount(); $i++)
+            {
+                try {
+                    $col = $this->currentStmt->getColumnMeta($i);
+                    if ($col === FALSE)
+                        continue;
+                } catch (PDOException $e) {
+                    continue;
+                }
+                if (array_key_exists("name", $col)) 
+                {
+                    $colName = $col["name"];
+                    $this->nameIndexMap[$colName] = $i;
+                }
+            }
+            
+            //Bind columns, paying special attention to bind Result as BLOB if found
+            $this->currentRow = array();
+            foreach ($this->nameIndexMap as $colName => $index)
+            {
+                if ($colName == "Result") {
+                    $this->currentStmt->bindColumn(($index+1), $this->currentRow[$colName], PDO::PARAM_LOB);
+                    //echo "Bound $colName as BLOB<br/>";
+                } else {
+                    $this->currentStmt->bindColumn(($index+1), $this->currentRow[$colName]);
+                }
+            }
+            
+            try
+            {
+                $res = $this->currentStmt->fetch();
+                if ($res === FALSE)
+                    return -1;
+            }
+            catch (PDOException $e)
+            {
+                return -1;
+            }
+            return SQLITE_ROW;
+        }
+        
+        public function GetString($name)
+        {
+            if (array_key_exists($name, $this->currentRow))
+                return $this->currentRow[$name];
+            return "";
+        }
+        
+        public function GetBlob($name)
+        {
+            $str = $this->currentRow[$name];
+            //SqliteBlob in C++ has a MgByteReader-style interface. Since
+            //in PHP if it quacks like a duck ... we'll give them an actual MgByteReader
+            $source = new MgByteSource($str, strlen($str));
+            return $source->GetReader();
+        }
+        
+        public function NextRow()
+        {
+            $this->currentRow = $this->currentStmt->fetch();
+            if ($this->currentRow === FALSE)
+                return -1;
+            return SQLITE_ROW;
+        }
+        
+        public function SqlFinalize()
+        {
+            if ($this->currentStmt != NULL)
+            {
+                $this->currentStmt->closeCursor();
+                $this->currentStmt = NULL;
+            }
+        }
+    }
+}
+
+?>
\ No newline at end of file

Modified: sandbox/jng/v30/UnitTest/WebTier/Php/Utils.php
===================================================================
--- sandbox/jng/v30/UnitTest/WebTier/Php/Utils.php	2014-06-15 11:14:08 UTC (rev 8224)
+++ sandbox/jng/v30/UnitTest/WebTier/Php/Utils.php	2014-06-15 17:30:25 UTC (rev 8225)
@@ -19,6 +19,8 @@
 
 //Class that defines static methods that are commonly used in the infrastructure
 
+require_once("SQLiteEngine.php");
+
 if (!defined('WEBCONFIGINI')) {
     if (array_key_exists("WEBCONFIGINI", $_SERVER)) {
         define("WEBCONFIGINI", $_SERVER["WEBCONFIGINI"]);
@@ -246,7 +248,7 @@
 
         return $dbPath;
     }
-
+    
     public static function CreateDumpFile($db, $overwrite = false)
     {
         $dbName = $db->GetName();

Modified: sandbox/jng/v30/UnitTest/WebTier/Php/Validate.php
===================================================================
--- sandbox/jng/v30/UnitTest/WebTier/Php/Validate.php	2014-06-15 11:14:08 UTC (rev 8224)
+++ sandbox/jng/v30/UnitTest/WebTier/Php/Validate.php	2014-06-15 17:30:25 UTC (rev 8225)
@@ -57,6 +57,8 @@
         //Get the file extension that will be used for a dump
         $actualExtension = ValidateUtils::GetExtension($mimeType);
 
+        $bAlwaysPass = false;
+        $passReason = "";
         //If we have an ALWAYSPASS parameter defined for the operation then skip the whole validation process
         //This parameter should only be used for clean up operations that are no related with the tests
         if ($this->vm->Execute("Select ParamValue from Params where ParamName=\"ALWAYSPASS\" and ParamSet=$paramSet")!=SQLITE_ROW)
@@ -170,6 +172,11 @@
                 }
             }
         }
+        else
+        {
+            $bAlwaysPass = true;
+            $passReason = $this->vm->GetString("ParamValue");
+        }
 
         if ($_POST['output']=="html")
         {
@@ -181,7 +188,9 @@
             }
             else
             {
-                HtmlPrinter::AddResultRow($operation, $outcome, $paramSet);
+                HtmlPrinter::AddResultRow($operation, $outcome, $paramSet, $resultData, $expectedResult);
+                //$str = sprintf("\n****ACTUAL RESULT****\n%s\n****EXPECTED RESULT****\n%s\n********\n\n\n", $resultData, $expectedResult);
+                //fwrite($file, $str);
             }
         }
         else
@@ -193,6 +202,21 @@
                 echo $str;
                 fwrite($file, $str);
             }
+            else
+            {
+                /*
+                if ($bAlwaysPass)
+                {
+                    echo "Test (ParamSet: $paramSet) set to ALWAYSPASS: ".$passReason;
+                    if (php_sapi_name() == 'cli')
+                        echo "\n";
+                    else
+                        echo "<br/>";
+                }
+                */
+                //$str = sprintf("\n****ACTUAL RESULT****\n%s\n****EXPECTED RESULT****\n%s\n********\n\n\n", $resultData, $expectedResult);
+                //fwrite($file, $str);
+            }
         }
         return $exitStatus;
     }
@@ -215,6 +239,8 @@
         //Get the extension for the dump file based on the content type of the result
         $actualExtension = ValidateUtils::GetExtension($contentType);
 
+        $bAlwaysPass = false;
+        $passReason = "";
         //If ALWAYSPASS parameter is defined the the whole validation is skipped.
         //This parameter should only be used for clean up operations that are no related to the test
         if ($this->vm->Execute("Select ParamValue from Params where ParamName=\"ALWAYSPASS\" and ParamSet=$paramSet")!=SQLITE_ROW)
@@ -348,6 +374,11 @@
                 }
             }
         }
+        else
+        {
+            $bAlwaysPass = true;
+            $passReason = $this->vm->GetString("ParamValue");
+        }
 
         if ($_POST['output']=="html")
         {
@@ -360,6 +391,8 @@
             else
             {
                 HtmlPrinter::AddResultRow($operation, $outcome, $paramSet);
+                //$str = sprintf("\n****ACTUAL RESULT****\n%s\n****EXPECTED RESULT****\n%s\n********\n\n\n", $resultData, $expectedResult);
+                //fwrite($file, $str);
             }
         }
         else
@@ -371,6 +404,21 @@
                 echo $str;
                 fwrite($file, $str);
             }
+            else
+            {
+                /*
+                if ($bAlwaysPass)
+                {
+                    echo "Test (ParamSet: $paramSet) set to ALWAYSPASS: ".$passReason;
+                    if (php_sapi_name() == 'cli')
+                        echo "\n";
+                    else
+                        echo "<br/>";
+                }
+                */
+                //$str = sprintf("\n****ACTUAL RESULT****\n%s\n****EXPECTED RESULT****\n%s\n********\n\n\n", $resultData, $expectedResult);
+                //fwrite($file, $str);
+            }
         }
         return $exitStatus;
     }

Modified: sandbox/jng/v30/run_tests.bat
===================================================================
--- sandbox/jng/v30/run_tests.bat	2014-06-15 11:14:08 UTC (rev 8224)
+++ sandbox/jng/v30/run_tests.bat	2014-06-15 17:30:25 UTC (rev 8225)
@@ -77,7 +77,7 @@
 :start_php_webserver
 if "%START_WEBSERVER%" == "1" (
     echo [test]: Starting PHP web server. Waiting %MGSERVER_WAIT%s
-    start php -n -d display_errors=Off -d upload_max_filesize=20M -d extension_dir="%PHP_EXT_DIR%" -d extension=php_mbstring.dll -d extension=php_curl.dll -d extension=php_MapGuideApi.dll -d extension=php_SQLitePhpApi.dll -S %SERVER_ADDR%:%SERVER_PORT% -t %CD%\UnitTest\WebTier\MapAgent\MapAgentForms %CD%\UnitTest\WebTier\Php\MapAgentShim\index.php
+    start php -n -d display_errors=Off -d upload_max_filesize=20M -d extension_dir="%PHP_EXT_DIR%" -d extension=php_mbstring.dll -d extension=php_curl.dll -d extension=php_MapGuideApi.dll -S %SERVER_ADDR%:%SERVER_PORT% -t %CD%\UnitTest\WebTier\MapAgent\MapAgentForms %CD%\UnitTest\WebTier\Php\MapAgentShim\index.php
     ping -n %MGSERVER_WAIT% 127.0.0.1 > NUL
 )
 :test_php
@@ -98,7 +98,7 @@
     if exist ResourceService\ResourceServiceTest.db del /F ResourceService\ResourceServiceTest.db
     popd
     pushd UnitTest\WebTier\Php
-    php.exe -n -d display_errors=Off -d extension_dir="%PHP_EXT_DIR%" -d extension=php_mbstring.dll -d extension=php_curl.dll -d extension=php_MapGuideApi.dll -d extension=php_SQLitePhpApi.dll RunTests.php
+    php.exe -n -d display_errors=Off -d extension_dir="%PHP_EXT_DIR%" -d extension=php_mbstring.dll -d extension=php_curl.dll -d extension=php_MapGuideApi.dll -d extension=php_pdo_sqlite.dll RunTests.php
     popd
 )
 :test_dotnet



More information about the mapguide-commits mailing list