public function setUp() { $this->di = $this->getDI(); $config = $this->di->get('config'); // // Load any additional services that mock some methods to prevent database unwanted changes // $this->di->set('modelsManager', new Manager()); // $this->di->set('modelsMetadata', new Files(array('metaDataDir' => $config->application->testsMetadataDir))); // $con = $this->getMock('\\Phalcon\\Db\\Adapter\\Pdo\\Mysql', array('getDialect', 'query', 'execute'), array(),'',false); // $this->dialect = $this->getMock('\\Phalcon\\Db\\Dialect\\Mysql', array('select'), array(), '', false); // $results = $this->getMock('\\Phalcon\\Db\\Result\\Pdo', array('numRows', 'setFetchMode', 'fetchall', 'create'), array(), '', false); // $results->expects($this->any()) // ->method('numRows') // ->will($this->returnValue(10)); // // $results->expects($this->any()) // ->method('create') // ->willReturn(true); // // $results->expects($this->any()) // ->method('fetchall') // ->will($this->returnValue(0)); // // $this->dialect->expects($this->any()) // ->method('select'); // // $con->expects($this->any()) // ->method('getDialect') // ->will($this->returnValue($this->dialect)); // // $con->expects($this->any()) // ->method('query') // ->will($this->returnValue($results)); // // $con->expects($this->any()) // ->method('execute'); // // $this->di->set('db', $con); // parent::setUp($this->di); $this->_loaded = true; }
public static function injectTo(Di $di) { $di->setShared('logger', function () use($di) { $logger = new Multiple(); $config = $di->get('config')['loggers']; foreach ($config as $logConfig) { $adapter = $logConfig['adapter']; $options = isset($logConfig['options']) ? $logConfig['options'] : null; $logger->push(new $adapter($logConfig['name'], $options)); } return $logger; }); }
/** * Tests resolving service * * @author Serghei Iakovlev <*****@*****.**> * @since 2016-01-29 */ public function testResolvingService() { $this->specify("Di does not resolves service correctly", function () { $di = new Di(); $di->set('resolved', function () { return new \SomeService(); }); $di->set('notResolved', function () { return new \SomeService(); }); expect($di->getService('resolved')->isResolved())->false(); expect($di->getService('notResolved')->isResolved())->false(); $di->get('resolved'); expect($di->getService('resolved')->isResolved())->true(); expect($di->getService('notResolved')->isResolved())->false(); }); }
/** * @param Di $di * @return int */ public static function getTimezoneOffset(Di $di) { /** * @var \Phalcon\Http\Request $request */ $request = $di->get('request'); $timezoneOffset = $request->getHeader('TimezoneOffset'); return (int) $timezoneOffset; }
/** * Get the Phalcon dependency injection instance. * * @param string $name service name * @return \Phalcon\Di|mixed */ public function getDI($name = null) { $name = $name; return null === $name ? $this->di : $this->di->get($name); }
public function testConfigFileLoaded() { $b = $this->getBootstrap()->run(); $this->assertEquals('bar', static::$di->get('config')->foo); }
public static function injectTo(Di $di) { $di->setShared('logger', $di->get('config')['logger']); }
protected function initEventsManager(Di $di) { if (!$di->has('eventsManager') || $di->getService('eventsManager')->getDefinition() == 'Phalcon\\Events\\Manager') { $di->setShared('eventsManager', $this->getEventsManager()); } $this->setEventsManager($di->get('eventsManager')); }
/** * getSession * @return SessionAdapter */ public function getSession() { return $this->_di->get('session'); }
public function get($name, $parameters = null) { return parent::get($name, $parameters); }
/** * getAssetManager * @return \interfaces\AssetManagerInterface */ public function getAssetManager() { return $this->_di->get('assetManager'); }
private function services() { $di = new Di(); // Store the api messages $di->set('messages', function ($input = null) { return $input; }); $di->set('view', function () { $view = new View(); // $view->setViewsDir('../apps/views/'); return $view; }); $di->set('dispatcher', function () use($di) { $eventsManager = new Manager(); $eventsManager->attach('dispatch:afterExecuteRoute', function ($event, $dispatcher) use($di) { // dd($event->getSource()->getActiveController()->getMessages()); // dd(get_class_methods($event->getSource()->getActiveController())); // dd(get_class_methods($event->getSource())); // dd($event->getData()); // dd($event->getSource()); $source = $event->getSource(); $returned = $source->getReturnedValue(); if (is_object($returned)) { if (!method_exists($returned, 'toArray')) { throw new ApiException('The returned value can not be parsed, try it manual.'); } $returned = $returned->toArray(); } $configs = $di->get('config'); $response = new Response(); if ($configs->api->cors->enabled) { $response->setHeader('Access-Control-Allow-Origin', $configs->api->cors->hosts); } foreach ($configs->api->headers as $name => $value) { $response->setHeader($name, $value); } $json_options = 0; if ($configs->debug) { $json_options = JSON_PRETTY_PRINT; } foreach ($configs->json->options as $option) { $json_options |= $option; } $activeController = $source->getActiveController(); $status = $activeController->getStatus(); $errors = $activeController->getErrors(); $messages = $activeController->getMessages(); $status_code = $activeController->getCode(); $data = ['status' => $status, 'status_code' => $status_code, 'errors' => $errors, 'messages' => $messages, 'data' => $returned]; $response->setJsonContent($data, $json_options); $event->getSource()->setReturnedValue($response); }); $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('App\\Controllers\\'); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); ///////!!!!!!!!!!!!!!!! $di->set('modelsMetadata', function () { return new \Phalcon\Mvc\Model\Metadata\Memory(); }); $di->set('modelsManager', function () { return new \Phalcon\Mvc\Model\Manager(); }); $this->setDi($di); }