com phpruntests: phpruntests - added test-case for file-external-section: src/testClassMap.php src/testcase/sections/executablesections/
_rtFileExternalSection.php src/testcase/sections/executab lesections/rtFileExternalSection.php tests/testcase/section
s/executablesections/rtFileExternalSectionTest.php

From: Date: Fri, 15 May 2009 23:11:20 +0000
Subject: com phpruntests: phpruntests - added test-case for file-external-section: src/testClassMap.php src/testcase/sections/executablesections/
_rtFileExternalSection.php src/testcase/sections/executab lesections/rtFileExternalSection.php tests/testcase/section
s/executablesections/rtFileExternalSectionTest.php
Groups: php.qa 
Request: Send a blank email to php-qa+get-66458@lists.php.net to get a copy of this message
Commit: 8616086585fb1c3534be6713ab59b307402b0198 Author: Georg Gradwohl <g2@php.net> Fri, 15 May 2009 23:11:20 +0000 Parents: 76126176a114eacfda38616a336d05a2d1b733f0 Branches: master Link: http://git.php.net/?p=phpruntests.git;a=commitdiff;h=8616086585fb1c3534be6713ab59b307402b0198 Log: phpruntests - added test-case for file-external-section Changed paths: M src/testClassMap.php M src/testcase/sections/executablesections/_rtFileExternalSection.php M src/testcase/sections/executablesections/rtFileExternalSection.php A tests/testcase/sections/executablesections/rtFileExternalSectionTest.php

diff --git a/src/testClassMap.php b/src/testClassMap.php index f9bb46b..49971a4 100644 --- a/src/testClassMap.php +++ b/src/testClassMap.php @@ -57,6 +57,7 @@ $rtClassMap = array( 'rtIniSection' => 'testcase/sections/configurationsections/rtIniSection.php', 'rtCleanSection' => 'testcase/sections/executablesections/rtCleanSection.php', 'rtFileSection' => 'testcase/sections/executablesections/rtFileSection.php', + 'rtFileExternalSection' => 'testcase/sections/executablesections/rtFileExternalSection.php', 'rtSkipIfSection' => 'testcase/sections/executablesections/rtSkipIfSection.php', 'rtCreditsSection' => 'testcase/sections/informationsections/rtCreditsSection.php', 'rtTestHeaderSection' => 'testcase/sections/informationsections/rtTestHeaderSection.php', diff --git a/src/testcase/sections/executablesections/_rtFileExternalSection.php b/src/testcase/sections/executablesections/_rtFileExternalSection.php index 452e638..040d052 100644 --- a/src/testcase/sections/executablesections/_rtFileExternalSection.php +++ b/src/testcase/sections/executablesections/_rtFileExternalSection.php @@ -3,7 +3,6 @@ * rtFileExternalSection * Executes the code in the --FILE_EXTERNAL-- section * - * * @category Testing * @package RUNTESTS * @author Zoe Slattery <zoe@php.net> @@ -13,82 +12,32 @@ * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @link http://qa.php.net/ */ -class rtFileExternalSection extends rtExecutableSection +class rtFileExternalSection extends rtFileSection { - private $twoBlankLines = '\r?\n\r?\n'; - - public function setExecutableFileName($testName) - { - $this->fileName = $testName.".php"; - } public function run(rtPhpTest $testCase, rtRuntestsConfiguration $runConfiguration) { - $this->status = array(); - $this->writeExecutableFile(); - - $this->copyExternalFileContent(); - - $phpExecutable = $testCase->testConfiguration->getPhpExecutable(); - - // The CGI excutable is null if it is not available, check and SKIP if necessary - if ($phpExecutable != null) { - $phpCommand = $phpExecutable; - $phpCommand .= ' '. $testCase->testConfiguration->getPhpCommandLineArguments(); - $phpCommand .= ' -f '.$this->fileName; - $phpCommand .= ' '.$testCase->testConfiguration->getTestCommandLineArguments(); - $phpCommand .= ' 2>&1 '.$testCase->testConfiguration->getInputFileString(); - - - $PhpRunner = new rtPhpRunner($phpCommand, - $testCase->testConfiguration->getEnvironmentVariables(), - $runConfiguration->getSetting('WorkingDirectory') - ); + if ($this->copyExternalFileContent() === true) { - try { - $this->output = $PhpRunner->runphp(); - - //If it's a CGI test sort the headers out here - if(substr($phpExecutable, -2) == '-C') { - - if (preg_match("/^(.*?)$this->twoBlankLines(.*)/s", $this->output, $match)) { - $this->output = $match[2]; - $this->headers = $match[1]; - } - - } - - - } catch (rtPhpRunnerException $e) { - $this->status['fail'] = $e->getMessage(); - } - } else { - $this->status['skip'] = 'The CGI executable is unavailable'; + return parent::run($testCase, $runConfiguration); } return $this->status; } - - - public function getHeaders() - { - return $this->headers; - } - + private function copyExternalFileContent() { if (sizeof($this->sectionContents) == 1) { - $c = $this->sectionContents[0]; + $file = $this->sectionContents[0]; // don't allow tests to retrieve files from anywhere but this subdirectory - $file = dirname($this->fileName).'/'.trim(str_replace('..', '', $c)); + $file = dirname($this->fileName).'/'.trim(str_replace('..', '', $file)); if (file_exists($file)) { - $content = file_get_contents($file); - file_put_contents($this->fileName, $content); + $this->sectionContents[0] = file_get_contents($file); return true; } else { diff --git a/src/testcase/sections/executablesections/rtFileExternalSection.php b/src/testcase/sections/executablesections/rtFileExternalSection.php index 040d052..7cc3dac 100644 --- a/src/testcase/sections/executablesections/rtFileExternalSection.php +++ b/src/testcase/sections/executablesections/rtFileExternalSection.php @@ -12,45 +12,114 @@ * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @link http://qa.php.net/ */ -class rtFileExternalSection extends rtFileSection +class rtFileExternalSection extends rtExecutableSection { - + + /** + * @var $twoBlankLines string + */ + private $twoBlankLines = '\r?\n\r?\n'; + + /** + * sets the executable filename + * + * @param $testName string the filename + * @return void + */ + public function setExecutableFileName($testName) + { + $this->fileName = $testName.".php"; + } + + /** + * runs a test-case and returns the status + * + * @param $testCase rtPhpTest + * @param $runConfiguration rtRuntestsConfiguration + * @return $status Array + */ public function run(rtPhpTest $testCase, rtRuntestsConfiguration $runConfiguration) { - if ($this->copyExternalFileContent() === true) { + $this->status = array(); + $this->writeExecutableFile(); - return parent::run($testCase, $runConfiguration); + $this->copyExternalFileContent(); + + $phpExecutable = $testCase->testConfiguration->getPhpExecutable(); + + // The CGI excutable is null if it is not available, check and SKIP if necessary + if (is_null($phpExecutable)) { + $this->status['skip'] = 'The CGI executable is unavailable'; + return $this->status; + } + + $phpCommand = $phpExecutable; + $phpCommand .= ' '. $testCase->testConfiguration->getPhpCommandLineArguments(); + $phpCommand .= ' -f '.$this->fileName; + $phpCommand .= ' '.$testCase->testConfiguration->getTestCommandLineArguments(); + $phpCommand .= ' 2>&1 '.$testCase->testConfiguration->getInputFileString(); + + + $PhpRunner = new rtPhpRunner($phpCommand, + $testCase->testConfiguration->getEnvironmentVariables(), + $runConfiguration->getSetting('WorkingDirectory') + ); + + try { + + $this->output = $PhpRunner->runphp(); + + // If it's a CGI test sort the headers out here + if (substr($phpExecutable, -2) == '-C') { + + if (preg_match("/^(.*?)$this->twoBlankLines(.*)/s", $this->output, $match)) { + $this->output = $match[2]; + $this->headers = $match[1]; + } + } + } catch (rtPhpRunnerException $e) { + + $this->status['fail'] = $e->getMessage(); } return $this->status; } - - private function copyExternalFileContent() + /** + * @return string + */ + public function getHeaders() { - if (sizeof($this->sectionContents) == 1) { - - $file = $this->sectionContents[0]; - - // don't allow tests to retrieve files from anywhere but this subdirectory - $file = dirname($this->fileName).'/'.trim(str_replace('..', '', $file)); - - if (file_exists($file)) { + return $this->headers; + } + + /** + * checks and copies the content of the external file to the testfile + * + * @return boolean + */ + protected function copyExternalFileContent() + { + if (sizeof($this->sectionContents) != 1) { + + $this->status['fail'] = 'One file per testcase permitted.'; + return false; + } + + $c = $this->sectionContents[0]; + + // don't allow tests to retrieve files from anywhere but this subdirectory + $file = dirname($this->fileName).'/'.trim(str_replace('..', '', $c)); - $this->sectionContents[0] = file_get_contents($file); - return true; + if (!file_exists($file)) { + + $this->status['fail'] = 'Can not open external file '.$file; + return false; + } - } else { - - $this->status['fail'] = 'Can not open external file '.$file; - } - - } else { - - $this->status['fail'] = 'One file per testcase permitted.'; - } - - return false; + $content = file_get_contents($file); + file_put_contents($this->fileName, $content); + return true; } } ?> diff --git a/tests/testcase/sections/executablesections/rtFileExternalSectionTest.php b/tests/testcase/sections/executablesections/rtFileExternalSectionTest.php new file mode 100644 index 0000000..6663583 --- /dev/null +++ b/tests/testcase/sections/executablesections/rtFileExternalSectionTest.php @@ -0,0 +1,55 @@ +<?php + +require_once 'PHPUnit/Framework.php'; +require_once dirname(__FILE__) . '../../../../../src/rtAutoload.php'; + +class rtFileExternalSectionTest extends PHPUnit_Framework_TestCase +{ + public function testCreateInstance() + { + $fileSection = new rtFileExternalSection('FILE_EXTERNAL', array('<?php', 'echo "hello world";', '?>')); + $code = $fileSection->getContents(); + + $this->assertEquals('<?php', $code[0]); + } + + public function testTooMuchFiles() + { + $wrapper = new rtFileExternalSectionTestWrapper('FILE_EXTERNAL', array('file1','file2')); + + $this->assertFalse($wrapper->copyExternalFileContentTest()); + + $status = $wrapper->getStatus(); + + $this->assertEquals('One file per testcase permitted.', $status['fail']); + } + + public function testNotExistingFile() + { + $wrapper = new rtFileExternalSectionTestWrapper('FILE_EXTERNAL', array('file1')); + + $this->assertFalse($wrapper->copyExternalFileContentTest()); + + $status = $wrapper->getStatus(); + + $this->assertEquals('Can not open external file /file1', $status['fail']); + } +} + +/** + * test-wrapper to acces protected methods and members + */ +class rtFileExternalSectionTestWrapper extends rtFileExternalSection +{ + public function copyExternalFileContentTest() + { + return parent::copyExternalFileContent(); + } + + public function getStatus() + { + return $this->status; + } +} + +?> \ No newline at end of file
« previous php.qa (#66458) next »