static function __static() { self::$rewriter = \lang\ClassLoader::defineClass('InliningOptimization··Rewriter', 'xp.compiler.ast.Visitor', [], '{ protected $replacements; protected $protect; public function __construct($replacements, $protect) { $this->replacements= $replacements; $this->protect= $protect; } protected function visitMethodCall(\\xp\\compiler\\ast\\MethodCallNode $node) { if ($node->name === $this->protect) $node->inlined= true; return parent::visitMethodCall($node); } protected function visitStaticMethodCall(\\xp\\compiler\\ast\\StaticMethodCallNode $node) { if ($node->name === $this->protect) $node->inlined= true; return parent::visitStaticMethodCall($node); } protected function visitVariable(\\xp\\compiler\\ast\\VariableNode $node) { return isset($this->replacements[$node->name]) ? clone $this->replacements[$node->name] : $node ; } }'); }
public static function defineCommandClass() { self::$class = ClassLoader::defineClass('util.cmd.unittest.BatchImport', Command::class, [], ['run' => function () { }]); self::$global = ClassLoader::defineClass('BatchImport', Command::class, [], ['run' => function () { }]); }
/** * Helper method * @param string name * @param string parent * @param string[] interfaces * @return lang.XPClass class * @throws unittest.AssertionFailedError */ protected function defineClass($name, $parent, $interfaces, $bytes) { if (class_exists(\xp::reflect($name), false)) { $this->fail('Class "' . $name . '" may not exist!'); } return \lang\ClassLoader::defineClass($name, $parent, $interfaces, $bytes); }
public static function defineLayout() { self::$layout = ClassLoader::defineClass(self::class . '_Layout', Object::class, [WebLayout::class], '{ public function mappedApplications($profile= null) { /* Intentionally empty */ } public function staticResources($profile= null) { /* Intentionally empty */ } }'); }
public static function defineExiterClass() { self::$exiterClass = \lang\ClassLoader::defineClass('net.xp_framework.unittest.core.Exiter', 'lang.Object', array(), '{ public function __construct() { throw new SystemExit(0); } public static function doExit() { new self(); } }'); }
/** * Creates a new instance creation fluent interface for a given class * * @param lang.mirrors.TypeMirror|lang.XPClass|string $type * @return lang.XPClass */ public static final function typeOf($type) { $mirror = $type instanceof TypeMirror ? $type : new TypeMirror($type); $type = $mirror->name(); if (!isset(self::$creations[$type])) { if (!$mirror->kind()->isClass() || $mirror->modifiers()->isAbstract()) { throw new IllegalArgumentException('Class ' . $type . ' is not instantiable'); } $constructor = $mirror->constructor(); if (!$constructor->present()) { throw new IllegalArgumentException('Class ' . $type . ' does not have a constructor'); } $setters = $args = ''; foreach ($constructor->parameters() as $parameter) { $name = $parameter->name(); if ($parameter->isOptional()) { $setters .= 'public $' . $name . '= ' . var_export($parameter->defaultValue(), true) . ';'; } else { $setters .= 'public $' . $name . ';'; } $setters .= "/**\n * @param " . $parameter->type() . "\n * @return self\n*/"; $setters .= 'public function ' . $name . '($value) { $this->' . $name . '= $value; return $this; }'; $args .= ', $this->' . $name; } self::$creations[$type] = ClassLoader::defineClass($type . 'Creation', 'lang.partial.InstanceCreation', [], '{ /** @return ' . $mirror->name() . ' */ public function create() { return new \\' . $mirror->reflect->name . '(' . substr($args, 2) . '); } ' . $setters . ' }'); } return self::$creations[$type]; }
static function __static() { // For singletonInstance test ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousSingleton', 'lang.Object', array(), '{ protected static $instance= NULL; static function getInstance() { if (!isset(self::$instance)) self::$instance= new AnonymousSingleton(); return self::$instance; } }'); // For returnNewObject and returnNewObjectViaReflection tests ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousList', 'lang.Object', array(), '{ public function __construct() { \\net\\xp_framework\\unittest\\core\\ReferencesTest::registry("list", $this); } }'); ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousFactory', 'lang.Object', array(), '{ static function factory() { return new AnonymousList(); } }'); ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousNewInstanceFactory', 'lang.Object', array(), '{ static function factory() { return XPClass::forName("net.xp_framework.unittest.core.AnonymousList")->newInstance(); } }'); }
public static function dummyConnectionClass() { self::$conn = \lang\ClassLoader::defineClass('RestClientExecutionTest_Connection', 'peer.http.HttpConnection', array(), '{ protected $result= NULL; protected $exception= NULL; public function __construct($status, $body, $headers) { parent::__construct("http://test"); if ($status instanceof Throwable) { $this->exception= $status; } else { $this->result= "HTTP/1.1 ".$status."\\r\\n"; foreach ($headers as $name => $value) { $this->result.= $name.": ".$value."\\r\\n"; } $this->result.= "\\r\\n".$body; } } public function send(HttpRequest $request) { if ($this->exception) { throw $this->exception; } else { return new HttpResponse(new MemoryInputStream($this->result)); } } }'); }
public static function defineExiterClass() { self::$exiterClass = ClassLoader::defineClass('net.xp_framework.unittest.core.Exiter', Object::class, [], '{ public function __construct() { throw new \\lang\\SystemExit(0); } public static function doExit() { new self(); } }'); }
public static function dummyConnectionClass() { self::$conn = ClassLoader::defineClass('EndpointExecutionTest_Connection', 'peer.http.HttpConnection', [], '{ private $result, $exception; public function __construct($status, $body, $headers) { parent::__construct("http://test"); if ($status instanceof \\lang\\Throwable) { $this->exception= $status; } else { $this->result= "HTTP/1.1 ".$status."\\r\\n"; foreach ($headers as $name => $value) { $this->result.= $name.": ".$value."\\r\\n"; } $this->result.= "\\r\\n".$body; } } public function send(\\peer\\http\\HttpRequest $request) { if ($this->exception) { throw $this->exception; } else { return new \\peer\\http\\HttpResponse(new \\io\\streams\\MemoryInputStream($this->result)); } } }'); }
/** * Constructor * * @param string bytes method sourcecode */ public function __construct($bytes) { $name = 'xp.unittest.DynamicallyGeneratedTestCase·' . self::$uniqId++; $this->testClass = \lang\ClassLoader::defineClass($name, 'unittest.TestCase', array(), '{ #[@test] public function run() { ' . $bytes . ' } }'); }
public static function defineResult() { self::$result = \lang\ClassLoader::defineClass('FileManagerTestEmitterResult', 'lang.Object', ['xp.compiler.emit.EmitterResult'], '{ protected $type= null; public function __construct($name) { $this->type= new \\xp\\compiler\\types\\TypeReference(new \\xp\\compiler\\types\\TypeName($name)); } public function type() { return $this->type; } public function extension() { return ".test"; } }'); }
public static function defineVisitor() { self::$visitor = ClassLoader::defineClass('VisitorTest··Visitor', 'xp.compiler.ast.Visitor', [], '{ public $visited= array(); public function visitOne($node) { $this->visited[]= $node; return parent::visitOne($node); } }'); }
static function __static() { self::$fileStreamAdapter = \lang\ClassLoader::defineClass('FileStreamAdapter', 'io.File', array(), '{ protected $stream= NULL; public function __construct($stream) { $this->stream= $stream; } public function exists() { return NULL !== $this->stream; } public function getURI() { return Streams::readableUri($this->stream); } public function getInputStream() { return $this->stream; } }'); }
static function __static() { self::$fileStreamAdapter = ClassLoader::defineClass('FileStreamAdapter', File::class, [], '{ protected $stream= null; public function __construct($stream) { $this->stream= $stream; } public function exists() { return null !== $this->stream; } public function getURI() { return \\io\\streams\\Streams::readableUri($this->stream); } public function getInputStream() { return $this->stream; } }'); }
public static function defineCloseableSubclasses() { self::$closes = ClassLoader::defineClass('_WithTest_C0', Object::class, [Closeable::class], '{ public $closed= false; public function close() { $this->closed= true; } }'); self::$raises = ClassLoader::defineClass('_WithTest_C1', Object::class, [Closeable::class], '{ public function close() { throw new \\lang\\IllegalArgumentException("Cannot close"); } }'); }
public function map_of_string_to_object() { \lang\ClassLoader::defineClass('GenericsBCTest_Map', 'lang.Object', array(), '{ public $__generic; public function getClassName() { return "Map<".$this->__generic[0].", ".$this->__generic[1].">"; } }'); $this->assertEquals('Map<String, Object>', create('new GenericsBCTest_Map<String, Object>')->getClassName()); }
public static function defineCloseableSubclasses() { self::$closes = ClassLoader::defineClass('_WithTest_C0', Object::class, [Closeable::class], '{ public $closed= false; public function close() { $this->closed= true; } }'); self::$raises = ClassLoader::defineClass('_WithTest_C1', Object::class, [Closeable::class], '{ private $throwable; public function __construct($class) { $this->throwable= $class; } public function close() { throw new $this->throwable("Cannot close"); } }'); }
/** * Constructor * * @param string $src method sourcecode */ public function __construct($input) { // Support <?php if (0 === strncmp($input, '<?', 2)) { $input = substr($input, strcspn($input, "\r\n\t =") + 1); } $fragment = trim($input, "\r\n\t ;") . ';'; $name = 'xp.unittest.DynamicallyGeneratedTestCase' . self::$uniqId++; $this->testClass = ClassLoader::defineClass($name, TestCase::class, [], '{ #[@test] public function run() { ' . $fragment . ' } }'); }
/** * Constructor * * @param string $src method sourcecode */ public function __construct($src) { // Support <?php $src = trim($src, ' ;') . ';'; if (0 === strncmp($src, '<?php', 5)) { $src = substr($src, 6); } $name = 'xp.unittest.DynamicallyGeneratedTestCase·' . self::$uniqId++; $this->testClass = ClassLoader::defineClass($name, 'unittest.TestCase', [], '{ #[@test] public function run() { ' . $src . ' } }'); }
/** * Returns a varargs method with a given signature. Uses caching. * * @param string $signature * @return string[] A reference to the method */ private function compile($signature, $apidoc = []) { $class = nameof($this) . md5($signature); if (!isset(self::$compiled[$class])) { self::$compiled[$class] = ClassLoader::defineClass($class, 'lang.Object', [], '{ /** ' . implode("\n* ", $apidoc) . ' */ public static function fixture(' . $signature . ') { } }'); } return [$class, 'fixture']; }
public static function requestEchoingConnectionClass() { self::$conn = ClassLoader::defineClass('RestClientSendTest_Connection', HttpConnection::class, [], '{ public function send(\\peer\\http\\HttpRequest $request) { $str= $request->getRequestString(); return new \\peer\\http\\HttpResponse(new \\io\\streams\\MemoryInputStream(sprintf( "HTTP/1.0 200 OK\\r\\nContent-Type: text/plain\\r\\nContent-Length: %d\\r\\n\\r\\n%s", strlen($str), $str ))); } }'); }
public static function mockSocket() { self::$proto = \lang\ClassLoader::defineClass('rdbms.unittest.tds.MockTdsProtocol', 'lang.Object', [], '{ protected $records= array(); public function __construct($records) { $this->records= $records; } public function fetch($fields) { return array_shift($this->records); } }'); }
public static function mockSocket() { self::$proto = \lang\ClassLoader::defineClass('net.xp_framework.unittest.rdbms.mysql.MockMysqlProtocol', 'lang.Object', array(), '{ protected $records= array(); public function __construct($records) { $this->records= $records; } public function fetch($fields) { return array_shift($this->records); } }'); }
public static function defineObservable() { self::$observable = ClassLoader::defineClass('net.xp_framework.unittest.util.ObservableFixture', Observable::class, [], '{ private $value= 0; public function setValue($value) { $this->value= $value; $this->setChanged(); $this->notifyObservers(); } public function getValue() { return $this->value; } }'); }
public static function registerTransport() { HttpTransport::register('test', \lang\ClassLoader::defineClass('TestHttpTransport', 'peer.http.HttpTransport', array(), '{ public $host, $port, $arg; public function __construct(URL $url, $arg) { $this->host= $url->getHost(); $this->port= $url->getPort(80); $this->arg= $arg; } public function send(HttpRequest $request, $timeout= 60, $connecttimeout= 2.0) { // Not implemented } }')); }
public static function defineMockSocket() { self::$mockSocket = \lang\ClassLoader::defineClass('net.xp_framework.unittest.remote.MockSocket', 'lang.Object', array(), '{ public function __construct($bytes) { $this->bytes= $bytes; $this->offset= 0; } public function readBinary($size= 8192) { if ($this->offset > strlen($this->bytes)) return FALSE; $chunk= substr($this->bytes, $this->offset, $size); $this->offset+= strlen($chunk); return $chunk; } }'); }
public static function requestEchoingConnectionClass() { self::$conn = \lang\ClassLoader::defineClass('RestClientSendTest_Connection', 'peer.http.HttpConnection', array(), '{ public function __construct() { parent::__construct("http://test"); } public function send(HttpRequest $request) { $str= $request->getRequestString(); return new HttpResponse(new MemoryInputStream(sprintf( "HTTP/1.0 200 OK\\r\\nContent-Type: text/plain\\r\\nContent-Length: %d\\r\\n\\r\\n%s", strlen($str), $str ))); } }'); }
public static function defineMock() { self::$shell = ClassLoader::defineClass('WScript_ShellMock', 'lang.Object', [], '{ protected $keys; public function __construct($keys) { $this->keys= $keys; } public function regRead($key) { $k= substr($key, strrpos($key, "\\\\") + 1); if (isset($this->keys[$k])) return $this->keys[$k]; throw new \\Exception("Cannot read $key from registry"); } }'); }
static function __static() { $self = new \lang\XPClass(__CLASS__); foreach (hash_algos() as $algo) { MessageDigest::register($algo, $self); } // Overwrite crc32b implementation if a buggy implementation is detected. // Workaround for http://bugs.php.net/bug.php?id=45028 if ('0a1cb779' === hash('crc32b', 'AAAAAAAA')) { MessageDigest::register('crc32b', \lang\ClassLoader::defineClass(__CLASS__ . '·CRC32bDigestImpl', $self->getName(), [], '{ public function doFinal() { $n= hexdec(hash_final($this->handle)); return sprintf("%08x", (($n & 0xFF) << 24) + (($n & 0xFF00) << 8) + (($n & 0xFF0000) >> 8) + (($n >> 24) & 0xFF)); } }')); } // Add crc16 \lang\XPClass::forName('security.checksum.CRC16DigestImpl'); }