Merge branch '4.4'

* 4.4:
  [HttpKernel] fix link to source generation
  [Doctrine Bridge] Check field type before adding Length constraint
  [FrameworkBundle] fix BC-breaking property in WebTestAssertionsTrait
  [Cache] Pass  arg to get callback everywhere
  Fix DoctrineBridge upgrade 5.0
  [FramworkBundle][HttpKernel] fix KernelBrowser BC layer
  Add a missing quote in getValue() DocBlock
  [Messenger] Add runtime check for ext redis version
  [HttpFoundation] Fixed case-sensitive handling of cache-control header in RedirectResponse constructor.
  minor: ChoiceType callable deprecation after/before seems wrong
This commit is contained in:
Nicolas Grekas 2019-06-06 12:05:45 +02:00
commit f930ed2374
22 changed files with 482 additions and 426 deletions

View File

@ -297,19 +297,17 @@ Form
`ArrayAccess` in `ResizeFormListener::preSubmit` method has been removed.
* Using callable strings as choice options in ChoiceType is not supported
anymore in favor of passing PropertyPath instances.
anymore.
Before:
```php
'choice_value' => new PropertyPath('range'),
'choice_label' => 'strtoupper',
```
After:
```php
'choice_value' => 'range',
'choice_label' => function ($choice) {
return strtoupper($choice);
},

View File

@ -91,7 +91,7 @@ DoctrineBridge
* Deprecated injecting `ClassMetadataFactory` in `DoctrineExtractor`, an instance of `EntityManagerInterface` should be
injected instead
* Passing an `IdReader` to the `DoctrineChoiceLoader` when the query cannot be optimized with single id field will throw an exception, pass `null` instead
* Not passing an `IdReader` to the `DoctrineChoiceLoader` when the query can be optimized with single id field will throw an exception
* Not passing an `IdReader` to the `DoctrineChoiceLoader` when the query can be optimized with single id field will not apply any optimization
DomCrawler

View File

@ -60,4 +60,13 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
* @ORM\Embedded(class=DoctrineLoaderEmbed::class)
*/
public $embedded;
/** @ORM\Column(type="text", nullable=true, length=1000) */
public $textField;
/** @ORM\Id @ORM\Column(type="guid", length=50) */
protected $guidField;
/** @ORM\Column(type="simple_array", length=100) */
public $simpleArrayField = [];
}

View File

@ -108,6 +108,16 @@ class DoctrineLoaderTest extends TestCase
$this->assertCount(1, $embeddedMaxLengthConstraints);
$this->assertInstanceOf(Length::class, $embeddedMaxLengthConstraints[0]);
$this->assertSame(25, $embeddedMaxLengthConstraints[0]->max);
$this->assertCount(0, $classMetadata->getPropertyMetadata('guidField'));
$this->assertCount(0, $classMetadata->getPropertyMetadata('simpleArrayField'));
$textFieldMetadata = $classMetadata->getPropertyMetadata('textField');
$this->assertCount(1, $textFieldMetadata);
$textFieldConstraints = $textFieldMetadata[0]->getConstraints();
$this->assertCount(1, $textFieldConstraints);
$this->assertInstanceOf(Length::class, $textFieldConstraints[0]);
$this->assertSame(1000, $textFieldConstraints[0]->max);
}
public function testFieldMappingsConfiguration()

View File

@ -73,7 +73,7 @@ final class DoctrineLoader implements LoaderInterface
$metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
}
if (null === ($mapping['length'] ?? null)) {
if (null === ($mapping['length'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
continue;
}

View File

@ -11,8 +11,196 @@
namespace Symfony\Bundle\FrameworkBundle;
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', Client::class, KernelBrowser::class), E_USER_DEPRECATED);
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
class Client extends KernelBrowser
/**
* Client simulates a browser and makes requests to a Kernel object.
*
* @deprecated since Symfony 4.3, use KernelBrowser instead.
*/
class Client extends HttpKernelBrowser
{
private $hasPerformedRequest = false;
private $profiler = false;
private $reboot = true;
/**
* {@inheritdoc}
*/
public function __construct(KernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
{
parent::__construct($kernel, $server, $history, $cookieJar);
}
/**
* Returns the container.
*
* @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet
*/
public function getContainer()
{
return $this->kernel->getContainer();
}
/**
* Returns the kernel.
*
* @return KernelInterface
*/
public function getKernel()
{
return $this->kernel;
}
/**
* Gets the profile associated with the current Response.
*
* @return HttpProfile|false A Profile instance
*/
public function getProfile()
{
if (!$this->kernel->getContainer()->has('profiler')) {
return false;
}
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
}
/**
* Enables the profiler for the very next request.
*
* If the profiler is not enabled, the call to this method does nothing.
*/
public function enableProfiler()
{
if ($this->kernel->getContainer()->has('profiler')) {
$this->profiler = true;
}
}
/**
* Disables kernel reboot between requests.
*
* By default, the Client reboots the Kernel for each request. This method
* allows to keep the same kernel across requests.
*/
public function disableReboot()
{
$this->reboot = false;
}
/**
* Enables kernel reboot between requests.
*/
public function enableReboot()
{
$this->reboot = true;
}
/**
* {@inheritdoc}
*
* @param Request $request A Request instance
*
* @return Response A Response instance
*/
protected function doRequest($request)
{
// avoid shutting down the Kernel if no request has been performed yet
// WebTestCase::createClient() boots the Kernel but do not handle a request
if ($this->hasPerformedRequest && $this->reboot) {
$this->kernel->shutdown();
} else {
$this->hasPerformedRequest = true;
}
if ($this->profiler) {
$this->profiler = false;
$this->kernel->boot();
$this->kernel->getContainer()->get('profiler')->enable();
}
return parent::doRequest($request);
}
/**
* {@inheritdoc}
*
* @param Request $request A Request instance
*
* @return Response A Response instance
*/
protected function doRequestInProcess($request)
{
$response = parent::doRequestInProcess($request);
$this->profiler = false;
return $response;
}
/**
* Returns the script to execute when the request must be insulated.
*
* It assumes that the autoloader is named 'autoload.php' and that it is
* stored in the same directory as the kernel (this is the case for the
* Symfony Standard Edition). If this is not your case, create your own
* client and override this method.
*
* @param Request $request A Request instance
*
* @return string The script content
*/
protected function getScript($request)
{
$kernel = var_export(serialize($this->kernel), true);
$request = var_export(serialize($request), true);
$errorReporting = error_reporting();
$requires = '';
foreach (get_declared_classes() as $class) {
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class);
$file = \dirname(\dirname($r->getFileName())).'/autoload.php';
if (file_exists($file)) {
$requires .= 'require_once '.var_export($file, true).";\n";
}
}
}
if (!$requires) {
throw new \RuntimeException('Composer autoloader not found.');
}
$requires .= 'require_once '.var_export((new \ReflectionObject($this->kernel))->getFileName(), true).";\n";
$profilerCode = '';
if ($this->profiler) {
$profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
}
$code = <<<EOF
<?php
error_reporting($errorReporting);
$requires
\$kernel = unserialize($kernel);
\$kernel->boot();
$profilerCode
\$request = unserialize($request);
EOF;
return $code.$this->getHandleScript();
}
}

View File

@ -11,196 +11,11 @@
namespace Symfony\Bundle\FrameworkBundle;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
/**
* Client simulates a browser and makes requests to a Kernel object.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class KernelBrowser extends HttpKernelBrowser
class KernelBrowser extends Client
{
private $hasPerformedRequest = false;
private $profiler = false;
private $reboot = true;
/**
* {@inheritdoc}
*/
public function __construct(KernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
{
parent::__construct($kernel, $server, $history, $cookieJar);
}
/**
* Returns the container.
*
* @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet
*/
public function getContainer()
{
return $this->kernel->getContainer();
}
/**
* Returns the kernel.
*
* @return KernelInterface
*/
public function getKernel()
{
return $this->kernel;
}
/**
* Gets the profile associated with the current Response.
*
* @return HttpProfile|false A Profile instance
*/
public function getProfile()
{
if (!$this->kernel->getContainer()->has('profiler')) {
return false;
}
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
}
/**
* Enables the profiler for the very next request.
*
* If the profiler is not enabled, the call to this method does nothing.
*/
public function enableProfiler()
{
if ($this->kernel->getContainer()->has('profiler')) {
$this->profiler = true;
}
}
/**
* Disables kernel reboot between requests.
*
* By default, the Client reboots the Kernel for each request. This method
* allows to keep the same kernel across requests.
*/
public function disableReboot()
{
$this->reboot = false;
}
/**
* Enables kernel reboot between requests.
*/
public function enableReboot()
{
$this->reboot = true;
}
/**
* {@inheritdoc}
*
* @param Request $request A Request instance
*
* @return Response A Response instance
*/
protected function doRequest($request)
{
// avoid shutting down the Kernel if no request has been performed yet
// WebTestCase::createClient() boots the Kernel but do not handle a request
if ($this->hasPerformedRequest && $this->reboot) {
$this->kernel->shutdown();
} else {
$this->hasPerformedRequest = true;
}
if ($this->profiler) {
$this->profiler = false;
$this->kernel->boot();
$this->kernel->getContainer()->get('profiler')->enable();
}
return parent::doRequest($request);
}
/**
* {@inheritdoc}
*
* @param Request $request A Request instance
*
* @return Response A Response instance
*/
protected function doRequestInProcess($request)
{
$response = parent::doRequestInProcess($request);
$this->profiler = false;
return $response;
}
/**
* Returns the script to execute when the request must be insulated.
*
* It assumes that the autoloader is named 'autoload.php' and that it is
* stored in the same directory as the kernel (this is the case for the
* Symfony Standard Edition). If this is not your case, create your own
* client and override this method.
*
* @param Request $request A Request instance
*
* @return string The script content
*/
protected function getScript($request)
{
$kernel = var_export(serialize($this->kernel), true);
$request = var_export(serialize($request), true);
$errorReporting = error_reporting();
$requires = '';
foreach (get_declared_classes() as $class) {
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class);
$file = \dirname(\dirname($r->getFileName())).'/autoload.php';
if (file_exists($file)) {
$requires .= 'require_once '.var_export($file, true).";\n";
}
}
}
if (!$requires) {
throw new \RuntimeException('Composer autoloader not found.');
}
$requires .= 'require_once '.var_export((new \ReflectionObject($this->kernel))->getFileName(), true).";\n";
$profilerCode = '';
if ($this->profiler) {
$profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
}
$code = <<<EOF
<?php
error_reporting($errorReporting);
$requires
\$kernel = unserialize($kernel);
\$kernel->boot();
$profilerCode
\$request = unserialize($request);
EOF;
return $code.$this->getHandleScript();
}
}

View File

@ -30,12 +30,12 @@ trait WebTestAssertionsTrait
{
public static function assertResponseIsSuccessful(string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
}
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
}
public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
@ -48,42 +48,42 @@ trait WebTestAssertionsTrait
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode));
}
self::assertThat(static::getResponse(), $constraint, $message);
self::assertThat(self::getResponse(), $constraint, $message);
}
public static function assertResponseHasHeader(string $headerName, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message);
}
public static function assertResponseNotHasHeader(string $headerName, string $message = ''): void
{
self::assertThat(static::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message);
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message);
}
public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message);
}
public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message);
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message);
}
public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message);
}
public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
{
self::assertThat(static::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message);
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message);
}
public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void
{
self::assertThat(static::getResponse(), LogicalAnd::fromConstraints(
self::assertThat(self::getResponse(), LogicalAnd::fromConstraints(
new ResponseConstraint\ResponseHasCookie($name, $path, $domain),
new ResponseConstraint\ResponseCookieValueSame($name, $expectedValue, $path, $domain)
), $message);
@ -91,17 +91,17 @@ trait WebTestAssertionsTrait
public static function assertBrowserHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
{
self::assertThat(static::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message);
self::assertThat(self::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message);
}
public static function assertBrowserNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
{
self::assertThat(static::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
self::assertThat(self::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
}
public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', string $domain = null, string $message = ''): void
{
self::assertThat(static::getClient(), LogicalAnd::fromConstraints(
self::assertThat(self::getClient(), LogicalAnd::fromConstraints(
new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain),
new BrowserKitConstraint\BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain)
), $message);
@ -109,17 +109,17 @@ trait WebTestAssertionsTrait
public static function assertSelectorExists(string $selector, string $message = ''): void
{
self::assertThat(static::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message);
self::assertThat(self::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message);
}
public static function assertSelectorNotExists(string $selector, string $message = ''): void
{
self::assertThat(static::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message);
self::assertThat(self::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message);
}
public static function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text)
), $message);
@ -127,7 +127,7 @@ trait WebTestAssertionsTrait
public static function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new DomCrawlerConstraint\CrawlerSelectorTextSame($selector, $text)
), $message);
@ -135,7 +135,7 @@ trait WebTestAssertionsTrait
public static function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text))
), $message);
@ -153,7 +153,7 @@ trait WebTestAssertionsTrait
public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
), $message);
@ -161,7 +161,7 @@ trait WebTestAssertionsTrait
public static function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue))
), $message);
@ -169,7 +169,7 @@ trait WebTestAssertionsTrait
public static function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message);
self::assertThat(self::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message);
}
public static function assertRouteSame($expectedRoute, array $parameters = [], string $message = ''): void
@ -183,21 +183,27 @@ trait WebTestAssertionsTrait
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
}
self::assertThat(static::getRequest(), $constraint, $message);
self::assertThat(self::getRequest(), $constraint, $message);
}
private static function getClient(): KernelBrowser
private static function getClient(KernelBrowser $newClient = null): ?KernelBrowser
{
if (!static::$client instanceof KernelBrowser) {
static::fail(\sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient"?', __CLASS__));
static $client;
if (0 < \func_num_args()) {
return $client = $newClient;
}
return static::$client;
if (!$client instanceof KernelBrowser) {
static::fail(\sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__));
}
return $client;
}
private static function getCrawler(): Crawler
{
if (!$crawler = static::getClient()->getCrawler()) {
if (!$crawler = self::getClient()->getCrawler()) {
static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?');
}
@ -206,7 +212,7 @@ trait WebTestAssertionsTrait
private static function getResponse(): Response
{
if (!$response = static::getClient()->getResponse()) {
if (!$response = self::getClient()->getResponse()) {
static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?');
}
@ -215,7 +221,7 @@ trait WebTestAssertionsTrait
private static function getRequest(): Request
{
if (!$request = static::getClient()->getRequest()) {
if (!$request = self::getClient()->getRequest()) {
static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?');
}

View File

@ -23,14 +23,10 @@ abstract class WebTestCase extends KernelTestCase
{
use WebTestAssertionsTrait;
/** @var KernelBrowser|null */
protected static $client;
protected function doTearDown(): void
{
parent::doTearDown();
static::$client = null;
self::getClient(null);
}
/**
@ -56,6 +52,6 @@ abstract class WebTestCase extends KernelTestCase
$client->setServerParameters($server);
return static::$client = $client;
return self::getClient($client);
}
}

View File

@ -277,11 +277,9 @@ class WebTestCaseTest extends TestCase
return new class($client) extends WebTestCase {
use WebTestAssertionsTrait;
protected static $client;
public function __construct(KernelBrowser $client)
{
static::$client = $client;
self::getClient($client);
}
};
}

View File

@ -62,7 +62,8 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
// ArrayAdapter works in memory, we don't care about stampede protection
if (INF === $beta || !$item->isHit()) {
$this->save($item->set($callback($item)));
$save = true;
$this->save($item->set($callback($item, $save)));
}
return $item->get();

View File

@ -42,7 +42,9 @@ class NullAdapter implements AdapterInterface, CacheInterface
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
return $callback(($this->createCacheItem)($key));
$save = true;
return $callback(($this->createCacheItem)($key), $save);
}
/**

View File

@ -103,9 +103,9 @@ class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterfa
return $this->doGet($this, $key, $callback, $beta, $metadata);
}
return $this->pool->get($this->getId($key), function ($innerItem) use ($key, $callback) {
return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) {
$item = ($this->createCacheItem)($key, $innerItem);
$item->set($value = $callback($item));
$item->set($value = $callback($item, $save));
($this->setInnerItem)($innerItem, (array) $item);
return $value;

View File

@ -45,10 +45,10 @@ class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInt
}
$isHit = true;
$callback = function (CacheItem $item) use ($callback, &$isHit) {
$callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
$isHit = $item->isHit();
return $callback($item);
return $callback($item, $save);
};
$event = $this->start(__FUNCTION__);

View File

@ -12,9 +12,12 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use PHPUnit\Framework\Assert;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Contracts\Cache\CallbackInterface;
abstract class AdapterTestCase extends CachePoolTest
{
@ -57,6 +60,22 @@ abstract class AdapterTestCase extends CachePoolTest
$this->assertSame($value, $item->get());
}, INF));
$this->assertFalse($isHit);
$this->assertSame($value, $cache->get('bar', new class($value) implements CallbackInterface {
private $value;
public function __construct(int $value)
{
$this->value = $value;
}
public function __invoke(CacheItemInterface $item, bool &$save)
{
Assert::assertSame('bar', $item->getKey());
return $this->value;
}
}));
}
public function testRecursiveGet()

View File

@ -42,7 +42,7 @@ class RedirectResponse extends Response
throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
}
if (301 == $status && !\array_key_exists('cache-control', $headers)) {
if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) {
$this->headers->remove('cache-control');
}
}

View File

@ -91,6 +91,10 @@ class RedirectResponseTest extends TestCase
$this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));
$this->assertTrue($response->headers->hasCacheControlDirective('max-age'));
$response = new RedirectResponse('foo.bar', 301, ['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

@ -11,8 +11,191 @@
namespace Symfony\Component\HttpKernel;
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', Client::class, HttpKernelBrowser::class), E_USER_DEPRECATED);
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\BrowserKit\Request as DomRequest;
use Symfony\Component\BrowserKit\Response as DomResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Client extends HttpKernelBrowser
/**
* Client simulates a browser and makes requests to an HttpKernel instance.
*
* @deprecated since Symfony 4.3, use HttpKernelBrowser instead.
*/
class Client extends AbstractBrowser
{
protected $kernel;
private $catchExceptions = true;
/**
* @param HttpKernelInterface $kernel An HttpKernel instance
* @param array $server The server parameters (equivalent of $_SERVER)
* @param History $history A History instance to store the browser history
* @param CookieJar $cookieJar A CookieJar instance to store the cookies
*/
public function __construct(HttpKernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
{
// These class properties must be set before calling the parent constructor, as it may depend on it.
$this->kernel = $kernel;
$this->followRedirects = false;
parent::__construct($server, $history, $cookieJar);
}
/**
* Sets whether to catch exceptions when the kernel is handling a request.
*
* @param bool $catchExceptions Whether to catch exceptions
*/
public function catchExceptions($catchExceptions)
{
$this->catchExceptions = $catchExceptions;
}
/**
* Makes a request.
*
* @return Response A Response instance
*/
protected function doRequest($request)
{
$response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $this->catchExceptions);
if ($this->kernel instanceof TerminableInterface) {
$this->kernel->terminate($request, $response);
}
return $response;
}
/**
* Returns the script to execute when the request must be insulated.
*
* @return string
*/
protected function getScript($request)
{
$kernel = var_export(serialize($this->kernel), true);
$request = var_export(serialize($request), true);
$errorReporting = error_reporting();
$requires = '';
foreach (get_declared_classes() as $class) {
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class);
$file = \dirname(\dirname($r->getFileName())).'/autoload.php';
if (file_exists($file)) {
$requires .= 'require_once '.var_export($file, true).";\n";
}
}
}
if (!$requires) {
throw new \RuntimeException('Composer autoloader not found.');
}
$code = <<<EOF
<?php
error_reporting($errorReporting);
$requires
\$kernel = unserialize($kernel);
\$request = unserialize($request);
EOF;
return $code.$this->getHandleScript();
}
protected function getHandleScript()
{
return <<<'EOF'
$response = $kernel->handle($request);
if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
$kernel->terminate($request, $response);
}
echo serialize($response);
EOF;
}
/**
* Converts the BrowserKit request to a HttpKernel request.
*
* @return Request A Request instance
*/
protected function filterRequest(DomRequest $request)
{
$httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
$httpRequest->files->set($key, $value);
}
return $httpRequest;
}
/**
* Filters an array of files.
*
* This method created test instances of UploadedFile so that the move()
* method can be called on those instances.
*
* If the size of a file is greater than the allowed size (from php.ini) then
* an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
*
* @see UploadedFile
*
* @return array An array with all uploaded files marked as already moved
*/
protected function filterFiles(array $files)
{
$filtered = [];
foreach ($files as $key => $value) {
if (\is_array($value)) {
$filtered[$key] = $this->filterFiles($value);
} elseif ($value instanceof UploadedFile) {
if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
$filtered[$key] = new UploadedFile(
'',
$value->getClientOriginalName(),
$value->getClientMimeType(),
UPLOAD_ERR_INI_SIZE,
true
);
} else {
$filtered[$key] = new UploadedFile(
$value->getPathname(),
$value->getClientOriginalName(),
$value->getClientMimeType(),
$value->getError(),
true
);
}
}
}
return $filtered;
}
/**
* Converts the HttpKernel response to a BrowserKit response.
*
* @return DomResponse A DomResponse instance
*/
protected function filterResponse($response)
{
// this is needed to support StreamedResponse
ob_start();
$response->sendContent();
$content = ob_get_clean();
return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
}
}

View File

@ -68,7 +68,7 @@ class FileLinkFormatter
*/
public function __sleep(): array
{
$this->getFileLinkFormat();
$this->fileLinkFormat = $this->getFileLinkFormat();
return ['fileLinkFormat'];
}
@ -87,17 +87,19 @@ class FileLinkFormatter
private function getFileLinkFormat()
{
if ($this->fileLinkFormat) {
return $this->fileLinkFormat;
}
if ($this->requestStack && $this->baseDir && $this->urlFormat) {
$request = $this->requestStack->getMasterRequest();
if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) {
$this->fileLinkFormat = [
return [
$request->getSchemeAndHttpHost().$request->getBasePath().$this->urlFormat,
$this->baseDir.\DIRECTORY_SEPARATOR, '',
];
}
}
return $this->fileLinkFormat;
}
}

View File

@ -11,15 +11,6 @@
namespace Symfony\Component\HttpKernel;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\BrowserKit\Request as DomRequest;
use Symfony\Component\BrowserKit\Response as DomResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Client simulates a browser and makes requests to an HttpKernel instance.
*
@ -28,177 +19,6 @@ use Symfony\Component\HttpFoundation\Response;
* @method Request getRequest() A Request instance
* @method Response getResponse() A Response instance
*/
class HttpKernelBrowser extends AbstractBrowser
class HttpKernelBrowser extends Client
{
protected $kernel;
private $catchExceptions = true;
/**
* @param HttpKernelInterface $kernel An HttpKernel instance
* @param array $server The server parameters (equivalent of $_SERVER)
* @param History $history A History instance to store the browser history
* @param CookieJar $cookieJar A CookieJar instance to store the cookies
*/
public function __construct(HttpKernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
{
// These class properties must be set before calling the parent constructor, as it may depend on it.
$this->kernel = $kernel;
$this->followRedirects = false;
parent::__construct($server, $history, $cookieJar);
}
/**
* Sets whether to catch exceptions when the kernel is handling a request.
*
* @param bool $catchExceptions Whether to catch exceptions
*/
public function catchExceptions($catchExceptions)
{
$this->catchExceptions = $catchExceptions;
}
/**
* Makes a request.
*
* @return Response A Response instance
*/
protected function doRequest($request)
{
$response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $this->catchExceptions);
if ($this->kernel instanceof TerminableInterface) {
$this->kernel->terminate($request, $response);
}
return $response;
}
/**
* Returns the script to execute when the request must be insulated.
*
* @return string
*/
protected function getScript($request)
{
$kernel = var_export(serialize($this->kernel), true);
$request = var_export(serialize($request), true);
$errorReporting = error_reporting();
$requires = '';
foreach (get_declared_classes() as $class) {
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class);
$file = \dirname(\dirname($r->getFileName())).'/autoload.php';
if (file_exists($file)) {
$requires .= 'require_once '.var_export($file, true).";\n";
}
}
}
if (!$requires) {
throw new \RuntimeException('Composer autoloader not found.');
}
$code = <<<EOF
<?php
error_reporting($errorReporting);
$requires
\$kernel = unserialize($kernel);
\$request = unserialize($request);
EOF;
return $code.$this->getHandleScript();
}
protected function getHandleScript()
{
return <<<'EOF'
$response = $kernel->handle($request);
if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
$kernel->terminate($request, $response);
}
echo serialize($response);
EOF;
}
/**
* Converts the BrowserKit request to a HttpKernel request.
*
* @return Request A Request instance
*/
protected function filterRequest(DomRequest $request)
{
$httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
$httpRequest->files->set($key, $value);
}
return $httpRequest;
}
/**
* Filters an array of files.
*
* This method created test instances of UploadedFile so that the move()
* method can be called on those instances.
*
* If the size of a file is greater than the allowed size (from php.ini) then
* an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
*
* @see UploadedFile
*
* @return array An array with all uploaded files marked as already moved
*/
protected function filterFiles(array $files)
{
$filtered = [];
foreach ($files as $key => $value) {
if (\is_array($value)) {
$filtered[$key] = $this->filterFiles($value);
} elseif ($value instanceof UploadedFile) {
if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
$filtered[$key] = new UploadedFile(
'',
$value->getClientOriginalName(),
$value->getClientMimeType(),
UPLOAD_ERR_INI_SIZE,
true
);
} else {
$filtered[$key] = new UploadedFile(
$value->getPathname(),
$value->getClientOriginalName(),
$value->getClientMimeType(),
$value->getError(),
true
);
}
}
}
return $filtered;
}
/**
* Converts the HttpKernel response to a BrowserKit response.
*
* @return DomResponse A DomResponse instance
*/
protected function filterResponse($response)
{
// this is needed to support StreamedResponse
ob_start();
$response->sendContent();
$content = ob_get_clean();
return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Transport\RedisExt;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\TransportException;
/**
@ -43,6 +44,10 @@ class Connection
public function __construct(array $configuration, array $connectionCredentials = [], array $redisOptions = [], \Redis $redis = null)
{
if (version_compare(phpversion('redis'), '4.3.0', '<')) {
throw new LogicException('The redis transport requires php-redis 4.3.0 or higher.');
}
$this->connection = $redis ?: new \Redis();
$this->connection->connect($connectionCredentials['host'] ?? '127.0.0.1', $connectionCredentials['port'] ?? 6379);
$this->connection->setOption(\Redis::OPT_SERIALIZER, $redisOptions['serializer'] ?? \Redis::SERIALIZER_PHP);

View File

@ -58,7 +58,7 @@ interface PropertyAccessorInterface
*
* $propertyAccessor = PropertyAccess::createPropertyAccessor();
*
* echo $propertyAccessor->getValue($object, 'child.name);
* echo $propertyAccessor->getValue($object, 'child.name');
* // equals echo $object->getChild()->getName();
*
* This method first tries to find a public getter for each property in the