public function newInstance() { $p = Runtime::getInstance()->getExecutable()->newInstance(array('-v')); $version = 'PHP ' . phpversion(); $this->assertEquals($version, $p->out->read(strlen($version))); $p->close(); }
/** * Issues a uses() command inside a new runtime for every class given * and returns a line indicating success or failure for each of them. * * @param string[] uses * @param string decl * @return var[] an array with three elements: exitcode, stdout and stderr contents */ protected function useAllOf($uses, $decl = '') { with($out = $err = '', $p = Runtime::getInstance()->newInstance(NULL, 'class', 'xp.runtime.Evaluate', array())); $p->in->write($decl . ' ClassLoader::registerPath(\'' . strtr($this->getClass()->getClassLoader()->path, '\\', '/') . '\'); $errors= 0; foreach (array("' . implode('", "', $uses) . '") as $class) { try { uses($class); echo "+OK ", $class, "\\n"; } catch (Throwable $e) { echo "-ERR ", $class, ": ", $e->getClassName(), "\\n"; $errors++; } } exit($errors); '); $p->in->close(); // Read output while ($b = $p->out->read()) { $out .= $b; } while ($b = $p->err->read()) { $err .= $b; } // Close child process $exitv = $p->close(); return array($exitv, explode("\n", rtrim($out)), explode("\n", rtrim($err))); }
/** * Set up test and create fixture * */ public function setUp() { if (!\lang\Runtime::getInstance()->extensionAvailable('soap')) { throw new \unittest\PrerequisitesNotMetError('PHP Soap extension not available', null, array('ext/soap')); } $this->fixture = new NativeSoapTypeMapper(); }
public function newInstance() { $p = Runtime::getInstance()->getExecutable()->newInstance($this->arguments('-v')); $version = defined('HHVM_VERSION') ? 'HipHop VM ' . HHVM_VERSION : 'PHP ' . PHP_VERSION; $this->assertEquals($version, $p->out->read(strlen($version))); $p->close(); }
/** * Setup method. Ensure extension we depend on is available * */ public function setUp() { $depend = $this->extension(); if (!\lang\Runtime::getInstance()->extensionAvailable($depend)) { throw new \unittest\PrerequisitesNotMetError(ucfirst($depend) . ' support not available', null, array('ext/' . $depend)); } }
/** * Constructor * * @param io.streams.OutputStreamWriter out */ public function __construct() { if (!Runtime::getInstance()->extensionAvailable('xdebug')) { throw new PrerequisitesNotMetError('code coverage not available. Please install the xdebug extension.'); } $this->processor = new DomXSLProcessor(); $this->processor->setXSLBuf($this->getClass()->getPackage()->getResource('coverage.xsl')); }
public function systemExitWithNonZeroExitCode() { try { \lang\Runtime::halt(127); $this->fail('Expected', NULL, 'lang.SystemExit'); } catch (SystemExit $e) { $this->assertEquals(127, $e->getCode()); } }
/** * Sets up test case * */ public function setUp() { parent::setUp(); $this->vendor = $this->vendorName(); // Check whether any PHP extensions are required by a specific test, // and skip if this extension is not loaded. This is done by checking // for an @ext annotation. $m = $this->getClass()->getMethod($this->name); if ($m->hasAnnotation('ext') && !\lang\Runtime::getInstance()->extensionAvailable($ext = $m->getAnnotation('ext'))) { throw new \unittest\PrerequisitesNotMetError('Extension not available', null, array($ext)); } }
/** * Runs sourcecode inside a new runtime * * @param string $src * @return var[] an array with three elements: exitcode, stdout and stderr contents */ protected function runInNewRuntime($src) { with($out = $err = '', $p = Runtime::getInstance()->newInstance(null, 'class', 'xp.runtime.Evaluate', [])); $p->in->write($src); $p->in->close(); // Read output while ($b = $p->out->read()) { $out .= $b; } while ($b = $p->err->read()) { $err .= $b; } // Close child process $exitv = $p->close(); return [$exitv, $out, $err]; }
/** * Starts server in background * * @param string protocol */ public static function startServerWith($protocol) { // Start server process with($rt = Runtime::getInstance()); self::$serverProcess = $rt->getExecutable()->newInstance(array_merge($rt->startupOptions()->asArguments(), array($rt->bootstrapScript('class')), array('net.xp_framework.unittest.peer.server.TestingServer', $protocol))); self::$serverProcess->in->close(); // Check if startup succeeded $status = self::$serverProcess->out->readLine(); if (2 != sscanf($status, '+ Service %[0-9.]:%d', self::$bindAddress[0], self::$bindAddress[1])) { try { self::shutdownServer(); } catch (\lang\IllegalStateException $e) { $status .= $e->getMessage(); } throw new \unittest\PrerequisitesNotMetError('Cannot start server: ' . $status, null); } }
/** * Starts server * * @param lang.XPClass $c * @return void * @throws unittest.PrerequisitesNotMetError */ public function beforeTestClass(\lang\XPClass $c) { // Start server process $this->serverProcess = Runtime::getInstance()->newInstance(null, 'class', $this->mainClass, $this->arguments); $this->serverProcess->in->close(); // Check if startup succeeded $status = $this->serverProcess->out->readLine(); if (1 != sscanf($status, '+ Service %[0-9.:]', $bindAddress)) { try { $this->afterTestClass($c); } catch (IllegalStateException $e) { $status .= $e->getMessage(); } throw new \unittest\PrerequisitesNotMetError('Cannot start server: ' . $status, null); } $c->getMethod($this->connected)->invoke(null, [$bindAddress]); }
/** * Issues a uses() command inside a new runtime for every class given * and returns a line indicating success or failure for each of them. * * @param string[] uses * @param string src * @return var[] an array with three elements: exitcode, stdout and stderr contents */ protected function runInNewRuntime($uses, $src) { with($out = $err = '', $p = Runtime::getInstance()->newInstance(null, 'class', 'xp.runtime.Evaluate', array())); $uses && $p->in->write('uses("' . implode('", "', $uses) . '");'); $p->in->write($src); $p->in->close(); // Read output while ($b = $p->out->read()) { $out .= $b; } while ($b = $p->err->read()) { $err .= $b; } // Close child process $exitv = $p->close(); return array($exitv, $out, $err); }
/** * Create a new runtime * * @return var[] an array with three elements: exitcode, stdout and stderr contents */ protected function run() { $runtime = Runtime::getInstance(); $options = $runtime->startupOptions()->withSetting('magic_quotes_gpc', 1)->withSetting('error_reporting', 'E_ALL'); $out = $err = ''; with($p = $runtime->newInstance($options, 'class', 'xp.runtime.Evaluate', array('return 1;'))); $p->in->close(); // Read output while ($b = $p->out->read()) { $out .= $b; } while ($b = $p->err->read()) { $err .= $b; } // Close child process $exitv = $p->close(); return array($exitv, $out, $err); }
/** * Serve requests * * @param string $source * @param string $profile * @param io.Path $webroot * @param io.Path $docroot * @param string[] $config */ public function serve($source, $profile, $webroot, $docroot, $config) { $runtime = Runtime::getInstance(); $startup = $runtime->startupOptions(); $backing = typeof($startup)->getField('backing')->setAccessible(true)->get($startup); // PHP doesn't start with a nonexistant document root if (!$docroot->exists()) { $docroot = getcwd(); } // Start `php -S`, the development webserver $arguments = ['-S', $this->host . ':' . $this->port, '-t', $docroot]; $options = newinstance(RuntimeOptions::class, [$backing], ['asArguments' => function () use($arguments) { return array_merge($arguments, parent::asArguments()); }]); $options->withSetting('user_dir', $source . PATH_SEPARATOR . implode(PATH_SEPARATOR, $config)); // Pass classpath (TODO: This is fixed in XP 7.6.0, remove once // this becomes minimum dependency) $cp = []; foreach (ClassLoader::getLoaders() as $delegate) { if ($delegate instanceof FileSystemClassLoader || $delegate instanceof ArchiveClassLoader) { $cp[] = $delegate->path; } } set_include_path(''); $options->withClassPath($cp); // Export environment putenv('DOCUMENT_ROOT=' . $docroot); putenv('WEB_ROOT=' . $webroot); putenv('SERVER_PROFILE=' . $profile); Console::writeLine("[33m@", $this, "[0m"); Console::writeLine("[1mServing ", (new Source($source, new Config($config)))->layout()); Console::writeLine("[36m", str_repeat('═', 72), "[0m"); Console::writeLine(); with($runtime->newInstance($options, 'web', '', []), function ($proc) { $proc->in->close(); Console::writeLine("[33;1m>[0m Server started: [35;4m", $this->url, "[0m (", date('r'), ')'); Console::writeLine(' PID ', $proc->getProcessId(), '; press Ctrl+C to exit'); Console::writeLine(); while (is_string($line = $proc->err->readLine())) { Console::writeLine(" [36m", $line, "[0m"); } }); }
public function phpRefusesToStart() { $runtime = Runtime::getInstance(); $options = $runtime->startupOptions()->withSetting('magic_quotes_gpc', 1)->withSetting('error_reporting', 'E_ALL'); $out = $err = ''; with($p = $runtime->newInstance($options, 'class', 'xp.runtime.Evaluate', ['return 1;'])); $p->in->close(); // Read output while ($b = $p->out->read()) { $out .= $b; } while ($b = $p->err->read()) { $err .= $b; } // Close child process $exitv = $p->close(); $this->assertEquals(1, $exitv, 'exitcode'); $this->assertTrue((bool) strstr($out . $err, "Directive 'magic_quotes_gpc' is no longer available in PHP"), \xp::stringOf(['out' => $out, 'err' => $err])); }
/** * Switch storage algorithm backing * * @param int $type one of BACKING_MCRYPT, BACKING_OPENSSL, BACKING_PLAINTEXT * @throws lang.IllegalArgumentException If illegal backing type was given * @throws lang.IllegalStateException If chosen backing missed a extension dependency * @return void */ public static function useBacking($type) { switch ($type) { case self::BACKING_OPENSSL: if (!Runtime::getInstance()->extensionAvailable('openssl')) { throw new IllegalStateException('Backing "openssl" required but extension not available.'); } $key = md5(uniqid()); $iv = substr(md5(uniqid()), 0, openssl_cipher_iv_length('des')); return self::setBacking(function ($value) use($key, $iv) { return openssl_encrypt($value, 'DES', $key, 0, $iv); }, function ($value) use($key, $iv) { return openssl_decrypt($value, 'DES', $key, 0, $iv); }); case self::BACKING_MCRYPT: // Deprecated, see https://wiki.php.net/rfc/mcrypt-viking-funeral if (!Runtime::getInstance()->extensionAvailable('mcrypt')) { throw new IllegalStateException('Backing "mcrypt" required but extension not available.'); } $engine = mcrypt_module_open(MCRYPT_DES, '', 'ecb', ''); $engineiv = mcrypt_create_iv(mcrypt_enc_get_iv_size($engine), MCRYPT_RAND); $key = substr(md5(uniqid()), 0, mcrypt_enc_get_key_size($engine)); mcrypt_generic_init($engine, $key, $engineiv); return self::setBacking(function ($value) use($engine) { return mcrypt_generic($engine, $value); }, function ($value) use($engine) { return rtrim(mdecrypt_generic($engine, $value), ""); }); case self::BACKING_PLAINTEXT: return self::setBacking(function ($value) { return base64_encode($value); }, function ($value) { return base64_decode($value); }); default: throw new IllegalArgumentException('Invalid backing given: ' . \xp::stringOf($type)); } }
/** * Sets up test case * */ public function setUp() { if (!Runtime::getInstance()->extensionAvailable('zlib')) { throw new \unittest\PrerequisitesNotMetError('ZLib support not available', null, array('ext/zlib')); } }
public function fatalsForNonExistingXars() { $r = $this->runWith(Runtime::getInstance()->startupOptions()->withClassPath('/does-not-exist.xar')); $this->assertEquals(255, $r[0], 'exitcode'); $this->assertTrue((bool) strstr($r[1] . $r[2], '[bootstrap] Classpath element [/does-not-exist.xar] not found'), \xp::stringOf(array('out' => $r[1], 'err' => $r[2]))); }
/** * Return a file that is known to exist * * @return string */ protected function fileKnownToExist() { return realpath(Runtime::getInstance()->getExecutable()->getFilename()); }
public function shutdownHookRunOnUncaughtException() { $out = $this->runInNewRuntime(Runtime::getInstance()->startupOptions(), ' Runtime::getInstance()->addShutdownHook(newinstance("lang.Runnable", array(), "{ public function run() { echo \'+OK Shutdown hook run\'; } }")); echo "+OK exiting"; xp::null()->error(); ', 255); $this->assertEquals('+OK exiting', substr($out, 0, 11), $out); $this->assertEquals('+OK Shutdown hook run', substr($out, -21), $out); }
/** * Sets up this unittest * * @throws unittest.PrerequisitesNotMetError */ public function setUp() { if (!Runtime::getInstance()->extensionAvailable('dom')) { throw new PrerequisitesNotMetError('DOM extension not loaded', null, ['ext/dom']); } }
public function pass_arguments_which_need_encoding($args, $expected) { $out = $this->runInNewRuntime(Runtime::getInstance()->startupOptions(), 'echo sizeof($argv), ": ", implode(" ", $argv);', 0, $args); $this->assertEquals($expected, $out); }
/** * Called when a test run finishes. * * @param unittest.TestSuite $suite * @param unittest.TestResult $result * @param unittest.StopTests $stop */ public function testRunFinished(\unittest\TestSuite $suite, \unittest\TestResult $result, \unittest\StopTests $stopped = null) { $failed = $result->failureCount(); if ($stopped) { $this->out->writeLine('|'); $indicator = ($this->colored ? "[43;1;30m■ " : 'STOP ') . $stopped->getMessage(); } else { if ($failed) { $this->out->writeLine(']'); $indicator = $this->colored ? "[41;1;37m×" : 'FAIL'; } else { $this->out->writeLine(']'); $indicator = $this->colored ? "[42;1;37m♥" : 'OK'; } } // Show failed test details if ($failed) { $this->out->writeLine(); foreach ($result->failed as $failure) { $this->out->writeLine('F ', $failure); } } $this->out->writeLinef("\n%s: %d/%d run (%d skipped), %d succeeded, %d failed%s", $indicator, $result->runCount(), $result->count(), $result->skipCount(), $result->successCount(), $result->failureCount(), $this->colored ? "[0m" : ''); $this->out->writeLinef('Memory used: %.2f kB (%.2f kB peak)', Runtime::getInstance()->memoryUsage() / 1024, Runtime::getInstance()->peakMemoryUsage() / 1024); $this->out->writeLinef('Time taken: %.3f seconds', $result->elapsed()); }
/** @return io.File */ protected function existingFile() { return new File(Runtime::getInstance()->bootstrapScript('class')); }
public function memoryLimit() { $this->assertEquals(\lang\Primitive::$INT, \lang\Type::forName(gettype(Runtime::getInstance()->memoryLimit()))); }
/** * Called when a test run finishes. * * @param unittest.TestSuite suite * @param unittest.TestResult result */ public function testRunFinished(\unittest\TestSuite $suite, \unittest\TestResult $result) { $this->out->writeLine(']'); // Show failed test details $fail = false; if ($result->failureCount() > 0) { $this->out->writeLine(); foreach ($result->failed as $failure) { $this->out->writeLine('F ', $failure); } $fail = true; } $this->out->writeLinef("\n%s: %d/%d run (%d skipped), %d succeeded, %d failed%s", $this->colored ? $fail ? "[41;1;37m✗" : "[42;1;37m✓" : ($fail ? 'FAIL' : 'OK'), $result->runCount(), $result->count(), $result->skipCount(), $result->successCount(), $result->failureCount(), $this->colored ? "[0m" : ''); $this->out->writeLinef('Memory used: %.2f kB (%.2f kB peak)', Runtime::getInstance()->memoryUsage() / 1024, Runtime::getInstance()->peakMemoryUsage() / 1024); $this->out->writeLinef('Time taken: %.3f seconds', $result->elapsed()); }
/** * Called when a test run finishes. * * @param unittest.TestSuite suite * @param unittest.TestResult result */ public function testRunFinished(\unittest\TestSuite $suite, \unittest\TestResult $result) { // Details if ($result->successCount() > 0) { $this->out->writeLine("\n---> Succeeeded:"); foreach (array_keys($result->succeeded) as $key) { $this->out->writeLine('* ', $result->succeeded[$key]); } } if ($result->skipCount() > 0) { $this->out->writeLine("\n---> Skipped:"); foreach (array_keys($result->skipped) as $key) { $this->out->writeLine('* ', $result->skipped[$key]); } } if ($result->failureCount() > 0) { $this->out->writeLine("\n---> Failed:"); foreach (array_keys($result->failed) as $key) { $this->out->writeLine('* ', $result->failed[$key]); } } $this->out->writeLinef("\n===> %s: %d run (%d skipped), %d succeeded, %d failed", $result->failureCount() ? 'FAIL' : 'OK', $result->runCount(), $result->skipCount(), $result->successCount(), $result->failureCount()); $this->out->writeLinef('===> Memory used: %.2f kB (%.2f kB peak)', Runtime::getInstance()->memoryUsage() / 1024, Runtime::getInstance()->peakMemoryUsage() / 1024); $this->out->writeLinef('===> Time taken: %.3f seconds', $result->elapsed()); }
/** * Called when a test run finishes. * * @param unittest.TestSuite suite * @param unittest.TestResult result */ public function testRunFinished(\unittest\TestSuite $suite, \unittest\TestResult $result) { $this->writeStatus(); $this->out->writeLine(); // Summary output $this->out->writeLinef("\n%s: %d/%d run (%d skipped), %d succeeded, %d failed", $result->failureCount() > 0 ? 'FAIL' : 'OK', $result->runCount(), $result->count(), $result->skipCount(), $result->successCount(), $result->failureCount()); $this->out->writeLinef('Memory used: %.2f kB (%.2f kB peak)', \lang\Runtime::getInstance()->memoryUsage() / 1024, \lang\Runtime::getInstance()->peakMemoryUsage() / 1024); $this->out->writeLinef('Time taken: %.3f seconds', $result->elapsed()); }
public function setBaseAddsTrailingDirectorySeparator() { $file = \lang\Runtime::getInstance()->getExecutable()->getFilename(); $path = rtrim(realpath(dirname($file)), DIRECTORY_SEPARATOR); $this->processor->setBase($path); $this->assertEquals($path . DIRECTORY_SEPARATOR, $this->processor->getBase()); }
/** * Start new SQLRunner process * * @return lang.Process */ protected function newProcess() { with($rt = Runtime::getInstance()); $proc = $rt->newInstance($rt->startupOptions(), 'class', 'rdbms.unittest.integration.SQLRunner', [$this->dsn]); $this->assertEquals('! Started', $proc->out->readLine()); return $proc; }