/** * Sets up the DI * * @author Nikolaos Dimopoulos <*****@*****.**> * @since 2014-10-13 * * @param bool $static * * @return \Phalcon\DI\FactoryDefault */ protected function prepareDI($static = false) { PhDI::reset(); $di = new PhDIFD(); $this->prepareUrl($di, $static); $this->prepareEscaper($di); return $di; }
/** * Initializes the request object and returns it * * @author Nikolaos Dimopoulos <*****@*****.**> * @since 2014-10-05 * * @return PhRequest */ protected function getRequestObject() { PhDI::reset(); $di = new PhDI(); $di->set('filter', function () { return new PhTFilter(); }); $request = new PhTRequest(); $request->setDI($di); return $request; }
/** * * @param \Symfony\Component\BrowserKit\Request $request * * @return \Symfony\Component\BrowserKit\Response */ public function doRequest($request) { $application = $this->getApplication(); $di = $application->getDI(); DI::reset(); DI::setDefault($di); $_SERVER = array(); foreach ($request->getServer() as $key => $value) { $_SERVER[strtoupper(str_replace('-', '_', $key))] = $value; } if (!$application instanceof \Phalcon\MVC\Application && !$application instanceof \Phalcon\MVC\Micro) { throw new \Exception('Unsupported application class'); } $_COOKIE = $request->getCookies(); $_FILES = $this->remapFiles($request->getFiles()); $_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod()); $_REQUEST = $this->remapRequestParameters($request->getParameters()); if (strtoupper($request->getMethod()) == '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'), [], array('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 = array(); } $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 testIssue2688() { // Init \Phalcon\DI::reset(); new \Phalcon\DI\FactoryDefault(); // Configuration $bag = new Phalcon\Session\Bag('container'); $value = array(); $bag->a = $value; // Asserts $this->assertEquals($value, $bag->a); }
protected function _getDI() { DI::reset(); $di = new DI(); $di->set('modelsManager', function () { return new Manager(); }); $di->set('modelsMetadata', function () { return new Memory(); }); $di->set('db', function () { require PATH_CONFIG . 'config.db.php'; return new Mysql($configMysql); }); return $di; }
public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL) { $this->checkExtension('phalcon'); if (!is_null($config)) { $this->config = $config; } if (is_null($di)) { // Reset the DI container DI::reset(); // Instantiate a new DI container $di = new FactoryDefault(); // Set the URL $di->set('collectionManager', function () { return new CollectionManager(); }, true); } $this->di = $di; }
/** * Sets the test up by loading the DI container and other stuff * * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-09-30 * * @return \Phalcon\DI */ protected function setUp() { $this->checkExtension('phalcon'); // Set the config up $this->config = Config::init(); // Reset the DI container \Phalcon\DI::reset(); // Instantiate a new DI container $di = new \Phalcon\DI(); // Set the URL $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }); $di->set('escaper', function () { return new \Phalcon\Escaper(); }); $this->di = $di; }
/** * @covers \Phalcon\Session\Bag::get() * @covers \Phalcon\Session\Bag::set() * @covers \Phalcon\Session\Bag::__get() * @covers \Phalcon\Session\Bag::__set() */ public function testGetSet() { \Phalcon\DI::reset(); new \Phalcon\DI\FactoryDefault(); @session_start(); // Using getters and setters. $bag = new Phalcon\Session\Bag('test1'); $bag->set('a', array('b' => 'c')); $this->assertEquals(array('b' => 'c'), $bag->get('a')); $this->assertEquals(array('b' => 'c'), $_SESSION['test1']['a']); // Using direct access. $bag = new Phalcon\Session\Bag('test2'); $bag->a = array('b' => 'c'); $this->assertEquals(array('b' => 'c'), $bag->{'a'}); $this->assertEquals(array('b' => 'c'), $_SESSION['test2']['a']); // Using direct access with initialising a variable. $bag = new Phalcon\Session\Bag('test3'); $bag->a['b'] = 'c'; $this->assertEquals(array('b' => 'c'), $bag->a); $this->assertEquals(array('b' => 'c'), $_SESSION['test3']['a']); }
/** * Sets the test up by loading the DI container and other stuff * * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-09-30 * @param \Phalcon\DiInterface $di * @param \Phalcon\Config $config * @return void */ protected function setUp(DiInterface $di = null, Config $config = null) { $this->checkExtension('phalcon'); if (!is_null($config)) { $this->config = $config; } if (is_null($di)) { // Reset the DI container DI::reset(); // Instantiate a new DI container $di = new FactoryDefault(); // Set the URL $di->set('url', function () { $url = new Url(); $url->setBaseUri('/'); return $url; }); $di->set('escaper', function () { return new \Phalcon\Escaper(); }); } $this->di = $di; }
public function _after(\Codeception\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; \Phalcon\DI::reset(); $_SESSION = array(); $_FILES = array(); $_GET = array(); $_POST = array(); $_COOKIE = array(); $_REQUEST = array(); }
<?php use Phalcon\DI, Phalcon\DI\FactoryDefault; ini_set('display_errors', 1); error_reporting(E_ALL); define('ROOT_PATH', __DIR__); define('PATH_LIBRARY', __DIR__ . '/../app/library/'); define('PATH_SERVICES', __DIR__ . '/../app/services/'); define('PATH_RESOURCES', __DIR__ . '/../app/resources/'); set_include_path(ROOT_PATH . PATH_SEPARATOR . get_include_path()); // required for phalcon/incubator include __DIR__ . "/../vendor/autoload.php"; // use the application autoloader to autoload the classes // autoload the dependencies found in composer $loader = new \Phalcon\Loader(); $loader->registerDirs(array(ROOT_PATH)); $loader->register(); $di = new FactoryDefault(); DI::reset(); // add any needed services to the DI here DI::setDefault($di);
/** * @covers ULogin\Auth::__construct() */ public function testConstructor() { // check thrown exception when session does not exists $this->di->offsetUnset('session'); try { new Auth(); } catch (\Phalcon\Session\Exception $e) { $this->assertInternalType('string', $e->getMessage(), "[-] Auth() must throw an exception if error called when session not instantiated"); } // at last reset DI to throw Exception DI::reset(); try { new Auth(); } catch (\Phalcon\DI\Exception $e) { $this->assertInternalType('string', $e->getMessage(), "[-] Auth() must throw an exception if error called when DI not configured"); } }
public function testContainerHasYamlService() { // Given: Container $container = ContainerFactory::build(); // Then: Expect to have defined yaml service $this->assertTrue($container->has('yaml')); // Then: Expect to yaml service be set as shared $this->assertTrue($container->getService('yaml')->isShared()); // Then: Expect to have yaml be instance of Symfony\Component\Yaml\Parser $this->assertInstanceOf('Symfony\\Component\\Yaml\\Parser', $container->get('yaml')); // Cleanup: Reset default Phalcon instance DI::reset(); }
public function callService($data) { echo "service is doing:.\n"; var_dump($data); echo ".\n"; \Xz\Lib\Core\RunTime::start(); // 判断数据是否正确 if (empty($data['data']['service']) || empty($data['data']['method']) || !isset($data['data']['args'])) { // 发送数据给客户端,请求包错误 return array('fd' => $data['fd'], 'data' => array('code' => 600, 'msg' => 'bad request', 'data' => 0)); } $di = new \Phalcon\DI(); require APP_ROOT . 'apps/config/loaderyar.php'; $backend = new \Xz\Lib\Core\BackendServer(); $ret = array(); $ret['data'] = $backend->callService($data['data']); $ret['fd'] = $data['fd']; \Phalcon\DI::reset(); echo "service is complete:.\n"; var_dump($ret); echo ".\n"; return $ret; }
public function testFilterMultiplesSourcesFilterJoin() { $this->markTestIncomplete('To be checked'); @unlink('unit-tests/assets/production/combined-3.js'); Phalcon\DI::reset(); $di = new Phalcon\DI(); $di['url'] = function () { $url = new Phalcon\Mvc\Url(); $url->setStaticBaseUri('/'); return $url; }; $di->set('escaper', function () { return new \Phalcon\Escaper(); }); $assets = new PhTAssetsManager(); $assets->useImplicitOutput(false); $js = $assets->collection('js'); $js->setTargetUri('production/combined-3.js'); $js->setTargetPath('unit-tests/assets/production/combined-3.js'); $jquery = new PhTAssetsResourceJs('unit-tests/assets/jquery.js', false, false); $jquery->setTargetUri('jquery.js'); $js->add($jquery); $gs = new PhTAssetsResourceJs('unit-tests/assets/gs.js'); $gs->setTargetUri('gs.js'); $gs->setTargetPath('gs.js'); $js->add($gs); $js->join(true); //Use two filters $js->addFilter(new Phalcon\Assets\Filters\None()); $js->addFilter(new Phalcon\Assets\Filters\None()); $this->assertEquals($assets->outputJs('js'), '<script type="text/javascript" src="/production/combined-3.js"></script>' . PHP_EOL); }