Пример #1
0
 public function testAsserts()
 {
     $wrapper = new Assert();
     $this->assertEquals(
         file_get_contents(__DIR__ . '/_files/Asserts-origin.php'),
         $wrapper->process(file_get_contents(__DIR__ . '/_files/Asserts.php')));
 }
Пример #2
0
 function __construct($name, $address)
 {
     Assert::isScalar($name);
     Assert::isScalar($address);
     $this->name = $name;
     $this->address = $address;
 }
Пример #3
0
 /**
  * @param string $func name of the aggregate
  * @param string $expression property to be used for aggregation
  * @param string $alias optional label for the result of the aggregator
  */
 function __construct($func, $expression = null, $alias = null)
 {
     Assert::isScalar($func);
     $this->func = $func;
     $this->expression = $expression;
     $this->alias = $alias;
 }
 protected function castNumber($number)
 {
     if (!$this->scalar && Assert::checkInteger($number)) {
         return (int) $number;
     }
     return $number;
 }
Пример #5
0
 /**
  * @param string $name name of the column
  * @param ISqlType $type SQL type of the column
  */
 function __construct($name, DBTable $table, ISqlType $type)
 {
     Assert::isScalar($name);
     $this->name = $name;
     $this->table = $table;
     $this->type = $type;
 }
 public function __construct()
 {
     require ONPHP_META_AUTO_DIR . 'schema.php';
     Assert::isTrue(isset($schema));
     $this->schema = $schema;
     // in case of unclean shutdown of previous tests
     foreach (DBTestPool::me()->getPool() as $name => $db) {
         foreach ($this->schema->getTableNames() as $name) {
             try {
                 $db->queryRaw(OSQL::dropTable($name, true)->toDialectString($db->getDialect()));
             } catch (DatabaseException $e) {
                 // ok
             }
             if ($db->hasSequences()) {
                 foreach ($this->schema->getTableByName($name)->getColumns() as $columnName => $column) {
                     try {
                         if ($column->isAutoincrement()) {
                             $db->queryRaw("DROP SEQUENCE {$name}_id;");
                         }
                     } catch (DatabaseException $e) {
                         // ok
                     }
                 }
             }
         }
     }
 }
 /**
  * @param boolean whether to use include path or not
  * @param boolean whether to allow pre-scanning technique or not. PreScan is important for 
  * huge and distributed class locations
  */
 function __construct($useIncludePath = true, $allowPreScan = false)
 {
     Assert::isBoolean($allowPreScan);
     $this->allowPreScan = $allowPreScan;
     parent::__construct($useIncludePath);
     $this->setExtension(PHOEBIUS_TYPE_EXTENSION);
 }
 public function set($name, $value)
 {
     if (!isset($this->mapping[$name])) {
         throw new WrongArgumentException("knows nothing about property '{$name}'");
     }
     $primitive = $this->mapping[$name];
     $setter = 'set' . ucfirst($primitive->getName());
     if (!method_exists($this->object, $setter)) {
         throw new WrongArgumentException("cannot find mutator for '{$name}' in class " . get_class($this->object));
     }
     if (is_object($value)) {
         if ($primitive instanceof PrimitiveAnyType && $value instanceof PrototypedEntity) {
             $value = ObjectToDTOConverter::create($value->entityProto())->make($value);
         } else {
             $value = $this->dtoValue($value, $primitive);
         }
     } elseif (is_array($value) && is_object(current($value))) {
         $dtoValue = array();
         foreach ($value as $oneValue) {
             Assert::isTrue(is_object($oneValue), 'array must contain only objects');
             $dtoValue[] = $this->dtoValue($oneValue, $primitive);
         }
         $value = $dtoValue;
     }
     return $this->object->{$setter}($value);
 }
 public function import($scope)
 {
     if (!$this->className) {
         throw new WrongStateException("no class defined for PrimitiveIdentifierList '{$this->name}'");
     }
     if (!BasePrimitive::import($scope)) {
         return null;
     }
     if (!is_array($scope[$this->name])) {
         return false;
     }
     $list = array_unique($scope[$this->name]);
     $values = array();
     foreach ($list as $id) {
         if (!Assert::checkInteger($id)) {
             return false;
         }
         $values[] = $id;
     }
     $objectList = array();
     foreach ($values as $value) {
         $className = $this->className;
         $objectList[] = new $className($value);
     }
     if (count($objectList) == count($values)) {
         $this->value = $objectList;
         return true;
     }
     return false;
 }
Пример #10
0
 /**
  * @param string $field name of the column
  * @param string $table optional name of the table that owns the column
  */
 function __construct($field, $table = null)
 {
     Assert::isScalar($field);
     Assert::isScalarOrNull($table);
     $this->field = $field;
     $this->table = $table;
 }
Пример #11
0
 public static function setAllowedMethodsHeaders($allowed_methods_arr)
 {
     Assert::assert(is_array($allowed_methods_arr));
     self::setAccessControlAllowOriginHeader();
     header('Access-Control-Allow-Methods: ' . implode(', ', $allowed_methods_arr));
     header('Allow: ' . implode(', ', $allowed_methods_arr));
 }
Пример #12
0
 /**
  * Configures PHP environment.
  * @return void
  */
 public static function setup()
 {
     self::$useColors = getenv('NETTE_TESTER_COLORS') !== FALSE ? (bool) getenv('NETTE_TESTER_COLORS') : PHP_SAPI === 'cli' && (function_exists('posix_isatty') && posix_isatty(STDOUT) || getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== FALSE);
     class_exists('Tester\\Runner\\Job');
     class_exists('Tester\\Dumper');
     class_exists('Tester\\Assert');
     error_reporting(E_ALL | E_STRICT);
     ini_set('display_errors', TRUE);
     ini_set('html_errors', FALSE);
     ini_set('log_errors', FALSE);
     set_exception_handler(array(__CLASS__, 'handleException'));
     set_error_handler(function ($severity, $message, $file, $line) {
         if (in_array($severity, array(E_RECOVERABLE_ERROR, E_USER_ERROR)) || ($severity & error_reporting()) === $severity) {
             Environment::handleException(new \ErrorException($message, 0, $severity, $file, $line));
         }
         return FALSE;
     });
     register_shutdown_function(function () {
         Assert::$onFailure = array(__CLASS__, 'handleException');
         // note that Runner is unable to catch this errors in CLI & PHP 5.4.0 - 5.4.6 due PHP bug #62725
         $error = error_get_last();
         if (in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE)) && ($error['type'] & error_reporting()) !== $error['type']) {
             register_shutdown_function(function () use($error) {
                 echo "\nFatal error: {$error['message']} in {$error['file']} on line {$error['line']}\n";
             });
         }
     });
 }
Пример #13
0
/**
 * @param int $iterations
 * @return array
 */
function run_is_true_with_four_chains($iterations)
{
    $start = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        Assert::argument()->is_true(true)->is_true(true)->is_true(true)->is_true(true);
    }
    $end = microtime(true);
    $assert_time = $end - $start;
    $start = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        Assert::argument()->is_true(true)->is_true(true)->is_true(true)->is_true(true);
    }
    $end = microtime(true);
    $assert_time = $end - $start;
    $start = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        if (!(true === true)) {
            throw new InvalidArgumentException("unacceptable!");
        }
        if (!(true === true)) {
            throw new InvalidArgumentException("unacceptable!");
        }
        if (!(true === true)) {
            throw new InvalidArgumentException("unacceptable!");
        }
        if (!(true === true)) {
            throw new InvalidArgumentException("unacceptable!");
        }
    }
    $end = microtime(true);
    $base_time = $end - $start;
    return [$base_time, $assert_time, $assert_time];
}
Пример #14
0
 /**
  * undocumented function
  *
  * @return void
  * @access public
  */
 function admin_index()
 {
     Assert::true(User::allowed($this->name, $this->action), '403');
     $defaults = array('model' => null, 'user_id' => null, 'my_limit' => 20, 'custom_limit' => false, 'start_date_day' => '01', 'start_date_year' => date('Y'), 'start_date_month' => '01', 'end_date_day' => '31', 'end_date_year' => date('Y'), 'end_date_month' => '12');
     $params = am($defaults, $this->params['url'], $this->params['named']);
     unset($params['ext']);
     unset($params['url']);
     if (is_numeric($params['custom_limit'])) {
         if ($params['custom_limit'] > 75) {
             $params['custom_limit'] = 75;
         }
         if ($params['custom_limit'] == 0) {
             $params['custom_limit'] = 50;
         }
         $params['my_limit'] = $params['custom_limit'];
     }
     $conditions = array();
     if (!empty($params['model'])) {
         $conditions['Log.model'] = $params['model'];
     }
     if (!empty($params['user_id'])) {
         $conditions['Log.user_id'] = $params['user_id'];
     }
     $conditions = $this->Log->dateRange($conditions, $params, 'created');
     $this->Session->write('logs_filter_conditions', $conditions);
     $userOptions = ClassRegistry::init('User')->find('list', array('conditions' => array('User.office_id' => $this->Session->read('Office.id'))));
     $this->paginate['Log'] = array('conditions' => $conditions, 'contain' => array('User', 'Gift', 'Transaction'), 'limit' => $params['my_limit'], 'order' => array('Log.continuous_id' => 'desc'));
     $logs = $this->paginate($this->Log);
     $this->set(compact('logs', 'params', 'userOptions'));
 }
Пример #15
0
 function __construct($title = null, $block = false)
 {
     Assert::isScalarOrNull($title);
     Assert::isBoolean($block);
     $this->title = $title;
     $this->block = $block;
 }
Пример #16
0
 function test_extract_jpeg_images_with_redirection()
 {
     $html = '<html><body><img src="http://ocafe.appspot.com/?redirect=true"/></body></html>';
     $images = p2mixi_extract_jpeg_images($html, 1);
     //		Assert::equals( count ($images['images']), 1, '');
     Assert::equals(strlen($images['images'][0]), 5221, '');
 }
Пример #17
0
 protected function __construct(array $dbs = array())
 {
     Assert::isArray($dbs);
     foreach ($dbs as $connector => $credentials) {
         $this->pool[$connector] = DB::spawn($connector, $credentials['user'], $credentials['pass'], $credentials['host'], $credentials['base']);
     }
 }
Пример #18
0
 public function unite(Identifiable $object, Identifiable $old)
 {
     Assert::isNotNull($object->getId());
     Assert::isTypelessEqual($object->getId(), $old->getId(), 'cannot merge different objects');
     $query = OSQL::update($this->getTable());
     foreach ($this->getProtoClass()->getPropertyList() as $property) {
         $getter = $property->getGetter();
         if ($property->getClassName() === null) {
             $changed = $old->{$getter}() !== $object->{$getter}();
         } else {
             /**
              * way to skip pointless update and hack for recursive
              * comparsion.
              **/
             $changed = $old->{$getter}() !== $object->{$getter}() || $old->{$getter}() != $object->{$getter}();
         }
         if ($changed) {
             $property->fillQuery($query, $object);
         }
     }
     if (!$query->getFieldsCount()) {
         return $object;
     }
     $this->targetizeUpdateQuery($query, $object);
     return $this->doInject($query, $object);
 }
 private function makeSelectQuery(SqlSelectiveFieldSet $fields, $source, IDalExpression $condition = null, SqlOrderChain $orderBy = null, $limit = 0, $offset = 0)
 {
     Assert::isInteger($limit);
     Assert::isInteger($offset);
     $dialect = $this->getDB()->getDialect();
     $query = array();
     $query[] = 'SELECT ' . $fields->toDialectString($dialect);
     if (is_scalar($source)) {
         $source = $dialect->quoteIdentifier($source);
     } else {
         Assert::isTrue(is_array($source));
         $tables = array();
         foreach ($source as $table) {
             $tables[] = $dialect->quoteIdentifier($table);
         }
         $source = join(", ", $tables);
     }
     $query[] = 'FROM ' . $source;
     if ($condition) {
         $query[] = 'WHERE ' . $condition->toDialectString($dialect);
     }
     if ($orderBy) {
         $query[] = $orderBy->toDialectString($dialect);
     }
     if ($limit) {
         $query[] = 'LIMIT ' . (int) $limit;
     }
     if ($offset) {
         $query[] = 'OFFSET ' . (int) $offset;
     }
     $query = join("\r\n", $query);
     return $query;
 }
 /**
  * @return GmpBigInteger
  **/
 public static function make($number, $base = 10)
 {
     Assert::isTrue(is_numeric($number));
     $result = new self();
     $result->resource = gmp_init($number, $base);
     return $result;
 }
Пример #21
0
 public static function generateLinks(array $classes)
 {
     $links = [];
     foreach ($classes as $class) {
         Assert::isInstance($class, 'MetaClass');
         foreach ($class->getProperties() as $property) {
             if ($property->getType() instanceof ObjectType && $property->getType()->getClassName() && $property->getRelation()) {
                 switch ($property->getRelation()->getId()) {
                     case MetaRelation::ONE_TO_ONE:
                         $rel = ' -- ';
                         break;
                     case MetaRelation::ONE_TO_MANY:
                         $rel = ' *-- ';
                         break;
                     case MetaRelation::MANY_TO_MANY:
                         $rel = ' *--* ';
                         break;
                     default:
                         throw new WrongStateException();
                         break;
                 }
                 $links[] = $class->getName() . $rel . $property->getType()->getClassName() . "\n";
             }
         }
         $links = array_unique($links);
     }
     return implode("", $links) . "\n";
 }
Пример #22
0
 public static function isIndexExists($array, $key, $message = null)
 {
     Assert::isArray($array);
     if (!array_key_exists($key, $array)) {
         throw new WrongArgumentException($message . ', ' . self::dumpArgument($key));
     }
 }
 /**
  * @return DoctypeDeclaration
  **/
 public function setPublic($isPublic)
 {
     Assert::isBoolean($isPublic);
     $this->public = $isPublic;
     $this->inline = false;
     return $this;
 }
Пример #24
0
 function __construct($title, $name)
 {
     Assert::isScalar($title);
     Assert::isScalar($name);
     $this->title = $title;
     $this->name = $name;
 }
Пример #25
0
 /**
  * Configures PHP error handling.
  * @return void
  */
 public static function setupErrors()
 {
     error_reporting(E_ALL | E_STRICT);
     ini_set('display_errors', TRUE);
     ini_set('html_errors', FALSE);
     ini_set('log_errors', FALSE);
     set_exception_handler(array(__CLASS__, 'handleException'));
     set_error_handler(function ($severity, $message, $file, $line) {
         if (in_array($severity, array(E_RECOVERABLE_ERROR, E_USER_ERROR), TRUE) || ($severity & error_reporting()) === $severity) {
             Environment::handleException(new \ErrorException($message, 0, $severity, $file, $line));
         }
         return FALSE;
     });
     register_shutdown_function(function () {
         Assert::$onFailure = array(__CLASS__, 'handleException');
         // note that Runner is unable to catch this errors in CLI & PHP 5.4.0 - 5.4.6 due PHP bug #62725
         $error = error_get_last();
         register_shutdown_function(function () use($error) {
             if (in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE), TRUE)) {
                 if (($error['type'] & error_reporting()) !== $error['type']) {
                     // show fatal errors hidden by @shutup
                     echo "\nFatal error: {$error['message']} in {$error['file']} on line {$error['line']}\n";
                 }
             } elseif (Environment::$checkAssertions && !Assert::$counter) {
                 echo "\nError: This test forgets to execute an assertion.\n";
                 exit(Runner\Job::CODE_FAIL);
             }
         });
     });
 }
Пример #26
0
 /**
  * @param ISqlQuery $query
  * @param string $errormsg
  * @param integer $errorno
  */
 function __construct(ISqlQuery $query, $errormsg, $errorno)
 {
     Assert::isScalar($errormsg);
     Assert::isNumeric($errorno);
     parent::__construct($errormsg, $errorno);
     $this->query = $query;
 }
 function setDefaultValue($value)
 {
     Assert::isScalar($value, 'default value shall be scalar');
     Assert::isTrue(in_array($value, $this->getAvailableValues()), 'trying to set a default value that is out of options range');
     $this->defaultValue = $value;
     return $this;
 }
Пример #28
0
 public function render($tabulations)
 {
     Assert::type(__METHOD__, 'is_string($tabulations)', is_string($tabulations));
     echo $tabulations . '<!--' . "\n";
     echo $tabulations . '	' . $this->m_content . "\n";
     echo $tabulations . '-->' . "\n";
 }
 public function import($scope)
 {
     if (!$this->className) {
         throw new WrongStateException("no class defined for PrimitiveIdentifier '{$this->name}'");
     }
     $className = $this->className;
     if (isset($scope[$this->name]) && $scope[$this->name] instanceof $className) {
         $value = $scope[$this->name];
         $this->raw = $value->getId();
         $this->setValue($value);
         return $this->imported = true;
     }
     $result = parent::import($scope);
     if ($result === true) {
         try {
             $result = $this->actualImportValue($this->value);
             Assert::isInstance($result, $className);
             $this->value = $result;
             return true;
         } catch (WrongArgumentException $e) {
             // not imported
         } catch (ObjectNotFoundException $e) {
             // not imported
         }
         $this->value = null;
         return false;
     }
     return $result;
 }
 public function import($scope)
 {
     if (!$this->className) {
         throw new WrongStateException("no class defined for PrimitiveIdentifierList '{$this->name}'");
     }
     if (!BasePrimitive::import($scope)) {
         return null;
     }
     if (!is_array($scope[$this->name])) {
         return false;
     }
     $list = array_unique($scope[$this->name]);
     $values = array();
     foreach ($list as $id) {
         if ((string) $id == "" && $this->isIgnoreEmpty()) {
             continue;
         }
         if ($this->scalar && !Assert::checkScalar($id) || !$this->scalar && !Assert::checkInteger($id)) {
             if (!$this->isIgnoreWrong()) {
                 return false;
             } else {
                 continue;
             }
             //just skip it
         }
         $values[] = $id;
     }
     $objectList = $this->dao()->getListByIds($values);
     if ((count($objectList) == count($values) || $this->isIgnoreWrong()) && !($this->min && count($values) < $this->min) && !($this->max && count($values) > $this->max)) {
         $this->value = $objectList;
         return true;
     }
     return false;
 }