CookieTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Illuminate\Tests\Integration\Cookie;
  3. use Illuminate\Contracts\Debug\ExceptionHandler;
  4. use Illuminate\Http\Response;
  5. use Illuminate\Session\NullSessionHandler;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Facades\Route;
  8. use Illuminate\Support\Facades\Session;
  9. use Illuminate\Support\Str;
  10. use Mockery;
  11. use Orchestra\Testbench\TestCase;
  12. class CookieTest extends TestCase
  13. {
  14. public function test_cookie_is_sent_back_with_proper_expire_time_when_should_expire_on_close()
  15. {
  16. $this->app['config']->set('session.expire_on_close', true);
  17. Route::get('/', function () {
  18. return 'hello world';
  19. })->middleware('web');
  20. $response = $this->get('/');
  21. $this->assertCount(2, $response->headers->getCookies());
  22. $this->assertEquals(0, ($response->headers->getCookies()[1])->getExpiresTime());
  23. }
  24. public function test_cookie_is_sent_back_with_proper_expire_time_with_respect_to_lifetime()
  25. {
  26. $this->app['config']->set('session.expire_on_close', false);
  27. $this->app['config']->set('session.lifetime', 1);
  28. Route::get('/', function () {
  29. return 'hello world';
  30. })->middleware('web');
  31. Carbon::setTestNow(Carbon::now());
  32. $response = $this->get('/');
  33. $this->assertCount(2, $response->headers->getCookies());
  34. $this->assertEquals(Carbon::now()->getTimestamp() + 60, ($response->headers->getCookies()[1])->getExpiresTime());
  35. }
  36. protected function getEnvironmentSetUp($app)
  37. {
  38. $app->instance(
  39. ExceptionHandler::class,
  40. $handler = Mockery::mock(ExceptionHandler::class)->shouldIgnoreMissing()
  41. );
  42. $handler->shouldReceive('render')->andReturn(new Response);
  43. $app['config']->set('app.key', Str::random(32));
  44. $app['config']->set('session.driver', 'fake-null');
  45. Session::extend('fake-null', function () {
  46. return new NullSessionHandler;
  47. });
  48. }
  49. }