public function modulesClosure(IntegrationTester $I) { $I->wantTo('handle request and get content by using single modules strategy (closure)'); Di::reset(); $_GET['_url'] = '/login'; $di = new FactoryDefault(); $di->set('router', function () { $router = new Router(false); $router->add('/index', ['controller' => 'index', 'module' => 'frontend', 'namespace' => 'Phalcon\\Test\\Modules\\Frontend\\Controllers']); $router->add('/login', ['controller' => 'login', 'module' => 'backend', 'namespace' => 'Phalcon\\Test\\Modules\\Backend\\Controllers']); return $router; }); $application = new Application(); $view = new View(); $application->registerModules(['frontend' => function ($di) use($view) { /** @var \Phalcon\DiInterface $di */ $di->set('view', function () use($view) { $view->setViewsDir(PATH_DATA . 'modules/frontend/views/'); return $view; }); }, 'backend' => function ($di) use($view) { /** @var \Phalcon\DiInterface $di */ $di->set('view', function () use($view) { $view->setViewsDir(PATH_DATA . 'modules/backend/views/'); return $view; }); }]); $application->setDI($di); $I->assertEquals('<html>here</html>' . PHP_EOL, $application->handle()->getContent()); }
/** * executed after each test */ protected function _after() { if ($this->previousDependencyInjector instanceof DiInterface) { Di::setDefault($this->previousDependencyInjector); } else { Di::reset(); } }
public static function setUpBeforeClass() { $opts = ["schema" => "sql/myapp/schema.sql", "db" => ["host" => "127.0.0.1", "username" => "root", "password" => "", "dbname" => "myapp_testing"]]; DbHelper::populateDb($opts); $di = DbHelper::createDi($opts["db"]); Di::reset(); Di::setDefault($di); }
/** * Makes a request. * * @param \Symfony\Component\BrowserKit\Request $request * * @return \Symfony\Component\BrowserKit\Response * @throws \RuntimeException */ public function doRequest($request) { $application = $this->getApplication(); $di = $application->getDI(); Di::reset(); Di::setDefault($di); $_SERVER = []; foreach ($request->getServer() as $key => $value) { $_SERVER[strtoupper(str_replace('-', '_', $key))] = $value; } if (!$application instanceof Application && !$application instanceof MicroApplication) { throw new RuntimeException('Unsupported application class.'); } $_COOKIE = $request->getCookies(); $_FILES = $this->remapFiles($request->getFiles()); $_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod()); $_REQUEST = $this->remapRequestParameters($request->getParameters()); if ($_SERVER['REQUEST_METHOD'] == 'GET') { $_GET = $_REQUEST; } else { $_POST = $_REQUEST; } $uri = str_replace('http://localhost', '', $request->getUri()); $_SERVER['REQUEST_URI'] = $uri; $_GET['_url'] = strtok($uri, '?'); $_SERVER['QUERY_STRING'] = http_build_query($_GET); $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $di['request'] = Stub::construct($di->get('request'), [], ['getRawBody' => $request->getContent()]); $response = $application->handle(); $headers = $response->getHeaders(); $status = (int) $headers->get('Status'); $headersProperty = new ReflectionProperty($headers, '_headers'); $headersProperty->setAccessible(true); $headers = $headersProperty->getValue($headers); if (!is_array($headers)) { $headers = []; } $cookiesProperty = new ReflectionProperty($di['cookies'], '_cookies'); $cookiesProperty->setAccessible(true); $cookies = $cookiesProperty->getValue($di['cookies']); if (is_array($cookies)) { $restoredProperty = new ReflectionProperty('\\Phalcon\\Http\\Cookie', '_restored'); $restoredProperty->setAccessible(true); $valueProperty = new ReflectionProperty('\\Phalcon\\Http\\Cookie', '_value'); $valueProperty->setAccessible(true); foreach ($cookies as $name => $cookie) { if (!$restoredProperty->getValue($cookie)) { $clientCookie = new Cookie($name, $valueProperty->getValue($cookie), $cookie->getExpiration(), $cookie->getPath(), $cookie->getDomain(), $cookie->getSecure(), $cookie->getHttpOnly()); $headers['Set-Cookie'][] = (string) $clientCookie; } } } return new Response($response->getContent(), $status ? $status : 200, $headers); }
public function clearData() { $count = Cars::count(); if ($count > 0) { $cars = Cars::find(); foreach ($cars as $car) { $car->delete(); } } Di::reset(); }
/** * Initializes the request object and returns it * * @author Nikolaos Dimopoulos <*****@*****.**> * @since 2014-10-05 * * @return Request */ protected function getRequestObject() { Di::reset(); $di = new Di(); $di->set('filter', function () { return new Filter(); }); $request = new Request(); $request->setDI($di); return $request; }
/** * This method is called before a test is executed. */ protected function setUp() { $this->checkExtension('phalcon'); // Reset the DI container Di::reset(); // Instantiate a new DI container $di = new Di(); // Set the URL $di->set('url', function () { $url = new Url(); $url->setBaseUri('/'); return $url; }); $di->set('escaper', function () { return new Escaper(); }); $this->di = $di; }
/** * HOOK: after scenario * * @param TestCase $test */ public function _after(TestCase $test) { if ($this->config['cleanup'] && isset($this->di['db'])) { while ($this->di['db']->isUnderTransaction()) { $level = $this->di['db']->getTransactionLevel(); try { $this->di['db']->rollback(true); } catch (PDOException $e) { } if ($level == $this->di['db']->getTransactionLevel()) { break; } } } $this->di = null; Di::reset(); $_SESSION = $_FILES = $_GET = $_POST = $_COOKIE = $_REQUEST = []; }
<?php error_reporting(E_ALL); define('APP_PATH', realpath('./../../')); use Phalcon\Di; try { /** * Read the configuration */ $config = (include APP_PATH . "/app/config/config.php"); /** * Read auto-loader */ include APP_PATH . "/app/config/loader.php"; /** * Read services */ include APP_PATH . "/app/config/services.php"; include_once APP_PATH . "/vendor/autoload.php"; Di::reset(); Di::setDefault($di); } catch (\Exception $e) { echo $e->getMessage() . '<br>'; echo '<pre>' . $e->getTraceAsString() . '</pre>'; }
/** * Set up the environment. * * @return Di */ private function setupDI() { Di::reset(); $di = new Di(); $di->setShared('session', function () { return new PhalconMemorySession(); }); $di->setShared('request', function () { return new Request(); }); return $di; }
/** * Sets the environment */ private function setupDI() { Di::reset(); $di = new Di(); $di->set('router', function () { $router = new Router(false); $router->add('/admin/:controller/p/:action', ['controller' => 1, 'action' => 2])->setName('adminProducts'); $router->add('/api/classes/{class}')->setName('classApi'); $router->add('/{year}/{month}/{title}')->setName('blogPost'); $router->add('/wiki/{article:[a-z]+}')->setName('wikipedia'); $router->add('/news/{country:[a-z]{2}}/([a-z+])/([a-z\\-+])/{page}', ['section' => 2, 'article' => 3])->setName('news'); $router->add('/([a-z]{2})/([a-zA-Z0-9_-]+)(/|)', ['lang' => 1, 'module' => 'main', 'controller' => 2, 'action' => 'index'])->setName('lang-controller'); $router->removeExtraSlashes(true); return $router; }); return $di; }
/** * Makes a request. * * @param \Symfony\Component\BrowserKit\Request $request * * @return \Symfony\Component\BrowserKit\Response * @throws \RuntimeException */ public function doRequest($request) { $application = $this->getApplication(); if (!$application instanceof Application && !$application instanceof MicroApplication) { throw new RuntimeException('Unsupported application class.'); } $di = $application->getDI(); /** @var \Phalcon\Http\Request $phRequest */ if ($di->has('request')) { $phRequest = $di->get('request'); } if (!$phRequest instanceof RequestInterface) { $phRequest = new Request(); } $uri = $request->getUri() ?: $phRequest->getURI(); $pathString = parse_url($uri, PHP_URL_PATH); $queryString = parse_url($uri, PHP_URL_QUERY); $_SERVER = $request->getServer(); $_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod()); $_SERVER['REQUEST_URI'] = null === $queryString ? $pathString : $pathString . '?' . $queryString; $_COOKIE = $request->getCookies(); $_FILES = $this->remapFiles($request->getFiles()); $_REQUEST = $this->remapRequestParameters($request->getParameters()); $_POST = []; $_GET = []; if ($_SERVER['REQUEST_METHOD'] == 'GET') { $_GET = $_REQUEST; } else { $_POST = $_REQUEST; } parse_str($queryString, $output); foreach ($output as $k => $v) { $_GET[$k] = $v; } $_GET['_url'] = $pathString; $_SERVER['QUERY_STRING'] = http_build_query($_GET); Di::reset(); Di::setDefault($di); $di['request'] = Stub::construct($phRequest, [], ['getRawBody' => $request->getContent()]); $response = $application->handle(); $headers = $response->getHeaders(); $status = (int) $headers->get('Status'); $headersProperty = new ReflectionProperty($headers, '_headers'); $headersProperty->setAccessible(true); $headers = $headersProperty->getValue($headers); if (!is_array($headers)) { $headers = []; } $cookiesProperty = new ReflectionProperty($di['cookies'], '_cookies'); $cookiesProperty->setAccessible(true); $cookies = $cookiesProperty->getValue($di['cookies']); if (is_array($cookies)) { $restoredProperty = new ReflectionProperty('\\Phalcon\\Http\\Cookie', '_restored'); $restoredProperty->setAccessible(true); $valueProperty = new ReflectionProperty('\\Phalcon\\Http\\Cookie', '_value'); $valueProperty->setAccessible(true); foreach ($cookies as $name => $cookie) { if (!$restoredProperty->getValue($cookie)) { $clientCookie = new Cookie($name, $valueProperty->getValue($cookie), $cookie->getExpiration(), $cookie->getPath(), $cookie->getDomain(), $cookie->getSecure(), $cookie->getHttpOnly()); $headers['Set-Cookie'][] = (string) $clientCookie; } } } return new Response($response->getContent(), $status ? $status : 200, $headers); }
public static function reset() { parent::reset(); }
public function testVoltMacrosIssue11771() { $this->specify("Volt macros can't accept objects", function () { $this->removeFiles([PATH_DATA . 'views/macro/list.volt.php', PATH_DATA . 'views/macro/form_row.volt.php']); Di::reset(); $view = new View(); $di = new Di(); $di->set('escaper', function () { return new Escaper(); }); $di->set('tag', function () { return new Tag(); }); $di->set('url', function () { return (new Url())->setBaseUri('/'); }); $view->setDI($di); $view->setViewsDir(PATH_DATA . 'views/'); $view->registerEngines(array('.volt' => function ($view, $di) { return new Volt($view, $di); })); $object = new \stdClass(); $object->foo = "bar"; $object->baz = "buz"; $object->pi = 3.14; $object->ary = ["some array"]; $object->obj = clone $object; $view->setVar('object', $object); $view->start(); $view->render('macro', 'list'); $view->finish(); ob_start(); var_dump($object); $actual = ob_get_clean(); // Trim xdebug first line (file path) $actual = substr($actual, strpos($actual, 'class')); $expected = substr($view->getContent(), strpos($view->getContent(), 'class')); expect($actual)->equals($expected); $form = new Form(); $form->add(new Password('password')); $view->setVar('formLogin', $form); $view->start(); $view->render('macro', 'form_row'); $view->finish(); $actual = <<<FORM <div class="form-group"> <label class="col-sm-2 control-label" for="password">password:</label> <div class="col-sm-6"><input type="password" id="password" name="password" class="form-control " /></div> </div> FORM; expect($actual)->equals($view->getContent()); $this->removeFiles([PATH_DATA . 'views/macro/list.volt.php', PATH_DATA . 'views/macro/form_row.volt.php']); }); }
/** * executed before each test */ protected function _before() { parent::_before(); Di::reset(); }
public static function reset() { Di::reset(); }