/** * @return void */ public function test_function() { // prepare Base::init([]); $reflection = new \ReflectionClass('\\DbMockLibrary\\Base'); $staticProperties = $reflection->getStaticProperties(); // test $this->assertInstanceOf('\\DbMockLibrary\\Base', $staticProperties['instance']); // invoke logic Base::getInstance()->destroy(); // prepare $staticProperties = $reflection->getStaticProperties(); // test $this->assertNull($staticProperties['instance']); }
public function addTestFile($file, $internalcall = false) { global $CFG; if ($this->performcoverage) { $refinfo = moodle_reflect_file($file); require_once $file; if ($refinfo->classes) { foreach ($refinfo->classes as $class) { $reflection = new ReflectionClass($class); if ($staticprops = $reflection->getStaticProperties()) { if (isset($staticprops['includecoverage']) && is_array($staticprops['includecoverage'])) { foreach ($staticprops['includecoverage'] as $toinclude) { $this->add_coverage_include_path($toinclude); } } if (isset($staticprops['excludecoverage']) && is_array($staticprops['excludecoverage'])) { foreach ($staticprops['excludecoverage'] as $toexclude) { $this->add_coverage_exclude_path($toexclude); } } } } // Automatically add the test dir itself, so nothing will be covered there $this->add_coverage_exclude_path(dirname($file)); } } parent::addTestFile($file, $internalcall); }
public function testRegisterValidator() { Comfort::registerValidator('flex', ComfortTestCustomerValidator::class); $reflClass = new \ReflectionClass(Comfort::class); $staticProperties = $reflClass->getStaticProperties(); $this->assertEquals(['registeredValidators' => ['flex' => ComfortTestCustomerValidator::class]], $staticProperties); }
/** * Dump all class members to array including objects * * @return string print_r() formatted array string; */ public function __toString() { # Dump all object vars to string # Recursively list all object vars $members = function ($object) use(&$members) { # Recursive closure... cool! if (!is_object($object) && !is_array($object)) { # We've found an actual member value return is_array($object) ? print_r($object) : $object; } # We've found a member that's an object, explore it's properties if (is_object($object)) { # Duplicate our object for inspection $reflection = new \ReflectionClass($object); # Get dynamic members $dynamics = get_object_vars($object); # Get constant members $constants = $reflection->getConstants(); # Get static members $statics = $reflection->getStaticProperties(); # = array_diff(get_class_vars(get_class($object)), $dynamic); # alternate method # Merge them $object = array_merge($constants, $statics, $dynamics); } # Recurse to check if the array has any further objects return array_map($members, $object); }; # All done build our string! return '<pre>' . print_r($members($this), true) . '</pre>'; }
/** * Test the initialization of the static counter (!== 0) */ public function testCounterNotNull() { $rand = new MicroTime(); $reflection_class = new \ReflectionClass("RandomLib\\Source\\MicroTime"); $static = $reflection_class->getStaticProperties(); $this->assertTrue($static['counter'] !== 0); }
public static function load($path = '') { if (!empty($path) and !in_array($path, self::$loaded)) { $path = !empty($path) ? '\\' . trim($path, '\\') : ''; $lang_class = '\\GCore' . $path . '\\Locales\\EnGb\\Lang'; $cutsom_lang = '\\GCore' . $path . '\\Locales\\' . Str::camilize(str_replace('-', '_', strtolower(Base::getConfig('site_language', 'en-gb')))) . '\\Lang'; if (class_exists($cutsom_lang)) { if (class_exists($lang_class)) { //load default language as well $lang_class_loaded = new \ReflectionClass($lang_class); self::$translations = array_merge((array) self::$translations, $lang_class_loaded->getConstants(), $lang_class_loaded->getStaticProperties()); self::$loaded[] = $path; } $lang_class = $cutsom_lang; } if (!class_exists($lang_class)) { return false; } $lang_class_loaded = new \ReflectionClass($lang_class); self::$translations = array_merge((array) self::$translations, $lang_class_loaded->getConstants(), $lang_class_loaded->getStaticProperties()); self::$loaded[] = $path; return true; } return false; }
public function setFormat($f) { //use reflection to valid value $ref = new ReflectionClass(__CLASS__); if (in_array($f, $ref->getStaticProperties())) { $this->_format = $f; } }
public function rewind() { $class_name = get_class($this); $reflection = new \ReflectionClass($class_name); $this->staticFieldIteratorData = $reflection->getStaticProperties(); $this->staticFieldIteratorKey = 0; reset($this->staticFieldIteratorData); }
public function testBasicValidSchemes() { $mirror = new \ReflectionClass(Url::class); $validSchemes = $mirror->getStaticProperties()['validSchemes']; foreach ($validSchemes as $scheme) { $this->assertTrue(Url::isValidScheme($scheme)); } }
/** * Test the initialization of the static counter (!== 0) */ public function testCounterNotNull() { $class = static::getTestedClass(); $rand = new $class(); $reflection_class = new \ReflectionClass($class); $static = $reflection_class->getStaticProperties(); $this->assertTrue($static['counter'] !== 0); }
/** * Get all the states in an array. * @return of question_state objects. */ public static function get_all() { $states = array(); $us = new ReflectionClass('question_state'); foreach ($us->getStaticProperties() as $name => $notused) { $states[] = self::${$name}; } return $states; }
public function testLoadVendorDatabaseFromFile() { // Clear database first to ensure that data actually gets loaded MacAddress::loadVendorDatabase(array()); // Pass default database. It should load without errors. MacAddress::loadVendorDatabaseFromFile(\Library\Module::getPath('data/MacAddress/manuf')); $reflectionClass = new \ReflectionClass('Library\\MacAddress'); $this->assertNotEmpty($reflectionClass->getStaticProperties()['_vendorList']); }
public function testExitCode() { $ref = new ReflectionClass("Application"); $static_values = $ref->getStaticProperties(); // $success_code = $ref->getStaticPropertyValue("SUCCESS"); $this->assertEquals(0, $static_values["SUCCESS"]); // $error_code = $ref->getStaticPropertyValue("ERROR"); $this->assertEquals(1, $static_values["ERROR"]); }
/** * List all available properties through reflection * FIXME: Move to parent class Struct, when php will have late static binding * * @return array or names */ public static function GetKeys() { $retval = array(); $ReflectionClassThis = new ReflectionClass(__CLASS__); foreach($ReflectionClassThis->getStaticProperties() as $Property) { $retval[] = $Property->name; } return($retval); }
/** * A function to be used for compatibility with legacy code: it gets the values of all global variables which used * to be declared, such as library version etc... and sets them to php classes. * It should be used by code which changed the values of those global variables to alter the working of the library. * Example code: * 1. include xmlrpc.inc * 2. set the values, e.g. $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8'; * 3. import them: PhpXmlRpc\PhpXmlRpc::importGlobals(); * 4. run your own code. */ public static function importGlobals() { $reflection = new \ReflectionClass('PhpXmlRpc\\PhpXmlRpc'); $staticProperties = $reflection->getStaticProperties(); foreach ($staticProperties as $name => $value) { if (isset($GLOBALS[$name])) { self::${$name} = $GLOBALS[$name]; } } }
public function testGetExceptionResponseErrorVariousErrorCodes() { $oResponseTypeMirror = new \ReflectionClass(HttpResponseType::class); $aStatusProperties = $oResponseTypeMirror->getStaticProperties(); $aStatuses = $aStatusProperties['aStatusList']; foreach ($aStatuses as $iStatus => $sResponseStatus) { $e = new ExceptionResponseError('test', $iStatus); $o = new ErrorProcessor($e); $this->assertEquals($iStatus, $o->getErrorCode()); } }
/** * Use reflection to ensure that all argument numbers are correct. Each * static method should accept 2 more arguments than their Stringy * equivalent. */ public function testArgumentNumbers() { $staticStringyClass = new ReflectionClass('Stringy\\StaticStringy'); $stringyClass = new ReflectionClass('Stringy\\Stringy'); // getStaticPropertyValue can't access protected properties $properties = $staticStringyClass->getStaticProperties(); foreach ($properties['methodArgs'] as $method => $expected) { $num = $stringyClass->getMethod($method)->getNumberOfParameters() + 2; $this->assertEquals($expected, $num, 'Invalid num args for ' . $method); } }
public function __construct(array $params = array()) { $c = new ReflectionClass(__CLASS__); $statics = $c->getStaticProperties(); foreach ($params as $key => $value) { if (!in_array($key, array_keys($statics))) { $this->{$key} = $value; } else { self::${$key} = $value; } } }
/** */ public function testGetStaticProperties() { $prop = new ReflectionProperty('test'); $propStatic = new ReflectionProperty('testStatic'); $this->object->addProperty($prop); $this->object->addProperty($propStatic); $propStatic->setDefaultValue('testValue'); $propStatic->setStatic(true); $this->assertEquals(2, count($this->object->getProperties())); $this->assertEquals(1, count($this->object->getStaticProperties())); $this->assertEquals($propStatic, array_pop($this->object->getStaticProperties())); }
/** * @param string $className extending PHPUnit_Extensions_SeleniumTestCase * @return PHPUnit_Extensions_SeleniumTestSuite */ public static function fromTestCaseClass($className) { $suite = new self(); $suite->setName($className); $class = new ReflectionClass($className); $classGroups = PHPUnit_Util_Test::getGroups($className); $staticProperties = $class->getStaticProperties(); //BC: renamed seleneseDirectory -> selenesePath if (!isset($staticProperties['selenesePath']) && isset($staticProperties['seleneseDirectory'])) { $staticProperties['selenesePath'] = $staticProperties['seleneseDirectory']; } // Create tests from Selenese/HTML files. if (isset($staticProperties['selenesePath']) && (is_dir($staticProperties['selenesePath']) || is_file($staticProperties['selenesePath']))) { if (is_dir($staticProperties['selenesePath'])) { $files = array_merge(self::getSeleneseFiles($staticProperties['selenesePath'], '.htm'), self::getSeleneseFiles($staticProperties['selenesePath'], '.html')); } else { $files[] = realpath($staticProperties['selenesePath']); } // Create tests from Selenese/HTML files for multiple browsers. if (!empty($staticProperties['browsers'])) { foreach ($staticProperties['browsers'] as $browser) { $browserSuite = PHPUnit_Extensions_SeleniumBrowserSuite::fromClassAndBrowser($className, $browser); foreach ($files as $file) { self::addGeneratedTestTo($browserSuite, new $className($file, array(), '', $browser), $classGroups); } $suite->addTest($browserSuite); } } else { // Create tests from Selenese/HTML files for single browser. foreach ($files as $file) { self::addGeneratedTestTo($suite, new $className($file), $classGroups); } } } // Create tests from test methods for multiple browsers. if (!empty($staticProperties['browsers'])) { foreach ($staticProperties['browsers'] as $browser) { $browserSuite = PHPUnit_Extensions_SeleniumBrowserSuite::fromClassAndBrowser($className, $browser); foreach ($class->getMethods() as $method) { $browserSuite->addTestMethod($class, $method); } $browserSuite->setupSpecificBrowser($browser); $suite->addTest($browserSuite); } } else { // Create tests from test methods for single browser. foreach ($class->getMethods() as $method) { $suite->addTestMethod($class, $method); } } return $suite; }
public static function enumerate() { if (PHP_VERSION_ID < 50300) { $class = callerName(); } else { $class = get_called_class(); } $ref = new ReflectionClass($class); $statics = $ref->getStaticProperties(); foreach ($statics as $name => $value) { $ref->setStaticPropertyValue($name, new $class($value)); } }
public static function reset() { static::$object = null; $clname = function_exists('get_called_class') ? get_called_class() : static::$class; $class = new ReflectionClass($clname); $arr = array_keys($class->getStaticProperties()); unset($arr[array_search('class', $arr)], $arr[array_search('stack', $arr)], $arr[array_search('object', $arr)], $arr[array_search('container', $arr)], $arr[array_search('variables', $arr)]); static::$variables = $arr; unset($arr[array_search('stack', $arr)]); foreach ($arr as $var) { static::${$var} = null; } static::$container = array(); }
protected static function build() { $class = get_called_class(); static::$_fields[$class] = array(); $reflector = new \ReflectionClass($class); foreach ($reflector->getStaticProperties() as $name => $f) { if ($f instanceof FormField) { $f->name = $name; $f->dbname = $name; $f->_form = $class; static::$_fields[$class][$name] = $f; } } }
protected static function setParameters($className) { $parameterClassName = __NAMESPACE__ . static::$nsChar . 'Config' . static::$nsChar . $className; if (!class_exists($parameterClassName, false)) { return; } $parameterClass = new \ReflectionClass($parameterClassName); $staticMembers = $parameterClass->getStaticProperties(); foreach ($staticMembers as $field => &$value) { if (property_exists($className, $field)) { $className::${$field} = $value; } } }
/** * @return void */ public function test_function() { // invoke logic $dataArray = ['foo1' => ['bar1' => ['baz1' => 1]], 'foo2' => ['bar2' => ['baz2' => 2]]]; $dependencies = [[DependencyHandler::DEPENDENT => ['foo1' => 'baz1'], DependencyHandler::ON => ['foo2' => 'baz2']]]; DependencyHandler::initDependencyHandler($dataArray, $dependencies); // prepare $reflection = new \ReflectionClass('\\DbMockLibrary\\DependencyHandler'); $staticProperties = $reflection->getStaticProperties(); $dependenciesProperty = $reflection->getProperty('dependencies'); $dependenciesProperty->setAccessible(true); // test $this->assertInstanceOf('\\DbMockLibrary\\DependencyHandler', $staticProperties['instance']); $this->assertEquals($dependencies, $dependenciesProperty->getValue(DependencyHandler::getInstance())); }
/** * Function to return value of a static property when class * name is dynamically defined (not hard coded). * This is because $myclass::$myvar works from PHP 5.3.0+ only * * @param string $class Class name * @param string $member Name of property * @return string Return value of static property. */ function getStaticMember($class, $member) { if (is_object($class)) { $class = get_class($class); } $classObj = new ReflectionClass($class); $result = null; foreach ($classObj->getStaticProperties() as $prop => $value) { if ($prop == $member) { $result = $value; break; } } return $result; }
private static function extendRequestFormats(Request $request, array $formats) { foreach ($formats as $format => $mimeTypes) { if (method_exists(get_class($request), 'getMimeTypes')) { $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($format)); } elseif (null !== $request->getMimeType($format)) { $class = new \ReflectionClass(get_class($request)); $properties = $class->getStaticProperties(); if (isset($properties['formats'][$format])) { $mimeTypes = array_merge($mimeTypes, $properties['formats'][$format]); } } $request->setFormat($format, array_unique($mimeTypes)); } }
private static function compile($exception_type, $object, $message, $parameters = array(), $prefix = null) { if (!is_object($object) && strpos($object, '\\') === false) { return new self(sprintf('Object supplied to %s is not a valid object or namespace.', __CLASS__)); } $rc = new \ReflectionClass($object); $props = array(); switch ($exception_type) { case self::E_CONSTANT: $props = $rc->getConstants(); $props = array_keys($props); break; case self::E_PRIVATE: $props = $rc->getProperties(\ReflectionProperty::IS_PRIVATE); break; case self::E_PUBLIC: $props = $rc->getProperties(\ReflectionProperty::IS_PUBLIC); break; case self::E_PROTECTED: $props = $rc->getProperties(\ReflectionProperty::IS_PROTECTED); break; case self::E_STATIC: $props = $rc->getStaticProperties(); break; } unset($rc); foreach ($props as &$prop) { // match by prefix if (!empty($prefix) && stripos($prop, $prefix) !== 0) { $prop = null; continue; } switch ($exception_type) { case self::E_CONSTANT: $prop = get_class($object) . '::' . $prop; break; case self::E_STATIC: $prop = $prop->class . '::' . $prop->name; break; default: $prop = $prop->class . '->$' . $prop->name; } } $props = array_filter($props); $parameters[] = implode(', ', $props); $message = vsprintf($message, $parameters); return new self($message); }
/** * @return void */ public function test_function() { // invoke logic $dataArray = [1]; DataContainer::initDataContainer($dataArray); // prepare $reflection = new \ReflectionClass('\\DbMockLibrary\\DataContainer'); $staticProperties = $reflection->getStaticProperties(); // test $this->assertInstanceOf('\\DbMockLibrary\\DataContainer', $staticProperties['instance']); $this->assertEquals($dataArray, $staticProperties['initialData']); // prepare $dataProperty = $reflection->getProperty('data'); $dataProperty->setAccessible(true); // test $this->assertEquals($dataArray, $dataProperty->getValue($staticProperties['instance'])); }
/** * @return void */ public function test_function() { // prepare $dataArray = ['testCollection' => [1 => ['foo' => 1, 'id' => 1]]]; // invoke logic Mongo::initMongo($dataArray, 'DbMockLibraryTest', []); // prepare $reflection = new \ReflectionClass('\\DbMockLibrary\\DbImplementations\\Mongo'); $staticProperties = $reflection->getStaticProperties(); // test $this->assertInstanceOf('\\DbMockLibrary\\DbImplementations\\Mongo', $staticProperties['instance']); $this->assertEquals($dataArray, $staticProperties['initialData']); // prepare $dataProperty = $reflection->getProperty('data'); $dataProperty->setAccessible(true); // test $this->assertEquals($dataArray, $dataProperty->getValue($staticProperties['instance'])); }