/** * @dataProvider provideTestDirs */ public function testAddDir($dirname, $expected_result, $manipulate, $zipContentsOnly = false) { # Create new archive $archive = '/tmp/archive.zip'; $zip = new ZipArchiveEx(); $zip->open($archive, ZIPARCHIVE::OVERWRITE); # Try to add directory: if ($zipContentsOnly) { $result = $zip->addDirContents($dirname); } else { $result = $zip->addDir($dirname); } $this->assertEquals($expected_result, $result); # Close archive: $zip->close(); # If directory was added successfully if ($result) { # Remove extracted testdirectory from # former testruns: $extractionDir = self::$tmpDir . '/' . basename($dirname); FileSystemManager::rrmdir($extractionDir); # Extract directory $output = array(); # -u Option forces update of already existing files, # importang for testing on travis-ci.org! $extractTo = $zipContentsOnly ? $extractionDir : self::$tmpDir; exec('unzip -u ' . $archive . ' -d ' . $extractTo, $output, $result); $this->assertEquals(0, $result); # 0 = successfull # $manipulate holds the file to manipulate, # so the following assertion fails. if ($manipulate) { file_put_contents($extractionDir . '/' . $manipulate, 'Lorem ipsum dolor sit amet.'); $expected_result = 1; } else { $expected_result = 0; } # Compare extracted directory and original one exec('diff -arq ' . $dirname . ' ' . $extractionDir, $output, $result); LogMore::debug('Output of diff-command: %s', implode(PHP_EOL, $output)); LogMore::debug('Expecting %d, got: %d', $expected_result, $result); $this->assertEquals($expected_result, $result); } }
/** * Function: parseFile * * Returns: * * The pretty-printed PHP code */ private function parseFile($file) { LogMore::debug('parsing file %s', $file); $statements = file_get_contents($file); # Create Parser $parser = new PHPParser_Parser(new PHPParser_Lexer()); # Create syntax tree $syntax_tree = $parser->parse($statements); LogMore::debug('Syntax tree parsed'); # Pretty print syntax tree/convert syntax tree back to PHP statements: return $this->prettyPrint($syntax_tree); }
/** * Function: recursiveAddDir * * Recursively adds the passed directory and all files * and folders beneath it. * * Parameters: * * $dirname - The directory to add. * $basedir - The basedir where $dirname resides. * $adddir - Add the basedir as directory itself * to the zipfile? * * Returns: * * TRUE on success or FALSE on failure. */ private function recursiveAddDir($dirname, $basedir = null, $adddir = true) { LogMore::debug('In recursiveAddDir'); $rc = false; # If $dirname is a directory if (is_dir($dirname)) { LogMore::debug('Is a directory: %s', $dirname); # Save current working directory $working_directory = getcwd(); # Switch to passed directory chdir($dirname); # Get basename of passed directory $basename = $basedir . basename($dirname); # Add empty directory with the name of the passed directory if ($adddir) { LogMore::debug('Add empty dir %s', $basename); $rc = $this->addEmptyDir($basename); LogMore::debug('RC: %d', $rc); $basename = $basename . '/'; } else { $basename = null; } # Get all files in the directory $files = glob('*'); LogMore::debug('Number of files in %s: %d', $basename, sizeof($files)); # Loop through files foreach ($files as $f) { # If file is directory if (is_dir($f)) { # Call recursiveAdd LogMore::debug('Add dir %s', $f); $this->recursiveAddDir($f, $basename); } else { LogMore::debug('Add file %s', $basename . $f); $rc = $this->addFile($f, $basename . $f); LogMore::debug('RC: %d', $rc); } } # Switch back to current working directory chdir($working_directory); $rc = true; } return $rc; }
<?php function foo() { echo 'foo', PHP_EOL; } # Project to test the filtering/replacing of function calls: foo(); LogMore::debug('Yuhu');
/** * Function: close * * Like open(), the use of close() is optional. * * Returns: * * true - on success * false - on failure */ public static function close() { if (!($rc = closelog())) { trigger_error('Failed to close log', E_USER_ERROR); } else { # Delete ident: self::$ident = null; # Reset counter: self::$messageCounter = 0; } return $rc; }
<?php require 'vendor/autoload.php'; require 'src/JuggleCode.php'; LogMore::disable(); class JuggleCodeTest extends PHPUnit_Framework_TestCase { /** * Testcase provider for juggling code */ public function provideCases() { return array(array('The quick brown fox jumps over the lazy dog.', null), array('The quick brown fox jumps over the lazy dog', array('oppressedFunctionCalls' => array('end_sentence'))), array('The fox jumps over the dog.', array('handleMethodCalls' => array('$fox' => array('getAttributes' => array('type' => JuggleCode::JC_OPPRESS)), '$dog' => array('getAttributes' => array('type' => JuggleCode::JC_OPPRESS))))), array('The quick brown fox jumps over the lazy dog!', array('replacedFunctionCalls' => array('end_sentence' => '"!"'))), array('Lorem ipsum.', array('replacedFunctionCalls' => array('implode' => 'implode(%arg1%, array("Lorem", "ipsum"))'))), array('The quick brown fox jumps over the lazy dog...', array('replacedFunctionCalls' => array('end_sentence' => 'end_sentence(%args% . "..")'))), array('The lazy dog jumps over the quick brown fox.', array('handleMethodCalls' => array('$fox' => array('getAttributes' => array('type' => JuggleCode::JC_REPLACE, 'expression' => '$dog->getAttributes()'), 'getSpecies' => array('type' => JuggleCode::JC_REPLACE, 'expression' => '$dog->getSpecies()')), '$dog' => array('getAttributes' => array('type' => JuggleCode::JC_REPLACE, 'expression' => '$fox->getAttributes()'), 'getSpecies' => array('type' => JuggleCode::JC_REPLACE, 'expression' => '$fox->getSpecies()'))))), array('The quick brown fox hops over the lazy dog.', array('handleMethodCalls' => array('Animal' => array('jump' => array('type' => JuggleCode::JC_REPLACE, 'expression' => 'Animal::hop()')))))); } /** * @dataProvider provideCases */ public function testJuggling($expected_result, $options) { # Create new object $j = new JuggleCode('tests/test1.php', 'tests/outfile.php'); # Set default options for this test-script: $j->mergeScripts = true; # Set options... if ($options) { foreach ($options as $member => $value) { $j->{$member} = $value; } } # Juggle code $j->run();
private function switchDirectory($newDirectory) { LogMore::debug('attempt to switch directory: %s', $newDirectory); $masterDirectory = null; if ($newDirectory && $newDirectory != '.') { LogMore::debug('switching to directory %s', $newDirectory); $masterDirectory = getcwd(); $rc = chdir($newDirectory); if ($rc) { LogMore::debug('directory switching attempt successfull; masterdir: %s', $masterDirectory); } } return $masterDirectory; }