public static function tearDownAfterClass() { if (self::$previousDependencyInjector instanceof DI) { DI::setDefault(self::$previousDependencyInjector); } spl_autoload_unregister(array(__CLASS__, 'autoloadModels')); }
/** * @inheritdoc */ protected function setUp() { $config = DI::getDefault()->get('config'); $bootstrap = new Bootstrap($config); $bootstrap->setup(); $this->di = $bootstrap->getDI(); $this->bootstrap = $bootstrap; DI::setDefault($bootstrap->getDI()); }
/** * Bootstraps the application. */ public function run($args = array()) { // initialize our required services $this->initConfig(); $this->initLoader(); foreach ($this->services as $service) { $function = 'init' . ucfirst($service); $this->{$function}(); } \Phalcon\DI::setDefault($this->di); }
/** * * @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 testRequiredServicesVerification() { $di = \Phalcon\DI::getDefault(); $emptyDI = new \Phalcon\DI\FactoryDefault(); \Phalcon\DI::setDefault($emptyDI); $exception = null; try { new Scaffolding(new Scaffolding\Adapter\Mongo()); } catch (NoRequiredServiceException $e) { $exception = $e; } $this->assertInstanceOf('\\Vegas\\Db\\Exception\\NoRequiredServiceException', $exception); //reverts DI \Phalcon\DI::setDefault($di); }
/** * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::register * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::__construct * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::getProfiler * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::setProfiler * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::setDI * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::getDI * @uses Fabfuel\Prophiler\Plugin\PluginAbstract * @uses Fabfuel\Prophiler\Plugin\Phalcon\Mvc\DispatcherPlugin */ public function testRegister() { DI::setDefault(new FactoryDefault()); $profiler = $this->getMockBuilder('Fabfuel\\Prophiler\\Profiler')->disableOriginalConstructor()->getMock(); $pluginManager = new Phalcon($profiler); $dispatcher = $this->getMockBuilder('Phalcon\\Mvc\\Dispatcher')->getMock(); $pluginManager->dispatcher = $dispatcher; $this->assertFalse($pluginManager->eventsManager->hasListeners('dispatch')); $this->assertFalse($pluginManager->eventsManager->hasListeners('view')); $this->assertFalse($pluginManager->eventsManager->hasListeners('db')); $pluginManager->register(); $this->assertTrue($pluginManager->eventsManager->hasListeners('dispatch')); $this->assertTrue($pluginManager->eventsManager->hasListeners('view')); $this->assertTrue($pluginManager->eventsManager->hasListeners('db')); }
public function setUp() { $this->di = new \Phalcon\DI\FactoryDefault(); $this->di->setShared('db', function () { $db = new TestDatabase(); $db->cleanUp(); $db->constructUsers(); return $db; }); $this->di->setShared('session', function () { $adapter = new TestSession(); $adapter->start(); return $adapter; }); $this->di->set('auth', new \AD\Auth\Security\Auth(new \Phalcon\Config(array('session_key' => 'testkey', 'hash_key' => 'hashkey', 'hash_method' => 'sha256')), '\\TestUser')); \Phalcon\DI::setDefault($this->di); }
public static function setUpBeforeClass() { $config = DI::getDefault()->get('config'); $di = DI::getDefault(); /** * Start the session the first time some component request the session service */ $di->set('session', function () use($config) { $sessionAdapter = new SessionAdapter($config->session->toArray()); if (!$sessionAdapter->isStarted()) { $sessionAdapter->start(); } return $sessionAdapter; }, true); $di->set('sessionManager', function () use($di) { $session = new \Vegas\Session($di->get('session')); return $session; }, true); \Phalcon\DI::setDefault($di); }
public function _before(\Codeception\TestCase $test) { $bootstrap = \Codeception\Configuration::projectDir() . $this->config['bootstrap']; $application = (require $bootstrap); if (!$application instanceof \Phalcon\DI\Injectable) { throw new \Exception('Bootstrap must return \\Phalcon\\DI\\Injectable object'); } $this->di = $application->getDi(); \Phalcon\DI::reset(); \Phalcon\DI::setDefault($this->di); if (isset($this->di['session'])) { $this->di['session'] = new PhalconMemorySession(); } if (isset($this->di['cookies'])) { $this->di['cookies']->useEncryption(false); } if ($this->config['cleanup'] && isset($this->di['db'])) { if ($this->config['savepoints']) { $this->di['db']->setNestedTransactionsWithSavepoints(true); } $this->di['db']->begin(); } $this->client->setApplication(function () use($bootstrap) { $currentDi = \Phalcon\DI::getDefault(); $application = (require $bootstrap); $di = $application->getDi(); if (isset($currentDi['db'])) { $di['db'] = $currentDi['db']; } if (isset($currentDi['session'])) { $di['session'] = $currentDi['session']; } if (isset($di['cookies'])) { $di['cookies']->useEncryption(false); } return $application; }); }
/** * Initialize testing object * * @uses Auth * @uses \ReflectionClass */ public function setUp() { $this->di = new FactoryDefault(); $this->di->reset(); // Setup DI $this->di = new DI(); $this->di->setShared('session', function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $this->di->set('tag', function () { $tag = new \Phalcon\Tag(); return $tag; }); $this->di->set('escaper', function () { $escaper = new \Phalcon\Escaper(); return $escaper; }); DI::setDefault($this->di); $this->reflection = new \ReflectionClass('ULogin\\Auth'); $this->auth = new Auth(); }
<?php //Test Suite bootstrap include __DIR__ . "/../vendor/autoload.php"; define('TESTS_ROOT_DIR', dirname(__FILE__)); $configArray = (require_once dirname(__FILE__) . '/config.php'); $config = new \Phalcon\Config($configArray); $di = new \Phalcon\DI\FactoryDefault(); \Phalcon\DI::setDefault($di);
}); it("should create a from a empty QueryBuilder", function () { $builder = $this->di->get('modelsManager')->createBuilder()->columns('id, name, email, balance')->from('Spec\\Models\\User')->where('balance < 0'); $dataTables = new DataTable(); $response = $dataTables->fromBuilder($builder)->getResponse(); expect($response)->toBe(['draw' => null, 'recordsTotal' => 0, 'recordsFiltered' => 0, 'data' => []]); }); it("should create a QueryBuilder", function () { $builder = $this->di->get('modelsManager')->createBuilder()->columns('id, name, email, balance')->from('Spec\\Models\\User'); $dataTables = new DataTable(); $response = $dataTables->fromBuilder($builder)->getResponse(); expect(count($response['data']))->toBe(20); expect(array_keys($response))->toBe(['draw', 'recordsTotal', 'recordsFiltered', 'data']); foreach ($response['data'] as $data) { expect(array_keys($data))->toBe(['id', 'name', 'email', 'balance', 'DT_RowId']); expect($data['DT_RowId'])->toBe($data['id']); } }); it("should disable view & send response", function () { $this->di->set('view', function () { $view = Stub::create(); return $view; }); \Phalcon\DI::setDefault($this->di); $builder = $this->di->get('modelsManager')->createBuilder()->columns('id, name, email, balance')->from('Spec\\Models\\User'); $dataTables = new DataTable(); expect(function () use($dataTables, $builder) { $dataTables->fromBuilder($builder)->sendResponse(); })->toEcho("{\"draw\":null,\"recordsTotal\":100,\"recordsFiltered\":100,\"data\":[{\"id\":\"0\",\"name\":\"wolff.jamel\",\"email\":\"rkuhn@dicki.com\",\"balance\":\"500\",\"DT_RowId\":\"0\"},{\"id\":\"1\",\"name\":\"violet36\",\"email\":\"imedhurst@paucek.com\",\"balance\":\"100\",\"DT_RowId\":\"1\"},{\"id\":\"2\",\"name\":\"beahan.abbigail\",\"email\":\"kenna23@mertz.biz\",\"balance\":\"200\",\"DT_RowId\":\"2\"},{\"id\":\"3\",\"name\":\"rickie05\",\"email\":\"echamplin@terry.com\",\"balance\":\"200\",\"DT_RowId\":\"3\"},{\"id\":\"4\",\"name\":\"runolfsdottir.evert\",\"email\":\"vern.goldner@rau.org\",\"balance\":\"200\",\"DT_RowId\":\"4\"},{\"id\":\"5\",\"name\":\"judson99\",\"email\":\"homenick.angeline@greenfelder.com\",\"balance\":\"200\",\"DT_RowId\":\"5\"},{\"id\":\"6\",\"name\":\"walter.eliseo\",\"email\":\"eleanore.gusikowski@yahoo.com\",\"balance\":\"100\",\"DT_RowId\":\"6\"},{\"id\":\"7\",\"name\":\"monserrate.lebsack\",\"email\":\"abraham.hane@gmail.com\",\"balance\":\"500\",\"DT_RowId\":\"7\"},{\"id\":\"8\",\"name\":\"llangworth\",\"email\":\"anderson.delphia@gmail.com\",\"balance\":\"100\",\"DT_RowId\":\"8\"},{\"id\":\"9\",\"name\":\"krajcik.rylee\",\"email\":\"franecki.conor@gmail.com\",\"balance\":\"500\",\"DT_RowId\":\"9\"},{\"id\":\"10\",\"name\":\"vsauer\",\"email\":\"maia14@conroy.com\",\"balance\":\"100\",\"DT_RowId\":\"10\"},{\"id\":\"11\",\"name\":\"green.german\",\"email\":\"hegmann.jane@hotmail.com\",\"balance\":\"200\",\"DT_RowId\":\"11\"},{\"id\":\"12\",\"name\":\"gebert\",\"email\":\"sdicki@gmail.com\",\"balance\":\"200\",\"DT_RowId\":\"12\"},{\"id\":\"13\",\"name\":\"michale68\",\"email\":\"qblock@yahoo.com\",\"balance\":\"200\",\"DT_RowId\":\"13\"},{\"id\":\"14\",\"name\":\"ismitham\",\"email\":\"hirthe.marjory@hotmail.com\",\"balance\":\"200\",\"DT_RowId\":\"14\"},{\"id\":\"15\",\"name\":\"jarrell09\",\"email\":\"cedrick04@gmail.com\",\"balance\":\"100\",\"DT_RowId\":\"15\"},{\"id\":\"16\",\"name\":\"iharvey\",\"email\":\"jude.christiansen@hotmail.com\",\"balance\":\"100\",\"DT_RowId\":\"16\"},{\"id\":\"17\",\"name\":\"bode.maxine\",\"email\":\"tmosciski@yahoo.com\",\"balance\":\"100\",\"DT_RowId\":\"17\"},{\"id\":\"18\",\"name\":\"murray.jeff\",\"email\":\"fjacobs@strosin.com\",\"balance\":\"200\",\"DT_RowId\":\"18\"},{\"id\":\"19\",\"name\":\"gmertz\",\"email\":\"marquardt.nicolas@williamson.com\",\"balance\":\"100\",\"DT_RowId\":\"19\"}]}"); }); });
/** * Executes all bootstrap initialization methods * This method can be overloaded to load own initialization method. * @return mixed */ public function setup() { $this->di->set('config', $this->config); $this->initEnvironment($this->config); $this->initLoader($this->config); $this->initServices($this->config); $this->initModules($this->config); $this->initRoutes($this->config); $this->initRequest(); $this->initDispatcher(); $this->application->setDI($this->di); DI::setDefault($this->di); return $this; }
<?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);
* */ $container->set('db', function () use($config) { return new DbAdapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname)); }); /** * Models metadata * */ $container->set('modelsMetadata', function () use($config) { return new MetaDataAdapter(); }); /** * Session * */ $container->set('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); /** * Dispatcher * */ $container->set('dispatcher', function () { $dispatcher = new Dispatcher(); return $dispatcher; }); DI::setDefault($container); return $container;
/** * Gets a raw response from the internal API * This will trick Phalcon into thinking its a * real PUT, POST, PATCH, GET or DELETE request * It will override the Default DI (Which will be the current site) * and will restore everything after the request * It seems hacky, but I am not sure if there is any better way, please * submit a pull request if you can improve! :) * todo test if the below does in fact require a new instance of DI * if it is being called from within the API * * @param $method * @param $path * @param RequestOptions $options * * @return Response */ private function getRawResponse($method, $path, RequestOptions $options = null) { // Backup super globals $request = $_REQUEST; $post = $_POST; $get = $_GET; // Override the request params $_GET = $options ? $options->getGetParams() : []; $_POST = $options ? $options->getPostParams() : []; // Set HTTP method in GET $_GET['method'] = $method; $_GET['_url'] = $path; $_REQUEST = ['type' => 'raw']; // todo is this requred? // Get current DI $defaultDI = DI::getDefault(); if ($defaultDI instanceof PhrestDI) { $apiDI = $defaultDI; } else { // Set API DI to the default, this is required for models etc. // As Phalcon will get the default DI to perform actions $apiDI = self::getInstance()->app->getDI(); DI::setDefault($apiDI); } // Cache the URI & method self::$uri = $path; self::$method = $method; // Get response from API // todo post not picked up try { $response = $this->app->handle($path); } catch (\Exception $e) { DI::setDefault($defaultDI); throw $e; } // Remove cached uri & method self::$uri = null; self::$method = null; // Restore default DI if (!$defaultDI instanceof PhrestDI) { DI::setDefault($defaultDI); } // Restore super globals $_REQUEST = $request; $_POST = $post; $_GET = $get; return $response; }
public function setUp() { $this->_service = $this->loadService(); \Phalcon\DI::setDefault($this->_service->getDI()); $this->_loaded = true; }
<?php use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp, Phalcon\DI; //Using the CLI factory default services container $di = DI::setDefault(new CliDI()); require __DIR__ . '/../../autoloader.php'; $helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($di->get('entityManager')); \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet, []);
/** * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::registerDatabase * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::ensureEventsManager * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::__construct * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::setProfiler * @covers Fabfuel\Prophiler\Plugin\Manager\Phalcon::getProfiler * @uses Fabfuel\Prophiler\Profiler * @uses Fabfuel\Prophiler\Plugin\PluginAbstract * @uses Fabfuel\Prophiler\Plugin\Phalcon\Mvc\DispatcherPlugin */ public function testRegisterDatabaseIfNotExisting() { $profiler = $this->getMock('Fabfuel\\Prophiler\\ProfilerInterface'); DI::setDefault(new FactoryDefault()); DI::getDefault()->set('db', new \stdClass()); $pluginManager = new Phalcon($profiler); $this->assertFalse($pluginManager->registerDatabase()); }