From dfd1c3d8708678e5a8217e3fbbb7ac7f40e88db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Sat, 10 Jun 2017 10:04:18 +0200 Subject: [PATCH] testing stuff --- lib/auth.php | 62 ++++++++++++++++++++++++++++++++++++++++++++---- lib/testcase.php | 26 ++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/lib/auth.php b/lib/auth.php index d64fe4b..f97783b 100644 --- a/lib/auth.php +++ b/lib/auth.php @@ -3,19 +3,71 @@ class Auth { + public static function get($default = null, $ns = 'web') + { + if (session_id()) { + $user = session($ns)->getUser(actual('auth.user')); + } else { + $user = actual('auth.user'); + } + + if ($user) { + $user = !is_array($user) ? $user->toArray() : $user; + actual('auth.user', $user); + + return $user; + } + + return $default; + } + + public static function make($user = null, $ns = 'web') + { + $user = $user ?: static::get($user, $ns); + + if ($user) { + $user = !is_array($user) ? $user->toArray() : $user; + actual('auth.user', $user); + + if (session_id()) { + session($ns)->setUser($user); + } + } + } + + public static function is($ns = 'web') + { + 'octodummy' !== static::get('octodummy', $ns); + } + + public static function guest($ns = 'web') + { + 'octodummy' === static::get('octodummy', $ns); + } + public static function login($user, $ns = 'web') { - session($ns)->setUser($user); + $user = !is_array($user) ? $user->toArray() : $user; + + actual('auth.user', $user); + + if (session_id()) { + session($ns)->setUser($user); + } } public static function logout($ns = 'web') { - session($ns)->erase('user'); + if (session_id()) { + session($ns)->erase('user'); + } + + actual('auth.user', null); } public static function id($ns = 'web') { - $user = session($ns)->getUser(); + $user = static::get(null, $ns); if ($user) { return $user['id']; @@ -26,7 +78,7 @@ public static function id($ns = 'web') public static function email($ns = 'web') { - $user = session($ns)->getUser(); + $user = static::get(null, $ns);; if ($user) { return isAke($user, 'email', null); @@ -37,7 +89,7 @@ public static function email($ns = 'web') public static function user($model = true, $ns = 'web', $em = 'user') { - $user = session($ns)->getUser(); + $user = static::get(null, $ns);; if ($user && $model) { return em($em)->find((int) $user['id']); diff --git a/lib/testcase.php b/lib/testcase.php index 1201bc0..f49b695 100644 --- a/lib/testcase.php +++ b/lib/testcase.php @@ -14,6 +14,32 @@ public function em($entity, $new = true) return dbMemory($entity, $new); } + public function signIn($user = null) + { + $user = $user ? : $this->create(App\entities\UserEntity::class); + Auth::login($user); + } + + public function make($class, $count = 1, $args = [], $lng = 'fr_FR') + { + if (is_array($count)) { + $args = $count; + $count = 1; + } + + return $this->factory($class, $count, $lng)->raw($args); + } + + public function create($class, $count = 1, $args = [], $lng = 'fr_FR') + { + if (is_array($count)) { + $args = $count; + $count = 1; + } + + return $this->factory($class, $count, $lng)->store($args); + } + public function factory($class, $count = 1, $lng = 'fr_FR') { $model = maker($class, [], false);