public function main() { HtmlPrinter::PrintHtmlHeader("HttpEnt Tests"); HtmlPrinter::PrintFormHeader("Run.php"); //Add your tests here //HtmlPrinter::PrintTestCases("YourTestName", "Path to database file where your data is stored", "Http") HtmlPrinter::PrintTestCases("FeatureService", "../../TestData/FeatureService/FeatureServiceTest.dump", "HttpEnt"); HtmlPrinter::PrintSelectTestMode(); HtmlPrinter::PrintSelectOutput(); HtmlPrinter::PrintFormFooter("HttpEnt", "RunTests"); HtmlPrinter::PrintHtmlFooter(); }
public function main() { HtmlPrinter::PrintHtmlHeader("Http Tests"); HtmlPrinter::PrintFormHeader("Run.php"); //Add your tests here //HtmlPrinter::PrintTestCases("YourTestName", "Path to database file where your data is stored", "Api") HtmlPrinter::PrintTestCases("ResourceService", "../../TestData/ResourceService/ResourceServiceTest.dump", "Http"); HtmlPrinter::PrintTestCases("DrawingService", "../../TestData/DrawingService/DrawingServiceTest.dump", "Http"); HtmlPrinter::PrintTestCases("FeatureService", "../../TestData/FeatureService/FeatureServiceTest.dump", "Http"); HtmlPrinter::PrintTestCases("MappingService", "../../TestData/MappingService/MappingServiceTest.dump", "Http"); HtmlPrinter::PrintTestCases("SiteServiceTests", "../../TestData/SiteService/SiteServiceTest.dump", "Http"); HtmlPrinter::PrintTestCases("WfsTests", "../../TestData/Wfs/WfsTest.dump", "Http"); HtmlPrinter::PrintTestCases("WmsTests", "../../TestData/Wms/WmsTest.dump", "Http"); HtmlPrinter::PrintSelectTestMode(); HtmlPrinter::PrintSelectOutput(); HtmlPrinter::PrintFormFooter("Http", "RunTests"); HtmlPrinter::PrintHtmlFooter(); }
public function ValidateHttpRequest($serviceType, $paramSet, $operation, $actualResult, $file) { //Default values for outcome $exitStatus = 0; $outcome = "pass"; //Get the content type of the result $contentType = $actualResult->GetContentType(); //We need to remove the stack trace if an exception is thrown because the different line numbers may fail the test $resultData = ValidateUtils::RemoveStackTrace($actualResult->GetResultData()); //If we have exception message we need to remove any parts that may contain system dependent information //Ex. file paths $resultData = ValidateUtils::ProcessExceptionMessage($resultData); //Get the extension for the dump file based on the content type of the result $actualExtension = ValidateUtils::GetExtension($contentType); //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) { //Form a file name that is going to be used for the outputting results to a file $type = substr($serviceType, 0, strpos($serviceType, "_")); $dumpFilePath = Utils::GetPath("../../TestData/" . $type . "/DumpFiles/"); $filePath = Utils::GetPath("../../TestData/" . $type . "/DumpFiles/" . $type . "HttpTest"); $fileName = $filePath . "_" . $paramSet . "." . $actualExtension; if ($_POST['testExecutionMode'] == "dump") { //if the folder "DumpFiles" is not exist, then user need to create the folder themself //or the "dump" operation fail, so add these code to create the folder automatically if (!file_exists($dumpFilePath)) { mkdir($dumpFilePath); } file_put_contents($fileName, "{$resultData}"); } else { //This section is special case handling for the operations that return different data after each call $resultData = ValidateUtils::SpecialDataHandling($operation, $resultData, $contentType); if ($_POST['testExecutionMode'] == "generate" && array_key_exists($paramSet, $_POST)) { //Get the sample result that is stored in the database. If we are using file on disk for validation //then do not overwrite the filename in the database //To distinguish between sample data and filename all filenames should be prefixed with "@@" $status = $this->vm->Execute("Select Result from HttpTestResults where ParamSet={$paramSet};"); //Get the sample data from the database $this->vm->Execute("Select Result from HttpTestResults where ParamSet = {$paramSet}"); //When use the GetString to get the result which is actually BlOB will cause exception //unfortunately, this expection can not be caught by the php engine, cause it is not legal php exception //maybe it is a problem of the sqlite in mapguide, so use GetBlob here, if the sqlite fix this problem //we can use try catch $byteReader = $this->vm->GetBlob("Result"); $sampleResult; while ($byteReader->Read($resultContent, 1024) > 0) { $sampleResult .= $resultContent; } if ("@@" != substr($sampleResult, 0, 2)) { $responseBody = ""; //Store the result as a BLOB in the database //Update the row for that param set or create a new row if we do not have it yet if ($status == SQLITE_ROW) { $this->vm->Prepare("update HttpTestResults set Result = :blob where ParamSet={$paramSet};"); } else { printf("<b>A new row has been created in HttpTestResults table to store the result for operation %s</b><br />", $paramSet); print "<b>Please, update the Description field for that row later</b><br /><br />"; $this->vm->Prepare("INSERT INTO HttpTestResults(ParamSet, Result) VALUES({$paramSet}, :blob);"); } $this->vm->BindBlob(":blob", $resultData, strlen($resultData)); $this->vm->Execute(); if ($contentType != null) { $this->vm->Execute("UPDATE HttpTestResults SET ContentType=\"{$contentType}\" WHERE ParamSet={$paramSet};"); } //Write the result to a file to ensure the correct data is populated in the database file_put_contents($fileName, $resultData); } } elseif ($_POST['testExecutionMode'] == "validate" || $_POST['testExecutionMode'] == "show") { $expectedResult = ""; $resultContent = ""; //Get the sample data from the database $this->vm->Execute("Select Result, ContentType from HttpTestResults where ParamSet={$paramSet}"); $byteReader = $this->vm->GetBlob("Result"); $expectedExtension = ValidateUtils::GetExtension($this->vm->GetString("ContentType")); while ($byteReader->Read($resultContent, 1024) > 0) { $expectedResult .= $resultContent; } //If we are validating from a file then get the contents of that file //File names should be prefixed with "@@" to distinguish them from BLOB data if ("@@" == substr($expectedResult, 0, 2)) { //Remove "@@" from the filename. Get the path to the file and pump the contents in a variable $sampleDataFile = substr($expectedResult, 2); $sampleDataFile = Utils::GetPath($expectedResult); $expectedResult = file_get_contents($sampleDataFile); } if ($_POST['testExecutionMode'] == "validate") { //Normalize line endings. Don't want stray CRLFs derailing verification $resultData = str_replace("\r\n", "\n", $resultData); $expectedResult = str_replace("\r\n", "\n", $expectedResult); //If the results are different and special validation fails then then the operation failed ->mark it red if (strncasecmp($resultData, $expectedResult, strlen($resultData . $expectedResult)) && !ValidateUtils::SpecialValidation($operation, $resultData, $expectedResult)) { $outcome = "fail"; $exitStatus = 1; if ($expectedExtension != "xml" && $expectedExtension != "html" && $expectedExtension != "txt") { $expectedResult = "Unable to display binary data"; } if ($actualExtension != "xml" && $actualExtension != "html" && $actualExtension != "txt") { $resultData = "Unable to display binary data"; } } } else { $type = substr($serviceType, 0, strpos($serviceType, "_")); $showPath = Utils::GetPath("../../TestData/" . $type . "/ShowFiles/" . $type . "HttpTest"); $showName = $showPath . "_" . $paramSet . "." . $actualExtension; file_put_contents($showName, $expectedResult); } } } } if ($_POST['output'] == "html") { if ($outcome == "fail") { 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 { HtmlPrinter::AddResultRow($operation, $outcome, $paramSet); } } else { if ($outcome == "fail") { printf("****" . $serviceType . " " . $paramSet . " " . $operation . " failed.\n"); $str = sprintf("\n****ACTUAL RESULT****\n%s\n****EXPECTED RESULT****\n%s\n********\n\n\n", $resultData, $expectedResult); echo $str; fwrite($file, $str); } } return $exitStatus; }
{ return "John Doe"; } function getCurrentPage() { return "current page content"; } } interface Printer { function printPage($page); } class PlainTextPrinter implements Printer { function printPage($page) { echo 'PlainText: ' . $page; } } class HtmlPrinter implements Printer { function printPage($page) { echo 'HTML: ' . $page; } } $book = new Book(); //$printer = new PlainTextPrinter(); $printer = new HtmlPrinter(); $page = $book->getCurrentPage(); $printer->printPage($page);
function RunTest(&$testsRun, $file) { try { $actualResult = new Result(); $exitStatus = 0; $testName = $_POST['testName']; //Add the test in the log file $entry = "\n\n************************************************************\n"; $entry .= sprintf("Executing %s\n", $testName); $entry .= "************************************************************\n\n"; fwrite($file, $entry); //Get the list of operations to be executed for this test. This list should comma separated, no spaces. $this->unitTestVm->Execute("Select ParamSets from TestCase where TestName=\"{$testName}\""); $sets = $this->unitTestVm->GetString("ParamSets"); $paramSet = strtok($sets, ","); if ($_POST['output'] == "html") { //Start the HTML table for the results HtmlPrinter::PrintResultTableHeader($_POST['requestType'] . " Test " . $testName); } //For each operation in the list //Execute the operation and validate the result while ($paramSet != "") { $this->unitTestVm->Execute("Select ParamValue from Params where ParamSet={$paramSet} AND ParamName=\"OPERATION\";"); $paramValue = $this->unitTestVm->GetString("ParamValue"); $testsRun++; //Add the operation to the log file $this->AddLogFileEntry($file, $paramValue, $paramSet, $this->unitTestResultVm); $actualResult = $this->operation->Execute($paramSet, $paramValue); if (substr_count($_POST['requestType'], "Http")) { $status = $this->validate->ValidateHttpRequest($_POST['testName'], $paramSet, $paramValue, $actualResult, $file); } else { $status = $this->validate->ValidateApiRequest($_POST['testName'], $paramSet, $paramValue, $actualResult, $file); } $exitStatus += $status; $paramSet = strtok(","); } //Closes the Html table. Also outputs hidden fields that are needed for Generation mode if ($_POST['output'] == "html") { HtmlPrinter::printTableEnd(); HtmlPrinter::PrintGenerateFormHiddenFields(); HtmlPrinter::PrintFormFooter($_POST['requestType'], "Generate"); } if ($_POST['testExecutionMode'] == "generate") { Utils::CreateDumpFile($this->unitTestDb); } return $exitStatus; } catch (MgException $e) { print $e->GetExceptionMessage("en") . "\n"; } catch (SqliteException $s) { print $s->GetExceptionMessage() . "\n"; } }