public function testFacadeCookie() { $_COOKIE = ['name' => 'test', 'email' => '*****@*****.**']; // Get and add cookies $this->assertCount(2, CookieManager::get()->cookies); $addCookies = array_merge(CookieManager::get()->cookies, ['age' => new CookieContainer('age', '20'), 'ageFind' => new CookieContainer('ageFind', '20', time() + 3600), 'token' => 'un---token']); $this->assertCount(5, $addCookies); Cookie::setCookies($addCookies); $this->assertCount(4, CookieManager::get()->cookies); // Check cookie exist $this->assertTrue(Cookie::exist('name')); $this->assertTrue(Cookie::exist('email')); $this->assertFalse(Cookie::exist('age')); $this->assertTrue(Cookie::exist('ageFind')); $this->assertFalse(Cookie::exist('token')); // Check value get $this->assertInstanceOf(CookieContainer::class, Cookie::getCookie('name')); $this->assertEquals('*****@*****.**', Cookie::get('email')); $this->assertNull(Cookie::get('age')); $this->assertNull(Cookie::get('token')); // Check unset cookie $this->assertCount(4, CookieManager::get()->cookies); Cookie::remove('age'); $this->assertCount(3, CookieManager::get()->cookies); Cookie::remove('email'); $this->assertCount(3, CookieManager::get()->cookies); // Check set cookie $this->assertCount(3, CookieManager::get()->cookies); Cookie::setValue('age', '21'); $this->assertCount(4, CookieManager::get()->cookies); Cookie::set('email', '*****@*****.**'); $this->assertCount(4, CookieManager::get()->cookies); }
/** * Permet de tester la générations des headers pour la réponse */ public function testGenerateHeaders() { $time = time() + 3600; $cookies = CookieManager::get(); $cookies->set('name', 'salut', $time, 'salut.fr', '/test', true, true); $this->assertCount(1, $cookies->cookies); $this->assertEquals('salut', $cookies['name']->value); $headers = $cookies->generateHeaders(); $this->assertCount(1, $headers); $this->assertEquals('Set-Cookie: name=salut; expires=' . gmdate('D, d-M-Y H:i:s T', $time) . '; path=/test; domain=salut.fr; secure; httponly', $headers[0]); $cookies['name'] = 'bonjour'; $this->assertCount(1, $cookies->cookies); $this->assertNull($cookies['name']); $headers = $cookies->generateHeaders(); $this->assertCount(1, $headers); $this->assertEquals('Set-Cookie: name=bonjour; path=/; httponly', $headers[0]); }
/** * Redirection vers n'importe quel URL * * @param string $url */ public static function redirect($url) { $response = new ResponseContent(); $cookies = CookieManager::get()->generateHeaders(); foreach ($cookies as $cookie) { $response->header($cookie); } $response->redirect($url); }
/** * @return string */ public function run() { $result = $this->startMiddleware(); $resultView = $result->view->render(); $cookies = CookieManager::get()->generateHeaders(); foreach ($cookies as $cookie) { $result->header($cookie); } return $resultView; }