AbstractSessionHandlerTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
  11. use PHPUnit\Framework\SkippedTestSuiteError;
  12. use PHPUnit\Framework\TestCase;
  13. class AbstractSessionHandlerTest extends TestCase
  14. {
  15. private static $server;
  16. public static function setUpBeforeClass(): void
  17. {
  18. $spec = [
  19. 1 => ['file', '/dev/null', 'w'],
  20. 2 => ['file', '/dev/null', 'w'],
  21. ];
  22. if (!self::$server = @proc_open('exec '.\PHP_BINARY.' -S localhost:8053', $spec, $pipes, __DIR__.'/Fixtures')) {
  23. throw new SkippedTestSuiteError('PHP server unable to start.');
  24. }
  25. sleep(1);
  26. }
  27. public static function tearDownAfterClass(): void
  28. {
  29. if (self::$server) {
  30. proc_terminate(self::$server);
  31. proc_close(self::$server);
  32. }
  33. }
  34. /**
  35. * @dataProvider provideSession
  36. */
  37. public function testSession($fixture)
  38. {
  39. $context = ['http' => ['header' => "Cookie: sid=123abc\r\n"]];
  40. $context = stream_context_create($context);
  41. $result = file_get_contents(sprintf('http://localhost:8053/%s.php', $fixture), false, $context);
  42. $result = preg_replace_callback('/expires=[^;]++/', function ($m) { return str_replace('-', ' ', $m[0]); }, $result);
  43. $this->assertStringEqualsFile(__DIR__.sprintf('/Fixtures/%s.expected', $fixture), $result);
  44. }
  45. public static function provideSession()
  46. {
  47. foreach (glob(__DIR__.'/Fixtures/*.php') as $file) {
  48. yield [pathinfo($file, \PATHINFO_FILENAME)];
  49. }
  50. }
  51. }