Esempio n. 1
0
 /**
  * @param HttpRequestA $oRequest
  * @return \vsc\domain\models\ModelA
  * @throws \vsc\presentation\responses\ExceptionResponseError
  */
 public function handleRequest(HttpRequestA $oRequest)
 {
     if (!$oRequest->isGet() && !RawHttpRequest::isValid($oRequest)) {
         $oRequest = new RawHttpRequest();
         vsc::getEnv()->setHttpRequest($oRequest);
     }
     switch ($oRequest->getHttpMethod()) {
         case HttpRequestTypes::GET:
             $oModel = $this->handleGet($oRequest);
             break;
         case HttpRequestTypes::HEAD:
             $oModel = $this->handleHead($oRequest);
             break;
         case HttpRequestTypes::POST:
             $oModel = $this->handlePost($oRequest);
             break;
         case HttpRequestTypes::PUT:
             $oModel = $this->handlePut($oRequest);
             break;
         case HttpRequestTypes::DELETE:
             $oModel = $this->handleDelete($oRequest);
             break;
         default:
             throw new ExceptionResponseError('Method [' . $oRequest->getHttpMethod() . '] is unavailable.', HttpResponseType::METHOD_NOT_ALLOWED);
     }
     return $oModel;
 }
Esempio n. 2
0
 /**
  * @returns HttpRequestA
  */
 public function getRequest()
 {
     if (!HttpRequestA::isValid($this->oRequest)) {
         $this->oRequest = vsc::getEnv()->getHttpRequest();
     }
     return $this->oRequest;
 }
Esempio n. 3
0
 public function testBasicGetRequest()
 {
     $o = new RwDispatcher();
     $oRequest = $o->getRequest();
     $oBlaReq = vsc::getEnv()->getHttpRequest();
     $this->assertSame($oRequest, $oBlaReq);
 }
Esempio n. 4
0
 public function handleRequest(HttpRequestA $oHttpRequest)
 {
     $o404 = vsc::getEnv()->getHttpResponse();
     $o404->setStatus(404);
     $this->getMap()->setResponse($o404);
     return parent::handleRequest($oHttpRequest);
 }
Esempio n. 5
0
 public function testBasicGetResponse()
 {
     $o = new RESTController();
     $oProcessor = new RESTProcessorA_underTest_getResponse();
     $r = $o->getResponse(vsc::getEnv()->getHttpRequest(), $oProcessor);
     $this->assertInstanceOf(HttpResponse::class, $r);
 }
Esempio n. 6
0
function getErrorHeaderOutput($e = null)
{
    $sRet = '';
    if (!vsc::isCli()) {
        if (!headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error');
        }
        $sRet = '<?xml version="1.0" encoding="utf-8"?>';
        $sRet .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
        $sRet .= '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
        $sRet .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
        $sRet .= '<head>';
        $sRet .= '<style>ul {padding:0; font-size:0.8em} li {padding:0.2em;display:inline} address {position:fixed;bottom:0;}</style>';
        $sRet .= '<title>Internal Error' . (!$e instanceof \Exception ? '' : ': ' . substr($e->getMessage(), 0, 20) . '...') . '</title>';
        $sRet .= '</head>';
        $sRet .= '<body>';
        $sRet .= '<strong>Internal Error' . (!$e instanceof \Exception ? '' : ': ' . $e->getMessage()) . '</strong>';
        $sRet .= '<address>&copy; VSC</address>';
        $sRet .= '<ul>';
        $sRet .= '<li><a href="#" onclick="p = document.getElementById(\'trace\'); if (p.style.display==\'block\') p.style.display=\'none\';else p.style.display=\'block\'; return false">toggle trace</a></li>';
        if (defined('ROOT_MAIL')) {
            $sRet .= '<li><a href="javascript: p = document.getElementById(\'trace\'); document.location.href =\'mailto:' . ROOT_MAIL . '?subject=Problems&amp;body=\' + p.innerHTML; return false">mail me</a></li>';
        }
        $sRet .= '</ul>';
        if ($e instanceof \Exception) {
            $sRet .= '<p style="font-size:.8em">Triggered in <strong>' . $e->getFile() . '</strong> at line ' . $e->getLine() . '</p>';
        }
        $sRet .= '<pre style="position:fixed;bottom:2em;display:' . (vsc::getEnv()->isDevelopment() ? 'block' : 'none') . ';font-size:.8em" id="trace">';
    }
    return $sRet;
}
Esempio n. 7
0
 public function testBasicGetResponse()
 {
     $o = new ContentTypeController();
     $p = new EmptyProcessor();
     $r = $o->getResponse(vsc::getEnv()->getHttpRequest(), $p);
     $this->assertNull($r);
 }
Esempio n. 8
0
 public function testGetResponseWithCacheableModelThatIsNewer()
 {
     $now = new \DateTime('now');
     $yesterday = clone $now;
     $yesterday->sub(new \DateInterval('P1D'));
     $r = $this->getMockBuilder(RawHttpRequest::class)->disableOriginalConstructor()->getMock();
     vsc::getEnv()->setHttpRequest($r);
     $r->method('getIfModifiedSince')->willReturn($yesterday->format('r'));
     /** @var ProcessorA $p */
     $p = $this->getMockBuilder(ProcessorA::class)->disableOriginalConstructor()->getMock();
     $m = $this->getMockBuilder(CacheableModelA::class)->setMethods(['getLastModified'])->disableOriginalConstructor()->getMock();
     $m->method('getLastModified')->willReturn($now->format('r'));
     $v = $this->getMockBuilder(CacheableViewA::class)->setMethods(['getModel', 'getLastModified', 'getMTime', 'display', 'append', 'assign'])->disableOriginalConstructor()->getMock();
     $v->expects($this->any())->method('getModel')->willReturn($m);
     $Controller = new CacheableController_underTest_getResponse();
     $Map = new ControllerMap('.', CacheableController_underTest_getResponse::class);
     $Controller->setView($v);
     $Controller->setMap($Map);
     $inTwoWeeks = $now->add(new \DateInterval('P2W'));
     $response = $Controller->getResponse($r, $p);
     $expires = new \DateTime($response->getExpires());
     $this->assertInstanceOf(HttpResponseA::class, $response);
     $this->assertEquals($inTwoWeeks->getTimestamp(), $expires->getTimestamp(), '', 2);
     $this->assertEquals(HttpResponseType::OK, $response->getStatus());
 }
Esempio n. 9
0
 public function testBasicHandleRequest()
 {
     date_default_timezone_set('UTC');
     $o = new StaticFileProcessor();
     $oModel = $o->handleRequest(vsc::getEnv()->getHttpRequest());
     $this->assertInstanceOf(StaticFileModel::class, $oModel);
 }
Esempio n. 10
0
 public function __get($sVarName)
 {
     $sVal = vsc::getEnv()->getHttpRequest()->getVar($sVarName);
     if (is_null($sVal) && vsc::getEnv()->isDevelopment()) {
         $sVal = '<span style="font-size:0.8em">[' . $sVarName . ' does not exist in the request]</span>';
     }
     return $sVal;
 }
Esempio n. 11
0
 public function testGetPlainResponse()
 {
     $Controller = new FrontControllerA_underTest_getResponse();
     $Map = new ControllerMap('.', FrontControllerA_underTest_getResponse::class);
     $Controller->setMap($Map);
     $Response = $Controller->getResponse(vsc::getEnv()->getHttpRequest());
     $this->assertInstanceOf(HttpResponseA::class, $Response);
 }
Esempio n. 12
0
 public function testBasicHandleRequest()
 {
     $o = new NotFoundProcessor();
     $oModel = $o->handleRequest(vsc::getEnv()->getHttpRequest());
     $this->assertInstanceOf(ErrorModel::class, $oModel);
     $this->assertInstanceOf(ExceptionResponseError::class, $oModel->getException());
     $this->assertEquals(HttpResponseType::NOT_FOUND, $oModel->getException()->getErrorCode());
 }
Esempio n. 13
0
 public function testBasicSetPath()
 {
     $o = new StaticFileProcessor();
     $o->setFilePath(__FILE__);
     $oModel = $o->handleRequest(vsc::getEnv()->getHttpRequest());
     $this->assertInstanceOf(StaticFileModel::class, $oModel);
     $this->assertEquals(__FILE__, $oModel->getFilePath());
 }
Esempio n. 14
0
 public function testBasicHandleRequest()
 {
     $o = new EmptyProcessor();
     $oModel = $o->handleRequest(vsc::getEnv()->getHttpRequest());
     $this->assertInstanceOf(EmptyModel::class, $oModel);
     $this->assertEquals('[ null ]', $oModel->getPageTitle());
     $this->assertEquals('[ NULL ]', $oModel->getPageContent());
 }
Esempio n. 15
0
 public function testBasicHandleRequest()
 {
     $e = new ExceptionResponseError('test', HttpResponseType::NO_CONTENT);
     $o = new ErrorProcessor($e);
     $oModel = $o->handleRequest(vsc::getEnv()->getHttpRequest());
     $this->assertInstanceOf(ErrorModel::class, $oModel);
     $this->assertInstanceOf(ExceptionResponseError::class, $oModel->getException());
     $this->assertEquals(HttpResponseType::NO_CONTENT, $oModel->getException()->getErrorCode());
 }
Esempio n. 16
0
 public function testUseless()
 {
     $_SERVER['CONTENT_TYPE'] = 'application/json';
     $req = new RawHttpRequest_underTest_init();
     vsc::getEnv()->setHttpRequest($req);
     $o = new RPCProcessorA_underTest_init();
     $this->assertNull($o->init());
     $this->assertEquals(1, $o->getResponse()->id);
 }
Esempio n. 17
0
 public function testGetProcessorController()
 {
     $sFixturePath = VSC_FIXTURE_PATH . 'config' . DIRECTORY_SEPARATOR;
     $o = new RwDispatcher();
     $o->loadSiteMap($sFixturePath . 'map.php');
     $oRequest = new PopulatedRequest();
     vsc::getEnv()->setHttpRequest($oRequest);
     $this->assertInstanceOf(ProcessorFixture::class, $o->getProcessController());
 }
Esempio n. 18
0
 public function testEmptyJsonRequestInitialization()
 {
     vsc::getEnv()->setHttpRequest(new RawHttpRequest_underTest___construct());
     $_SERVER['CONTENT_TYPE'] = 'application/json';
     $o = new JsonRPCRequest();
     $this->assertEmpty($o->id);
     $this->assertEmpty($o->method);
     $this->assertEmpty($o->params);
 }
Esempio n. 19
0
 public function testSetDispatcher()
 {
     /* @var RwDispatcher $oDispatcher */
     $oDispatcher = new RwDispatcher();
     vsc::getEnv()->setDispatcher($oDispatcher);
     $oSetDispatcher = vsc::getEnv()->getDispatcher();
     $this->assertInstanceOf(DispatcherA::class, $oSetDispatcher);
     $this->assertSame($oDispatcher, $oSetDispatcher);
 }
Esempio n. 20
0
 public function testTemplatePath()
 {
     $sFixturePath = VSC_FIXTURE_PATH . 'config' . DIRECTORY_SEPARATOR;
     $o = new RwDispatcher();
     $o->loadSiteMap($sFixturePath . 'map.php');
     $oRequest = new PopulatedRequest();
     vsc::getEnv()->setHttpRequest($oRequest);
     $genericTemplatePath = VSC_FIXTURE_PATH . 'templates' . DIRECTORY_SEPARATOR;
     $this->assertEquals($genericTemplatePath, $o->getTemplatePath());
 }
Esempio n. 21
0
 /**
  * @param \vsc\presentation\requests\HttpRequestA $oRequest
  */
 public function __construct($oRequest = null)
 {
     /* @var RawHttpRequest $oRequest */
     if (!RawHttpRequest::isValid($oRequest)) {
         $oRequest = new RawHttpRequest();
         vsc::getEnv()->setHttpRequest($oRequest);
     }
     $this->id = $oRequest->getVar('id');
     $this->method = $oRequest->getVar('method');
     $this->params = $oRequest->getVar('params');
 }
Esempio n. 22
0
 public function testParentModuleMap()
 {
     vsc::getEnv()->getHttpRequest()->setUri('/');
     $o = new RwDispatcher();
     $this->assertTrue($o->loadSiteMap(VSC_RES_PATH . 'config/map.php'));
     $moduleMap = $o->getCurrentModuleMap();
     $this->assertInstanceOf(MappingA::class, $moduleMap);
     $this->assertInstanceOf(ModuleMap::class, $moduleMap);
     $this->assertEquals(VSC_RES_PATH . 'templates' . DIRECTORY_SEPARATOR, $moduleMap->getTemplatePath());
     $this->assertEquals(VSC_RES_PATH . 'templates' . DIRECTORY_SEPARATOR, $moduleMap->getMainTemplatePath());
     $this->assertEquals('main.php', $moduleMap->getMainTemplate());
 }
Esempio n. 23
0
 public function testBasicDelegateRequest()
 {
     $o = new ProcessorFixture();
     $sValue = 'test';
     $oHttpRequest = new PopulatedRequest();
     $oNewProcessor = new ProcessorFixture();
     $oNewProcessor->return = $sValue;
     $sMapPath = VSC_FIXTURE_PATH . 'config' . DIRECTORY_SEPARATOR . 'map.php';
     vsc::getEnv()->setDispatcher(new RwDispatcher());
     vsc::getEnv()->getDispatcher()->loadSiteMap($sMapPath);
     $this->assertEquals($sValue, $o->delegateRequest($oHttpRequest, $oNewProcessor));
 }
Esempio n. 24
0
 public function testBasicGetFrontController()
 {
     $sFixturePath = VSC_FIXTURE_PATH . 'config' . DIRECTORY_SEPARATOR;
     $o = new RwDispatcher();
     $this->assertTrue(is_file($sFixturePath . 'map.php'));
     $this->assertTrue($o->loadSiteMap($sFixturePath . 'map.php'));
     $oRequest = new PopulatedRequest();
     vsc::getEnv()->setHttpRequest($oRequest);
     try {
         $oFront = $o->getFrontController();
         $this->assertInstanceOf(FrontControllerA::class, $oFront);
     } catch (\Exception $e) {
         $this->assertInstanceOf(ExceptionResponseError::class, $e);
     }
 }
Esempio n. 25
0
 /**
  * @param string $sSessionName
  * @throws ExceptionRequest
  */
 public static function startSession($sSessionName = null)
 {
     if (!static::hasSession()) {
         if ((double) PHP_VERSION >= 5.4 && session_status() == PHP_SESSION_DISABLED) {
             throw new ExceptionRequest('Sessions are not available');
         }
         if ((double) PHP_VERSION >= 5.4 && session_status() == PHP_SESSION_NONE) {
             $oRequest = vsc::getEnv()->getHttpRequest();
             if (!vsc::isCli()) {
                 session_set_cookie_params(0, '/', $oRequest->getUriObject()->getHost(), HttpRequestA::isSecure(), true);
             }
             if (@session_start()) {
                 $_SESSION = array();
                 if (!is_null($sSessionName)) {
                     session_id($sSessionName);
                 }
             }
         }
     }
 }
Esempio n. 26
0
 /**
  * @fixme
  */
 public function testGetDefaultViewWithAcceptHeader()
 {
     $o = new RESTController();
     $oRequest = new PopulatedRESTRequest();
     $oRequest->setHttpAccept('application/json');
     vsc::getEnv()->setHttpRequest($oRequest);
     $oDefaultView = $o->getView();
     $this->assertInstanceOf(JsonView::class, $oDefaultView);
     $this->assertInstanceOf(ViewA::class, $oDefaultView);
     $oRequest->setHttpAccept('application/xml');
     $oDefaultView = $o->getView();
     //		$this->assertInstanceOf(XmlView::class, $oDefaultView);
     $this->assertInstanceOf(ViewA::class, $oDefaultView);
     $oRequest->setHttpAccept('application/pdf');
     $oDefaultView = $o->getDefaultView();
     //		$this->assertInstanceOf(StaticFileView::class, $oDefaultView);
     $this->assertInstanceOf(ViewA::class, $oDefaultView);
     //
     $oRequest->setHttpAccept('image/*');
     $oDefaultView = $o->getDefaultView();
     //		$this->assertInstanceOf(StaticFileView::class, $oDefaultView);
     $this->assertInstanceOf(ViewA::class, $oDefaultView);
 }
Esempio n. 27
0
 public function getOutput()
 {
     $sResponseBody = null;
     $oView = null;
     if (ViewA::isValid($this->getView())) {
         $this->setContentType($this->getView()->getContentType());
     } else {
         $this->setContentType('*/*');
     }
     $oRequest = vsc::getEnv()->getHttpRequest();
     try {
         if (!$oRequest->isHead() && !$this->isRedirect()) {
             $oView = $this->getView();
             $sResponseBody = $oView->getOutput();
         } else {
             $this->setContentLength(0);
         }
     } catch (ExceptionResponseError $r) {
         $this->setStatus($r->getCode());
         if (ViewA::isValid($oView)) {
             $oView->setModel(new ErrorModel($r));
             $sResponseBody = $oView->getOutput();
         }
     }
     return $sResponseBody;
 }
Esempio n. 28
0
 public function testGetDispatcher()
 {
     /* @var RwDispatcher $oDispatcher */
     $oDispatcher = vsc::getEnv()->getDispatcher();
     $this->assertInstanceOf(DispatcherA::class, $oDispatcher);
 }
Esempio n. 29
0
<?php

/* @var \vsc\domain\models\ErrorModel $model */
use vsc\infrastructure\vsc;
?>
<style>ul {padding:0; font-size:0.8em} li {padding:0.2em;display:inline} address {position:fixed;bottom:0;}</style>

<?php 
$e = $model->getException();
?>
<strong>Internal Error <?php 
echo !$e ? '' : ': ' . $e->getMessage();
?>
</strong>
<?php 
if (vsc::getEnv()->isDevelopment()) {
    ?>
<ul>
	<li>
		<a href="#" onclick="p = document.getElementById('trace'); if (p.style.display=='block') p.style.display='none';else p.style.display='block'; return false">toggle trace</a>
	</li>
<?php 
    if (defined('ROOT_MAIL')) {
        ?>
	<li>
		<a href="javascript: p = document.getElementById('trace'); document.location.href ='mailto:<?php 
        echo ROOT_MAIL;
        ?>
?subject=Problems&amp;body=' + p.innerHTML; return false">mail me</a>
	</li>
<?php 
Esempio n. 30
0
 public function testBasicSetInstance()
 {
     vsc::setInstance(new vsc_underTest_setInstance());
     $this->assertInstanceOf(vsc_underTest_setInstance::class, vsc::getEnv());
     $this->assertInstanceOf(vsc::class, vsc::getEnv());
 }