Exemple #1
0
 /**
  * @param \WP_User $user
  * @return UserEntity|null
  */
 protected function _convertToEntity(\WP_User $user)
 {
     $id = (int) $user->ID;
     if ($entity = $this->_getEntity($id)) {
         return $entity;
     }
     $caps = $user->get_role_caps();
     $isAdmin = (bool) Vars::get($caps['administrator'], false);
     $data = new Data(['id' => $id, 'login' => $user->get('user_login'), 'name' => $user->get('display_name'), 'email' => $user->get('user_email'), 'is_admin' => $isAdmin]);
     $this->_users[$id] = new UserEntity($data);
     return $this->_users[$id];
 }
Exemple #2
0
 public function testFunctionOverhead()
 {
     runBench(array('Clean' => function () {
         return pathinfo(__FILE__, PATHINFO_BASENAME);
     }, 'Wrapper' => function () {
         return FS::base(__FILE__);
     }), array('name' => 'Pathinfo overhead', 'count' => 10000));
     runBench(array('Vars::get' => function () {
         return Vars::get($GLOBALS['somevar']);
     }, 'isset' => function () {
         return isset($GLOBALS['somevar']);
     }), array('name' => 'Isset overhead', 'count' => 10000));
     $randArr = array_fill(0, 100, null);
     for ($i = 0; $i < 100; $i += 1) {
         $randArr[$i] = mt_rand(0, 9);
     }
     runBench(array('array_keys(+flip)' => function () use($randArr) {
         return Arr::unique($randArr, false);
     }, 'array_unique' => function () use($randArr) {
         return Arr::unique($randArr, true);
     }), array('name' => 'Isset overhead', 'count' => 1000));
 }
Exemple #3
0
 public function testLastKey()
 {
     $test = array('a' => array('a' => 'b', 'c' => 'd'));
     is('c', Arr::lastKey(Vars::get($test['a'])));
 }
Exemple #4
0
 /**
  * Format the results, rounding numbers, showing difference percentages
  * and removing a flat time based on the benchmark overhead
  *
  * @param  array $results array($name => array('time' => 1.0))
  * @return array array(array('Test' => $name, 'Time' => '1000 ms', 'Perc' => '100 %'))
  */
 public function formatResults(array $results)
 {
     uasort($results['list'], function ($testOne, $testTwo) {
         if ($testOne['time'] === $testTwo['time']) {
             return 0;
         } else {
             return $testOne['time'] < $testTwo['time'] ? -1 : 1;
         }
     });
     $minTime = INF;
     $minMemory = INF;
     foreach ($results['list'] as $name => $result) {
         // time
         $time = $result['time'];
         //$time -= $this->_overhead['time']; // Substract base_time
         $results['list'][$name]['time'] = $time;
         $minTime = min($minTime, $time);
         // memory
         $memory = $results['list'][$name]['memory'];
         $memory -= $this->_overhead['memory'];
         $results['list'][$name]['memory'] = $memory;
         $minMemory = min($minMemory, $memory);
     }
     $output = array();
     $isOne = count($results['list']) === 1;
     foreach ($results['list'] as $name => $result) {
         if ($isOne) {
             $output[] = array(self::COL_NAME => $name, self::COL_TIME => $this->_timeFormat($result['time'], 0), self::COL_TIME_ONE => $this->_timeFormat($result['time'] / $this->_count), self::COL_MEMORY => FS::format($result['memory'], 2));
         } else {
             $output[] = array(self::COL_NAME => $name, self::COL_TIME => $this->_timeFormat($result['time'], 0), self::COL_TIME_ONE => $this->_timeFormat($result['time'] / $this->_count), self::COL_TIME_REL => Vars::relativePercent($minTime, $result['time']), self::COL_MEMORY => FS::format($result['memory'], 2), self::COL_MEMORY_REL => Vars::relativePercent($minMemory, $result['memory']));
         }
     }
     return $output;
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.Superglobals)
  */
 public function dbPrefix()
 {
     return Vars::get($GLOBALS['table_prefix'], '');
 }
Exemple #6
0
 /**
  * Is current request - ajax
  * @return bool
  */
 public function isAjax()
 {
     $method = Vars::get($_SERVER['HTTP_X_REQUESTED_WITH'], '');
     $method = Filter::cmd($method);
     $orig = Filter::cmd('XMLHttpRequest');
     return $orig === $method;
 }
Exemple #7
0
 /**
  * @param $id
  * @return User|null
  */
 protected function _getEntity($id)
 {
     return Vars::get($this->_users[$id]);
 }
Exemple #8
0
 public function testRelativePercent()
 {
     isSame('200', Vars::relativePercent(50, 100));
     isSame('33', Vars::relativePercent(150, 50));
     isSame('300', Vars::relativePercent(50, 150));
     isSame('100', Vars::relativePercent(100, 100));
     isSame('10 000', Vars::relativePercent(1, 100));
     isSame('1', Vars::relativePercent(100, 1));
     isSame('100', Vars::relativePercent(0, 1));
 }
Exemple #9
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.Superglobals)
  */
 public function getVersion()
 {
     return Vars::get($GLOBALS['wp_version'], '1.0.0');
 }
Exemple #10
0
 /**
  * @param int $newTTL In seconds (1 to 365 days)
  */
 public function setCacheTTL($newTTL)
 {
     $newTTL = Filter::int($newTTL);
     $newTTL = Vars::limit($newTTL, 1, 86400 * 365);
     $this->_cache_ttl = $newTTL;
 }
Exemple #11
0
     }
     if (isset($_REQUEST['test-header-title'])) {
         $cms['header']->setTitle($_REQUEST['test-header-title']);
     }
     if (isset($_REQUEST['test-header-noindex'])) {
         $cms['header']->noindex();
     }
     if (isset($_REQUEST['test-header-description'])) {
         $cms['header']->setDesc($_REQUEST['test-header-description']);
     }
     if (isset($_REQUEST['test-header-keywords'])) {
         $cms['header']->setKeywords($_REQUEST['test-header-keywords']);
     }
     if (isset($_REQUEST['test-header-addmeta'])) {
         $_REQUEST['test-header-addmeta']['meta'] = str_replace(array('\\"', "\\'"), array('"', "'"), $_REQUEST['test-header-addmeta']['meta']);
         $cms['header']->addMeta($_REQUEST['test-header-addmeta']['meta'], Vars::get($_REQUEST['test-header-addmeta']['value']));
     }
 });
 /* Response *******************************************************************************************************/
 add_action('wp', function () use($cms) {
     if (isset($_REQUEST['test-response-set404'])) {
         $cms['response']->set404();
     }
     if (isset($_REQUEST['test-response-set500'])) {
         $cms['response']->set500($_REQUEST['test-response-set500']);
     }
     if (isset($_REQUEST['test-response-redirect'])) {
         $cms['response']->redirect($_REQUEST['test-response-redirect']);
     }
     if (isset($_REQUEST['test-response-json'])) {
         $cms['response']->json((array) $_REQUEST['test-response-json'], true);
Exemple #12
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.Superglobals)
  */
 public function __construct(Cms $cms)
 {
     parent::__construct($cms);
     $this->_wp_query = Vars::get($GLOBALS['wp_query']);
 }
Exemple #13
0
 public function onAfterDispatch()
 {
     if (!$this->_request('jbzoo-phpunit')) {
         return false;
     }
     /* Header *****************************************************************************************************/
     if ($test = $this->_request('test-header-title')) {
         $this->_cms['header']->setTitle($test);
     }
     if ($test = $this->_request('test-header-keywords')) {
         $this->_cms['header']->setKeywords($test);
     }
     if ($test = $this->_request('test-header-description')) {
         $this->_cms['header']->setDesc($test);
     }
     if ($test = $this->_request('test-header-noindex')) {
         $this->_cms['header']->noindex();
     }
     if ($test = $this->_request('test-header-jsfile')) {
         $this->_cms['header']->jsFile($test . '.js');
     }
     if ($test = $this->_request('test-header-cssfile')) {
         $this->_cms['header']->cssFile($test . '.css');
     }
     if ($test = $this->_request('test-header-jscode')) {
         $this->_cms['header']->jsCode($test);
     }
     if ($test = $this->_request('test-header-csscode')) {
         $this->_cms['header']->cssCode($test);
     }
     if ($test = $this->_request('test-header-addmeta')) {
         $this->_cms['header']->addMeta($_REQUEST['test-header-addmeta']['meta'], Vars::get($_REQUEST['test-header-addmeta']['value']));
     }
     /* Response ***************************************************************************************************/
     if ($test = $this->_request('test-response-set404')) {
         $this->_cms['response']->set404();
     }
     if ($test = $this->_request('test-response-set500')) {
         $this->_cms['response']->set500($test);
     }
     if ($test = $this->_request('test-response-redirect')) {
         $this->_cms['response']->redirect($test);
     }
     if ($test = $this->_request('test-response-json')) {
         $this->_cms['response']->json((array) $test, true);
     }
     if ($test = $this->_request('test-response-nocache')) {
         $this->_cms['response']->noCache();
     }
     if ($test = $this->_request('test-response-raw')) {
         $this->_cms['response']->raw();
     }
     if ($test = $this->_request('test-response-component')) {
         $this->_cms['response']->component();
     }
     /* Libs *******************************************************************************************************/
     if ($test = $this->_request('test-libs-jquery')) {
         $this->_cms['libs']->jQuery();
     }
     if ($test = $this->_request('test-libs-jqueryui')) {
         $this->_cms['libs']->jQueryUI();
     }
     if ($test = $this->_request('test-libs-autocomplete')) {
         $this->_cms['libs']->jQueryAutocomplete();
     }
     if ($test = $this->_request('test-libs-datepicker')) {
         $this->_cms['libs']->jQueryDatePicker();
     }
     if ($test = $this->_request('test-libs-colorpicker')) {
         $this->_cms['libs']->colorPicker();
     }
 }
Exemple #14
0
 public function testClean()
 {
     $dirSep = DIRECTORY_SEPARATOR;
     $empty = Vars::get($_SERVER['DOCUMENT_ROOT'], '');
     isSame($empty, FS::clean(''));
     isSame($empty, FS::clean(false));
     isSame($empty, FS::clean(null));
     isSame('path', FS::clean('path'));
     isSame("{$dirSep}path", FS::clean('/path'));
     isSame("{$dirSep}path", FS::clean(' /path '));
     isSame("{$dirSep}path{$dirSep}", FS::clean('/path/'));
     isSame("{$dirSep}path{$dirSep}", FS::clean('///path///'));
     isSame("{$dirSep}path{$dirSep}path", FS::clean('///path///path'));
     isSame("{$dirSep}path{$dirSep}path{$dirSep}path", FS::clean('///path///path/path'));
     isSame("{$dirSep}path{$dirSep}path{$dirSep}path{$dirSep}", FS::clean('\\path\\path\\path\\\\\\\\'));
     isSame('\\path\\path\\path\\', FS::clean('\\path\\path\\path\\\\\\\\', '\\'));
     isSame('\\path\\path\\path\\', FS::clean('\\path\\path\\path\\\\\\\\', '\\'));
     isSame('\\\\path\\path\\path\\', FS::clean('\\\\path\\path\\path\\\\\\\\', '\\'));
     isSame('../../path/', FS::clean('..///..///path/', '/'));
     isSame('./../path/', FS::clean('.///..///path/', '/'));
     isSame('/../../path/', FS::clean('/..///..///path/', '/'));
 }