示例#1
0
 /**
  * Test validator service
  */
 public function testValidator()
 {
     $config = array('server' => array('url' => '/', 'hostname' => 'localhost'));
     $app = new \Duality\App(dirname(__FILE__), $config);
     $server = $app->call('server');
     $request = new Request(new Url('http://localhost/dummy'));
     $request->setParams(array('key' => 'value'));
     $request->setMethod('GET');
     $server->setRequest($request);
     $validator = $app->call('validator');
     $item = new RuleItem('key', $request->getParam('key'), 'required|number|alpha|email|equals|password|length:2:3', 'The key is valid', 'The key is invalid');
     $item->setPassMessage('The key is valid');
     $item->setFailMessage('The key is invalid');
     $validator->addRuleItem($item);
     $validator->validate();
     $expected = false;
     $result = $validator->ok();
     $this->assertEquals($expected, $result);
     $expected = array('key' => 'The key is invalid');
     $result = $validator->getMessages();
     $this->assertEquals($expected, $result);
     $expected = array('key' => 'The key is invalid');
     $result = $validator->getErrorMessages();
     $this->assertEquals($expected, $result);
     $expected = 'The key is invalid';
     $result = $validator->getMessage('key');
     $this->assertEquals($expected, $result);
     $validator->terminate();
 }
示例#2
0
 /**
  * Test security service
  */
 public function testSecurity()
 {
     $config = array('server' => array('url' => '/', 'hostname' => 'localhost'), 'security' => array('algo' => 'sha1', 'salt' => 'dummy'));
     $app = new \Duality\App(dirname(__FILE__), $config);
     $request = new Request(new Url('http://localhost/items'));
     $request->setParams(array('key' => 'value', 'array' => array('value')));
     $request->setMethod('POST');
     $app->call('server')->setRequest($request);
     $security = $app->call('security');
     $expected = 'f64133af6818761d95c8230953e5c9ddee1d0cf3';
     $result = $security->encrypt('dummy');
     $this->assertEquals($expected, $result);
     $expected = 'f64133af6818761d95c8230953e5c9ddee1d0cf3';
     $result = $security->decrypt($result);
     $this->assertEquals($expected, $result);
     $security->terminate();
 }
示例#3
0
    /**
     * Test server service
     */
    public function testHome()
    {
        $config = array('server' => array('url' => '/', 'hostname' => 'localhost'));
        $app = new \Duality\App(dirname(__FILE__), $config);
        $server = $app->call('server');
        $server->setHostname('localhost');
        $url = new Url('http://localhost/dummy');
        $url->setHost('localhost');
        $request = new Request($url);
        $request->setParams(array('key' => 'value'));
        $request->setMethod('GET');
        $server->setRequest($request);
        $expected = <<<EOF
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Duality default controller - Replace me!</title>
    </head>
    <body><h1>Duality default controller - Replace me!</h1></body>
</html>
EOF;
        $server->setHome('\\Duality\\Service\\Controller\\Base@doIndex');
        ob_start();
        $server->listen();
        $result = ob_get_clean();
        $this->assertEquals($expected, $result);
        $result = $server->getResponse();
        $this->assertInstanceOf('\\Duality\\Structure\\Http\\Response', $result);
        $expected = 'dummy';
        $server->setHostname($expected);
        $result = $server->getHostname();
        $this->assertEquals($expected, $result);
        $expected = 'http://dummy/';
        $server->setBaseUrl(new Url('http://dummy'));
        $result = $server->getBaseUrl();
        $this->assertEquals($expected, (string) $result);
        $expected = 'http://dummy/dummy';
        $server->setBaseUrl(new Url('http://dummy'));
        $result = $server->createUrl('/dummy');
        $this->assertEquals($expected, (string) $result);
        $result = $server->createRedirect();
        $this->assertInstanceOf('\\Duality\\Structure\\Http\\Response', $result);
        $server->terminate();
    }
示例#4
0
 /**
  * Test HTTP
  */
 public function testHTTP()
 {
     $request = new \Duality\Structure\Http\Request(new \Duality\Structure\Url('http://localhost/dummy'));
     $request->setParams(array('key' => 'value'));
     $request->setMethod('GET');
     $request->getHeaders();
     $request->getHeaderItem('Content-Type');
     $response = new \Duality\Structure\Http\Response();
     $response->setUrl(new \Duality\Structure\Url('http://localhost/dummy'));
     $response->getUrl();
     $response->setMethod('GET');
     $response->getMethod();
     $response->setStatus(200);
     $response->getStatus();
     $response->setHeaders(array('Content-Type' => 'text/html'));
     $response->getHeaders();
     $response->addHeader('Content-Type', 'text/html');
     $cookie = array();
     $cookie['name'] = 'duality';
     $cookie['value'] = 'dummy';
     $cookie['expire'] = 0;
     $cookie['path'] = '/';
     $cookie['domain'] = 'domain.com';
     $cookie['secure'] = false;
     $response->setCookies(array($cookie));
     $response->getCookies();
     $response->setContent('dummy');
     $response->getContent();
     $response->setTimestamp(time());
     $response->getTimestamp();
     $response->setAjax(true);
     $response->isAjax();
 }
示例#5
0
 /**
  * Parses HTTP properties from PHP global environment
  * 
  * @param array $server The global $_SERVER variable
  * @param array $params The global $_REQUEST/$_GET/$_POST variable
  * 
  * @return Request The resulting request instance
  */
 public function getRequestFromGlobals($server, $params)
 {
     if (empty($server['REQUEST_METHOD'])) {
         return false;
     }
     // Filter input
     array_filter($server, function (&$var) {
         $var = filter_var($var, FILTER_UNSAFE_RAW);
     });
     array_filter($params, function (&$var) {
         $var = filter_var($var, FILTER_UNSAFE_RAW);
     });
     // Detect base URL and URI
     $server['SERVER_NAME'] = empty($server['SERVER_NAME']) ? $this->hostname : $server['SERVER_NAME'];
     $server['SCRIPT_NAME'] = empty($server['SCRIPT_NAME']) ? '/index.php' : $server['SCRIPT_NAME'];
     $server['REQUEST_URI'] = empty($server['REQUEST_URI']) ? '/' : $server['REQUEST_URI'];
     $baseUrl = (empty($server['HTTPS']) ? 'http' : 'https') . "://" . $server['SERVER_NAME'] . dirname($server['SCRIPT_NAME']);
     $uri = $server['REQUEST_URI'];
     $uri = str_replace(dirname($server['SCRIPT_NAME']), '', $uri);
     $uri = str_replace(basename($server['SCRIPT_NAME']), '', $uri);
     $uri = '/' . trim($uri, '/');
     // Set base URL and URI strings
     $this->setBaseUrl(new Url($baseUrl));
     $request = new Request(new Url($baseUrl . $uri));
     $request->setMethod($server['REQUEST_METHOD']);
     $request->setContent(file_get_contents('php://input'));
     $request->setTimestamp(empty($server['REQUEST_TIME']) ? time() : $server['REQUEST_TIME']);
     $headers = array('Http-Accept' => empty($server['HTTP_ACCEPT']) ? 'text/html' : $server['HTTP_ACCEPT'], 'Http-Accept-Language' => !empty($server['HTTP_ACCEPT_LANGUAGE']) ? $server['HTTP_ACCEPT_LANGUAGE'] : 'en-US', 'Http-Accept-Charset' => !empty($server['HTTP_ACCEPT_CHARSET']) ? $server['HTTP_ACCEPT_CHARSET'] : !empty($server['HTTP_ACCEPT_ENCODING']) ? $server['HTTP_ACCEPT_ENCODING'] : 'utf-8', 'Http-Host' => empty($server['REMOTE_HOST']) ? empty($server['REMOTE_ADDR']) ? '' : $server['REMOTE_ADDR'] : $server['REMOTE_HOST'], 'Referer' => empty($server['REFERER']) ? '' : $server['REFERER']);
     $request->setHeaders($headers);
     $request->setParams($params);
     if (!empty($server['HTTP_X_REQUESTED_WITH']) && strtolower($server['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
         $request->setAjax(true);
     }
     return $request;
 }