Added Laravel project

This commit is contained in:
2017-09-17 00:35:10 +02:00
parent a3c19304d5
commit ecf605b8f5
6246 changed files with 682270 additions and 2 deletions

View File

@@ -0,0 +1,113 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\AcceptHeaderItem;
class AcceptHeaderItemTest extends TestCase
{
/**
* @dataProvider provideFromStringData
*/
public function testFromString($string, $value, array $attributes)
{
$item = AcceptHeaderItem::fromString($string);
$this->assertEquals($value, $item->getValue());
$this->assertEquals($attributes, $item->getAttributes());
}
public function provideFromStringData()
{
return array(
array(
'text/html',
'text/html', array(),
),
array(
'"this;should,not=matter"',
'this;should,not=matter', array(),
),
array(
"text/plain; charset=utf-8;param=\"this;should,not=matter\";\tfootnotes=true",
'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
),
array(
'"this;should,not=matter";charset=utf-8',
'this;should,not=matter', array('charset' => 'utf-8'),
),
);
}
/**
* @dataProvider provideToStringData
*/
public function testToString($value, array $attributes, $string)
{
$item = new AcceptHeaderItem($value, $attributes);
$this->assertEquals($string, (string) $item);
}
public function provideToStringData()
{
return array(
array(
'text/html', array(),
'text/html',
),
array(
'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
'text/plain;charset=utf-8;param="this;should,not=matter";footnotes=true',
),
);
}
public function testValue()
{
$item = new AcceptHeaderItem('value', array());
$this->assertEquals('value', $item->getValue());
$item->setValue('new value');
$this->assertEquals('new value', $item->getValue());
$item->setValue(1);
$this->assertEquals('1', $item->getValue());
}
public function testQuality()
{
$item = new AcceptHeaderItem('value', array());
$this->assertEquals(1.0, $item->getQuality());
$item->setQuality(0.5);
$this->assertEquals(0.5, $item->getQuality());
$item->setAttribute('q', 0.75);
$this->assertEquals(0.75, $item->getQuality());
$this->assertFalse($item->hasAttribute('q'));
}
public function testAttribute()
{
$item = new AcceptHeaderItem('value', array());
$this->assertEquals(array(), $item->getAttributes());
$this->assertFalse($item->hasAttribute('test'));
$this->assertNull($item->getAttribute('test'));
$this->assertEquals('default', $item->getAttribute('test', 'default'));
$item->setAttribute('test', 'value');
$this->assertEquals(array('test' => 'value'), $item->getAttributes());
$this->assertTrue($item->hasAttribute('test'));
$this->assertEquals('value', $item->getAttribute('test'));
$this->assertEquals('value', $item->getAttribute('test', 'default'));
}
}

View File

@@ -0,0 +1,103 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\AcceptHeader;
use Symfony\Component\HttpFoundation\AcceptHeaderItem;
class AcceptHeaderTest extends TestCase
{
public function testFirst()
{
$header = AcceptHeader::fromString('text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c');
$this->assertSame('text/html', $header->first()->getValue());
}
/**
* @dataProvider provideFromStringData
*/
public function testFromString($string, array $items)
{
$header = AcceptHeader::fromString($string);
$parsed = array_values($header->all());
// reset index since the fixtures don't have them set
foreach ($parsed as $item) {
$item->setIndex(0);
}
$this->assertEquals($items, $parsed);
}
public function provideFromStringData()
{
return array(
array('', array()),
array('gzip', array(new AcceptHeaderItem('gzip'))),
array('gzip,deflate,sdch', array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch'))),
array("gzip, deflate\t,sdch", array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch'))),
array('"this;should,not=matter"', array(new AcceptHeaderItem('this;should,not=matter'))),
);
}
/**
* @dataProvider provideToStringData
*/
public function testToString(array $items, $string)
{
$header = new AcceptHeader($items);
$this->assertEquals($string, (string) $header);
}
public function provideToStringData()
{
return array(
array(array(), ''),
array(array(new AcceptHeaderItem('gzip')), 'gzip'),
array(array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch')), 'gzip,deflate,sdch'),
array(array(new AcceptHeaderItem('this;should,not=matter')), 'this;should,not=matter'),
);
}
/**
* @dataProvider provideFilterData
*/
public function testFilter($string, $filter, array $values)
{
$header = AcceptHeader::fromString($string)->filter($filter);
$this->assertEquals($values, array_keys($header->all()));
}
public function provideFilterData()
{
return array(
array('fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', '/fr.*/', array('fr-FR', 'fr')),
);
}
/**
* @dataProvider provideSortingData
*/
public function testSorting($string, array $values)
{
$header = AcceptHeader::fromString($string);
$this->assertEquals($values, array_keys($header->all()));
}
public function provideSortingData()
{
return array(
'quality has priority' => array('*;q=0.3,ISO-8859-1,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
'order matters when q is equal' => array('*;q=0.3,ISO-8859-1;q=0.7,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
'order matters when q is equal2' => array('*;q=0.3,utf-8;q=0.7,ISO-8859-1;q=0.7', array('utf-8', 'ISO-8859-1', '*')),
);
}
}

View File

@@ -0,0 +1,93 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ApacheRequest;
class ApacheRequestTest extends TestCase
{
/**
* @dataProvider provideServerVars
*/
public function testUriMethods($server, $expectedRequestUri, $expectedBaseUrl, $expectedPathInfo)
{
$request = new ApacheRequest();
$request->server->replace($server);
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
$this->assertEquals($expectedBaseUrl, $request->getBaseUrl(), '->getBaseUrl() is correct');
$this->assertEquals($expectedPathInfo, $request->getPathInfo(), '->getPathInfo() is correct');
}
public function provideServerVars()
{
return array(
array(
array(
'REQUEST_URI' => '/foo/app_dev.php/bar',
'SCRIPT_NAME' => '/foo/app_dev.php',
'PATH_INFO' => '/bar',
),
'/foo/app_dev.php/bar',
'/foo/app_dev.php',
'/bar',
),
array(
array(
'REQUEST_URI' => '/foo/bar',
'SCRIPT_NAME' => '/foo/app_dev.php',
),
'/foo/bar',
'/foo',
'/bar',
),
array(
array(
'REQUEST_URI' => '/app_dev.php/foo/bar',
'SCRIPT_NAME' => '/app_dev.php',
'PATH_INFO' => '/foo/bar',
),
'/app_dev.php/foo/bar',
'/app_dev.php',
'/foo/bar',
),
array(
array(
'REQUEST_URI' => '/foo/bar',
'SCRIPT_NAME' => '/app_dev.php',
),
'/foo/bar',
'',
'/foo/bar',
),
array(
array(
'REQUEST_URI' => '/app_dev.php',
'SCRIPT_NAME' => '/app_dev.php',
),
'/app_dev.php',
'/app_dev.php',
'/',
),
array(
array(
'REQUEST_URI' => '/',
'SCRIPT_NAME' => '/app_dev.php',
),
'/',
'',
'/',
),
);
}
}

View File

@@ -0,0 +1,352 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Stream;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Tests\File\FakeFile;
class BinaryFileResponseTest extends ResponseTestCase
{
public function testConstruction()
{
$file = __DIR__.'/../README.md';
$response = new BinaryFileResponse($file, 404, array('X-Header' => 'Foo'), true, null, true, true);
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals('Foo', $response->headers->get('X-Header'));
$this->assertTrue($response->headers->has('ETag'));
$this->assertTrue($response->headers->has('Last-Modified'));
$this->assertFalse($response->headers->has('Content-Disposition'));
$response = BinaryFileResponse::create($file, 404, array(), true, ResponseHeaderBag::DISPOSITION_INLINE);
$this->assertEquals(404, $response->getStatusCode());
$this->assertFalse($response->headers->has('ETag'));
$this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
}
public function testConstructWithNonAsciiFilename()
{
touch(sys_get_temp_dir().'/fööö.html');
$response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, array(), true, 'attachment');
@unlink(sys_get_temp_dir().'/fööö.html');
$this->assertSame('fööö.html', $response->getFile()->getFilename());
}
/**
* @expectedException \LogicException
*/
public function testSetContent()
{
$response = new BinaryFileResponse(__FILE__);
$response->setContent('foo');
}
public function testGetContent()
{
$response = new BinaryFileResponse(__FILE__);
$this->assertFalse($response->getContent());
}
public function testSetContentDispositionGeneratesSafeFallbackFilename()
{
$response = new BinaryFileResponse(__FILE__);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
}
public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
{
$response = new BinaryFileResponse(__FILE__);
$iso88591EncodedFilename = utf8_decode('föö.html');
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
// the parameter filename* is invalid in this case (rawurldecode('f%F6%F6') does not provide a UTF-8 string but an ISO-8859-1 encoded one)
$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition'));
}
/**
* @dataProvider provideRanges
*/
public function testRequests($requestRange, $offset, $length, $responseRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
// do a request to get the ETag
$request = Request::create('/');
$response->prepare($request);
$etag = $response->headers->get('ETag');
// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('If-Range', $etag);
$request->headers->set('Range', $requestRange);
$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
fseek($file, $offset);
$data = fread($file, $length);
fclose($file);
$this->expectOutputString($data);
$response = clone $response;
$response->prepare($request);
$response->sendContent();
$this->assertEquals(206, $response->getStatusCode());
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
$this->assertSame($length, $response->headers->get('Content-Length'));
}
/**
* @dataProvider provideRanges
*/
public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
// do a request to get the LastModified
$request = Request::create('/');
$response->prepare($request);
$lastModified = $response->headers->get('Last-Modified');
// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('If-Range', $lastModified);
$request->headers->set('Range', $requestRange);
$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
fseek($file, $offset);
$data = fread($file, $length);
fclose($file);
$this->expectOutputString($data);
$response = clone $response;
$response->prepare($request);
$response->sendContent();
$this->assertEquals(206, $response->getStatusCode());
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
}
public function provideRanges()
{
return array(
array('bytes=1-4', 1, 4, 'bytes 1-4/35'),
array('bytes=-5', 30, 5, 'bytes 30-34/35'),
array('bytes=30-', 30, 5, 'bytes 30-34/35'),
array('bytes=30-30', 30, 1, 'bytes 30-30/35'),
array('bytes=30-34', 30, 5, 'bytes 30-34/35'),
);
}
public function testRangeRequestsWithoutLastModifiedDate()
{
// prevent auto last modified
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'), true, null, false, false);
// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
$request->headers->set('Range', 'bytes=1-4');
$this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
$response = clone $response;
$response->prepare($request);
$response->sendContent();
$this->assertEquals(200, $response->getStatusCode());
$this->assertNull($response->headers->get('Content-Range'));
}
/**
* @dataProvider provideFullFileRanges
*/
public function testFullFileRequests($requestRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('Range', $requestRange);
$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
$data = fread($file, 35);
fclose($file);
$this->expectOutputString($data);
$response = clone $response;
$response->prepare($request);
$response->sendContent();
$this->assertEquals(200, $response->getStatusCode());
}
public function provideFullFileRanges()
{
return array(
array('bytes=0-'),
array('bytes=0-34'),
array('bytes=-35'),
// Syntactical invalid range-request should also return the full resource
array('bytes=20-10'),
array('bytes=50-40'),
);
}
/**
* @dataProvider provideInvalidRanges
*/
public function testInvalidRequests($requestRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('Range', $requestRange);
$response = clone $response;
$response->prepare($request);
$response->sendContent();
$this->assertEquals(416, $response->getStatusCode());
$this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
}
public function provideInvalidRanges()
{
return array(
array('bytes=-40'),
array('bytes=30-40'),
);
}
/**
* @dataProvider provideXSendfileFiles
*/
public function testXSendfile($file)
{
$request = Request::create('/');
$request->headers->set('X-Sendfile-Type', 'X-Sendfile');
BinaryFileResponse::trustXSendfileTypeHeader();
$response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
$response->prepare($request);
$this->expectOutputString('');
$response->sendContent();
$this->assertContains('README.md', $response->headers->get('X-Sendfile'));
}
public function provideXSendfileFiles()
{
return array(
array(__DIR__.'/../README.md'),
array('file://'.__DIR__.'/../README.md'),
);
}
/**
* @dataProvider getSampleXAccelMappings
*/
public function testXAccelMapping($realpath, $mapping, $virtual)
{
$request = Request::create('/');
$request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
$request->headers->set('X-Accel-Mapping', $mapping);
$file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
BinaryFileResponse::trustXSendfileTypeHeader();
$response = new BinaryFileResponse($file, 200, array('Content-Type' => 'application/octet-stream'));
$reflection = new \ReflectionObject($response);
$property = $reflection->getProperty('file');
$property->setAccessible(true);
$property->setValue($response, $file);
$response->prepare($request);
$this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
}
public function testDeleteFileAfterSend()
{
$request = Request::create('/');
$path = __DIR__.'/File/Fixtures/to_delete';
touch($path);
$realPath = realpath($path);
$this->assertFileExists($realPath);
$response = new BinaryFileResponse($realPath, 200, array('Content-Type' => 'application/octet-stream'));
$response->deleteFileAfterSend(true);
$response->prepare($request);
$response->sendContent();
$this->assertFileNotExists($path);
}
public function testAcceptRangeOnUnsafeMethods()
{
$request = Request::create('/', 'POST');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
$response->prepare($request);
$this->assertEquals('none', $response->headers->get('Accept-Ranges'));
}
public function testAcceptRangeNotOverriden()
{
$request = Request::create('/', 'POST');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
$response->headers->set('Accept-Ranges', 'foo');
$response->prepare($request);
$this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
}
public function getSampleXAccelMappings()
{
return array(
array('/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'),
array('/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'),
);
}
public function testStream()
{
$request = Request::create('/');
$response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, array('Content-Type' => 'text/plain'));
$response->prepare($request);
$this->assertNull($response->headers->get('Content-Length'));
}
protected function provideResponse()
{
return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
}
public static function tearDownAfterClass()
{
$path = __DIR__.'/../Fixtures/to_delete';
if (file_exists($path)) {
@unlink($path);
}
}
}

View File

@@ -0,0 +1,223 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
/**
* CookieTest.
*
* @author John Kary <john@johnkary.net>
* @author Hugo Hamon <hugo.hamon@sensio.com>
*
* @group time-sensitive
*/
class CookieTest extends TestCase
{
public function invalidNames()
{
return array(
array(''),
array(',MyName'),
array(';MyName'),
array(' MyName'),
array("\tMyName"),
array("\rMyName"),
array("\nMyName"),
array("\013MyName"),
array("\014MyName"),
);
}
/**
* @dataProvider invalidNames
* @expectedException \InvalidArgumentException
*/
public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
{
new Cookie($name);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidExpiration()
{
new Cookie('MyCookie', 'foo', 'bar');
}
public function testNegativeExpirationIsNotPossible()
{
$cookie = new Cookie('foo', 'bar', -100);
$this->assertSame(0, $cookie->getExpiresTime());
}
public function testGetValue()
{
$value = 'MyValue';
$cookie = new Cookie('MyCookie', $value);
$this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
}
public function testGetPath()
{
$cookie = new Cookie('foo', 'bar');
$this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
}
public function testGetExpiresTime()
{
$cookie = new Cookie('foo', 'bar');
$this->assertEquals(0, $cookie->getExpiresTime(), '->getExpiresTime() returns the default expire date');
$cookie = new Cookie('foo', 'bar', $expire = time() + 3600);
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
}
public function testGetExpiresTimeIsCastToInt()
{
$cookie = new Cookie('foo', 'bar', 3600.9);
$this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
}
public function testConstructorWithDateTime()
{
$expire = new \DateTime();
$cookie = new Cookie('foo', 'bar', $expire);
$this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
}
/**
* @requires PHP 5.5
*/
public function testConstructorWithDateTimeImmutable()
{
$expire = new \DateTimeImmutable();
$cookie = new Cookie('foo', 'bar', $expire);
$this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
}
public function testGetExpiresTimeWithStringValue()
{
$value = '+1 day';
$cookie = new Cookie('foo', 'bar', $value);
$expire = strtotime($value);
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
}
public function testGetDomain()
{
$cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com');
$this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
}
public function testIsSecure()
{
$cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', true);
$this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
}
public function testIsHttpOnly()
{
$cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', false, true);
$this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
}
public function testCookieIsNotCleared()
{
$cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
$this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
}
public function testCookieIsCleared()
{
$cookie = new Cookie('foo', 'bar', time() - 20);
$this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
}
public function testToString()
{
$cookie = new Cookie('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
$cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
$this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; max-age='.($expire - time()).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
$cookie = new Cookie('foo', 'bar', 0, '/', '');
$this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);
}
public function testRawCookie()
{
$cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
$this->assertFalse($cookie->isRaw());
$this->assertEquals('foo=b%20a%20r; path=/', (string) $cookie);
$cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
$this->assertTrue($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
}
public function testGetMaxAge()
{
$cookie = new Cookie('foo', 'bar');
$this->assertEquals(0, $cookie->getMaxAge());
$cookie = new Cookie('foo', 'bar', $expire = time() + 100);
$this->assertEquals($expire - time(), $cookie->getMaxAge());
$cookie = new Cookie('foo', 'bar', $expire = time() - 100);
$this->assertEquals($expire - time(), $cookie->getMaxAge());
}
public function testFromString()
{
$cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
$this->assertEquals(new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, true), $cookie);
$cookie = Cookie::fromString('foo=bar', true);
$this->assertEquals(new Cookie('foo', 'bar', 0, '/', null, false, false), $cookie);
}
public function testFromStringWithHttpOnly()
{
$cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
$this->assertTrue($cookie->isHttpOnly());
$cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure');
$this->assertFalse($cookie->isHttpOnly());
}
public function testSameSiteAttributeIsCaseInsensitive()
{
$cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, 'Lax');
$this->assertEquals('lax', $cookie->getSameSite());
}
}

View File

@@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpFoundation\ExpressionRequestMatcher;
use Symfony\Component\HttpFoundation\Request;
class ExpressionRequestMatcherTest extends TestCase
{
/**
* @expectedException \LogicException
*/
public function testWhenNoExpressionIsSet()
{
$expressionRequestMatcher = new ExpressionRequestMatcher();
$expressionRequestMatcher->matches(new Request());
}
/**
* @dataProvider provideExpressions
*/
public function testMatchesWhenParentMatchesIsTrue($expression, $expected)
{
$request = Request::create('/foo');
$expressionRequestMatcher = new ExpressionRequestMatcher();
$expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression);
$this->assertSame($expected, $expressionRequestMatcher->matches($request));
}
/**
* @dataProvider provideExpressions
*/
public function testMatchesWhenParentMatchesIsFalse($expression)
{
$request = Request::create('/foo');
$request->attributes->set('foo', 'foo');
$expressionRequestMatcher = new ExpressionRequestMatcher();
$expressionRequestMatcher->matchAttribute('foo', 'bar');
$expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression);
$this->assertFalse($expressionRequestMatcher->matches($request));
}
public function provideExpressions()
{
return array(
array('request.getMethod() == method', true),
array('request.getPathInfo() == path', true),
array('request.getHost() == host', true),
array('request.getClientIp() == ip', true),
array('request.attributes.all() == attributes', true),
array('request.getMethod() == method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', true),
array('request.getMethod() != method', false),
array('request.getMethod() != method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', false),
);
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\File;
use Symfony\Component\HttpFoundation\File\File as OrigFile;
class FakeFile extends OrigFile
{
private $realpath;
public function __construct($realpath, $path)
{
$this->realpath = $realpath;
parent::__construct($path, false);
}
public function isReadable()
{
return true;
}
public function getRealpath()
{
return $this->realpath;
}
public function getSize()
{
return 42;
}
public function getMTime()
{
return time();
}
}

View File

@@ -0,0 +1,180 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\File;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
class FileTest extends TestCase
{
protected $file;
public function testGetMimeTypeUsesMimeTypeGuessers()
{
$file = new File(__DIR__.'/Fixtures/test.gif');
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
MimeTypeGuesser::getInstance()->register($guesser);
$this->assertEquals('image/gif', $file->getMimeType());
}
public function testGuessExtensionWithoutGuesser()
{
$file = new File(__DIR__.'/Fixtures/directory/.empty');
$this->assertNull($file->guessExtension());
}
public function testGuessExtensionIsBasedOnMimeType()
{
$file = new File(__DIR__.'/Fixtures/test');
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
MimeTypeGuesser::getInstance()->register($guesser);
$this->assertEquals('gif', $file->guessExtension());
}
/**
* @requires extension fileinfo
*/
public function testGuessExtensionWithReset()
{
$file = new File(__DIR__.'/Fixtures/other-file.example');
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
MimeTypeGuesser::getInstance()->register($guesser);
$this->assertEquals('gif', $file->guessExtension());
MimeTypeGuesser::reset();
$this->assertNull($file->guessExtension());
}
public function testConstructWhenFileNotExists()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
new File(__DIR__.'/Fixtures/not_here');
}
public function testMove()
{
$path = __DIR__.'/Fixtures/test.copy.gif';
$targetDir = __DIR__.'/Fixtures/directory';
$targetPath = $targetDir.'/test.copy.gif';
@unlink($path);
@unlink($targetPath);
copy(__DIR__.'/Fixtures/test.gif', $path);
$file = new File($path);
$movedFile = $file->move($targetDir);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
$this->assertFileExists($targetPath);
$this->assertFileNotExists($path);
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
@unlink($targetPath);
}
public function testMoveWithNewName()
{
$path = __DIR__.'/Fixtures/test.copy.gif';
$targetDir = __DIR__.'/Fixtures/directory';
$targetPath = $targetDir.'/test.newname.gif';
@unlink($path);
@unlink($targetPath);
copy(__DIR__.'/Fixtures/test.gif', $path);
$file = new File($path);
$movedFile = $file->move($targetDir, 'test.newname.gif');
$this->assertFileExists($targetPath);
$this->assertFileNotExists($path);
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
@unlink($targetPath);
}
public function getFilenameFixtures()
{
return array(
array('original.gif', 'original.gif'),
array('..\\..\\original.gif', 'original.gif'),
array('../../original.gif', 'original.gif'),
array(айлfile.gif', айлfile.gif'),
array('..\\..\\файлfile.gif', айлfile.gif'),
array('../../файлfile.gif', айлfile.gif'),
);
}
/**
* @dataProvider getFilenameFixtures
*/
public function testMoveWithNonLatinName($filename, $sanitizedFilename)
{
$path = __DIR__.'/Fixtures/'.$sanitizedFilename;
$targetDir = __DIR__.'/Fixtures/directory/';
$targetPath = $targetDir.$sanitizedFilename;
@unlink($path);
@unlink($targetPath);
copy(__DIR__.'/Fixtures/test.gif', $path);
$file = new File($path);
$movedFile = $file->move($targetDir, $filename);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
$this->assertFileExists($targetPath);
$this->assertFileNotExists($path);
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
@unlink($targetPath);
}
public function testMoveToAnUnexistentDirectory()
{
$sourcePath = __DIR__.'/Fixtures/test.copy.gif';
$targetDir = __DIR__.'/Fixtures/directory/sub';
$targetPath = $targetDir.'/test.copy.gif';
@unlink($sourcePath);
@unlink($targetPath);
@rmdir($targetDir);
copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
$file = new File($sourcePath);
$movedFile = $file->move($targetDir);
$this->assertFileExists($targetPath);
$this->assertFileNotExists($sourcePath);
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
@unlink($sourcePath);
@unlink($targetPath);
@rmdir($targetDir);
}
protected function createMockGuesser($path, $mimeType)
{
$guesser = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface')->getMock();
$guesser
->expects($this->once())
->method('guess')
->with($this->equalTo($path))
->will($this->returnValue($mimeType))
;
return $guesser;
}
}

View File

@@ -0,0 +1 @@
f

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 B

View File

@@ -0,0 +1,90 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\File\MimeType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
/**
* @requires extension fileinfo
*/
class MimeTypeTest extends TestCase
{
protected $path;
public function testGuessImageWithoutExtension()
{
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
}
public function testGuessImageWithDirectory()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/directory');
}
public function testGuessImageWithFileBinaryMimeTypeGuesser()
{
$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new FileBinaryMimeTypeGuesser());
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
}
public function testGuessImageWithKnownExtension()
{
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
}
public function testGuessFileWithUnknownExtension()
{
$this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
}
public function testGuessWithIncorrectPath()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
}
public function testGuessWithNonReadablePath()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Can not verify chmod operations on Windows');
}
if (!getenv('USER') || 'root' === getenv('USER')) {
$this->markTestSkipped('This test will fail if run under superuser');
}
$path = __DIR__.'/../Fixtures/to_delete';
touch($path);
@chmod($path, 0333);
if (substr(sprintf('%o', fileperms($path)), -4) == '0333') {
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
MimeTypeGuesser::getInstance()->guess($path);
} else {
$this->markTestSkipped('Can not verify chmod operations, change of file permissions failed');
}
}
public static function tearDownAfterClass()
{
$path = __DIR__.'/../Fixtures/to_delete';
if (file_exists($path)) {
@chmod($path, 0666);
@unlink($path);
}
}
}

View File

@@ -0,0 +1,273 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\File;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class UploadedFileTest extends TestCase
{
protected function setUp()
{
if (!ini_get('file_uploads')) {
$this->markTestSkipped('file_uploads is disabled in php.ini');
}
}
public function testConstructWhenFileNotExists()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
new UploadedFile(
__DIR__.'/Fixtures/not_here',
'original.gif',
null
);
}
public function testFileUploadsWithNoMimeType()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/test.gif'),
UPLOAD_ERR_OK
);
$this->assertEquals('application/octet-stream', $file->getClientMimeType());
if (extension_loaded('fileinfo')) {
$this->assertEquals('image/gif', $file->getMimeType());
}
}
public function testFileUploadsWithUnknownMimeType()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/.unknownextension',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/.unknownextension'),
UPLOAD_ERR_OK
);
$this->assertEquals('application/octet-stream', $file->getClientMimeType());
}
public function testGuessClientExtension()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals('gif', $file->guessClientExtension());
}
public function testGuessClientExtensionWithIncorrectMimeType()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/jpeg',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals('jpeg', $file->guessClientExtension());
}
public function testErrorIsOkByDefault()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals(UPLOAD_ERR_OK, $file->getError());
}
public function testGetClientOriginalName()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals('original.gif', $file->getClientOriginalName());
}
public function testGetClientOriginalExtension()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals('gif', $file->getClientOriginalExtension());
}
/**
* @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
*/
public function testMoveLocalFileIsNotAllowed()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
UPLOAD_ERR_OK
);
$movedFile = $file->move(__DIR__.'/Fixtures/directory');
}
public function testMoveLocalFileIsAllowedInTestMode()
{
$path = __DIR__.'/Fixtures/test.copy.gif';
$targetDir = __DIR__.'/Fixtures/directory';
$targetPath = $targetDir.'/test.copy.gif';
@unlink($path);
@unlink($targetPath);
copy(__DIR__.'/Fixtures/test.gif', $path);
$file = new UploadedFile(
$path,
'original.gif',
'image/gif',
filesize($path),
UPLOAD_ERR_OK,
true
);
$movedFile = $file->move(__DIR__.'/Fixtures/directory');
$this->assertFileExists($targetPath);
$this->assertFileNotExists($path);
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
@unlink($targetPath);
}
public function testGetClientOriginalNameSanitizeFilename()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'../../original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals('original.gif', $file->getClientOriginalName());
}
public function testGetSize()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
$file = new UploadedFile(
__DIR__.'/Fixtures/test',
'original.gif',
'image/gif'
);
$this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
}
public function testGetExtension()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
null
);
$this->assertEquals('gif', $file->getExtension());
}
public function testIsValid()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/test.gif'),
UPLOAD_ERR_OK,
true
);
$this->assertTrue($file->isValid());
}
/**
* @dataProvider uploadedFileErrorProvider
*/
public function testIsInvalidOnUploadError($error)
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/test.gif'),
$error
);
$this->assertFalse($file->isValid());
}
public function uploadedFileErrorProvider()
{
return array(
array(UPLOAD_ERR_INI_SIZE),
array(UPLOAD_ERR_FORM_SIZE),
array(UPLOAD_ERR_PARTIAL),
array(UPLOAD_ERR_NO_TMP_DIR),
array(UPLOAD_ERR_EXTENSION),
);
}
public function testIsInvalidIfNotHttpUpload()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/test.gif'),
UPLOAD_ERR_OK
);
$this->assertFalse($file->isValid());
}
}

View File

@@ -0,0 +1,149 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\FileBag;
/**
* FileBagTest.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
class FileBagTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testFileMustBeAnArrayOrUploadedFile()
{
new FileBag(array('file' => 'foo'));
}
public function testShouldConvertsUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$bag = new FileBag(array('file' => array(
'name' => basename($tmpFile),
'type' => 'text/plain',
'tmp_name' => $tmpFile,
'error' => 0,
'size' => 100,
)));
$this->assertEquals($file, $bag->get('file'));
}
public function testShouldSetEmptyUploadedFilesToNull()
{
$bag = new FileBag(array('file' => array(
'name' => '',
'type' => '',
'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE,
'size' => 0,
)));
$this->assertNull($bag->get('file'));
}
public function testShouldConvertUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$bag = new FileBag(array(
'child' => array(
'name' => array(
'file' => basename($tmpFile),
),
'type' => array(
'file' => 'text/plain',
),
'tmp_name' => array(
'file' => $tmpFile,
),
'error' => array(
'file' => 0,
),
'size' => array(
'file' => 100,
),
),
));
$files = $bag->all();
$this->assertEquals($file, $files['child']['file']);
}
public function testShouldConvertNestedUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$bag = new FileBag(array(
'child' => array(
'name' => array(
'sub' => array('file' => basename($tmpFile)),
),
'type' => array(
'sub' => array('file' => 'text/plain'),
),
'tmp_name' => array(
'sub' => array('file' => $tmpFile),
),
'error' => array(
'sub' => array('file' => 0),
),
'size' => array(
'sub' => array('file' => 100),
),
),
));
$files = $bag->all();
$this->assertEquals($file, $files['child']['sub']['file']);
}
public function testShouldNotConvertNestedUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$bag = new FileBag(array('image' => array('file' => $file)));
$files = $bag->all();
$this->assertEquals($file, $files['image']['file']);
}
protected function createTempFile()
{
return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
}
protected function setUp()
{
mkdir(sys_get_temp_dir().'/form_test', 0777, true);
}
protected function tearDown()
{
foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
unlink($file);
}
rmdir(sys_get_temp_dir().'/form_test');
}
}

View File

@@ -0,0 +1,205 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\HeaderBag;
class HeaderBagTest extends TestCase
{
public function testConstructor()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$this->assertTrue($bag->has('foo'));
}
public function testToStringNull()
{
$bag = new HeaderBag();
$this->assertEquals('', $bag->__toString());
}
public function testToStringNotNull()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$this->assertEquals("Foo: bar\r\n", $bag->__toString());
}
public function testKeys()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$keys = $bag->keys();
$this->assertEquals('foo', $keys[0]);
}
public function testGetDate()
{
$bag = new HeaderBag(array('foo' => 'Tue, 4 Sep 2012 20:00:00 +0200'));
$headerDate = $bag->getDate('foo');
$this->assertInstanceOf('DateTime', $headerDate);
}
/**
* @expectedException \RuntimeException
*/
public function testGetDateException()
{
$bag = new HeaderBag(array('foo' => 'Tue'));
$headerDate = $bag->getDate('foo');
}
public function testGetCacheControlHeader()
{
$bag = new HeaderBag();
$bag->addCacheControlDirective('public', '#a');
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertEquals('#a', $bag->getCacheControlDirective('public'));
}
public function testAll()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
$bag = new HeaderBag(array('FOO' => 'BAR'));
$this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
}
public function testReplace()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$bag->replace(array('NOPE' => 'BAR'));
$this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
$this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
}
public function testGet()
{
$bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
$this->assertEquals('bar', $bag->get('foo'), '->get return current value');
$this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive');
$this->assertEquals(array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
// defaults
$this->assertNull($bag->get('none'), '->get unknown values returns null');
$this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
$this->assertEquals(array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
$bag->set('foo', 'bor', false);
$this->assertEquals('bar', $bag->get('foo'), '->get return first value');
$this->assertEquals(array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
}
public function testSetAssociativeArray()
{
$bag = new HeaderBag();
$bag->set('foo', array('bad-assoc-index' => 'value'));
$this->assertSame('value', $bag->get('foo'));
$this->assertEquals(array('value'), $bag->get('foo', 'nope', false), 'assoc indices of multi-valued headers are ignored');
}
public function testContains()
{
$bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
$this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
$this->assertTrue($bag->contains('fuzz', 'bizz'), '->contains second value');
$this->assertFalse($bag->contains('nope', 'nope'), '->contains unknown value');
$this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
// Multiple values
$bag->set('foo', 'bor', false);
$this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
$this->assertTrue($bag->contains('foo', 'bor'), '->contains second value');
$this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
}
public function testCacheControlDirectiveAccessors()
{
$bag = new HeaderBag();
$bag->addCacheControlDirective('public');
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertTrue($bag->getCacheControlDirective('public'));
$this->assertEquals('public', $bag->get('cache-control'));
$bag->addCacheControlDirective('max-age', 10);
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
$this->assertEquals('max-age=10, public', $bag->get('cache-control'));
$bag->removeCacheControlDirective('max-age');
$this->assertFalse($bag->hasCacheControlDirective('max-age'));
}
public function testCacheControlDirectiveParsing()
{
$bag = new HeaderBag(array('cache-control' => 'public, max-age=10'));
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertTrue($bag->getCacheControlDirective('public'));
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
$bag->addCacheControlDirective('s-maxage', 100);
$this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control'));
}
public function testCacheControlDirectiveParsingQuotedZero()
{
$bag = new HeaderBag(array('cache-control' => 'max-age="0"'));
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(0, $bag->getCacheControlDirective('max-age'));
}
public function testCacheControlDirectiveOverrideWithReplace()
{
$bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));
$bag->replace(array('cache-control' => 'public, max-age=10'));
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertTrue($bag->getCacheControlDirective('public'));
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
}
public function testCacheControlClone()
{
$headers = array('foo' => 'bar');
$bag1 = new HeaderBag($headers);
$bag2 = new HeaderBag($bag1->all());
$this->assertEquals($bag1->all(), $bag2->all());
}
public function testGetIterator()
{
$headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm');
$headerBag = new HeaderBag($headers);
$i = 0;
foreach ($headerBag as $key => $val) {
++$i;
$this->assertEquals(array($headers[$key]), $val);
}
$this->assertEquals(count($headers), $i);
}
public function testCount()
{
$headers = array('foo' => 'bar', 'HELLO' => 'WORLD');
$headerBag = new HeaderBag($headers);
$this->assertEquals(count($headers), count($headerBag));
}
}

View File

@@ -0,0 +1,85 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\IpUtils;
class IpUtilsTest extends TestCase
{
/**
* @dataProvider getIpv4Data
*/
public function testIpv4($matches, $remoteAddr, $cidr)
{
$this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
}
public function getIpv4Data()
{
return array(
array(true, '192.168.1.1', '192.168.1.1'),
array(true, '192.168.1.1', '192.168.1.1/1'),
array(true, '192.168.1.1', '192.168.1.0/24'),
array(false, '192.168.1.1', '1.2.3.4/1'),
array(false, '192.168.1.1', '192.168.1.1/33'), // invalid subnet
array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
array(true, '1.2.3.4', '0.0.0.0/0'),
array(true, '1.2.3.4', '192.168.1.0/0'),
array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
array(false, 'an_invalid_ip', '192.168.1.0/24'),
);
}
/**
* @dataProvider getIpv6Data
*/
public function testIpv6($matches, $remoteAddr, $cidr)
{
if (!defined('AF_INET6')) {
$this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
}
$this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
}
public function getIpv6Data()
{
return array(
array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
array(true, '0:0:0:0:0:0:0:1', '::1'),
array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
);
}
/**
* @expectedException \RuntimeException
* @requires extension sockets
*/
public function testAnIpv6WithOptionDisabledIpv6()
{
if (defined('AF_INET6')) {
$this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
}
IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
}
}

View File

@@ -0,0 +1,257 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
class JsonResponseTest extends TestCase
{
public function testConstructorEmptyCreatesJsonObject()
{
$response = new JsonResponse();
$this->assertSame('{}', $response->getContent());
}
public function testConstructorWithArrayCreatesJsonArray()
{
$response = new JsonResponse(array(0, 1, 2, 3));
$this->assertSame('[0,1,2,3]', $response->getContent());
}
public function testConstructorWithAssocArrayCreatesJsonObject()
{
$response = new JsonResponse(array('foo' => 'bar'));
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
public function testConstructorWithSimpleTypes()
{
$response = new JsonResponse('foo');
$this->assertSame('"foo"', $response->getContent());
$response = new JsonResponse(0);
$this->assertSame('0', $response->getContent());
$response = new JsonResponse(0.1);
$this->assertSame('0.1', $response->getContent());
$response = new JsonResponse(true);
$this->assertSame('true', $response->getContent());
}
public function testConstructorWithCustomStatus()
{
$response = new JsonResponse(array(), 202);
$this->assertSame(202, $response->getStatusCode());
}
public function testConstructorAddsContentTypeHeader()
{
$response = new JsonResponse();
$this->assertSame('application/json', $response->headers->get('Content-Type'));
}
public function testConstructorWithCustomHeaders()
{
$response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
$this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertSame('foo', $response->headers->get('ETag'));
}
public function testConstructorWithCustomContentType()
{
$headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
$response = new JsonResponse(array(), 200, $headers);
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
}
public function testSetJson()
{
$response = new JsonResponse('1', 200, array(), true);
$this->assertEquals('1', $response->getContent());
$response = new JsonResponse('[1]', 200, array(), true);
$this->assertEquals('[1]', $response->getContent());
$response = new JsonResponse(null, 200, array());
$response->setJson('true');
$this->assertEquals('true', $response->getContent());
}
public function testCreate()
{
$response = JsonResponse::create(array('foo' => 'bar'), 204);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
$this->assertEquals(204, $response->getStatusCode());
}
public function testStaticCreateEmptyJsonObject()
{
$response = JsonResponse::create();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{}', $response->getContent());
}
public function testStaticCreateJsonArray()
{
$response = JsonResponse::create(array(0, 1, 2, 3));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('[0,1,2,3]', $response->getContent());
}
public function testStaticCreateJsonObject()
{
$response = JsonResponse::create(array('foo' => 'bar'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
public function testStaticCreateWithSimpleTypes()
{
$response = JsonResponse::create('foo');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('"foo"', $response->getContent());
$response = JsonResponse::create(0);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('0', $response->getContent());
$response = JsonResponse::create(0.1);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('0.1', $response->getContent());
$response = JsonResponse::create(true);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('true', $response->getContent());
}
public function testStaticCreateWithCustomStatus()
{
$response = JsonResponse::create(array(), 202);
$this->assertSame(202, $response->getStatusCode());
}
public function testStaticCreateAddsContentTypeHeader()
{
$response = JsonResponse::create();
$this->assertSame('application/json', $response->headers->get('Content-Type'));
}
public function testStaticCreateWithCustomHeaders()
{
$response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
$this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertSame('foo', $response->headers->get('ETag'));
}
public function testStaticCreateWithCustomContentType()
{
$headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
$response = JsonResponse::create(array(), 200, $headers);
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
}
public function testSetCallback()
{
$response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
$this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
$this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
}
public function testJsonEncodeFlags()
{
$response = new JsonResponse('<>\'&"');
$this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
}
public function testGetEncodingOptions()
{
$response = new JsonResponse();
$this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
}
public function testSetEncodingOptions()
{
$response = new JsonResponse();
$response->setData(array(array(1, 2, 3)));
$this->assertEquals('[[1,2,3]]', $response->getContent());
$response->setEncodingOptions(JSON_FORCE_OBJECT);
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
}
public function testItAcceptsJsonAsString()
{
$response = JsonResponse::fromJsonString('{"foo":"bar"}');
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testSetCallbackInvalidIdentifier()
{
$response = new JsonResponse('foo');
$response->setCallback('+invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testSetContent()
{
JsonResponse::create("\xB1\x31");
}
/**
* @expectedException \Exception
* @expectedExceptionMessage This error is expected
*/
public function testSetContentJsonSerializeError()
{
if (!interface_exists('JsonSerializable', false)) {
$this->markTestSkipped('JsonSerializable is required.');
}
$serializable = new JsonSerializableObject();
JsonResponse::create($serializable);
}
public function testSetComplexCallback()
{
$response = JsonResponse::create(array('foo' => 'bar'));
$response->setCallback('ಠ_ಠ["foo"].bar[0]');
$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
}
}
if (interface_exists('JsonSerializable', false)) {
class JsonSerializableObject implements \JsonSerializable
{
public function jsonSerialize()
{
throw new \Exception('This error is expected');
}
}
}

View File

@@ -0,0 +1,194 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
class ParameterBagTest extends TestCase
{
public function testConstructor()
{
$this->testAll();
}
public function testAll()
{
$bag = new ParameterBag(array('foo' => 'bar'));
$this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
}
public function testKeys()
{
$bag = new ParameterBag(array('foo' => 'bar'));
$this->assertEquals(array('foo'), $bag->keys());
}
public function testAdd()
{
$bag = new ParameterBag(array('foo' => 'bar'));
$bag->add(array('bar' => 'bas'));
$this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
}
public function testRemove()
{
$bag = new ParameterBag(array('foo' => 'bar'));
$bag->add(array('bar' => 'bas'));
$this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
$bag->remove('bar');
$this->assertEquals(array('foo' => 'bar'), $bag->all());
}
public function testReplace()
{
$bag = new ParameterBag(array('foo' => 'bar'));
$bag->replace(array('FOO' => 'BAR'));
$this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument');
$this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
}
public function testGet()
{
$bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
$this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
$this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
}
public function testGetDoesNotUseDeepByDefault()
{
$bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
$this->assertNull($bag->get('foo[bar]'));
}
public function testSet()
{
$bag = new ParameterBag(array());
$bag->set('foo', 'bar');
$this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
$bag->set('foo', 'baz');
$this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
}
public function testHas()
{
$bag = new ParameterBag(array('foo' => 'bar'));
$this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
$this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
}
public function testGetAlpha()
{
$bag = new ParameterBag(array('word' => 'foo_BAR_012'));
$this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
$this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
}
public function testGetAlnum()
{
$bag = new ParameterBag(array('word' => 'foo_BAR_012'));
$this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
$this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
}
public function testGetDigits()
{
$bag = new ParameterBag(array('word' => 'foo_BAR_012'));
$this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
$this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
}
public function testGetInt()
{
$bag = new ParameterBag(array('digits' => '0123'));
$this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
$this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
}
public function testFilter()
{
$bag = new ParameterBag(array(
'digits' => '0123ab',
'email' => 'example@example.com',
'url' => 'http://example.com/foo',
'dec' => '256',
'hex' => '0x100',
'array' => array('bang'),
));
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
$this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
$this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
// This test is repeated for code-coverage
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
$this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff),
)), '->filter() gets a value of parameter as integer between boundaries');
$this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff),
)), '->filter() gets a value of parameter as integer between boundaries');
$this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
}
public function testGetIterator()
{
$parameters = array('foo' => 'bar', 'hello' => 'world');
$bag = new ParameterBag($parameters);
$i = 0;
foreach ($bag as $key => $val) {
++$i;
$this->assertEquals($parameters[$key], $val);
}
$this->assertEquals(count($parameters), $i);
}
public function testCount()
{
$parameters = array('foo' => 'bar', 'hello' => 'world');
$bag = new ParameterBag($parameters);
$this->assertEquals(count($parameters), count($bag));
}
public function testGetBoolean()
{
$parameters = array('string_true' => 'true', 'string_false' => 'false');
$bag = new ParameterBag($parameters);
$this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
$this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
$this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
}
}

View File

@@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
class RedirectResponseTest extends TestCase
{
public function testGenerateMetaRedirect()
{
$response = new RedirectResponse('foo.bar');
$this->assertEquals(1, preg_match(
'#<meta http-equiv="refresh" content="\d+;url=foo\.bar" />#',
preg_replace(array('/\s+/', '/\'/'), array(' ', '"'), $response->getContent())
));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testRedirectResponseConstructorNullUrl()
{
$response = new RedirectResponse(null);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testRedirectResponseConstructorWrongStatusCode()
{
$response = new RedirectResponse('foo.bar', 404);
}
public function testGenerateLocationHeader()
{
$response = new RedirectResponse('foo.bar');
$this->assertTrue($response->headers->has('Location'));
$this->assertEquals('foo.bar', $response->headers->get('Location'));
}
public function testGetTargetUrl()
{
$response = new RedirectResponse('foo.bar');
$this->assertEquals('foo.bar', $response->getTargetUrl());
}
public function testSetTargetUrl()
{
$response = new RedirectResponse('foo.bar');
$response->setTargetUrl('baz.beep');
$this->assertEquals('baz.beep', $response->getTargetUrl());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testSetTargetUrlNull()
{
$response = new RedirectResponse('foo.bar');
$response->setTargetUrl(null);
}
public function testCreate()
{
$response = RedirectResponse::create('foo', 301);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
}
public function testCacheHeaders()
{
$response = new RedirectResponse('foo.bar', 301);
$this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));
$response = new RedirectResponse('foo.bar', 301, array('cache-control' => 'max-age=86400'));
$this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));
$this->assertTrue($response->headers->hasCacheControlDirective('max-age'));
$response = new RedirectResponse('foo.bar', 302);
$this->assertTrue($response->headers->hasCacheControlDirective('no-cache'));
}
}

View File

@@ -0,0 +1,151 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\Request;
class RequestMatcherTest extends TestCase
{
/**
* @dataProvider getMethodData
*/
public function testMethod($requestMethod, $matcherMethod, $isMatch)
{
$matcher = new RequestMatcher();
$matcher->matchMethod($matcherMethod);
$request = Request::create('', $requestMethod);
$this->assertSame($isMatch, $matcher->matches($request));
$matcher = new RequestMatcher(null, null, $matcherMethod);
$request = Request::create('', $requestMethod);
$this->assertSame($isMatch, $matcher->matches($request));
}
public function getMethodData()
{
return array(
array('get', 'get', true),
array('get', array('get', 'post'), true),
array('get', 'post', false),
array('get', 'GET', true),
array('get', array('GET', 'POST'), true),
array('get', 'POST', false),
);
}
public function testScheme()
{
$httpRequest = $request = $request = Request::create('');
$httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on'));
$matcher = new RequestMatcher();
$matcher->matchScheme('https');
$this->assertFalse($matcher->matches($httpRequest));
$this->assertTrue($matcher->matches($httpsRequest));
$matcher->matchScheme('http');
$this->assertFalse($matcher->matches($httpsRequest));
$this->assertTrue($matcher->matches($httpRequest));
$matcher = new RequestMatcher();
$this->assertTrue($matcher->matches($httpsRequest));
$this->assertTrue($matcher->matches($httpRequest));
}
/**
* @dataProvider getHostData
*/
public function testHost($pattern, $isMatch)
{
$matcher = new RequestMatcher();
$request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
$matcher->matchHost($pattern);
$this->assertSame($isMatch, $matcher->matches($request));
$matcher = new RequestMatcher(null, $pattern);
$this->assertSame($isMatch, $matcher->matches($request));
}
public function getHostData()
{
return array(
array('.*\.example\.com', true),
array('\.example\.com$', true),
array('^.*\.example\.com$', true),
array('.*\.sensio\.com', false),
array('.*\.example\.COM', true),
array('\.example\.COM$', true),
array('^.*\.example\.COM$', true),
array('.*\.sensio\.COM', false),
);
}
public function testPath()
{
$matcher = new RequestMatcher();
$request = Request::create('/admin/foo');
$matcher->matchPath('/admin/.*');
$this->assertTrue($matcher->matches($request));
$matcher->matchPath('/admin');
$this->assertTrue($matcher->matches($request));
$matcher->matchPath('^/admin/.*$');
$this->assertTrue($matcher->matches($request));
$matcher->matchMethod('/blog/.*');
$this->assertFalse($matcher->matches($request));
}
public function testPathWithLocaleIsNotSupported()
{
$matcher = new RequestMatcher();
$request = Request::create('/en/login');
$request->setLocale('en');
$matcher->matchPath('^/{_locale}/login$');
$this->assertFalse($matcher->matches($request));
}
public function testPathWithEncodedCharacters()
{
$matcher = new RequestMatcher();
$request = Request::create('/admin/fo%20o');
$matcher->matchPath('^/admin/fo o*$');
$this->assertTrue($matcher->matches($request));
}
public function testAttributes()
{
$matcher = new RequestMatcher();
$request = Request::create('/admin/foo');
$request->attributes->set('foo', 'foo_bar');
$matcher->matchAttribute('foo', 'foo_.*');
$this->assertTrue($matcher->matches($request));
$matcher->matchAttribute('foo', 'foo');
$this->assertTrue($matcher->matches($request));
$matcher->matchAttribute('foo', '^foo_bar$');
$this->assertTrue($matcher->matches($request));
$matcher->matchAttribute('foo', 'babar');
$this->assertFalse($matcher->matches($request));
}
}

View File

@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestStackTest extends TestCase
{
public function testGetCurrentRequest()
{
$requestStack = new RequestStack();
$this->assertNull($requestStack->getCurrentRequest());
$request = Request::create('/foo');
$requestStack->push($request);
$this->assertSame($request, $requestStack->getCurrentRequest());
$this->assertSame($request, $requestStack->pop());
$this->assertNull($requestStack->getCurrentRequest());
$this->assertNull($requestStack->pop());
}
public function testGetMasterRequest()
{
$requestStack = new RequestStack();
$this->assertNull($requestStack->getMasterRequest());
$masterRequest = Request::create('/foo');
$subRequest = Request::create('/bar');
$requestStack->push($masterRequest);
$requestStack->push($subRequest);
$this->assertSame($masterRequest, $requestStack->getMasterRequest());
}
public function testGetParentRequest()
{
$requestStack = new RequestStack();
$this->assertNull($requestStack->getParentRequest());
$masterRequest = Request::create('/foo');
$requestStack->push($masterRequest);
$this->assertNull($requestStack->getParentRequest());
$firstSubRequest = Request::create('/bar');
$requestStack->push($firstSubRequest);
$this->assertSame($masterRequest, $requestStack->getParentRequest());
$secondSubRequest = Request::create('/baz');
$requestStack->push($secondSubRequest);
$this->assertSame($firstSubRequest, $requestStack->getParentRequest());
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,339 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Cookie;
/**
* @group time-sensitive
*/
class ResponseHeaderBagTest extends TestCase
{
/**
* @dataProvider provideAllPreserveCase
*/
public function testAllPreserveCase($headers, $expected)
{
$bag = new ResponseHeaderBag($headers);
$this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
}
public function provideAllPreserveCase()
{
return array(
array(
array('fOo' => 'BAR'),
array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache, private')),
),
array(
array('ETag' => 'xyzzy'),
array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate')),
),
array(
array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache, private')),
),
array(
array('P3P' => 'CP="CAO PSA OUR"'),
array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache, private')),
),
array(
array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache, private')),
),
array(
array('X-UA-Compatible' => 'IE=edge,chrome=1'),
array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache, private')),
),
array(
array('X-XSS-Protection' => '1; mode=block'),
array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache, private')),
),
);
}
public function testCacheControlHeader()
{
$bag = new ResponseHeaderBag(array());
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
$bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
$this->assertEquals('public', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('public'));
$bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('private'));
$this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
$this->assertFalse($bag->hasCacheControlDirective('max-age'));
$bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array(
'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
'Cache-Control' => 'max-age=3600',
));
$this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
$this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
$this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
$this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
$this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
$bag = new ResponseHeaderBag();
$bag->set('Last-Modified', 'abcde');
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
}
public function testCacheControlClone()
{
$headers = array('foo' => 'bar');
$bag1 = new ResponseHeaderBag($headers);
$bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
$this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
}
public function testToStringIncludesCookieHeaders()
{
$bag = new ResponseHeaderBag(array());
$bag->setCookie(new Cookie('foo', 'bar'));
$this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
$bag->clearCookie('foo');
$this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; httponly', $bag);
}
public function testClearCookieSecureNotHttpOnly()
{
$bag = new ResponseHeaderBag(array());
$bag->clearCookie('foo', '/', null, true, false);
$this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; secure', $bag);
}
public function testReplace()
{
$bag = new ResponseHeaderBag(array());
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
$bag->replace(array('Cache-Control' => 'public'));
$this->assertEquals('public', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('public'));
}
public function testReplaceWithRemove()
{
$bag = new ResponseHeaderBag(array());
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
$bag->remove('Cache-Control');
$bag->replace(array());
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
}
public function testCookiesWithSameNames()
{
$bag = new ResponseHeaderBag();
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
$bag->setCookie(new Cookie('foo', 'bar'));
$this->assertCount(4, $bag->getCookies());
$this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
$this->assertEquals(array(
'foo=bar; path=/path/foo; domain=foo.bar; httponly',
'foo=bar; path=/path/bar; domain=foo.bar; httponly',
'foo=bar; path=/path/bar; domain=bar.foo; httponly',
'foo=bar; path=/; httponly',
), $bag->get('set-cookie', null, false));
$this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
$this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
$this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
$this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
$this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
$this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
$this->assertTrue(isset($cookies['']['/']['foo']));
}
public function testRemoveCookie()
{
$bag = new ResponseHeaderBag();
$this->assertFalse($bag->has('set-cookie'));
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
$bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
$this->assertTrue($bag->has('set-cookie'));
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
$bag->removeCookie('foo', '/path/foo', 'foo.bar');
$this->assertTrue($bag->has('set-cookie'));
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
$bag->removeCookie('bar', '/path/bar', 'foo.bar');
$this->assertFalse($bag->has('set-cookie'));
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertFalse(isset($cookies['foo.bar']));
}
public function testRemoveCookieWithNullRemove()
{
$bag = new ResponseHeaderBag();
$bag->setCookie(new Cookie('foo', 'bar', 0));
$bag->setCookie(new Cookie('bar', 'foo', 0));
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertTrue(isset($cookies['']['/']));
$bag->removeCookie('foo', null);
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertFalse(isset($cookies['']['/']['foo']));
$bag->removeCookie('bar', null);
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertFalse(isset($cookies['']['/']['bar']));
}
public function testSetCookieHeader()
{
$bag = new ResponseHeaderBag();
$bag->set('set-cookie', 'foo=bar');
$this->assertEquals(array(new Cookie('foo', 'bar', 0, '/', null, false, false, true)), $bag->getCookies());
$bag->set('set-cookie', 'foo2=bar2', false);
$this->assertEquals(array(
new Cookie('foo', 'bar', 0, '/', null, false, false, true),
new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
), $bag->getCookies());
$bag->remove('set-cookie');
$this->assertEquals(array(), $bag->getCookies());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGetCookiesWithInvalidArgument()
{
$bag = new ResponseHeaderBag();
$bag->getCookies('invalid_argument');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testMakeDispositionInvalidDisposition()
{
$headers = new ResponseHeaderBag();
$headers->makeDisposition('invalid', 'foo.html');
}
/**
* @dataProvider provideMakeDisposition
*/
public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
{
$headers = new ResponseHeaderBag();
$this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
}
public function testToStringDoesntMessUpHeaders()
{
$headers = new ResponseHeaderBag();
$headers->set('Location', 'http://www.symfony.com');
$headers->set('Content-type', 'text/html');
(string) $headers;
$allHeaders = $headers->allPreserveCase();
$this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
$this->assertEquals(array('text/html'), $allHeaders['Content-type']);
}
public function provideMakeDisposition()
{
return array(
array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
);
}
/**
* @dataProvider provideMakeDispositionFail
* @expectedException \InvalidArgumentException
*/
public function testMakeDispositionFail($disposition, $filename)
{
$headers = new ResponseHeaderBag();
$headers->makeDisposition($disposition, $filename);
}
public function provideMakeDispositionFail()
{
return array(
array('attachment', 'foo%20bar.html'),
array('attachment', 'foo/bar.html'),
array('attachment', '/foo.html'),
array('attachment', 'foo\bar.html'),
array('attachment', '\foo.html'),
array('attachment', 'föö.html'),
);
}
private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
{
$this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
}
}

View File

@@ -0,0 +1,981 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* @group time-sensitive
*/
class ResponseTest extends ResponseTestCase
{
public function testCreate()
{
$response = Response::create('foo', 301, array('Foo' => 'bar'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals('bar', $response->headers->get('foo'));
}
public function testToString()
{
$response = new Response();
$response = explode("\r\n", $response);
$this->assertEquals('HTTP/1.0 200 OK', $response[0]);
$this->assertEquals('Cache-Control: no-cache, private', $response[1]);
}
public function testClone()
{
$response = new Response();
$responseClone = clone $response;
$this->assertEquals($response, $responseClone);
}
public function testSendHeaders()
{
$response = new Response();
$headers = $response->sendHeaders();
$this->assertObjectHasAttribute('headers', $headers);
$this->assertObjectHasAttribute('content', $headers);
$this->assertObjectHasAttribute('version', $headers);
$this->assertObjectHasAttribute('statusCode', $headers);
$this->assertObjectHasAttribute('statusText', $headers);
$this->assertObjectHasAttribute('charset', $headers);
}
public function testSend()
{
$response = new Response();
$responseSend = $response->send();
$this->assertObjectHasAttribute('headers', $responseSend);
$this->assertObjectHasAttribute('content', $responseSend);
$this->assertObjectHasAttribute('version', $responseSend);
$this->assertObjectHasAttribute('statusCode', $responseSend);
$this->assertObjectHasAttribute('statusText', $responseSend);
$this->assertObjectHasAttribute('charset', $responseSend);
}
public function testGetCharset()
{
$response = new Response();
$charsetOrigin = 'UTF-8';
$response->setCharset($charsetOrigin);
$charset = $response->getCharset();
$this->assertEquals($charsetOrigin, $charset);
}
public function testIsCacheable()
{
$response = new Response();
$this->assertFalse($response->isCacheable());
}
public function testIsCacheableWithErrorCode()
{
$response = new Response('', 500);
$this->assertFalse($response->isCacheable());
}
public function testIsCacheableWithNoStoreDirective()
{
$response = new Response();
$response->headers->set('cache-control', 'private');
$this->assertFalse($response->isCacheable());
}
public function testIsCacheableWithSetTtl()
{
$response = new Response();
$response->setTtl(10);
$this->assertTrue($response->isCacheable());
}
public function testMustRevalidate()
{
$response = new Response();
$this->assertFalse($response->mustRevalidate());
}
public function testMustRevalidateWithMustRevalidateCacheControlHeader()
{
$response = new Response();
$response->headers->set('cache-control', 'must-revalidate');
$this->assertTrue($response->mustRevalidate());
}
public function testMustRevalidateWithProxyRevalidateCacheControlHeader()
{
$response = new Response();
$response->headers->set('cache-control', 'proxy-revalidate');
$this->assertTrue($response->mustRevalidate());
}
public function testSetNotModified()
{
$response = new Response();
$modified = $response->setNotModified();
$this->assertObjectHasAttribute('headers', $modified);
$this->assertObjectHasAttribute('content', $modified);
$this->assertObjectHasAttribute('version', $modified);
$this->assertObjectHasAttribute('statusCode', $modified);
$this->assertObjectHasAttribute('statusText', $modified);
$this->assertObjectHasAttribute('charset', $modified);
$this->assertEquals(304, $modified->getStatusCode());
}
public function testIsSuccessful()
{
$response = new Response();
$this->assertTrue($response->isSuccessful());
}
public function testIsNotModified()
{
$response = new Response();
$modified = $response->isNotModified(new Request());
$this->assertFalse($modified);
}
public function testIsNotModifiedNotSafe()
{
$request = Request::create('/homepage', 'POST');
$response = new Response();
$this->assertFalse($response->isNotModified($request));
}
public function testIsNotModifiedLastModified()
{
$before = 'Sun, 25 Aug 2013 18:32:31 GMT';
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$after = 'Sun, 25 Aug 2013 19:33:31 GMT';
$request = new Request();
$request->headers->set('If-Modified-Since', $modified);
$response = new Response();
$response->headers->set('Last-Modified', $modified);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('Last-Modified', $before);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('Last-Modified', $after);
$this->assertFalse($response->isNotModified($request));
$response->headers->set('Last-Modified', '');
$this->assertFalse($response->isNotModified($request));
}
public function testIsNotModifiedEtag()
{
$etagOne = 'randomly_generated_etag';
$etagTwo = 'randomly_generated_etag_2';
$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
$response = new Response();
$response->headers->set('ETag', $etagOne);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('ETag', $etagTwo);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('ETag', '');
$this->assertFalse($response->isNotModified($request));
}
public function testIsNotModifiedLastModifiedAndEtag()
{
$before = 'Sun, 25 Aug 2013 18:32:31 GMT';
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$after = 'Sun, 25 Aug 2013 19:33:31 GMT';
$etag = 'randomly_generated_etag';
$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);
$response = new Response();
$response->headers->set('ETag', $etag);
$response->headers->set('Last-Modified', $after);
$this->assertFalse($response->isNotModified($request));
$response->headers->set('ETag', 'non-existent-etag');
$response->headers->set('Last-Modified', $before);
$this->assertFalse($response->isNotModified($request));
$response->headers->set('ETag', $etag);
$response->headers->set('Last-Modified', $modified);
$this->assertTrue($response->isNotModified($request));
}
public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
{
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$etag = 'randomly_generated_etag';
$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);
$response = new Response();
$response->headers->set('ETag', $etag);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('ETag', 'non-existent-etag');
$this->assertFalse($response->isNotModified($request));
}
public function testIsValidateable()
{
$response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
$this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
$response = new Response('', 200, array('ETag' => '"12345"'));
$this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
$response = new Response();
$this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
}
public function testGetDate()
{
$oneHourAgo = $this->createDateTimeOneHourAgo();
$response = new Response('', 200, array('Date' => $oneHourAgo->format(DATE_RFC2822)));
$date = $response->getDate();
$this->assertEquals($oneHourAgo->getTimestamp(), $date->getTimestamp(), '->getDate() returns the Date header if present');
$response = new Response();
$date = $response->getDate();
$this->assertEquals(time(), $date->getTimestamp(), '->getDate() returns the current Date if no Date header present');
$response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
$now = $this->createDateTimeNow();
$response->headers->set('Date', $now->format(DATE_RFC2822));
$date = $response->getDate();
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified');
$response = new Response('', 200);
$now = $this->createDateTimeNow();
$response->headers->remove('Date');
$date = $response->getDate();
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the current Date when the header has previously been removed');
}
public function testGetMaxAge()
{
$response = new Response();
$response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
$this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
$response = new Response();
$response->headers->set('Cache-Control', 'max-age=600');
$this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
$response = new Response();
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
$this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
$response = new Response();
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Expires', -1);
$this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(DATE_RFC822));
$response = new Response();
$this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
}
public function testSetSharedMaxAge()
{
$response = new Response();
$response->setSharedMaxAge(20);
$cacheControl = $response->headers->get('Cache-Control');
$this->assertEquals('public, s-maxage=20', $cacheControl);
}
public function testIsPrivate()
{
$response = new Response();
$response->headers->set('Cache-Control', 'max-age=100');
$response->setPrivate();
$this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
$this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
$response = new Response();
$response->headers->set('Cache-Control', 'public, max-age=100');
$response->setPrivate();
$this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
$this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
$this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
}
public function testExpire()
{
$response = new Response();
$response->headers->set('Cache-Control', 'max-age=100');
$response->expire();
$this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
$response = new Response();
$response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
$response->expire();
$this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
$response = new Response();
$response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
$response->headers->set('Age', '1000');
$response->expire();
$this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
$response = new Response();
$response->expire();
$this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
$response = new Response();
$response->headers->set('Expires', -1);
$response->expire();
$this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
}
public function testGetTtl()
{
$response = new Response();
$this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
$response = new Response();
$response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
$this->assertEquals(3600, $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
$response = new Response();
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
$this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
$response = new Response();
$response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822));
$response->headers->set('Age', 0);
$this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
$response = new Response();
$response->headers->set('Cache-Control', 'max-age=60');
$this->assertEquals(60, $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
}
public function testSetClientTtl()
{
$response = new Response();
$response->setClientTtl(10);
$this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
}
public function testGetSetProtocolVersion()
{
$response = new Response();
$this->assertEquals('1.0', $response->getProtocolVersion());
$response->setProtocolVersion('1.1');
$this->assertEquals('1.1', $response->getProtocolVersion());
}
public function testGetVary()
{
$response = new Response();
$this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
$response = new Response();
$response->headers->set('Vary', 'Accept-Language');
$this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
$response = new Response();
$response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
$this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
$response = new Response();
$response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
$this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
$vary = array('Accept-Language', 'User-Agent', 'X-foo');
$response = new Response();
$response->headers->set('Vary', $vary);
$this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
$response = new Response();
$response->headers->set('Vary', 'Accept-Language, User-Agent, X-foo');
$this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
}
public function testSetVary()
{
$response = new Response();
$response->setVary('Accept-Language');
$this->assertEquals(array('Accept-Language'), $response->getVary());
$response->setVary('Accept-Language, User-Agent');
$this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() replace the vary header by default');
$response->setVary('X-Foo', false);
$this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->setVary() doesn\'t wipe out earlier Vary headers if replace is set to false');
}
public function testDefaultContentType()
{
$headerMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\ResponseHeaderBag')->setMethods(array('set'))->getMock();
$headerMock->expects($this->at(0))
->method('set')
->with('Content-Type', 'text/html');
$headerMock->expects($this->at(1))
->method('set')
->with('Content-Type', 'text/html; charset=UTF-8');
$response = new Response('foo');
$response->headers = $headerMock;
$response->prepare(new Request());
}
public function testContentTypeCharset()
{
$response = new Response();
$response->headers->set('Content-Type', 'text/css');
// force fixContentType() to be called
$response->prepare(new Request());
$this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
}
public function testPrepareDoesNothingIfContentTypeIsSet()
{
$response = new Response('foo');
$response->headers->set('Content-Type', 'text/plain');
$response->prepare(new Request());
$this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('content-type'));
}
public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
{
$response = new Response('foo');
$response->prepare(new Request());
$this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
}
public function testPrepareSetContentType()
{
$response = new Response('foo');
$request = Request::create('/');
$request->setRequestFormat('json');
$response->prepare($request);
$this->assertEquals('application/json', $response->headers->get('content-type'));
}
public function testPrepareRemovesContentForHeadRequests()
{
$response = new Response('foo');
$request = Request::create('/', 'HEAD');
$length = 12345;
$response->headers->set('Content-Length', $length);
$response->prepare($request);
$this->assertEquals('', $response->getContent());
$this->assertEquals($length, $response->headers->get('Content-Length'), 'Content-Length should be as if it was GET; see RFC2616 14.13');
}
public function testPrepareRemovesContentForInformationalResponse()
{
$response = new Response('foo');
$request = Request::create('/');
$response->setContent('content');
$response->setStatusCode(101);
$response->prepare($request);
$this->assertEquals('', $response->getContent());
$this->assertFalse($response->headers->has('Content-Type'));
$this->assertFalse($response->headers->has('Content-Type'));
$response->setContent('content');
$response->setStatusCode(304);
$response->prepare($request);
$this->assertEquals('', $response->getContent());
$this->assertFalse($response->headers->has('Content-Type'));
$this->assertFalse($response->headers->has('Content-Length'));
}
public function testPrepareRemovesContentLength()
{
$response = new Response('foo');
$request = Request::create('/');
$response->headers->set('Content-Length', 12345);
$response->prepare($request);
$this->assertEquals(12345, $response->headers->get('Content-Length'));
$response->headers->set('Transfer-Encoding', 'chunked');
$response->prepare($request);
$this->assertFalse($response->headers->has('Content-Length'));
}
public function testPrepareSetsPragmaOnHttp10Only()
{
$request = Request::create('/', 'GET');
$request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
$response = new Response('foo');
$response->prepare($request);
$this->assertEquals('no-cache', $response->headers->get('pragma'));
$this->assertEquals('-1', $response->headers->get('expires'));
$request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
$response = new Response('foo');
$response->prepare($request);
$this->assertFalse($response->headers->has('pragma'));
$this->assertFalse($response->headers->has('expires'));
}
public function testSetCache()
{
$response = new Response();
//array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
try {
$response->setCache(array('wrong option' => 'value'));
$this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
} catch (\Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
$this->assertContains('"wrong option"', $e->getMessage());
}
$options = array('etag' => '"whatever"');
$response->setCache($options);
$this->assertEquals($response->getEtag(), '"whatever"');
$now = $this->createDateTimeNow();
$options = array('last_modified' => $now);
$response->setCache($options);
$this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
$options = array('max_age' => 100);
$response->setCache($options);
$this->assertEquals($response->getMaxAge(), 100);
$options = array('s_maxage' => 200);
$response->setCache($options);
$this->assertEquals($response->getMaxAge(), 200);
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
$response->setCache(array('public' => true));
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
$response->setCache(array('public' => false));
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
$response->setCache(array('private' => true));
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
$response->setCache(array('private' => false));
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
}
public function testSendContent()
{
$response = new Response('test response rendering', 200);
ob_start();
$response->sendContent();
$string = ob_get_clean();
$this->assertContains('test response rendering', $string);
}
public function testSetPublic()
{
$response = new Response();
$response->setPublic();
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
}
public function testSetExpires()
{
$response = new Response();
$response->setExpires(null);
$this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
$now = $this->createDateTimeNow();
$response->setExpires($now);
$this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
}
public function testSetLastModified()
{
$response = new Response();
$response->setLastModified($this->createDateTimeNow());
$this->assertNotNull($response->getLastModified());
$response->setLastModified(null);
$this->assertNull($response->getLastModified());
}
public function testIsInvalid()
{
$response = new Response();
try {
$response->setStatusCode(99);
$this->fail();
} catch (\InvalidArgumentException $e) {
$this->assertTrue($response->isInvalid());
}
try {
$response->setStatusCode(650);
$this->fail();
} catch (\InvalidArgumentException $e) {
$this->assertTrue($response->isInvalid());
}
$response = new Response('', 200);
$this->assertFalse($response->isInvalid());
}
/**
* @dataProvider getStatusCodeFixtures
*/
public function testSetStatusCode($code, $text, $expectedText)
{
$response = new Response();
$response->setStatusCode($code, $text);
$statusText = new \ReflectionProperty($response, 'statusText');
$statusText->setAccessible(true);
$this->assertEquals($expectedText, $statusText->getValue($response));
}
public function getStatusCodeFixtures()
{
return array(
array('200', null, 'OK'),
array('200', false, ''),
array('200', 'foo', 'foo'),
array('199', null, 'unknown status'),
array('199', false, ''),
array('199', 'foo', 'foo'),
);
}
public function testIsInformational()
{
$response = new Response('', 100);
$this->assertTrue($response->isInformational());
$response = new Response('', 200);
$this->assertFalse($response->isInformational());
}
public function testIsRedirectRedirection()
{
foreach (array(301, 302, 303, 307) as $code) {
$response = new Response('', $code);
$this->assertTrue($response->isRedirection());
$this->assertTrue($response->isRedirect());
}
$response = new Response('', 304);
$this->assertTrue($response->isRedirection());
$this->assertFalse($response->isRedirect());
$response = new Response('', 200);
$this->assertFalse($response->isRedirection());
$this->assertFalse($response->isRedirect());
$response = new Response('', 404);
$this->assertFalse($response->isRedirection());
$this->assertFalse($response->isRedirect());
$response = new Response('', 301, array('Location' => '/good-uri'));
$this->assertFalse($response->isRedirect('/bad-uri'));
$this->assertTrue($response->isRedirect('/good-uri'));
}
public function testIsNotFound()
{
$response = new Response('', 404);
$this->assertTrue($response->isNotFound());
$response = new Response('', 200);
$this->assertFalse($response->isNotFound());
}
public function testIsEmpty()
{
foreach (array(204, 304) as $code) {
$response = new Response('', $code);
$this->assertTrue($response->isEmpty());
}
$response = new Response('', 200);
$this->assertFalse($response->isEmpty());
}
public function testIsForbidden()
{
$response = new Response('', 403);
$this->assertTrue($response->isForbidden());
$response = new Response('', 200);
$this->assertFalse($response->isForbidden());
}
public function testIsOk()
{
$response = new Response('', 200);
$this->assertTrue($response->isOk());
$response = new Response('', 404);
$this->assertFalse($response->isOk());
}
public function testIsServerOrClientError()
{
$response = new Response('', 404);
$this->assertTrue($response->isClientError());
$this->assertFalse($response->isServerError());
$response = new Response('', 500);
$this->assertFalse($response->isClientError());
$this->assertTrue($response->isServerError());
}
public function testHasVary()
{
$response = new Response();
$this->assertFalse($response->hasVary());
$response->setVary('User-Agent');
$this->assertTrue($response->hasVary());
}
public function testSetEtag()
{
$response = new Response('', 200, array('ETag' => '"12345"'));
$response->setEtag();
$this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
}
/**
* @dataProvider validContentProvider
*/
public function testSetContent($content)
{
$response = new Response();
$response->setContent($content);
$this->assertEquals((string) $content, $response->getContent());
}
/**
* @expectedException \UnexpectedValueException
* @dataProvider invalidContentProvider
*/
public function testSetContentInvalid($content)
{
$response = new Response();
$response->setContent($content);
}
public function testSettersAreChainable()
{
$response = new Response();
$setters = array(
'setProtocolVersion' => '1.0',
'setCharset' => 'UTF-8',
'setPublic' => null,
'setPrivate' => null,
'setDate' => $this->createDateTimeNow(),
'expire' => null,
'setMaxAge' => 1,
'setSharedMaxAge' => 1,
'setTtl' => 1,
'setClientTtl' => 1,
);
foreach ($setters as $setter => $arg) {
$this->assertEquals($response, $response->{$setter}($arg));
}
}
public function testNoDeprecationsAreTriggered()
{
new DefaultResponse();
$this->getMockBuilder(Response::class)->getMock();
// we just need to ensure that subclasses of Response can be created without any deprecations
// being triggered if the subclass does not override any final methods
$this->addToAssertionCount(1);
}
public function validContentProvider()
{
return array(
'obj' => array(new StringableObject()),
'string' => array('Foo'),
'int' => array(2),
);
}
public function invalidContentProvider()
{
return array(
'obj' => array(new \stdClass()),
'array' => array(array()),
'bool' => array(true, '1'),
);
}
protected function createDateTimeOneHourAgo()
{
return $this->createDateTimeNow()->sub(new \DateInterval('PT1H'));
}
protected function createDateTimeOneHourLater()
{
return $this->createDateTimeNow()->add(new \DateInterval('PT1H'));
}
protected function createDateTimeNow()
{
$date = new \DateTime();
return $date->setTimestamp(time());
}
protected function provideResponse()
{
return new Response();
}
/**
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
*
* @author Fábio Pacheco
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/
public function ianaCodesReasonPhrasesProvider()
{
if (!in_array('https', stream_get_wrappers(), true)) {
$this->markTestSkipped('The "https" wrapper is not available');
}
$ianaHttpStatusCodes = new \DOMDocument();
libxml_set_streams_context(stream_context_create(array(
'http' => array(
'method' => 'GET',
'timeout' => 30,
),
)));
$ianaHttpStatusCodes->load('https://www.iana.org/assignments/http-status-codes/http-status-codes.xml');
if (!$ianaHttpStatusCodes->relaxNGValidate(__DIR__.'/schema/http-status-codes.rng')) {
self::fail('Invalid IANA\'s HTTP status code list.');
}
$ianaCodesReasonPhrases = array();
$xpath = new \DomXPath($ianaHttpStatusCodes);
$xpath->registerNamespace('ns', 'http://www.iana.org/assignments');
$records = $xpath->query('//ns:record');
foreach ($records as $record) {
$value = $xpath->query('.//ns:value', $record)->item(0)->nodeValue;
$description = $xpath->query('.//ns:description', $record)->item(0)->nodeValue;
if (in_array($description, array('Unassigned', '(Unused)'), true)) {
continue;
}
if (preg_match('/^([0-9]+)\s*\-\s*([0-9]+)$/', $value, $matches)) {
for ($value = $matches[1]; $value <= $matches[2]; ++$value) {
$ianaCodesReasonPhrases[] = array($value, $description);
}
} else {
$ianaCodesReasonPhrases[] = array($value, $description);
}
}
return $ianaCodesReasonPhrases;
}
/**
* @dataProvider ianaCodesReasonPhrasesProvider
*/
public function testReasonPhraseDefaultsAgainstIana($code, $reasonPhrase)
{
$this->assertEquals($reasonPhrase, Response::$statusTexts[$code]);
}
}
class StringableObject
{
public function __toString()
{
return 'Foo';
}
}
class DefaultResponse extends Response
{
}
class ExtendedResponse extends Response
{
public function setLastModified(\DateTime $date = null)
{
}
public function getDate()
{
}
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
abstract class ResponseTestCase extends TestCase
{
public function testNoCacheControlHeaderOnAttachmentUsingHTTPSAndMSIE()
{
// Check for HTTPS and IE 8
$request = new Request();
$request->server->set('HTTPS', true);
$request->server->set('HTTP_USER_AGENT', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)');
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertFalse($response->headers->has('Cache-Control'));
// Check for IE 10 and HTTPS
$request->server->set('HTTP_USER_AGENT', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)');
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertTrue($response->headers->has('Cache-Control'));
// Check for IE 9 and HTTPS
$request->server->set('HTTP_USER_AGENT', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)');
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertTrue($response->headers->has('Cache-Control'));
// Check for IE 9 and HTTP
$request->server->set('HTTPS', false);
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertTrue($response->headers->has('Cache-Control'));
// Check for IE 8 and HTTP
$request->server->set('HTTP_USER_AGENT', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)');
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertTrue($response->headers->has('Cache-Control'));
// Check for non-IE and HTTPS
$request->server->set('HTTPS', true);
$request->server->set('HTTP_USER_AGENT', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17');
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertTrue($response->headers->has('Cache-Control'));
// Check for non-IE and HTTP
$request->server->set('HTTPS', false);
$response = $this->provideResponse();
$response->headers->set('Content-Disposition', 'attachment; filename="fname.ext"');
$response->prepare($request);
$this->assertTrue($response->headers->has('Cache-Control'));
}
abstract protected function provideResponse();
}

View File

@@ -0,0 +1,170 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ServerBag;
/**
* ServerBagTest.
*
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
class ServerBagTest extends TestCase
{
public function testShouldExtractHeadersFromServerArray()
{
$server = array(
'SOME_SERVER_VARIABLE' => 'value',
'SOME_SERVER_VARIABLE2' => 'value',
'ROOT' => 'value',
'HTTP_CONTENT_TYPE' => 'text/html',
'HTTP_CONTENT_LENGTH' => '0',
'HTTP_ETAG' => 'asdf',
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => 'bar',
);
$bag = new ServerBag($server);
$this->assertEquals(array(
'CONTENT_TYPE' => 'text/html',
'CONTENT_LENGTH' => '0',
'ETAG' => 'asdf',
'AUTHORIZATION' => 'Basic '.base64_encode('foo:bar'),
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => 'bar',
), $bag->getHeaders());
}
public function testHttpPasswordIsOptional()
{
$bag = new ServerBag(array('PHP_AUTH_USER' => 'foo'));
$this->assertEquals(array(
'AUTHORIZATION' => 'Basic '.base64_encode('foo:'),
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => '',
), $bag->getHeaders());
}
public function testHttpBasicAuthWithPhpCgi()
{
$bag = new ServerBag(array('HTTP_AUTHORIZATION' => 'Basic '.base64_encode('foo:bar')));
$this->assertEquals(array(
'AUTHORIZATION' => 'Basic '.base64_encode('foo:bar'),
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => 'bar',
), $bag->getHeaders());
}
public function testHttpBasicAuthWithPhpCgiBogus()
{
$bag = new ServerBag(array('HTTP_AUTHORIZATION' => 'Basic_'.base64_encode('foo:bar')));
// Username and passwords should not be set as the header is bogus
$headers = $bag->getHeaders();
$this->assertFalse(isset($headers['PHP_AUTH_USER']));
$this->assertFalse(isset($headers['PHP_AUTH_PW']));
}
public function testHttpBasicAuthWithPhpCgiRedirect()
{
$bag = new ServerBag(array('REDIRECT_HTTP_AUTHORIZATION' => 'Basic '.base64_encode('username:pass:word')));
$this->assertEquals(array(
'AUTHORIZATION' => 'Basic '.base64_encode('username:pass:word'),
'PHP_AUTH_USER' => 'username',
'PHP_AUTH_PW' => 'pass:word',
), $bag->getHeaders());
}
public function testHttpBasicAuthWithPhpCgiEmptyPassword()
{
$bag = new ServerBag(array('HTTP_AUTHORIZATION' => 'Basic '.base64_encode('foo:')));
$this->assertEquals(array(
'AUTHORIZATION' => 'Basic '.base64_encode('foo:'),
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => '',
), $bag->getHeaders());
}
public function testHttpDigestAuthWithPhpCgi()
{
$digest = 'Digest username="foo", realm="acme", nonce="'.md5('secret').'", uri="/protected, qop="auth"';
$bag = new ServerBag(array('HTTP_AUTHORIZATION' => $digest));
$this->assertEquals(array(
'AUTHORIZATION' => $digest,
'PHP_AUTH_DIGEST' => $digest,
), $bag->getHeaders());
}
public function testHttpDigestAuthWithPhpCgiBogus()
{
$digest = 'Digest_username="foo", realm="acme", nonce="'.md5('secret').'", uri="/protected, qop="auth"';
$bag = new ServerBag(array('HTTP_AUTHORIZATION' => $digest));
// Username and passwords should not be set as the header is bogus
$headers = $bag->getHeaders();
$this->assertFalse(isset($headers['PHP_AUTH_USER']));
$this->assertFalse(isset($headers['PHP_AUTH_PW']));
}
public function testHttpDigestAuthWithPhpCgiRedirect()
{
$digest = 'Digest username="foo", realm="acme", nonce="'.md5('secret').'", uri="/protected, qop="auth"';
$bag = new ServerBag(array('REDIRECT_HTTP_AUTHORIZATION' => $digest));
$this->assertEquals(array(
'AUTHORIZATION' => $digest,
'PHP_AUTH_DIGEST' => $digest,
), $bag->getHeaders());
}
public function testOAuthBearerAuth()
{
$headerContent = 'Bearer L-yLEOr9zhmUYRkzN1jwwxwQ-PBNiKDc8dgfB4hTfvo';
$bag = new ServerBag(array('HTTP_AUTHORIZATION' => $headerContent));
$this->assertEquals(array(
'AUTHORIZATION' => $headerContent,
), $bag->getHeaders());
}
public function testOAuthBearerAuthWithRedirect()
{
$headerContent = 'Bearer L-yLEOr9zhmUYRkzN1jwwxwQ-PBNiKDc8dgfB4hTfvo';
$bag = new ServerBag(array('REDIRECT_HTTP_AUTHORIZATION' => $headerContent));
$this->assertEquals(array(
'AUTHORIZATION' => $headerContent,
), $bag->getHeaders());
}
/**
* @see https://github.com/symfony/symfony/issues/17345
*/
public function testItDoesNotOverwriteTheAuthorizationHeaderIfItIsAlreadySet()
{
$headerContent = 'Bearer L-yLEOr9zhmUYRkzN1jwwxwQ-PBNiKDc8dgfB4hTfvo';
$bag = new ServerBag(array('PHP_AUTH_USER' => 'foo', 'HTTP_AUTHORIZATION' => $headerContent));
$this->assertEquals(array(
'AUTHORIZATION' => $headerContent,
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => '',
), $bag->getHeaders());
}
}

View File

@@ -0,0 +1,189 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Attribute;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
/**
* Tests AttributeBag.
*
* @author Drak <drak@zikula.org>
*/
class AttributeBagTest extends TestCase
{
/**
* @var array
*/
private $array;
/**
* @var AttributeBag
*/
private $bag;
protected function setUp()
{
$this->array = array(
'hello' => 'world',
'always' => 'be happy',
'user.login' => 'drak',
'csrf.token' => array(
'a' => '1234',
'b' => '4321',
),
'category' => array(
'fishing' => array(
'first' => 'cod',
'second' => 'sole',
),
),
);
$this->bag = new AttributeBag('_sf2');
$this->bag->initialize($this->array);
}
protected function tearDown()
{
$this->bag = null;
$this->array = array();
}
public function testInitialize()
{
$bag = new AttributeBag();
$bag->initialize($this->array);
$this->assertEquals($this->array, $bag->all());
$array = array('should' => 'change');
$bag->initialize($array);
$this->assertEquals($array, $bag->all());
}
public function testGetStorageKey()
{
$this->assertEquals('_sf2', $this->bag->getStorageKey());
$attributeBag = new AttributeBag('test');
$this->assertEquals('test', $attributeBag->getStorageKey());
}
public function testGetSetName()
{
$this->assertEquals('attributes', $this->bag->getName());
$this->bag->setName('foo');
$this->assertEquals('foo', $this->bag->getName());
}
/**
* @dataProvider attributesProvider
*/
public function testHas($key, $value, $exists)
{
$this->assertEquals($exists, $this->bag->has($key));
}
/**
* @dataProvider attributesProvider
*/
public function testGet($key, $value, $expected)
{
$this->assertEquals($value, $this->bag->get($key));
}
public function testGetDefaults()
{
$this->assertNull($this->bag->get('user2.login'));
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
}
/**
* @dataProvider attributesProvider
*/
public function testSet($key, $value, $expected)
{
$this->bag->set($key, $value);
$this->assertEquals($value, $this->bag->get($key));
}
public function testAll()
{
$this->assertEquals($this->array, $this->bag->all());
$this->bag->set('hello', 'fabien');
$array = $this->array;
$array['hello'] = 'fabien';
$this->assertEquals($array, $this->bag->all());
}
public function testReplace()
{
$array = array();
$array['name'] = 'jack';
$array['foo.bar'] = 'beep';
$this->bag->replace($array);
$this->assertEquals($array, $this->bag->all());
$this->assertNull($this->bag->get('hello'));
$this->assertNull($this->bag->get('always'));
$this->assertNull($this->bag->get('user.login'));
}
public function testRemove()
{
$this->assertEquals('world', $this->bag->get('hello'));
$this->bag->remove('hello');
$this->assertNull($this->bag->get('hello'));
$this->assertEquals('be happy', $this->bag->get('always'));
$this->bag->remove('always');
$this->assertNull($this->bag->get('always'));
$this->assertEquals('drak', $this->bag->get('user.login'));
$this->bag->remove('user.login');
$this->assertNull($this->bag->get('user.login'));
}
public function testClear()
{
$this->bag->clear();
$this->assertEquals(array(), $this->bag->all());
}
public function attributesProvider()
{
return array(
array('hello', 'world', true),
array('always', 'be happy', true),
array('user.login', 'drak', true),
array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
array('user2.login', null, false),
array('never', null, false),
array('bye', null, false),
array('bye/for/now', null, false),
);
}
public function testGetIterator()
{
$i = 0;
foreach ($this->bag as $key => $val) {
$this->assertEquals($this->array[$key], $val);
++$i;
}
$this->assertEquals(count($this->array), $i);
}
public function testCount()
{
$this->assertEquals(count($this->array), count($this->bag));
}
}

View File

@@ -0,0 +1,185 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Attribute;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
/**
* Tests NamespacedAttributeBag.
*
* @author Drak <drak@zikula.org>
*/
class NamespacedAttributeBagTest extends TestCase
{
/**
* @var array
*/
private $array;
/**
* @var NamespacedAttributeBag
*/
private $bag;
protected function setUp()
{
$this->array = array(
'hello' => 'world',
'always' => 'be happy',
'user.login' => 'drak',
'csrf.token' => array(
'a' => '1234',
'b' => '4321',
),
'category' => array(
'fishing' => array(
'first' => 'cod',
'second' => 'sole',
),
),
);
$this->bag = new NamespacedAttributeBag('_sf2', '/');
$this->bag->initialize($this->array);
}
protected function tearDown()
{
$this->bag = null;
$this->array = array();
}
public function testInitialize()
{
$bag = new NamespacedAttributeBag();
$bag->initialize($this->array);
$this->assertEquals($this->array, $this->bag->all());
$array = array('should' => 'not stick');
$bag->initialize($array);
// should have remained the same
$this->assertEquals($this->array, $this->bag->all());
}
public function testGetStorageKey()
{
$this->assertEquals('_sf2', $this->bag->getStorageKey());
$attributeBag = new NamespacedAttributeBag('test');
$this->assertEquals('test', $attributeBag->getStorageKey());
}
/**
* @dataProvider attributesProvider
*/
public function testHas($key, $value, $exists)
{
$this->assertEquals($exists, $this->bag->has($key));
}
/**
* @dataProvider attributesProvider
*/
public function testGet($key, $value, $expected)
{
$this->assertEquals($value, $this->bag->get($key));
}
public function testGetDefaults()
{
$this->assertNull($this->bag->get('user2.login'));
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
}
/**
* @dataProvider attributesProvider
*/
public function testSet($key, $value, $expected)
{
$this->bag->set($key, $value);
$this->assertEquals($value, $this->bag->get($key));
}
public function testAll()
{
$this->assertEquals($this->array, $this->bag->all());
$this->bag->set('hello', 'fabien');
$array = $this->array;
$array['hello'] = 'fabien';
$this->assertEquals($array, $this->bag->all());
}
public function testReplace()
{
$array = array();
$array['name'] = 'jack';
$array['foo.bar'] = 'beep';
$this->bag->replace($array);
$this->assertEquals($array, $this->bag->all());
$this->assertNull($this->bag->get('hello'));
$this->assertNull($this->bag->get('always'));
$this->assertNull($this->bag->get('user.login'));
}
public function testRemove()
{
$this->assertEquals('world', $this->bag->get('hello'));
$this->bag->remove('hello');
$this->assertNull($this->bag->get('hello'));
$this->assertEquals('be happy', $this->bag->get('always'));
$this->bag->remove('always');
$this->assertNull($this->bag->get('always'));
$this->assertEquals('drak', $this->bag->get('user.login'));
$this->bag->remove('user.login');
$this->assertNull($this->bag->get('user.login'));
}
public function testRemoveExistingNamespacedAttribute()
{
$this->assertSame('cod', $this->bag->remove('category/fishing/first'));
}
public function testRemoveNonexistingNamespacedAttribute()
{
$this->assertNull($this->bag->remove('foo/bar/baz'));
}
public function testClear()
{
$this->bag->clear();
$this->assertEquals(array(), $this->bag->all());
}
public function attributesProvider()
{
return array(
array('hello', 'world', true),
array('always', 'be happy', true),
array('user.login', 'drak', true),
array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
array('csrf.token/a', '1234', true),
array('csrf.token/b', '4321', true),
array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true),
array('category/fishing/missing/first', null, false),
array('category/fishing/first', 'cod', true),
array('category/fishing/second', 'sole', true),
array('category/fishing/missing/second', null, false),
array('user2.login', null, false),
array('never', null, false),
array('bye', null, false),
array('bye/for/now', null, false),
);
}
}

View File

@@ -0,0 +1,156 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Flash;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag as FlashBag;
/**
* AutoExpireFlashBagTest.
*
* @author Drak <drak@zikula.org>
*/
class AutoExpireFlashBagTest extends TestCase
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag
*/
private $bag;
/**
* @var array
*/
protected $array = array();
protected function setUp()
{
parent::setUp();
$this->bag = new FlashBag();
$this->array = array('new' => array('notice' => array('A previous flash message')));
$this->bag->initialize($this->array);
}
protected function tearDown()
{
$this->bag = null;
parent::tearDown();
}
public function testInitialize()
{
$bag = new FlashBag();
$array = array('new' => array('notice' => array('A previous flash message')));
$bag->initialize($array);
$this->assertEquals(array('A previous flash message'), $bag->peek('notice'));
$array = array('new' => array(
'notice' => array('Something else'),
'error' => array('a'),
));
$bag->initialize($array);
$this->assertEquals(array('Something else'), $bag->peek('notice'));
$this->assertEquals(array('a'), $bag->peek('error'));
}
public function testGetStorageKey()
{
$this->assertEquals('_sf2_flashes', $this->bag->getStorageKey());
$attributeBag = new FlashBag('test');
$this->assertEquals('test', $attributeBag->getStorageKey());
}
public function testGetSetName()
{
$this->assertEquals('flashes', $this->bag->getName());
$this->bag->setName('foo');
$this->assertEquals('foo', $this->bag->getName());
}
public function testPeek()
{
$this->assertEquals(array(), $this->bag->peek('non_existing'));
$this->assertEquals(array('default'), $this->bag->peek('non_existing', array('default')));
$this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
$this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
}
public function testSet()
{
$this->bag->set('notice', 'Foo');
$this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
}
public function testHas()
{
$this->assertFalse($this->bag->has('nothing'));
$this->assertTrue($this->bag->has('notice'));
}
public function testKeys()
{
$this->assertEquals(array('notice'), $this->bag->keys());
}
public function testPeekAll()
{
$array = array(
'new' => array(
'notice' => 'Foo',
'error' => 'Bar',
),
);
$this->bag->initialize($array);
$this->assertEquals(array(
'notice' => 'Foo',
'error' => 'Bar',
), $this->bag->peekAll()
);
$this->assertEquals(array(
'notice' => 'Foo',
'error' => 'Bar',
), $this->bag->peekAll()
);
}
public function testGet()
{
$this->assertEquals(array(), $this->bag->get('non_existing'));
$this->assertEquals(array('default'), $this->bag->get('non_existing', array('default')));
$this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
$this->assertEquals(array(), $this->bag->get('notice'));
}
public function testSetAll()
{
$this->bag->setAll(array('a' => 'first', 'b' => 'second'));
$this->assertFalse($this->bag->has('a'));
$this->assertFalse($this->bag->has('b'));
}
public function testAll()
{
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->assertEquals(array(
'notice' => array('A previous flash message'),
), $this->bag->all()
);
$this->assertEquals(array(), $this->bag->all());
}
public function testClear()
{
$this->assertEquals(array('notice' => array('A previous flash message')), $this->bag->clear());
}
}

View File

@@ -0,0 +1,135 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Flash;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
/**
* FlashBagTest.
*
* @author Drak <drak@zikula.org>
*/
class FlashBagTest extends TestCase
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
*/
private $bag;
/**
* @var array
*/
protected $array = array();
protected function setUp()
{
parent::setUp();
$this->bag = new FlashBag();
$this->array = array('notice' => array('A previous flash message'));
$this->bag->initialize($this->array);
}
protected function tearDown()
{
$this->bag = null;
parent::tearDown();
}
public function testInitialize()
{
$bag = new FlashBag();
$bag->initialize($this->array);
$this->assertEquals($this->array, $bag->peekAll());
$array = array('should' => array('change'));
$bag->initialize($array);
$this->assertEquals($array, $bag->peekAll());
}
public function testGetStorageKey()
{
$this->assertEquals('_sf2_flashes', $this->bag->getStorageKey());
$attributeBag = new FlashBag('test');
$this->assertEquals('test', $attributeBag->getStorageKey());
}
public function testGetSetName()
{
$this->assertEquals('flashes', $this->bag->getName());
$this->bag->setName('foo');
$this->assertEquals('foo', $this->bag->getName());
}
public function testPeek()
{
$this->assertEquals(array(), $this->bag->peek('non_existing'));
$this->assertEquals(array('default'), $this->bag->peek('not_existing', array('default')));
$this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
$this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
}
public function testGet()
{
$this->assertEquals(array(), $this->bag->get('non_existing'));
$this->assertEquals(array('default'), $this->bag->get('not_existing', array('default')));
$this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
$this->assertEquals(array(), $this->bag->get('notice'));
}
public function testAll()
{
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->assertEquals(array(
'notice' => array('Foo'),
'error' => array('Bar'), ), $this->bag->all()
);
$this->assertEquals(array(), $this->bag->all());
}
public function testSet()
{
$this->bag->set('notice', 'Foo');
$this->bag->set('notice', 'Bar');
$this->assertEquals(array('Bar'), $this->bag->peek('notice'));
}
public function testHas()
{
$this->assertFalse($this->bag->has('nothing'));
$this->assertTrue($this->bag->has('notice'));
}
public function testKeys()
{
$this->assertEquals(array('notice'), $this->bag->keys());
}
public function testPeekAll()
{
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->assertEquals(array(
'notice' => array('Foo'),
'error' => array('Bar'),
), $this->bag->peekAll()
);
$this->assertTrue($this->bag->has('notice'));
$this->assertTrue($this->bag->has('error'));
$this->assertEquals(array(
'notice' => array('Foo'),
'error' => array('Bar'),
), $this->bag->peekAll()
);
}
}

View File

@@ -0,0 +1,224 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/**
* SessionTest.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Robert Schönthal <seroscho@googlemail.com>
* @author Drak <drak@zikula.org>
*/
class SessionTest extends TestCase
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
*/
protected $storage;
/**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
protected function setUp()
{
$this->storage = new MockArraySessionStorage();
$this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
}
protected function tearDown()
{
$this->storage = null;
$this->session = null;
}
public function testStart()
{
$this->assertEquals('', $this->session->getId());
$this->assertTrue($this->session->start());
$this->assertNotEquals('', $this->session->getId());
}
public function testIsStarted()
{
$this->assertFalse($this->session->isStarted());
$this->session->start();
$this->assertTrue($this->session->isStarted());
}
public function testSetId()
{
$this->assertEquals('', $this->session->getId());
$this->session->setId('0123456789abcdef');
$this->session->start();
$this->assertEquals('0123456789abcdef', $this->session->getId());
}
public function testSetName()
{
$this->assertEquals('MOCKSESSID', $this->session->getName());
$this->session->setName('session.test.com');
$this->session->start();
$this->assertEquals('session.test.com', $this->session->getName());
}
public function testGet()
{
// tests defaults
$this->assertNull($this->session->get('foo'));
$this->assertEquals(1, $this->session->get('foo', 1));
}
/**
* @dataProvider setProvider
*/
public function testSet($key, $value)
{
$this->session->set($key, $value);
$this->assertEquals($value, $this->session->get($key));
}
/**
* @dataProvider setProvider
*/
public function testHas($key, $value)
{
$this->session->set($key, $value);
$this->assertTrue($this->session->has($key));
$this->assertFalse($this->session->has($key.'non_value'));
}
public function testReplace()
{
$this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
$this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
$this->session->replace(array());
$this->assertEquals(array(), $this->session->all());
}
/**
* @dataProvider setProvider
*/
public function testAll($key, $value, $result)
{
$this->session->set($key, $value);
$this->assertEquals($result, $this->session->all());
}
/**
* @dataProvider setProvider
*/
public function testClear($key, $value)
{
$this->session->set('hi', 'fabien');
$this->session->set($key, $value);
$this->session->clear();
$this->assertEquals(array(), $this->session->all());
}
public function setProvider()
{
return array(
array('foo', 'bar', array('foo' => 'bar')),
array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
array('great', 'symfony is great', array('great' => 'symfony is great')),
);
}
/**
* @dataProvider setProvider
*/
public function testRemove($key, $value)
{
$this->session->set('hi.world', 'have a nice day');
$this->session->set($key, $value);
$this->session->remove($key);
$this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
}
public function testInvalidate()
{
$this->session->set('invalidate', 123);
$this->session->invalidate();
$this->assertEquals(array(), $this->session->all());
}
public function testMigrate()
{
$this->session->set('migrate', 321);
$this->session->migrate();
$this->assertEquals(321, $this->session->get('migrate'));
}
public function testMigrateDestroy()
{
$this->session->set('migrate', 333);
$this->session->migrate(true);
$this->assertEquals(333, $this->session->get('migrate'));
}
public function testSave()
{
$this->session->start();
$this->session->save();
$this->assertFalse($this->session->isStarted());
}
public function testGetId()
{
$this->assertEquals('', $this->session->getId());
$this->session->start();
$this->assertNotEquals('', $this->session->getId());
}
public function testGetFlashBag()
{
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
}
public function testGetIterator()
{
$attributes = array('hello' => 'world', 'symfony' => 'rocks');
foreach ($attributes as $key => $val) {
$this->session->set($key, $val);
}
$i = 0;
foreach ($this->session as $key => $val) {
$this->assertEquals($attributes[$key], $val);
++$i;
}
$this->assertEquals(count($attributes), $i);
}
public function testGetCount()
{
$this->session->set('hello', 'world');
$this->session->set('symfony', 'rocks');
$this->assertCount(2, $this->session);
}
public function testGetMeta()
{
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
}
}

View File

@@ -0,0 +1,133 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
/**
* @requires extension memcache
* @group time-sensitive
*/
class MemcacheSessionHandlerTest extends TestCase
{
const PREFIX = 'prefix_';
const TTL = 1000;
/**
* @var MemcacheSessionHandler
*/
protected $storage;
protected $memcache;
protected function setUp()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('PHPUnit_MockObject cannot mock the Memcache class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
}
parent::setUp();
$this->memcache = $this->getMockBuilder('Memcache')->getMock();
$this->storage = new MemcacheSessionHandler(
$this->memcache,
array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
);
}
protected function tearDown()
{
$this->memcache = null;
$this->storage = null;
parent::tearDown();
}
public function testOpenSession()
{
$this->assertTrue($this->storage->open('', ''));
}
public function testCloseSession()
{
$this->assertTrue($this->storage->close());
}
public function testReadSession()
{
$this->memcache
->expects($this->once())
->method('get')
->with(self::PREFIX.'id')
;
$this->assertEquals('', $this->storage->read('id'));
}
public function testWriteSession()
{
$this->memcache
->expects($this->once())
->method('set')
->with(self::PREFIX.'id', 'data', 0, $this->equalTo(time() + self::TTL, 2))
->will($this->returnValue(true))
;
$this->assertTrue($this->storage->write('id', 'data'));
}
public function testDestroySession()
{
$this->memcache
->expects($this->once())
->method('delete')
->with(self::PREFIX.'id')
->will($this->returnValue(true))
;
$this->assertTrue($this->storage->destroy('id'));
}
public function testGcSession()
{
$this->assertTrue($this->storage->gc(123));
}
/**
* @dataProvider getOptionFixtures
*/
public function testSupportedOptions($options, $supported)
{
try {
new MemcacheSessionHandler($this->memcache, $options);
$this->assertTrue($supported);
} catch (\InvalidArgumentException $e) {
$this->assertFalse($supported);
}
}
public function getOptionFixtures()
{
return array(
array(array('prefix' => 'session'), true),
array(array('expiretime' => 100), true),
array(array('prefix' => 'session', 'expiretime' => 200), true),
array(array('expiretime' => 100, 'foo' => 'bar'), false),
);
}
public function testGetConnection()
{
$method = new \ReflectionMethod($this->storage, 'getMemcache');
$method->setAccessible(true);
$this->assertInstanceOf('\Memcache', $method->invoke($this->storage));
}
}

View File

@@ -0,0 +1,139 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;
/**
* @requires extension memcached
* @group time-sensitive
*/
class MemcachedSessionHandlerTest extends TestCase
{
const PREFIX = 'prefix_';
const TTL = 1000;
/**
* @var MemcachedSessionHandler
*/
protected $storage;
protected $memcached;
protected function setUp()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('PHPUnit_MockObject cannot mock the Memcached class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
}
parent::setUp();
if (version_compare(phpversion('memcached'), '2.2.0', '>=') && version_compare(phpversion('memcached'), '3.0.0b1', '<')) {
$this->markTestSkipped('Tests can only be run with memcached extension 2.1.0 or lower, or 3.0.0b1 or higher');
}
$this->memcached = $this->getMockBuilder('Memcached')->getMock();
$this->storage = new MemcachedSessionHandler(
$this->memcached,
array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
);
}
protected function tearDown()
{
$this->memcached = null;
$this->storage = null;
parent::tearDown();
}
public function testOpenSession()
{
$this->assertTrue($this->storage->open('', ''));
}
public function testCloseSession()
{
$this->assertTrue($this->storage->close());
}
public function testReadSession()
{
$this->memcached
->expects($this->once())
->method('get')
->with(self::PREFIX.'id')
;
$this->assertEquals('', $this->storage->read('id'));
}
public function testWriteSession()
{
$this->memcached
->expects($this->once())
->method('set')
->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
->will($this->returnValue(true))
;
$this->assertTrue($this->storage->write('id', 'data'));
}
public function testDestroySession()
{
$this->memcached
->expects($this->once())
->method('delete')
->with(self::PREFIX.'id')
->will($this->returnValue(true))
;
$this->assertTrue($this->storage->destroy('id'));
}
public function testGcSession()
{
$this->assertTrue($this->storage->gc(123));
}
/**
* @dataProvider getOptionFixtures
*/
public function testSupportedOptions($options, $supported)
{
try {
new MemcachedSessionHandler($this->memcached, $options);
$this->assertTrue($supported);
} catch (\InvalidArgumentException $e) {
$this->assertFalse($supported);
}
}
public function getOptionFixtures()
{
return array(
array(array('prefix' => 'session'), true),
array(array('expiretime' => 100), true),
array(array('prefix' => 'session', 'expiretime' => 200), true),
array(array('expiretime' => 100, 'foo' => 'bar'), false),
);
}
public function testGetConnection()
{
$method = new \ReflectionMethod($this->storage, 'getMemcached');
$method->setAccessible(true);
$this->assertInstanceOf('\Memcached', $method->invoke($this->storage));
}
}

View File

@@ -0,0 +1,332 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
* @group time-sensitive
*/
class MongoDbSessionHandlerTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mongo;
private $storage;
public $options;
protected function setUp()
{
parent::setUp();
if (extension_loaded('mongodb')) {
if (!class_exists('MongoDB\Client')) {
$this->markTestSkipped('The mongodb/mongodb package is required.');
}
} elseif (!extension_loaded('mongo')) {
$this->markTestSkipped('The Mongo or MongoDB extension is required.');
}
if (phpversion('mongodb')) {
$mongoClass = 'MongoDB\Client';
} else {
$mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient';
}
$this->mongo = $this->getMockBuilder($mongoClass)
->disableOriginalConstructor()
->getMock();
$this->options = array(
'id_field' => '_id',
'data_field' => 'data',
'time_field' => 'time',
'expiry_field' => 'expires_at',
'database' => 'sf2-test',
'collection' => 'session-test',
);
$this->storage = new MongoDbSessionHandler($this->mongo, $this->options);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testConstructorShouldThrowExceptionForInvalidMongo()
{
new MongoDbSessionHandler(new \stdClass(), $this->options);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testConstructorShouldThrowExceptionForMissingOptions()
{
new MongoDbSessionHandler($this->mongo, array());
}
public function testOpenMethodAlwaysReturnTrue()
{
$this->assertTrue($this->storage->open('test', 'test'), 'The "open" method should always return true');
}
public function testCloseMethodAlwaysReturnTrue()
{
$this->assertTrue($this->storage->close(), 'The "close" method should always return true');
}
public function testRead()
{
$collection = $this->createMongoCollectionMock();
$this->mongo->expects($this->once())
->method('selectCollection')
->with($this->options['database'], $this->options['collection'])
->will($this->returnValue($collection));
// defining the timeout before the actual method call
// allows to test for "greater than" values in the $criteria
$testTimeout = time() + 1;
$collection->expects($this->once())
->method('findOne')
->will($this->returnCallback(function ($criteria) use ($testTimeout) {
$this->assertArrayHasKey($this->options['id_field'], $criteria);
$this->assertEquals($criteria[$this->options['id_field']], 'foo');
$this->assertArrayHasKey($this->options['expiry_field'], $criteria);
$this->assertArrayHasKey('$gte', $criteria[$this->options['expiry_field']]);
if (phpversion('mongodb')) {
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $criteria[$this->options['expiry_field']]['$gte']);
$this->assertGreaterThanOrEqual(round((string) $criteria[$this->options['expiry_field']]['$gte'] / 1000), $testTimeout);
} else {
$this->assertInstanceOf('MongoDate', $criteria[$this->options['expiry_field']]['$gte']);
$this->assertGreaterThanOrEqual($criteria[$this->options['expiry_field']]['$gte']->sec, $testTimeout);
}
$fields = array(
$this->options['id_field'] => 'foo',
);
if (phpversion('mongodb')) {
$fields[$this->options['data_field']] = new \MongoDB\BSON\Binary('bar', \MongoDB\BSON\Binary::TYPE_OLD_BINARY);
$fields[$this->options['id_field']] = new \MongoDB\BSON\UTCDateTime(time() * 1000);
} else {
$fields[$this->options['data_field']] = new \MongoBinData('bar', \MongoBinData::BYTE_ARRAY);
$fields[$this->options['id_field']] = new \MongoDate();
}
return $fields;
}));
$this->assertEquals('bar', $this->storage->read('foo'));
}
public function testWrite()
{
$collection = $this->createMongoCollectionMock();
$this->mongo->expects($this->once())
->method('selectCollection')
->with($this->options['database'], $this->options['collection'])
->will($this->returnValue($collection));
$data = array();
$methodName = phpversion('mongodb') ? 'updateOne' : 'update';
$collection->expects($this->once())
->method($methodName)
->will($this->returnCallback(function ($criteria, $updateData, $options) use (&$data) {
$this->assertEquals(array($this->options['id_field'] => 'foo'), $criteria);
if (phpversion('mongodb')) {
$this->assertEquals(array('upsert' => true), $options);
} else {
$this->assertEquals(array('upsert' => true, 'multiple' => false), $options);
}
$data = $updateData['$set'];
}));
$expectedExpiry = time() + (int) ini_get('session.gc_maxlifetime');
$this->assertTrue($this->storage->write('foo', 'bar'));
if (phpversion('mongodb')) {
$this->assertEquals('bar', $data[$this->options['data_field']]->getData());
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $data[$this->options['time_field']]);
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $data[$this->options['expiry_field']]);
$this->assertGreaterThanOrEqual($expectedExpiry, round((string) $data[$this->options['expiry_field']] / 1000));
} else {
$this->assertEquals('bar', $data[$this->options['data_field']]->bin);
$this->assertInstanceOf('MongoDate', $data[$this->options['time_field']]);
$this->assertInstanceOf('MongoDate', $data[$this->options['expiry_field']]);
$this->assertGreaterThanOrEqual($expectedExpiry, $data[$this->options['expiry_field']]->sec);
}
}
public function testWriteWhenUsingExpiresField()
{
$this->options = array(
'id_field' => '_id',
'data_field' => 'data',
'time_field' => 'time',
'database' => 'sf2-test',
'collection' => 'session-test',
'expiry_field' => 'expiresAt',
);
$this->storage = new MongoDbSessionHandler($this->mongo, $this->options);
$collection = $this->createMongoCollectionMock();
$this->mongo->expects($this->once())
->method('selectCollection')
->with($this->options['database'], $this->options['collection'])
->will($this->returnValue($collection));
$data = array();
$methodName = phpversion('mongodb') ? 'updateOne' : 'update';
$collection->expects($this->once())
->method($methodName)
->will($this->returnCallback(function ($criteria, $updateData, $options) use (&$data) {
$this->assertEquals(array($this->options['id_field'] => 'foo'), $criteria);
if (phpversion('mongodb')) {
$this->assertEquals(array('upsert' => true), $options);
} else {
$this->assertEquals(array('upsert' => true, 'multiple' => false), $options);
}
$data = $updateData['$set'];
}));
$this->assertTrue($this->storage->write('foo', 'bar'));
if (phpversion('mongodb')) {
$this->assertEquals('bar', $data[$this->options['data_field']]->getData());
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $data[$this->options['time_field']]);
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $data[$this->options['expiry_field']]);
} else {
$this->assertEquals('bar', $data[$this->options['data_field']]->bin);
$this->assertInstanceOf('MongoDate', $data[$this->options['time_field']]);
$this->assertInstanceOf('MongoDate', $data[$this->options['expiry_field']]);
}
}
public function testReplaceSessionData()
{
$collection = $this->createMongoCollectionMock();
$this->mongo->expects($this->once())
->method('selectCollection')
->with($this->options['database'], $this->options['collection'])
->will($this->returnValue($collection));
$data = array();
$methodName = phpversion('mongodb') ? 'updateOne' : 'update';
$collection->expects($this->exactly(2))
->method($methodName)
->will($this->returnCallback(function ($criteria, $updateData, $options) use (&$data) {
$data = $updateData;
}));
$this->storage->write('foo', 'bar');
$this->storage->write('foo', 'foobar');
if (phpversion('mongodb')) {
$this->assertEquals('foobar', $data['$set'][$this->options['data_field']]->getData());
} else {
$this->assertEquals('foobar', $data['$set'][$this->options['data_field']]->bin);
}
}
public function testDestroy()
{
$collection = $this->createMongoCollectionMock();
$this->mongo->expects($this->once())
->method('selectCollection')
->with($this->options['database'], $this->options['collection'])
->will($this->returnValue($collection));
$methodName = phpversion('mongodb') ? 'deleteOne' : 'remove';
$collection->expects($this->once())
->method($methodName)
->with(array($this->options['id_field'] => 'foo'));
$this->assertTrue($this->storage->destroy('foo'));
}
public function testGc()
{
$collection = $this->createMongoCollectionMock();
$this->mongo->expects($this->once())
->method('selectCollection')
->with($this->options['database'], $this->options['collection'])
->will($this->returnValue($collection));
$methodName = phpversion('mongodb') ? 'deleteOne' : 'remove';
$collection->expects($this->once())
->method($methodName)
->will($this->returnCallback(function ($criteria) {
if (phpversion('mongodb')) {
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $criteria[$this->options['expiry_field']]['$lt']);
$this->assertGreaterThanOrEqual(time() - 1, round((string) $criteria[$this->options['expiry_field']]['$lt'] / 1000));
} else {
$this->assertInstanceOf('MongoDate', $criteria[$this->options['expiry_field']]['$lt']);
$this->assertGreaterThanOrEqual(time() - 1, $criteria[$this->options['expiry_field']]['$lt']->sec);
}
}));
$this->assertTrue($this->storage->gc(1));
}
public function testGetConnection()
{
$method = new \ReflectionMethod($this->storage, 'getMongo');
$method->setAccessible(true);
if (phpversion('mongodb')) {
$mongoClass = 'MongoDB\Client';
} else {
$mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient';
}
$this->assertInstanceOf($mongoClass, $method->invoke($this->storage));
}
private function createMongoCollectionMock()
{
$collectionClass = 'MongoCollection';
if (phpversion('mongodb')) {
$collectionClass = 'MongoDB\Collection';
}
$collection = $this->getMockBuilder($collectionClass)
->disableOriginalConstructor()
->getMock();
return $collection;
}
}

View File

@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
/**
* Test class for NativeFileSessionHandler.
*
* @author Drak <drak@zikula.org>
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class NativeFileSessionHandlerTest extends TestCase
{
public function testConstruct()
{
$storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeFileSessionHandler(sys_get_temp_dir()));
$this->assertEquals('files', $storage->getSaveHandler()->getSaveHandlerName());
$this->assertEquals('user', ini_get('session.save_handler'));
$this->assertEquals(sys_get_temp_dir(), ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
}
/**
* @dataProvider savePathDataProvider
*/
public function testConstructSavePath($savePath, $expectedSavePath, $path)
{
$handler = new NativeFileSessionHandler($savePath);
$this->assertEquals($expectedSavePath, ini_get('session.save_path'));
$this->assertTrue(is_dir(realpath($path)));
rmdir($path);
}
public function savePathDataProvider()
{
$base = sys_get_temp_dir();
return array(
array("$base/foo", "$base/foo", "$base/foo"),
array("5;$base/foo", "5;$base/foo", "$base/foo"),
array("5;0600;$base/foo", "5;0600;$base/foo", "$base/foo"),
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testConstructException()
{
$handler = new NativeFileSessionHandler('something;invalid;with;too-many-args');
}
public function testConstructDefault()
{
$path = ini_get('session.save_path');
$storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeFileSessionHandler());
$this->assertEquals($path, ini_get('session.save_path'));
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
/**
* Test class for NativeSessionHandler.
*
* @author Drak <drak@zikula.org>
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class NativeSessionHandlerTest extends TestCase
{
public function testConstruct()
{
$handler = new NativeSessionHandler();
$this->assertTrue($handler instanceof \SessionHandler);
$this->assertTrue($handler instanceof NativeSessionHandler);
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Test class for NullSessionHandler.
*
* @author Drak <drak@zikula.org>
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class NullSessionHandlerTest extends TestCase
{
public function testSaveHandlers()
{
$storage = $this->getStorage();
$this->assertEquals('user', ini_get('session.save_handler'));
}
public function testSession()
{
session_id('nullsessionstorage');
$storage = $this->getStorage();
$session = new Session($storage);
$this->assertNull($session->get('something'));
$session->set('something', 'unique');
$this->assertEquals('unique', $session->get('something'));
}
public function testNothingIsPersisted()
{
session_id('nullsessionstorage');
$storage = $this->getStorage();
$session = new Session($storage);
$session->start();
$this->assertEquals('nullsessionstorage', $session->getId());
$this->assertNull($session->get('something'));
}
public function getStorage()
{
return new NativeSessionStorage(array(), new NullSessionHandler());
}
}

View File

@@ -0,0 +1,370 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
/**
* @requires extension pdo_sqlite
* @group time-sensitive
*/
class PdoSessionHandlerTest extends TestCase
{
private $dbFile;
protected function tearDown()
{
// make sure the temporary database file is deleted when it has been created (even when a test fails)
if ($this->dbFile) {
@unlink($this->dbFile);
}
parent::tearDown();
}
protected function getPersistentSqliteDsn()
{
$this->dbFile = tempnam(sys_get_temp_dir(), 'sf2_sqlite_sessions');
return 'sqlite:'.$this->dbFile;
}
protected function getMemorySqlitePdo()
{
$pdo = new \PDO('sqlite::memory:');
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$storage = new PdoSessionHandler($pdo);
$storage->createTable();
return $pdo;
}
/**
* @expectedException \InvalidArgumentException
*/
public function testWrongPdoErrMode()
{
$pdo = $this->getMemorySqlitePdo();
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
$storage = new PdoSessionHandler($pdo);
}
/**
* @expectedException \RuntimeException
*/
public function testInexistentTable()
{
$storage = new PdoSessionHandler($this->getMemorySqlitePdo(), array('db_table' => 'inexistent_table'));
$storage->open('', 'sid');
$storage->read('id');
$storage->write('id', 'data');
$storage->close();
}
/**
* @expectedException \RuntimeException
*/
public function testCreateTableTwice()
{
$storage = new PdoSessionHandler($this->getMemorySqlitePdo());
$storage->createTable();
}
public function testWithLazyDsnConnection()
{
$dsn = $this->getPersistentSqliteDsn();
$storage = new PdoSessionHandler($dsn);
$storage->createTable();
$storage->open('', 'sid');
$data = $storage->read('id');
$storage->write('id', 'data');
$storage->close();
$this->assertSame('', $data, 'New session returns empty string data');
$storage->open('', 'sid');
$data = $storage->read('id');
$storage->close();
$this->assertSame('data', $data, 'Written value can be read back correctly');
}
public function testWithLazySavePathConnection()
{
$dsn = $this->getPersistentSqliteDsn();
// Open is called with what ini_set('session.save_path', $dsn) would mean
$storage = new PdoSessionHandler(null);
$storage->open($dsn, 'sid');
$storage->createTable();
$data = $storage->read('id');
$storage->write('id', 'data');
$storage->close();
$this->assertSame('', $data, 'New session returns empty string data');
$storage->open($dsn, 'sid');
$data = $storage->read('id');
$storage->close();
$this->assertSame('data', $data, 'Written value can be read back correctly');
}
public function testReadWriteReadWithNullByte()
{
$sessionData = 'da'."\0".'ta';
$storage = new PdoSessionHandler($this->getMemorySqlitePdo());
$storage->open('', 'sid');
$readData = $storage->read('id');
$storage->write('id', $sessionData);
$storage->close();
$this->assertSame('', $readData, 'New session returns empty string data');
$storage->open('', 'sid');
$readData = $storage->read('id');
$storage->close();
$this->assertSame($sessionData, $readData, 'Written value can be read back correctly');
}
public function testReadConvertsStreamToString()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('PHPUnit_MockObject cannot mock the PDOStatement class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
}
$pdo = new MockPdo('pgsql');
$pdo->prepareResult = $this->getMockBuilder('PDOStatement')->getMock();
$content = 'foobar';
$stream = $this->createStream($content);
$pdo->prepareResult->expects($this->once())->method('fetchAll')
->will($this->returnValue(array(array($stream, 42, time()))));
$storage = new PdoSessionHandler($pdo);
$result = $storage->read('foo');
$this->assertSame($content, $result);
}
public function testReadLockedConvertsStreamToString()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('PHPUnit_MockObject cannot mock the PDOStatement class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
}
$pdo = new MockPdo('pgsql');
$selectStmt = $this->getMockBuilder('PDOStatement')->getMock();
$insertStmt = $this->getMockBuilder('PDOStatement')->getMock();
$pdo->prepareResult = function ($statement) use ($selectStmt, $insertStmt) {
return 0 === strpos($statement, 'INSERT') ? $insertStmt : $selectStmt;
};
$content = 'foobar';
$stream = $this->createStream($content);
$exception = null;
$selectStmt->expects($this->atLeast(2))->method('fetchAll')
->will($this->returnCallback(function () use (&$exception, $stream) {
return $exception ? array(array($stream, 42, time())) : array();
}));
$insertStmt->expects($this->once())->method('execute')
->will($this->returnCallback(function () use (&$exception) {
throw $exception = new \PDOException('', '23');
}));
$storage = new PdoSessionHandler($pdo);
$result = $storage->read('foo');
$this->assertSame($content, $result);
}
public function testReadingRequiresExactlySameId()
{
$storage = new PdoSessionHandler($this->getMemorySqlitePdo());
$storage->open('', 'sid');
$storage->write('id', 'data');
$storage->write('test', 'data');
$storage->write('space ', 'data');
$storage->close();
$storage->open('', 'sid');
$readDataCaseSensitive = $storage->read('ID');
$readDataNoCharFolding = $storage->read('tést');
$readDataKeepSpace = $storage->read('space ');
$readDataExtraSpace = $storage->read('space ');
$storage->close();
$this->assertSame('', $readDataCaseSensitive, 'Retrieval by ID should be case-sensitive (collation setting)');
$this->assertSame('', $readDataNoCharFolding, 'Retrieval by ID should not do character folding (collation setting)');
$this->assertSame('data', $readDataKeepSpace, 'Retrieval by ID requires spaces as-is');
$this->assertSame('', $readDataExtraSpace, 'Retrieval by ID requires spaces as-is');
}
/**
* Simulates session_regenerate_id(true) which will require an INSERT or UPDATE (replace).
*/
public function testWriteDifferentSessionIdThanRead()
{
$storage = new PdoSessionHandler($this->getMemorySqlitePdo());
$storage->open('', 'sid');
$storage->read('id');
$storage->destroy('id');
$storage->write('new_id', 'data_of_new_session_id');
$storage->close();
$storage->open('', 'sid');
$data = $storage->read('new_id');
$storage->close();
$this->assertSame('data_of_new_session_id', $data, 'Data of regenerated session id is available');
}
public function testWrongUsageStillWorks()
{
// wrong method sequence that should no happen, but still works
$storage = new PdoSessionHandler($this->getMemorySqlitePdo());
$storage->write('id', 'data');
$storage->write('other_id', 'other_data');
$storage->destroy('inexistent');
$storage->open('', 'sid');
$data = $storage->read('id');
$otherData = $storage->read('other_id');
$storage->close();
$this->assertSame('data', $data);
$this->assertSame('other_data', $otherData);
}
public function testSessionDestroy()
{
$pdo = $this->getMemorySqlitePdo();
$storage = new PdoSessionHandler($pdo);
$storage->open('', 'sid');
$storage->read('id');
$storage->write('id', 'data');
$storage->close();
$this->assertEquals(1, $pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn());
$storage->open('', 'sid');
$storage->read('id');
$storage->destroy('id');
$storage->close();
$this->assertEquals(0, $pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn());
$storage->open('', 'sid');
$data = $storage->read('id');
$storage->close();
$this->assertSame('', $data, 'Destroyed session returns empty string');
}
public function testSessionGC()
{
$previousLifeTime = ini_set('session.gc_maxlifetime', 1000);
$pdo = $this->getMemorySqlitePdo();
$storage = new PdoSessionHandler($pdo);
$storage->open('', 'sid');
$storage->read('id');
$storage->write('id', 'data');
$storage->close();
$storage->open('', 'sid');
$storage->read('gc_id');
ini_set('session.gc_maxlifetime', -1); // test that you can set lifetime of a session after it has been read
$storage->write('gc_id', 'data');
$storage->close();
$this->assertEquals(2, $pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn(), 'No session pruned because gc not called');
$storage->open('', 'sid');
$data = $storage->read('gc_id');
$storage->gc(-1);
$storage->close();
ini_set('session.gc_maxlifetime', $previousLifeTime);
$this->assertSame('', $data, 'Session already considered garbage, so not returning data even if it is not pruned yet');
$this->assertEquals(1, $pdo->query('SELECT COUNT(*) FROM sessions')->fetchColumn(), 'Expired session is pruned');
}
public function testGetConnection()
{
$storage = new PdoSessionHandler($this->getMemorySqlitePdo());
$method = new \ReflectionMethod($storage, 'getConnection');
$method->setAccessible(true);
$this->assertInstanceOf('\PDO', $method->invoke($storage));
}
public function testGetConnectionConnectsIfNeeded()
{
$storage = new PdoSessionHandler('sqlite::memory:');
$method = new \ReflectionMethod($storage, 'getConnection');
$method->setAccessible(true);
$this->assertInstanceOf('\PDO', $method->invoke($storage));
}
private function createStream($content)
{
$stream = tmpfile();
fwrite($stream, $content);
fseek($stream, 0);
return $stream;
}
}
class MockPdo extends \PDO
{
public $prepareResult;
private $driverName;
private $errorMode;
public function __construct($driverName = null, $errorMode = null)
{
$this->driverName = $driverName;
$this->errorMode = null !== $errorMode ?: \PDO::ERRMODE_EXCEPTION;
}
public function getAttribute($attribute)
{
if (\PDO::ATTR_ERRMODE === $attribute) {
return $this->errorMode;
}
if (\PDO::ATTR_DRIVER_NAME === $attribute) {
return $this->driverName;
}
return parent::getAttribute($attribute);
}
public function prepare($statement, $driverOptions = array())
{
return is_callable($this->prepareResult)
? call_user_func($this->prepareResult, $statement, $driverOptions)
: $this->prepareResult;
}
public function beginTransaction()
{
}
public function rollBack()
{
}
}

View File

@@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\WriteCheckSessionHandler;
/**
* @author Adrien Brault <adrien.brault@gmail.com>
*/
class WriteCheckSessionHandlerTest extends TestCase
{
public function test()
{
$wrappedSessionHandlerMock = $this->getMockBuilder('SessionHandlerInterface')->getMock();
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock);
$wrappedSessionHandlerMock
->expects($this->once())
->method('close')
->with()
->will($this->returnValue(true))
;
$this->assertTrue($writeCheckSessionHandler->close());
}
public function testWrite()
{
$wrappedSessionHandlerMock = $this->getMockBuilder('SessionHandlerInterface')->getMock();
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock);
$wrappedSessionHandlerMock
->expects($this->once())
->method('write')
->with('foo', 'bar')
->will($this->returnValue(true))
;
$this->assertTrue($writeCheckSessionHandler->write('foo', 'bar'));
}
public function testSkippedWrite()
{
$wrappedSessionHandlerMock = $this->getMockBuilder('SessionHandlerInterface')->getMock();
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock);
$wrappedSessionHandlerMock
->expects($this->once())
->method('read')
->with('foo')
->will($this->returnValue('bar'))
;
$wrappedSessionHandlerMock
->expects($this->never())
->method('write')
;
$this->assertEquals('bar', $writeCheckSessionHandler->read('foo'));
$this->assertTrue($writeCheckSessionHandler->write('foo', 'bar'));
}
public function testNonSkippedWrite()
{
$wrappedSessionHandlerMock = $this->getMockBuilder('SessionHandlerInterface')->getMock();
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock);
$wrappedSessionHandlerMock
->expects($this->once())
->method('read')
->with('foo')
->will($this->returnValue('bar'))
;
$wrappedSessionHandlerMock
->expects($this->once())
->method('write')
->with('foo', 'baZZZ')
->will($this->returnValue(true))
;
$this->assertEquals('bar', $writeCheckSessionHandler->read('foo'));
$this->assertTrue($writeCheckSessionHandler->write('foo', 'baZZZ'));
}
}

View File

@@ -0,0 +1,142 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
/**
* Test class for MetadataBag.
*
* @group time-sensitive
*/
class MetadataBagTest extends TestCase
{
/**
* @var MetadataBag
*/
protected $bag;
/**
* @var array
*/
protected $array = array();
protected function setUp()
{
parent::setUp();
$this->bag = new MetadataBag();
$this->array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 0);
$this->bag->initialize($this->array);
}
protected function tearDown()
{
$this->array = array();
$this->bag = null;
parent::tearDown();
}
public function testInitialize()
{
$sessionMetadata = array();
$bag1 = new MetadataBag();
$bag1->initialize($sessionMetadata);
$this->assertGreaterThanOrEqual(time(), $bag1->getCreated());
$this->assertEquals($bag1->getCreated(), $bag1->getLastUsed());
sleep(1);
$bag2 = new MetadataBag();
$bag2->initialize($sessionMetadata);
$this->assertEquals($bag1->getCreated(), $bag2->getCreated());
$this->assertEquals($bag1->getLastUsed(), $bag2->getLastUsed());
$this->assertEquals($bag2->getCreated(), $bag2->getLastUsed());
sleep(1);
$bag3 = new MetadataBag();
$bag3->initialize($sessionMetadata);
$this->assertEquals($bag1->getCreated(), $bag3->getCreated());
$this->assertGreaterThan($bag2->getLastUsed(), $bag3->getLastUsed());
$this->assertNotEquals($bag3->getCreated(), $bag3->getLastUsed());
}
public function testGetSetName()
{
$this->assertEquals('__metadata', $this->bag->getName());
$this->bag->setName('foo');
$this->assertEquals('foo', $this->bag->getName());
}
public function testGetStorageKey()
{
$this->assertEquals('_sf2_meta', $this->bag->getStorageKey());
}
public function testGetLifetime()
{
$bag = new MetadataBag();
$array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 1000);
$bag->initialize($array);
$this->assertEquals(1000, $bag->getLifetime());
}
public function testGetCreated()
{
$this->assertEquals(1234567, $this->bag->getCreated());
}
public function testGetLastUsed()
{
$this->assertLessThanOrEqual(time(), $this->bag->getLastUsed());
}
public function testClear()
{
$this->bag->clear();
// the clear method has no side effects, we just want to ensure it doesn't trigger any exceptions
$this->addToAssertionCount(1);
}
public function testSkipLastUsedUpdate()
{
$bag = new MetadataBag('', 30);
$timeStamp = time();
$created = $timeStamp - 15;
$sessionMetadata = array(
MetadataBag::CREATED => $created,
MetadataBag::UPDATED => $created,
MetadataBag::LIFETIME => 1000,
);
$bag->initialize($sessionMetadata);
$this->assertEquals($created, $sessionMetadata[MetadataBag::UPDATED]);
}
public function testDoesNotSkipLastUsedUpdate()
{
$bag = new MetadataBag('', 30);
$timeStamp = time();
$created = $timeStamp - 45;
$sessionMetadata = array(
MetadataBag::CREATED => $created,
MetadataBag::UPDATED => $created,
MetadataBag::LIFETIME => 1000,
);
$bag->initialize($sessionMetadata);
$this->assertEquals($timeStamp, $sessionMetadata[MetadataBag::UPDATED]);
}
}

View File

@@ -0,0 +1,131 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
/**
* Test class for MockArraySessionStorage.
*
* @author Drak <drak@zikula.org>
*/
class MockArraySessionStorageTest extends TestCase
{
/**
* @var MockArraySessionStorage
*/
private $storage;
/**
* @var AttributeBag
*/
private $attributes;
/**
* @var FlashBag
*/
private $flashes;
private $data;
protected function setUp()
{
$this->attributes = new AttributeBag();
$this->flashes = new FlashBag();
$this->data = array(
$this->attributes->getStorageKey() => array('foo' => 'bar'),
$this->flashes->getStorageKey() => array('notice' => 'hello'),
);
$this->storage = new MockArraySessionStorage();
$this->storage->registerBag($this->flashes);
$this->storage->registerBag($this->attributes);
$this->storage->setSessionData($this->data);
}
protected function tearDown()
{
$this->data = null;
$this->flashes = null;
$this->attributes = null;
$this->storage = null;
}
public function testStart()
{
$this->assertEquals('', $this->storage->getId());
$this->storage->start();
$id = $this->storage->getId();
$this->assertNotEquals('', $id);
$this->storage->start();
$this->assertEquals($id, $this->storage->getId());
}
public function testRegenerate()
{
$this->storage->start();
$id = $this->storage->getId();
$this->storage->regenerate();
$this->assertNotEquals($id, $this->storage->getId());
$this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all());
$this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->peekAll());
$id = $this->storage->getId();
$this->storage->regenerate(true);
$this->assertNotEquals($id, $this->storage->getId());
$this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all());
$this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->peekAll());
}
public function testGetId()
{
$this->assertEquals('', $this->storage->getId());
$this->storage->start();
$this->assertNotEquals('', $this->storage->getId());
}
public function testClearClearsBags()
{
$this->storage->clear();
$this->assertSame(array(), $this->storage->getBag('attributes')->all());
$this->assertSame(array(), $this->storage->getBag('flashes')->peekAll());
}
public function testClearStartsSession()
{
$this->storage->clear();
$this->assertTrue($this->storage->isStarted());
}
public function testClearWithNoBagsStartsSession()
{
$storage = new MockArraySessionStorage();
$storage->clear();
$this->assertTrue($storage->isStarted());
}
/**
* @expectedException \RuntimeException
*/
public function testUnstartedSave()
{
$this->storage->save();
}
}

View File

@@ -0,0 +1,127 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
/**
* Test class for MockFileSessionStorage.
*
* @author Drak <drak@zikula.org>
*/
class MockFileSessionStorageTest extends TestCase
{
/**
* @var string
*/
private $sessionDir;
/**
* @var MockFileSessionStorage
*/
protected $storage;
protected function setUp()
{
$this->sessionDir = sys_get_temp_dir().'/sf2test';
$this->storage = $this->getStorage();
}
protected function tearDown()
{
$this->sessionDir = null;
$this->storage = null;
array_map('unlink', glob($this->sessionDir.'/*.session'));
if (is_dir($this->sessionDir)) {
rmdir($this->sessionDir);
}
}
public function testStart()
{
$this->assertEquals('', $this->storage->getId());
$this->assertTrue($this->storage->start());
$id = $this->storage->getId();
$this->assertNotEquals('', $this->storage->getId());
$this->assertTrue($this->storage->start());
$this->assertEquals($id, $this->storage->getId());
}
public function testRegenerate()
{
$this->storage->start();
$this->storage->getBag('attributes')->set('regenerate', 1234);
$this->storage->regenerate();
$this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
$this->storage->regenerate(true);
$this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
}
public function testGetId()
{
$this->assertEquals('', $this->storage->getId());
$this->storage->start();
$this->assertNotEquals('', $this->storage->getId());
}
public function testSave()
{
$this->storage->start();
$id = $this->storage->getId();
$this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
$this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
$this->storage->getBag('attributes')->set('new', '108');
$this->storage->getBag('flashes')->set('newkey', 'test');
$this->storage->save();
$storage = $this->getStorage();
$storage->setId($id);
$storage->start();
$this->assertEquals('108', $storage->getBag('attributes')->get('new'));
$this->assertTrue($storage->getBag('flashes')->has('newkey'));
$this->assertEquals(array('test'), $storage->getBag('flashes')->peek('newkey'));
}
public function testMultipleInstances()
{
$storage1 = $this->getStorage();
$storage1->start();
$storage1->getBag('attributes')->set('foo', 'bar');
$storage1->save();
$storage2 = $this->getStorage();
$storage2->setId($storage1->getId());
$storage2->start();
$this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
}
/**
* @expectedException \RuntimeException
*/
public function testSaveWithoutStart()
{
$storage1 = $this->getStorage();
$storage1->save();
}
private function getStorage()
{
$storage = new MockFileSessionStorage($this->sessionDir);
$storage->registerBag(new FlashBag());
$storage->registerBag(new AttributeBag());
return $storage;
}
}

View File

@@ -0,0 +1,247 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
/**
* Test class for NativeSessionStorage.
*
* @author Drak <drak@zikula.org>
*
* These tests require separate processes.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class NativeSessionStorageTest extends TestCase
{
private $savePath;
protected function setUp()
{
$this->iniSet('session.save_handler', 'files');
$this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
if (!is_dir($this->savePath)) {
mkdir($this->savePath);
}
}
protected function tearDown()
{
session_write_close();
array_map('unlink', glob($this->savePath.'/*'));
if (is_dir($this->savePath)) {
rmdir($this->savePath);
}
$this->savePath = null;
}
/**
* @param array $options
*
* @return NativeSessionStorage
*/
protected function getStorage(array $options = array())
{
$storage = new NativeSessionStorage($options);
$storage->registerBag(new AttributeBag());
return $storage;
}
public function testBag()
{
$storage = $this->getStorage();
$bag = new FlashBag();
$storage->registerBag($bag);
$this->assertSame($bag, $storage->getBag($bag->getName()));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testRegisterBagException()
{
$storage = $this->getStorage();
$storage->getBag('non_existing');
}
/**
* @expectedException \LogicException
*/
public function testRegisterBagForAStartedSessionThrowsException()
{
$storage = $this->getStorage();
$storage->start();
$storage->registerBag(new AttributeBag());
}
public function testGetId()
{
$storage = $this->getStorage();
$this->assertSame('', $storage->getId(), 'Empty ID before starting session');
$storage->start();
$id = $storage->getId();
$this->assertInternalType('string', $id);
$this->assertNotSame('', $id);
$storage->save();
$this->assertSame($id, $storage->getId(), 'ID stays after saving session');
}
public function testRegenerate()
{
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$storage->getBag('attributes')->set('lucky', 7);
$storage->regenerate();
$this->assertNotEquals($id, $storage->getId());
$this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
}
public function testRegenerateDestroy()
{
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$storage->getBag('attributes')->set('legs', 11);
$storage->regenerate(true);
$this->assertNotEquals($id, $storage->getId());
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
}
public function testSessionGlobalIsUpToDateAfterIdRegeneration()
{
$storage = $this->getStorage();
$storage->start();
$storage->getBag('attributes')->set('lucky', 7);
$storage->regenerate();
$storage->getBag('attributes')->set('lucky', 42);
$this->assertEquals(42, $_SESSION['_sf2_attributes']['lucky']);
}
public function testRegenerationFailureDoesNotFlagStorageAsStarted()
{
$storage = $this->getStorage();
$this->assertFalse($storage->regenerate());
$this->assertFalse($storage->isStarted());
}
public function testDefaultSessionCacheLimiter()
{
$this->iniSet('session.cache_limiter', 'nocache');
$storage = new NativeSessionStorage();
$this->assertEquals('', ini_get('session.cache_limiter'));
}
public function testExplicitSessionCacheLimiter()
{
$this->iniSet('session.cache_limiter', 'nocache');
$storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
$this->assertEquals('public', ini_get('session.cache_limiter'));
}
public function testCookieOptions()
{
$options = array(
'cookie_lifetime' => 123456,
'cookie_path' => '/my/cookie/path',
'cookie_domain' => 'symfony.example.com',
'cookie_secure' => true,
'cookie_httponly' => false,
);
$this->getStorage($options);
$temp = session_get_cookie_params();
$gco = array();
foreach ($temp as $key => $value) {
$gco['cookie_'.$key] = $value;
}
$this->assertEquals($options, $gco);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testSetSaveHandlerException()
{
$storage = $this->getStorage();
$storage->setSaveHandler(new \stdClass());
}
public function testSetSaveHandler()
{
$this->iniSet('session.save_handler', 'files');
$storage = $this->getStorage();
$storage->setSaveHandler();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
$storage->setSaveHandler(null);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
$storage->setSaveHandler(new SessionHandlerProxy(new NativeSessionHandler()));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
$storage->setSaveHandler(new NativeSessionHandler());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
$storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
$storage->setSaveHandler(new NullSessionHandler());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
}
/**
* @expectedException \RuntimeException
*/
public function testStarted()
{
$storage = $this->getStorage();
$this->assertFalse($storage->getSaveHandler()->isActive());
$this->assertFalse($storage->isStarted());
session_start();
$this->assertTrue(isset($_SESSION));
$this->assertTrue($storage->getSaveHandler()->isActive());
// PHP session might have started, but the storage driver has not, so false is correct here
$this->assertFalse($storage->isStarted());
$key = $storage->getMetadataBag()->getStorageKey();
$this->assertFalse(isset($_SESSION[$key]));
$storage->start();
}
public function testRestart()
{
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$storage->getBag('attributes')->set('lucky', 7);
$storage->save();
$storage->start();
$this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
$this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
}
}

View File

@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
/**
* Test class for PhpSessionStorage.
*
* @author Drak <drak@zikula.org>
*
* These tests require separate processes.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class PhpBridgeSessionStorageTest extends TestCase
{
private $savePath;
protected function setUp()
{
$this->iniSet('session.save_handler', 'files');
$this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
if (!is_dir($this->savePath)) {
mkdir($this->savePath);
}
}
protected function tearDown()
{
session_write_close();
array_map('unlink', glob($this->savePath.'/*'));
if (is_dir($this->savePath)) {
rmdir($this->savePath);
}
$this->savePath = null;
}
/**
* @return PhpBridgeSessionStorage
*/
protected function getStorage()
{
$storage = new PhpBridgeSessionStorage();
$storage->registerBag(new AttributeBag());
return $storage;
}
public function testPhpSession()
{
$storage = $this->getStorage();
$this->assertFalse($storage->getSaveHandler()->isActive());
$this->assertFalse($storage->isStarted());
session_start();
$this->assertTrue(isset($_SESSION));
// in PHP 5.4 we can reliably detect a session started
$this->assertTrue($storage->getSaveHandler()->isActive());
// PHP session might have started, but the storage driver has not, so false is correct here
$this->assertFalse($storage->isStarted());
$key = $storage->getMetadataBag()->getStorageKey();
$this->assertFalse(isset($_SESSION[$key]));
$storage->start();
$this->assertTrue(isset($_SESSION[$key]));
}
public function testClear()
{
$storage = $this->getStorage();
session_start();
$_SESSION['drak'] = 'loves symfony';
$storage->getBag('attributes')->set('symfony', 'greatness');
$key = $storage->getBag('attributes')->getStorageKey();
$this->assertEquals($_SESSION[$key], array('symfony' => 'greatness'));
$this->assertEquals($_SESSION['drak'], 'loves symfony');
$storage->clear();
$this->assertEquals($_SESSION[$key], array());
$this->assertEquals($_SESSION['drak'], 'loves symfony');
}
}

View File

@@ -0,0 +1,145 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Proxy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
// Note until PHPUnit_Mock_Objects 1.2 is released you cannot mock abstracts due to
// https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73
class ConcreteProxy extends AbstractProxy
{
}
class ConcreteSessionHandlerInterfaceProxy extends AbstractProxy implements \SessionHandlerInterface
{
public function open($savePath, $sessionName)
{
}
public function close()
{
}
public function read($id)
{
}
public function write($id, $data)
{
}
public function destroy($id)
{
}
public function gc($maxlifetime)
{
}
}
/**
* Test class for AbstractProxy.
*
* @author Drak <drak@zikula.org>
*/
class AbstractProxyTest extends TestCase
{
/**
* @var AbstractProxy
*/
protected $proxy;
protected function setUp()
{
$this->proxy = new ConcreteProxy();
}
protected function tearDown()
{
$this->proxy = null;
}
public function testGetSaveHandlerName()
{
$this->assertNull($this->proxy->getSaveHandlerName());
}
public function testIsSessionHandlerInterface()
{
$this->assertFalse($this->proxy->isSessionHandlerInterface());
$sh = new ConcreteSessionHandlerInterfaceProxy();
$this->assertTrue($sh->isSessionHandlerInterface());
}
public function testIsWrapper()
{
$this->assertFalse($this->proxy->isWrapper());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsActive()
{
$this->assertFalse($this->proxy->isActive());
session_start();
$this->assertTrue($this->proxy->isActive());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testName()
{
$this->assertEquals(session_name(), $this->proxy->getName());
$this->proxy->setName('foo');
$this->assertEquals('foo', $this->proxy->getName());
$this->assertEquals(session_name(), $this->proxy->getName());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @expectedException \LogicException
*/
public function testNameException()
{
session_start();
$this->proxy->setName('foo');
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testId()
{
$this->assertEquals(session_id(), $this->proxy->getId());
$this->proxy->setId('foo');
$this->assertEquals('foo', $this->proxy->getId());
$this->assertEquals(session_id(), $this->proxy->getId());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @expectedException \LogicException
*/
public function testIdException()
{
session_start();
$this->proxy->setId('foo');
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Proxy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
/**
* Test class for NativeProxy.
*
* @author Drak <drak@zikula.org>
*/
class NativeProxyTest extends TestCase
{
public function testIsWrapper()
{
$proxy = new NativeProxy();
$this->assertFalse($proxy->isWrapper());
}
public function testGetSaveHandlerName()
{
$name = ini_get('session.save_handler');
$proxy = new NativeProxy();
$this->assertEquals($name, $proxy->getSaveHandlerName());
}
}

View File

@@ -0,0 +1,124 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Proxy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
/**
* Tests for SessionHandlerProxy class.
*
* @author Drak <drak@zikula.org>
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class SessionHandlerProxyTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_Matcher
*/
private $mock;
/**
* @var SessionHandlerProxy
*/
private $proxy;
protected function setUp()
{
$this->mock = $this->getMockBuilder('SessionHandlerInterface')->getMock();
$this->proxy = new SessionHandlerProxy($this->mock);
}
protected function tearDown()
{
$this->mock = null;
$this->proxy = null;
}
public function testOpenTrue()
{
$this->mock->expects($this->once())
->method('open')
->will($this->returnValue(true));
$this->assertFalse($this->proxy->isActive());
$this->proxy->open('name', 'id');
$this->assertFalse($this->proxy->isActive());
}
public function testOpenFalse()
{
$this->mock->expects($this->once())
->method('open')
->will($this->returnValue(false));
$this->assertFalse($this->proxy->isActive());
$this->proxy->open('name', 'id');
$this->assertFalse($this->proxy->isActive());
}
public function testClose()
{
$this->mock->expects($this->once())
->method('close')
->will($this->returnValue(true));
$this->assertFalse($this->proxy->isActive());
$this->proxy->close();
$this->assertFalse($this->proxy->isActive());
}
public function testCloseFalse()
{
$this->mock->expects($this->once())
->method('close')
->will($this->returnValue(false));
$this->assertFalse($this->proxy->isActive());
$this->proxy->close();
$this->assertFalse($this->proxy->isActive());
}
public function testRead()
{
$this->mock->expects($this->once())
->method('read');
$this->proxy->read('id');
}
public function testWrite()
{
$this->mock->expects($this->once())
->method('write');
$this->proxy->write('id', 'data');
}
public function testDestroy()
{
$this->mock->expects($this->once())
->method('destroy');
$this->proxy->destroy('id');
}
public function testGc()
{
$this->mock->expects($this->once())
->method('gc');
$this->proxy->gc(86400);
}
}

View File

@@ -0,0 +1,115 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class StreamedResponseTest extends TestCase
{
public function testConstructor()
{
$response = new StreamedResponse(function () { echo 'foo'; }, 404, array('Content-Type' => 'text/plain'));
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals('text/plain', $response->headers->get('Content-Type'));
}
public function testPrepareWith11Protocol()
{
$response = new StreamedResponse(function () { echo 'foo'; });
$request = Request::create('/');
$request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
$response->prepare($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertNotEquals('chunked', $response->headers->get('Transfer-Encoding'), 'Apache assumes responses with a Transfer-Encoding header set to chunked to already be encoded.');
}
public function testPrepareWith10Protocol()
{
$response = new StreamedResponse(function () { echo 'foo'; });
$request = Request::create('/');
$request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
$response->prepare($request);
$this->assertEquals('1.0', $response->getProtocolVersion());
$this->assertNull($response->headers->get('Transfer-Encoding'));
}
public function testPrepareWithHeadRequest()
{
$response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Content-Length' => '123'));
$request = Request::create('/', 'HEAD');
$response->prepare($request);
$this->assertSame('123', $response->headers->get('Content-Length'));
}
public function testPrepareWithCacheHeaders()
{
$response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public'));
$request = Request::create('/', 'GET');
$response->prepare($request);
$this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control'));
}
public function testSendContent()
{
$called = 0;
$response = new StreamedResponse(function () use (&$called) { ++$called; });
$response->sendContent();
$this->assertEquals(1, $called);
$response->sendContent();
$this->assertEquals(1, $called);
}
/**
* @expectedException \LogicException
*/
public function testSendContentWithNonCallable()
{
$response = new StreamedResponse(null);
$response->sendContent();
}
/**
* @expectedException \LogicException
*/
public function testSetContent()
{
$response = new StreamedResponse(function () { echo 'foo'; });
$response->setContent('foo');
}
public function testGetContent()
{
$response = new StreamedResponse(function () { echo 'foo'; });
$this->assertFalse($response->getContent());
}
public function testCreate()
{
$response = StreamedResponse::create(function () {}, 204);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
$this->assertEquals(204, $response->getStatusCode());
}
}

View File

@@ -0,0 +1,31 @@
<?xml version='1.0'?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
ns="http://www.iana.org/assignments">
<include href="iana-registry.rng"/>
<start>
<element name="registry">
<ref name="registryMeta"/>
<element name="registry">
<ref name="registryMeta"/>
<zeroOrMore>
<element name="record">
<optional>
<attribute name="date"><ref name="genericDate"/></attribute>
</optional>
<optional>
<attribute name="updated"><ref name="genericDate"/></attribute>
</optional>
<element name="value"><ref name="genericRange"/></element>
<element name="description"><text/></element>
<ref name="references"/>
</element>
</zeroOrMore>
</element>
<ref name="people"/>
</element>
</start>
</grammar>

View File

@@ -0,0 +1,198 @@
<?xml version='1.0'?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
ns="http://www.iana.org/assignments">
<define name="registryMeta">
<interleave>
<attribute name="id"><data type="ID"/></attribute>
<optional><element name="title"><ref name="text_with_references"/></element></optional>
<optional><element name="created"><ref name="genericDate"/></element></optional>
<optional><element name="updated"><data type="date"/></element></optional>
<optional><element name="registration_rule"><ref
name="text_with_references"/></element></optional>
<optional><element name="expert"><text/></element></optional>
<optional><element name="description"><ref name="text_with_references"/></element></optional>
<zeroOrMore><element name="note"><ref name="text_with_references"/></element></zeroOrMore>
<ref name="references"/>
<optional><element name="hide"><empty/></element></optional>
<zeroOrMore><element name="category"><text/></element></zeroOrMore>
<zeroOrMore><ref name="range"/></zeroOrMore>
<optional><ref name="file"/></optional>
</interleave>
</define>
<define name="range">
<element name="range">
<interleave>
<element name="value"><text/></element>
<optional><element name="hex"><text/></element></optional>
<element name="registration_rule"><ref name="text_with_references"/></element>
<optional><element name="note"><ref name="text_with_references"/></element></optional>
<optional><ref name="xref"/></optional>
</interleave>
</element>
</define>
<define name="people">
<element name="people">
<zeroOrMore>
<element name="person">
<attribute name="id"><data type="ID"/></attribute>
<optional><element name="name"><text/></element></optional>
<optional><element name="org"><text/></element></optional>
<zeroOrMore><element name="uri"><data type="anyURI"/></element></zeroOrMore>
<optional><element name="updated"><ref name="genericDate"/></element></optional>
</element>
</zeroOrMore>
</element>
</define>
<define name="xref">
<element name="xref">
<optional>
<attribute name="lastupdated"><ref name="genericDate"/></attribute>
</optional>
<choice>
<group>
<attribute name="type"><value>uri</value></attribute>
<attribute name="data"><data type="anyURI"/></attribute>
</group>
<group>
<attribute name="type"><value>rfc</value></attribute>
<attribute name="data">
<data type="string">
<param name="pattern">(rfc|bcp|std)\d+</param>
</data>
</attribute>
</group>
<group>
<attribute name="type"><value>rfc-errata</value></attribute>
<attribute name="data"><data type="positiveInteger"/></attribute>
</group>
<group>
<attribute name="type"><value>draft</value></attribute>
<attribute name="data">
<data type="string">
<param name="pattern">(draft|RFC)(-[a-zA-Z0-9]+)+</param>
</data>
</attribute>
</group>
<group>
<attribute name="type"><value>registry</value></attribute>
<attribute name="data"><data type="NCName"/></attribute>
</group>
<group>
<attribute name="type"><value>person</value></attribute>
<attribute name="data"><data type="NCName"/></attribute>
</group>
<group>
<attribute name="type"><value>text</value></attribute>
</group>
<group>
<attribute name="type"><value>note</value></attribute>
<attribute name="data"><data type="positiveInteger"/></attribute>
</group>
<group>
<attribute name="type"><value>unicode</value></attribute>
<attribute name="data">
<data type="string">
<param name="pattern">ucd\d+\.\d+\.\d+</param>
</data>
</attribute>
</group>
</choice>
<text/>
</element>
</define>
<define name="references">
<zeroOrMore>
<ref name="xref"/>
</zeroOrMore>
</define>
<define name="text_with_references">
<interleave>
<zeroOrMore>
<text/>
<optional><ref name="xref"/></optional>
</zeroOrMore>
</interleave>
</define>
<define name="richText">
<zeroOrMore>
<choice>
<interleave>
<ref name="text_with_references"/>
<optional><element name="br"><empty/></element></optional>
</interleave>
<element name="paragraph">
<interleave>
<ref name="text_with_references"/>
<optional><element name="br"><empty/></element></optional>
</interleave>
</element>
<element name="artwork"><text/></element>
</choice>
</zeroOrMore>
</define>
<define name="genericRange">
<data type="string">
<param name="pattern">(\d+|0x[\da-fA-F]+)(\s*-\s*(\d+|0x[\da-fA-F]+))?</param>
</data>
</define>
<define name="genericDate">
<choice>
<data type="date"/>
<data type="gYearMonth"/>
</choice>
</define>
<define name="hex32">
<data type="string">
<param name="pattern">0x[0-9]{8}</param>
</data>
</define>
<define name="binary">
<data type="string">
<param name="pattern">[0-1]+</param>
</data>
</define>
<define name="footnotes">
<zeroOrMore>
<element name="footnote">
<attribute name="anchor"><data type="positiveInteger"/></attribute>
<interleave>
<zeroOrMore>
<text/>
<optional><ref name="xref"/></optional>
</zeroOrMore>
</interleave>
</element>
</zeroOrMore>
</define>
<define name="file">
<element name="file">
<attribute name="type">
<choice>
<value>legacy</value>
<value>mib</value>
<value>template</value>
<value>json</value>
</choice>
</attribute>
<optional>
<attribute name="name"/>
</optional>
<data type="anyURI"/>
</element>
</define>
</grammar>