Example #1
0
 public function sqlite(UnitTester $I)
 {
     $I->wantToTest("Model validation by using SQLite as RDBMS");
     /** @var \Phalcon\Di\FactoryDefault $di */
     $di = Di::getDefault();
     $connection = $di->getShared('db');
     $di->remove('db');
     $di->setShared('db', function () {
         $connection = new Sqlite(['dbname' => TEST_DB_SQLITE_NAME]);
         /** @var \PDO $pdo */
         $pdo = $connection->getInternalHandler();
         $pdo->sqliteCreateFunction('now', function () {
             return date('Y-m-d H:i:s');
         });
         return $connection;
     });
     Di::setDefault($di);
     $this->success($I);
     $this->presenceOf($I);
     $this->email($I);
     $this->emailWithDot($I);
     $this->exclusionIn($I);
     $this->inclusionIn($I);
     $this->uniqueness1($I);
     $this->uniqueness2($I);
     $this->regex($I);
     $this->tooLong($I);
     $this->tooShort($I);
     $di->remove('db');
     $di->setShared('db', $connection);
 }
Example #2
0
 public function setDI(DiInterface $di)
 {
     $di->setShared('config', static::$config);
     $this->initEventsManager($di);
     parent::setDI($di);
     Di::setDefault($di);
 }
Example #3
0
 /**
  * executed after each test
  */
 protected function _after()
 {
     if ($this->previousDependencyInjector instanceof DiInterface) {
         Di::setDefault($this->previousDependencyInjector);
     } else {
         Di::reset();
     }
 }
Example #4
0
 /**
  * Executed before each test
  *
  * @param IntegrationTester $I
  */
 public function _before(IntegrationTester $I)
 {
     Di::setDefault($I->getApplication()->getDI());
     $this->modelsManager = $I->getApplication()->getDI()->getShared('modelsManager');
     $I->haveServiceInDi('modelsMetadata', function () {
         return new Memory();
     }, true);
 }
Example #5
0
 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);
 }
Example #6
0
 /**
  * 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);
 }
Example #7
0
 /**
  * executed before each test
  */
 protected function _before()
 {
     parent::_before();
     $di = $this->tester->getApplication()->getDI();
     $di->set('modelsManager', function () {
         return new Manager();
     });
     $di->set('modelsMetadata', function () {
         return new Memory();
     });
     Di::setDefault($di);
 }
Example #8
0
 public function resolve(\Phalcon\Config $config)
 {
     $di = new \Phalcon\Di\FactoryDefault();
     $di->set('config', $config, true);
     $di->set('mongo', function () use($config) {
         $connectionString = "mongodb://{$config->mongo->host}:{$config->mongo->port}";
         $mongo = new \MongoClient($connectionString);
         return $mongo->selectDb($config->mongo->dbname);
     }, true);
     $di->set('collectionManager', function () {
         return new \Phalcon\Mvc\Collection\Manager();
     }, true);
     \Phalcon\Di::setDefault($di);
 }
Example #9
0
 /**
  * HOOK: before scenario
  *
  * @param TestCase $test
  * @throws ModuleException
  */
 public function _before(TestCase $test)
 {
     $application = (require $this->bootstrapFile);
     if (!$application instanceof Injectable) {
         throw new ModuleException(__CLASS__, 'Bootstrap must return \\Phalcon\\Di\\Injectable object');
     }
     $this->di = $application->getDI();
     if (!$this->di instanceof DiInterface) {
         throw new ModuleException(__CLASS__, 'Dependency injector container must implement DiInterface');
     }
     Di::reset();
     Di::setDefault($this->di);
     if ($this->di->has('session')) {
         $this->di['session'] = new PhalconMemorySession();
     }
     if ($this->di->has('cookies')) {
         $this->di['cookies']->useEncryption(false);
     }
     if ($this->config['cleanup'] && $this->di->has('db')) {
         if ($this->config['savepoints']) {
             $this->di['db']->setNestedTransactionsWithSavepoints(true);
         }
         $this->di['db']->begin();
     }
     // localize
     $bootstrap = $this->bootstrapFile;
     $this->client->setApplication(function () use($bootstrap) {
         $currentDi = Di::getDefault();
         $application = (require $bootstrap);
         $di = $application->getDI();
         if ($currentDi->has('db')) {
             $di['db'] = $currentDi['db'];
         }
         if ($currentDi->has('session')) {
             $di['session'] = $currentDi['session'];
         }
         if ($di->has('cookies')) {
             $di['cookies']->useEncryption(false);
         }
         return $application;
     });
 }
Example #10
0
<?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>';
}
Example #11
0
 /**
  * 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);
 }
Example #12
0
 public static function setDefault(DiInterface $dependencyInjector)
 {
     parent::setDefault($dependencyInjector);
 }
Example #13
0
 /**
  * executed before each test
  */
 protected function _before()
 {
     $di = $this->tester->getApplication()->getDI();
     Di::setDefault($di);
 }
Example #14
0
 public static function setDefault(DiInterface $di)
 {
     Di::setDefault($di);
 }
Example #15
0
 /**
  * Get the available container instance.
  *
  * @param  string  $make
  * @param  array   $parameters
  * @return mixed|\Engine\DI\Factory
  */
 function di($make = null, $parameters = [])
 {
     if (is_null($make)) {
         return DI::getDefault();
     } elseif ($make instanceof DI) {
         return DI::setDefault($make);
     }
     return DI::getDefault()->get($make, $parameters);
 }