From 4d0d036be9422d315c472c6f9fc6fc40de7f7b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Wed, 7 Jun 2017 14:58:27 +0200 Subject: [PATCH] testing --- composer.json | 3 +- lib/controllerbase.php | 26 ++----- lib/helpers.php | 150 ++++++++++++++++++++++++----------------- lib/lib.php | 18 ++++- lib/octalia.php | 8 +-- lib/octaliasession.php | 98 +++++++++++++++++++++++++++ lib/testcase.php | 46 +++++++++++++ 7 files changed, 259 insertions(+), 90 deletions(-) create mode 100644 lib/octaliasession.php create mode 100644 lib/testcase.php diff --git a/composer.json b/composer.json index 8d4a2c3..af7047e 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ }, "autoload": { "files": [ - "lib/lib.php" + "lib/lib.php", + "lib/testcase.php" ] }, "config": { diff --git a/lib/controllerbase.php b/lib/controllerbase.php index f064575..d70c8d8 100644 --- a/lib/controllerbase.php +++ b/lib/controllerbase.php @@ -314,32 +314,18 @@ protected function model($model, $force = false) return $this->models[$model]; } - public function em() + public function em($model, $engine = 'engine', $force = false) { - return call_user_func_array('\\Octo\\em', func_get_args()); + return em($model, $engine, $force); } - public function middleware() + public function middleware($class) { - $coreMiddlewares = Registry::get('core.middlewares', []); - - $args = func_get_args(); - - $middlewares = array_shift($args); - - if (!is_array($middlewares)) { - $middlewares = [$middlewares]; + if (is_string($class)) { + $class = maker($class, [], false); } - $a = array_merge([$this], $args); - - foreach ($middlewares as $middlewareKey) { - $middleware = isAke($coreMiddlewares, $middlewareKey, null); - - if (is_callable($middleware)) { - call_user_func_array($middleware, $a); - } - } + return $class->handle(); } public function action($action) diff --git a/lib/helpers.php b/lib/helpers.php index cd4b1ea..fa13b9a 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -1,36 +1,36 @@ toArray() : $args; + $callable = isAke($binds, $make, null); if ($callable && is_callable($callable) && $singleton) { @@ -6072,6 +6074,8 @@ function guard($ns = 'web', $em = 'user') }); $class->macro('login', function ($user) use ($ns) { + $user = !is_array($user) ? $user->toArray() : $user; + session($ns)->setUser($user); }); @@ -6117,7 +6121,7 @@ function guard($ns = 'web', $em = 'user') $user = em($em)->find((int) $id); if ($user) { - session($ns)->setUser($user); + session($ns)->setUser($user->toArray()); go(urlFor($route)); } else { ptption('guard', "Unknown id."); @@ -6125,6 +6129,7 @@ function guard($ns = 'web', $em = 'user') }); $class->macro('logByUser', function ($user, $route = 'home') use ($ns) { + $user = !is_array($user) ? $user->toArray() : $user; session($ns)->setUser($user); go(urlFor($route)); }); @@ -6154,6 +6159,13 @@ function guard($ns = 'web', $em = 'user') return $class; } + function be($user, $ns = 'web') + { + $user = !is_array($user) ? $user->toArray() : $user; + + session($ns)->setUser($user); + } + class OctoLab { public static function __callStatic($m, $a) diff --git a/lib/octalia.php b/lib/octalia.php index b498526..f2b1d0f 100644 --- a/lib/octalia.php +++ b/lib/octalia.php @@ -1041,7 +1041,7 @@ public function __call($m, $a) $method = 'scope' . ucfirst(Strings::camelize($m)); if (in_array($method, $methods)) { - return call_user_func_array([$entity, $method], $a); + return call_user_func_array([$entity, $method], array_merge([$this], $a)); } } @@ -1185,7 +1185,7 @@ public function __call($m, $a) $value = array_shift($a); } - return $this->where([$field, $op, $value]); + return $this->where($field, $op, $value); } if (fnmatch('getBy*', $m) && strlen($m) > 5) { @@ -1200,7 +1200,7 @@ public function __call($m, $a) $value = array_shift($a); } - return $this->where([$field, $op, $value]); + return $this->where($field, $op, $value); } if (fnmatch('where*', $m) && strlen($m) > 5) { @@ -1215,7 +1215,7 @@ public function __call($m, $a) $value = array_shift($a); } - return $this->where([$field, $op, $value]); + return $this->where($field, $op, $value); } if (fnmatch('by*', $m) && strlen($m) > 2) { diff --git a/lib/octaliasession.php b/lib/octaliasession.php new file mode 100644 index 0000000..5c6feb2 --- /dev/null +++ b/lib/octaliasession.php @@ -0,0 +1,98 @@ +db = em('systemSession'); + } + + public function open($savePath = null, $sessionName = null) + { + return true; + } + + public function close() + { + return true; + } + + public function read($session_id) + { + $row = $this->db->firstOrCreate([ + 'session_id' => $session_id + ]); + + if (!$row['data']) { + return ''; + } + + return $row['data']; + } + + public function write($session_id, $data) + { + $row = $this->db->firstOrCreate([ + 'session_id' => $session_id + ]); + + try { + $expiry_time = time() + SESSION_DURATION; + $row->data = $data; + $row->expiry = $expiry_time; + + $row->save(); + + return true; + } catch (\Exception $error) { + error_log($error); + + exit; + } + + return false; + } + + public function destroy($session_id) + { + try { + $this->db->firstOrCreate([ + 'session_id' => $session_id + ])->delete(); + + return true; + } catch (\Exception $error) { + error_log($error); + + exit; + } + + return false; + } + + public function gc($maxlifetime = SESSION_DURATION) + { + try { + $this->db->where('expiry', '<', time())->delete(); + return true; + } catch (\Exception $error) { + error_log($error); + + exit; + } + + return false; + } + } + /** + * $session_handler = new OctaliaSession(); + * session_set_save_handler($session_handler, true); + * session_name('MySessionName'); + * start_session(); + * session_regenerate_id(true); + */ diff --git a/lib/testcase.php b/lib/testcase.php new file mode 100644 index 0000000..e289fdb --- /dev/null +++ b/lib/testcase.php @@ -0,0 +1,46 @@ +app) { + $this->refreshApplication(); + } + } + + protected function refreshApplication() + { + putenv('APPLICATION_ENV=testing'); + + $this->app = $this->makeApplication(); + } + + protected function tearDown() + { + if ($this->app) { + $this->app = null; + } + + if (property_exists($this, 'serverVariables')) { + $this->serverVariables = []; + } + + if (class_exists('Mockery')) { + Mockery::close(); + } + } + }