public function attachVoltService($voltCacheDir) { $this->_di->set('voltService', function ($view, $di) use($voltCacheDir) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(['compiledPath' => $voltCacheDir, 'compiledSeparator' => '_', 'compiledExtension' => '.compiled']); return $volt; }); return $this; }
/** * executed before each test */ protected function _before() { $this->previousDependencyInjector = Di::getDefault(); $di = new Di(); $di->set('modelsManager', new ModelManager()); $di->set('collectionManager', new CollectionManager()); if ($this->previousDependencyInjector instanceof DiInterface) { Di::setDefault($di); } }
/** * @expectedException \Phalcon\Mvc\Model\Exception * @expectedExceptionMessage Field name must be a string */ public function testValidateIncorrectFieldType() { $di = new Di(); $di->set('modelsManager', new Manager()); require_once __DIR__ . '/resources/TestCardNumberIncorrectField.php'; $obj = new \TestCardNumberIncorrectField(); $obj->validation(); }
/** * Setup viewCache service and DI * @return Di */ protected function getDi() { $di = new Di(); $di->set('viewCache', function () { return new File(new Output(['lifetime' => 2]), ['cacheDir' => PATH_CACHE]); }); return $di; }
/** * 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(); }); }
/** * 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; }
public function testAvailableUniquenessWithDefaultDI() { $di = new Di(); $di->set('db', $this->getDbStub()); $uniquenessOptions = ['table' => 'users', 'column' => 'login']; $uniqueness = new Uniqueness($uniquenessOptions); $this->validation->add('login', $uniqueness); $messages = $this->validation->validate(['login' => 'login_free']); $this->assertCount(0, $messages); }
/** * 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; }
public static function register(Di $di) { static::$di = $di; $di->set('Phalcon\\Http\\Cookie', 'Phwoolcon\\Http\\Cookie'); static::$cookies = static::$di->getShared('cookies'); static::$cookies->reset(); static::$options = $options = Config::get('cookies'); static::$cookies->useEncryption($encrypt = $options['encrypt']); $encrypt and static::$di->getShared('crypt')->setKey($options['encrypt_key'])->setPadding(Crypt::PADDING_ZERO); /* @var \Phalcon\Http\Response $response */ if ($response = $di->getShared('response')) { $response->setCookies(static::$cookies); } Events::attach('view:generatePhwoolconJsOptions', function (Event $event) { $options = $event->getData() ?: []; $options['cookies'] = ['domain' => static::$options['domain'], 'path' => static::$options['path']]; $event->setData($options); return $options; }); }
private function _getDI() { $di = new Di(); $frontendCache = new FrontendCache(array('lifetime' => 2)); $backendCache = new BackendCache($frontendCache, array('cacheDir' => 'unit-tests/cache/')); $di->set('viewCache', $backendCache); return $di; }
public function testValidateCustomMessage() { $di = new Di(); $di->set('modelsManager', new Manager()); require_once __DIR__ . '/resources/TestBetweenModel.php'; $obj = new \TestBetweenModel(); $obj->min = 1; $obj->max = 2; $obj->position = 3; $obj->message = 'test 123'; $obj->validation(); $messages = $obj->getMessages(); $this->assertEquals($messages[0]->getMessage(), 'test 123'); }
/** * 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; }
public function testDispatcher() { $this->specify("Dispatcher doesn't throw exception when handler can't be loaded", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("index"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "IndexController handler class cannot be loaded"]]); $this->specify("Dispatcher doesn't throw exception when handler can't be loaded (2)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("essai"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "EssaiController handler class cannot be loaded"]]); $this->specify("Dispatcher doesn't throw exception when handler can't be loaded (3)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test0"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Test0Controller handler class cannot be loaded"]]); $this->specify("Dispatcher doesn't throw exception when action can't be found", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test1"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'index' was not found on handler 'test1'"]]); $this->specify("Dispatcher doesn't return controller after dispatching", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $controller = $dispatcher->dispatch(); expect($controller)->isInstanceOf("Test2Controller"); }); $this->specify("Dispatcher doesn't throw exception when action can't be found (2)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("essai"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'essai' was not found on handler 'test2'"]]); $this->specify("Dispatcher doesn't work as expected", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("other"); $dispatcher->setParams([]); $controller = $dispatcher->dispatch(); expect($controller)->isInstanceOf("Test2Controller"); }); $this->specify("Dispatcher doesn't work as expected (2)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("another"); $dispatcher->setParams([]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals(100); }); $this->specify("Dispatcher doesn't work as expected (3)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("anotherTwo"); $dispatcher->setParams([2, "3"]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals(5); }); $this->specify("Dispatcher doesn't work as expected (4)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("anotherthree"); $dispatcher->setParams([]); $dispatcher->dispatch(); $value = $dispatcher->getActionName(); expect($value)->equals("anotherfour"); $value = $dispatcher->getReturnedValue(); expect($value)->equals(120); }); $this->specify("Dispatcher doesn't work as expected (5)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("anotherFive"); $dispatcher->setParams(["param1" => 2, "param2" => 3]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals(5); }); $this->specify("Dispatcher doesn't work as expected (6)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test7"); $dispatcher->setActionName("service"); $dispatcher->setParams([]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals("hello"); $value = $dispatcher->getControllerClass(); expect($value)->equals("Test7Controller"); }); }
public function testModules() { $this->specify("CLI Console doesn't work with modules", function () { $di = new Di(); $di->set('data', function () { return "data"; }); $console = new Console(); $console->setDI($di); $expected = ['devtools' => ['className' => 'dummy', 'path' => 'dummy_file']]; $console->registerModules($expected); expect($console->getModules())->equals($expected); $userModules = ['front' => ['className' => 'front', 'path' => 'front_file'], 'worker' => ['className' => 'worker', 'path' => 'worker_file']]; $expected = ['devtools' => ['className' => 'dummy', 'path' => 'dummy_file'], 'front' => ['className' => 'front', 'path' => 'front_file'], 'worker' => ['className' => 'worker', 'path' => 'worker_file']]; $console->registerModules($userModules, true); expect($console->getModules())->equals($expected); }); }
private function _getDi($service = 'viewCache', $lifetime = 60) { $di = new Di(); $frontendCache = new FrontendCache(array('lifetime' => $lifetime)); $backendCache = new BackendCache($frontendCache, array('cacheDir' => 'unit-tests/cache/')); $di->set($service, $backendCache); return $di; }
private function database(Di $di) { if ($this->config()->db->adapter === "mongo") { $config = $this->config(); $di->set('mongo', function () use(&$config) { $mongo = new MongoClient($config->db->host . ":" . $config->db->port); return $mongo->selectDB($config->db->dbname); }, true); $di->set('collectionManager', function () { $modelsManager = new CollectionManager(); return $modelsManager; }, true); } }
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']); }); }
<?php use Phalcon\Di; use Phalcon\Loader; use Phalcon\Db\Adapter\Pdo\Mysql; /** * Very simple MVC structure */ $loader = new Loader(); $loader->registerDirs(array('../apps/controllers/', '../apps/models/')); $loader->register(); $di = new Di(); //Registering a router $di->set('router', 'Phalcon\\Mvc\\Router'); //Registering a dispatcher $di->set('dispatcher', 'Phalcon\\Mvc\\Dispatcher'); //Registering a Http\Response $di->set('response', 'Phalcon\\Http\\Response'); //Registering the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/views/'); return $view; }); $di->set('db', function () { return new Database(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "invo")); }); //Registering the Models-Metadata $di->set('modelsMetadata', 'Phalcon\\Mvc\\Model\\Metadata\\Memory'); //Registering the Models Manager $di->set('modelsManager', 'Phalcon\\Mvc\\Model\\Manager');
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); }
public function set($name, $definition, $shared = false) { return parent::set($name, $definition, $shared); }