[FrameworkBundle] remove deprecated code paths
This commit is contained in:
parent
d559bd1c39
commit
30418cfaf3
@ -9,6 +9,13 @@ CHANGELOG
|
||||
* Removed `ResolveControllerNameSubscriber`
|
||||
* Removed support for `bundle:controller:action` to reference controllers. Use `serviceOrFqcn::method` instead
|
||||
* Removed support for PHP templating, use Twig instead
|
||||
* Removed `Controller`, use `AbstractController` instead
|
||||
* Removed `Client`, use `KernelBrowser` instead
|
||||
* Removed `ContainerAwareCommand`, use dependency injection instead
|
||||
* Removed the `validation.strict_email` option, use `validation.email_validation_mode` instead
|
||||
* Removed the `cache.app.simple` service and its corresponding PSR-16 autowiring alias
|
||||
* Removed cache-related compiler passes and `RequestDataCollector`
|
||||
* Removed the `translator.selector` and `session.save_listener` services
|
||||
|
||||
4.4.0
|
||||
-----
|
||||
|
@ -14,7 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Doctrine\Common\Annotations\CachedReader;
|
||||
use Doctrine\Common\Annotations\Reader;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\Cache\DoctrineProvider;
|
||||
|
||||
@ -32,16 +31,9 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
|
||||
|
||||
/**
|
||||
* @param string $phpArrayFile The PHP file where annotations are cached
|
||||
* @param string $excludeRegexp
|
||||
* @param bool $debug
|
||||
*/
|
||||
public function __construct(Reader $annotationReader, string $phpArrayFile, $excludeRegexp = null, $debug = false)
|
||||
public function __construct(Reader $annotationReader, string $phpArrayFile, string $excludeRegexp = null, bool $debug = false)
|
||||
{
|
||||
if ($excludeRegexp instanceof CacheItemPoolInterface) {
|
||||
@trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.', __METHOD__), E_USER_DEPRECATED);
|
||||
$excludeRegexp = $debug;
|
||||
$debug = 4 < \func_num_args() && \func_get_arg(4);
|
||||
}
|
||||
parent::__construct($phpArrayFile);
|
||||
$this->annotationReader = $annotationReader;
|
||||
$this->excludeRegexp = $excludeRegexp;
|
||||
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
@ -36,9 +35,6 @@ class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
|
||||
*/
|
||||
public function __construct(array $loaders, string $phpArrayFile)
|
||||
{
|
||||
if (2 < \func_num_args() && \func_get_arg(2) instanceof CacheItemPoolInterface) {
|
||||
@trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.', __METHOD__), E_USER_DEPRECATED);
|
||||
}
|
||||
parent::__construct($phpArrayFile);
|
||||
$this->loaders = $loaders;
|
||||
}
|
||||
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
|
||||
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache;
|
||||
@ -22,7 +21,6 @@ use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
|
||||
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
|
||||
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
|
||||
use Symfony\Component\Validator\ValidatorBuilder;
|
||||
use Symfony\Component\Validator\ValidatorBuilderInterface;
|
||||
|
||||
/**
|
||||
* Warms up XML and YAML validator metadata.
|
||||
@ -34,17 +32,10 @@ class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
|
||||
private $validatorBuilder;
|
||||
|
||||
/**
|
||||
* @param ValidatorBuilder $validatorBuilder
|
||||
* @param string $phpArrayFile The PHP file where metadata are cached
|
||||
*/
|
||||
public function __construct($validatorBuilder, string $phpArrayFile)
|
||||
public function __construct(ValidatorBuilder $validatorBuilder, string $phpArrayFile)
|
||||
{
|
||||
if (!$validatorBuilder instanceof ValidatorBuilder && !$validatorBuilder instanceof ValidatorBuilderInterface) {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, ValidatorBuilder::class, \is_object($validatorBuilder) ? \get_class($validatorBuilder) : \gettype($validatorBuilder)));
|
||||
}
|
||||
if (2 < \func_num_args() && \func_get_arg(2) instanceof CacheItemPoolInterface) {
|
||||
@trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.', __METHOD__), E_USER_DEPRECATED);
|
||||
}
|
||||
parent::__construct($phpArrayFile);
|
||||
$this->validatorBuilder = $validatorBuilder;
|
||||
}
|
||||
|
@ -1,206 +0,0 @@
|
||||
<?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\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.
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
}
|
@ -44,14 +44,10 @@ class AssetsInstallCommand extends Command
|
||||
private $filesystem;
|
||||
private $projectDir;
|
||||
|
||||
public function __construct(Filesystem $filesystem, string $projectDir = null)
|
||||
public function __construct(Filesystem $filesystem, string $projectDir)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if (null === $projectDir) {
|
||||
@trigger_error(sprintf('Not passing the project directory to the constructor of %s is deprecated since Symfony 4.3 and will not be supported in 5.0.', __CLASS__), E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$this->filesystem = $filesystem;
|
||||
$this->projectDir = $projectDir;
|
||||
}
|
||||
|
@ -1,60 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use "%s" with dependency injection instead.', ContainerAwareCommand::class, Command::class), E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Command.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @deprecated since Symfony 4.2, use {@see Command} instead.
|
||||
*/
|
||||
abstract class ContainerAwareCommand extends Command implements ContainerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface|null
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* @return ContainerInterface
|
||||
*
|
||||
* @throws \LogicException
|
||||
*/
|
||||
protected function getContainer()
|
||||
{
|
||||
if (null === $this->container) {
|
||||
$application = $this->getApplication();
|
||||
if (null === $application) {
|
||||
throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.');
|
||||
}
|
||||
|
||||
$this->container = $application->getKernel()->getContainer();
|
||||
}
|
||||
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
}
|
@ -50,7 +50,6 @@ class ContainerDebugCommand extends Command
|
||||
$this
|
||||
->setDefinition([
|
||||
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
|
||||
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Used to show public *and* private services (deprecated)'),
|
||||
new InputOption('show-arguments', null, InputOption::VALUE_NONE, 'Used to show arguments in services'),
|
||||
new InputOption('show-hidden', null, InputOption::VALUE_NONE, 'Used to show hidden (internal) services'),
|
||||
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Shows all services with a specific tag'),
|
||||
@ -116,10 +115,6 @@ EOF
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
if ($input->getOption('show-private')) {
|
||||
@trigger_error('The "--show-private" option no longer has any effect and is deprecated since Symfony 4.1.', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$errorIo = $io->getErrorStyle();
|
||||
|
||||
|
@ -26,7 +26,6 @@ use Symfony\Component\Translation\LoggingTranslator;
|
||||
use Symfony\Component\Translation\MessageCatalogue;
|
||||
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
@ -53,14 +52,8 @@ class TranslationDebugCommand extends Command
|
||||
private $transPaths;
|
||||
private $viewsPaths;
|
||||
|
||||
/**
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct($translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null, array $transPaths = [], array $viewsPaths = [])
|
||||
public function __construct(TranslatorInterface $translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null, array $transPaths = [], array $viewsPaths = [])
|
||||
{
|
||||
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
|
||||
}
|
||||
parent::__construct();
|
||||
|
||||
$this->translator = $translator;
|
||||
|
@ -12,19 +12,37 @@
|
||||
namespace Symfony\Bundle\FrameworkBundle\Controller;
|
||||
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Fig\Link\GenericLinkProvider;
|
||||
use Fig\Link\Link;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Csrf\CsrfToken;
|
||||
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
|
||||
use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
@ -35,8 +53,6 @@ use Twig\Environment;
|
||||
*/
|
||||
abstract class AbstractController implements ServiceSubscriberInterface
|
||||
{
|
||||
use ControllerTrait;
|
||||
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
@ -58,8 +74,6 @@ abstract class AbstractController implements ServiceSubscriberInterface
|
||||
* Gets a container parameter by its name.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function getParameter(string $name)
|
||||
{
|
||||
@ -89,4 +103,325 @@ abstract class AbstractController implements ServiceSubscriberInterface
|
||||
'messenger.default_bus' => '?'.MessageBusInterface::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the service id is defined.
|
||||
*/
|
||||
protected function has(string $id): bool
|
||||
{
|
||||
return $this->container->has($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a container service by its id.
|
||||
*
|
||||
* @return object The service
|
||||
*/
|
||||
protected function get(string $id): object
|
||||
{
|
||||
return $this->container->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a URL from the given parameters.
|
||||
*
|
||||
* @see UrlGeneratorInterface
|
||||
*/
|
||||
protected function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
|
||||
{
|
||||
return $this->container->get('router')->generate($route, $parameters, $referenceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards the request to another controller.
|
||||
*
|
||||
* @param string $controller The controller name (a string like Bundle\BlogBundle\Controller\PostController::indexAction)
|
||||
*/
|
||||
protected function forward(string $controller, array $path = [], array $query = []): Response
|
||||
{
|
||||
$request = $this->container->get('request_stack')->getCurrentRequest();
|
||||
$path['_controller'] = $controller;
|
||||
$subRequest = $request->duplicate($query, null, $path);
|
||||
|
||||
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a RedirectResponse to the given URL.
|
||||
*/
|
||||
protected function redirect(string $url, int $status = 302): RedirectResponse
|
||||
{
|
||||
return new RedirectResponse($url, $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a RedirectResponse to the given route with the given parameters.
|
||||
*/
|
||||
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
|
||||
{
|
||||
return $this->redirect($this->generateUrl($route, $parameters), $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
|
||||
*/
|
||||
protected function json($data, int $status = 200, array $headers = [], array $context = []): JsonResponse
|
||||
{
|
||||
if ($this->container->has('serializer')) {
|
||||
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge([
|
||||
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
|
||||
], $context));
|
||||
|
||||
return new JsonResponse($json, $status, $headers, true);
|
||||
}
|
||||
|
||||
return new JsonResponse($data, $status, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
|
||||
*
|
||||
* @param \SplFileInfo|string $file File object or path to file to be sent as response
|
||||
*/
|
||||
protected function file($file, string $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT): BinaryFileResponse
|
||||
{
|
||||
$response = new BinaryFileResponse($file);
|
||||
$response->setContentDisposition($disposition, null === $fileName ? $response->getFile()->getFilename() : $fileName);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a flash message to the current session for type.
|
||||
*
|
||||
* @throws \LogicException
|
||||
*/
|
||||
protected function addFlash(string $type, string $message): void
|
||||
{
|
||||
if (!$this->container->has('session')) {
|
||||
throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
|
||||
}
|
||||
|
||||
$this->container->get('session')->getFlashBag()->add($type, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the attributes are granted against the current authentication token and optionally supplied subject.
|
||||
*
|
||||
* @throws \LogicException
|
||||
*/
|
||||
protected function isGranted($attributes, $subject = null): bool
|
||||
{
|
||||
if (!$this->container->has('security.authorization_checker')) {
|
||||
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
|
||||
}
|
||||
|
||||
return $this->container->get('security.authorization_checker')->isGranted($attributes, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception unless the attributes are granted against the current authentication token and optionally
|
||||
* supplied subject.
|
||||
*
|
||||
* @throws AccessDeniedException
|
||||
*/
|
||||
protected function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.'): void
|
||||
{
|
||||
if (!$this->isGranted($attributes, $subject)) {
|
||||
$exception = $this->createAccessDeniedException($message);
|
||||
$exception->setAttributes($attributes);
|
||||
$exception->setSubject($subject);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a rendered view.
|
||||
*/
|
||||
protected function renderView(string $view, array $parameters = []): string
|
||||
{
|
||||
if (!$this->container->has('twig')) {
|
||||
throw new \LogicException('You can not use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
|
||||
}
|
||||
|
||||
return $this->container->get('twig')->render($view, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a view.
|
||||
*/
|
||||
protected function render(string $view, array $parameters = [], Response $response = null): Response
|
||||
{
|
||||
$content = $this->renderView($view, $parameters);
|
||||
|
||||
if (null === $response) {
|
||||
$response = new Response();
|
||||
}
|
||||
|
||||
$response->setContent($content);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams a view.
|
||||
*/
|
||||
protected function stream(string $view, array $parameters = [], StreamedResponse $response = null): StreamedResponse
|
||||
{
|
||||
if (!$this->container->has('twig')) {
|
||||
throw new \LogicException('You can not use the "stream" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
|
||||
}
|
||||
|
||||
$twig = $this->container->get('twig');
|
||||
|
||||
$callback = function () use ($twig, $view, $parameters) {
|
||||
$twig->display($view, $parameters);
|
||||
};
|
||||
|
||||
if (null === $response) {
|
||||
return new StreamedResponse($callback);
|
||||
}
|
||||
|
||||
$response->setCallback($callback);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a NotFoundHttpException.
|
||||
*
|
||||
* This will result in a 404 response code. Usage example:
|
||||
*
|
||||
* throw $this->createNotFoundException('Page not found!');
|
||||
*/
|
||||
protected function createNotFoundException(string $message = 'Not Found', \Throwable $previous = null): NotFoundHttpException
|
||||
{
|
||||
return new NotFoundHttpException($message, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an AccessDeniedException.
|
||||
*
|
||||
* This will result in a 403 response code. Usage example:
|
||||
*
|
||||
* throw $this->createAccessDeniedException('Unable to access this page!');
|
||||
*
|
||||
* @throws \LogicException If the Security component is not available
|
||||
*/
|
||||
protected function createAccessDeniedException(string $message = 'Access Denied.', \Throwable $previous = null): AccessDeniedException
|
||||
{
|
||||
if (!class_exists(AccessDeniedException::class)) {
|
||||
throw new \LogicException('You can not use the "createAccessDeniedException" method if the Security component is not available. Try running "composer require symfony/security-bundle".');
|
||||
}
|
||||
|
||||
return new AccessDeniedException($message, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a Form instance from the type of the form.
|
||||
*/
|
||||
protected function createForm(string $type, $data = null, array $options = []): FormInterface
|
||||
{
|
||||
return $this->container->get('form.factory')->create($type, $data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a form builder instance.
|
||||
*/
|
||||
protected function createFormBuilder($data = null, array $options = []): FormBuilderInterface
|
||||
{
|
||||
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut to return the Doctrine Registry service.
|
||||
*
|
||||
* @throws \LogicException If DoctrineBundle is not available
|
||||
*/
|
||||
protected function getDoctrine(): ManagerRegistry
|
||||
{
|
||||
if (!$this->container->has('doctrine')) {
|
||||
throw new \LogicException('The DoctrineBundle is not registered in your application. Try running "composer require symfony/orm-pack".');
|
||||
}
|
||||
|
||||
return $this->container->get('doctrine');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user from the Security Token Storage.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \LogicException If SecurityBundle is not available
|
||||
*
|
||||
* @see TokenInterface::getUser()
|
||||
*/
|
||||
protected function getUser()
|
||||
{
|
||||
if (!$this->container->has('security.token_storage')) {
|
||||
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
|
||||
}
|
||||
|
||||
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_object($user = $token->getUser())) {
|
||||
// e.g. anonymous authentication
|
||||
return;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the validity of a CSRF token.
|
||||
*
|
||||
* @param string $id The id used when generating the token
|
||||
* @param string|null $token The actual token sent with the request that should be validated
|
||||
*/
|
||||
protected function isCsrfTokenValid(string $id, ?string $token): bool
|
||||
{
|
||||
if (!$this->container->has('security.csrf.token_manager')) {
|
||||
throw new \LogicException('CSRF protection is not enabled in your application. Enable it with the "csrf_protection" key in "config/packages/framework.yaml".');
|
||||
}
|
||||
|
||||
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a message to the bus.
|
||||
*
|
||||
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
|
||||
*/
|
||||
protected function dispatchMessage($message): Envelope
|
||||
{
|
||||
if (!$this->container->has('messenger.default_bus')) {
|
||||
$message = class_exists(Envelope::class) ? 'You need to define the "messenger.default_bus" configuration option.' : 'Try running "composer require symfony/messenger".';
|
||||
throw new \LogicException('The message bus is not enabled in your application. '.$message);
|
||||
}
|
||||
|
||||
return $this->container->get('messenger.default_bus')->dispatch($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Link HTTP header to the current response.
|
||||
*
|
||||
* @see https://tools.ietf.org/html/rfc5988
|
||||
*/
|
||||
protected function addLink(Request $request, Link $link): void
|
||||
{
|
||||
if (!class_exists(AddLinkHeaderListener::class)) {
|
||||
throw new \LogicException('You can not use the "addLink" method if the WebLink component is not available. Try running "composer require symfony/web-link".');
|
||||
}
|
||||
|
||||
if (null === $linkProvider = $request->attributes->get('_links')) {
|
||||
$request->attributes->set('_links', new GenericLinkProvider([$link]));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$request->attributes->set('_links', $linkProvider->withLink($link));
|
||||
}
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Controller;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* Controller is a simple implementation of a Controller.
|
||||
*
|
||||
* It provides methods to common features needed in controllers.
|
||||
*
|
||||
* @deprecated since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
abstract class Controller implements ContainerAwareInterface
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
use ControllerTrait;
|
||||
|
||||
/**
|
||||
* Gets a container configuration parameter by its name.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function getParameter(string $name)
|
||||
{
|
||||
return $this->container->getParameter($name);
|
||||
}
|
||||
}
|
@ -1,413 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Controller;
|
||||
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Fig\Link\GenericLinkProvider;
|
||||
use Fig\Link\Link;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Csrf\CsrfToken;
|
||||
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
|
||||
|
||||
/**
|
||||
* Common features needed in controllers.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @property ContainerInterface $container
|
||||
*/
|
||||
trait ControllerTrait
|
||||
{
|
||||
/**
|
||||
* Returns true if the service id is defined.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function has(string $id): bool
|
||||
{
|
||||
return $this->container->has($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a container service by its id.
|
||||
*
|
||||
* @return object The service
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function get(string $id)
|
||||
{
|
||||
return $this->container->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a URL from the given parameters.
|
||||
*
|
||||
* @see UrlGeneratorInterface
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
|
||||
{
|
||||
return $this->container->get('router')->generate($route, $parameters, $referenceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards the request to another controller.
|
||||
*
|
||||
* @param string $controller The controller name (a string like Bundle\BlogBundle\Controller\PostController::indexAction)
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function forward(string $controller, array $path = [], array $query = []): Response
|
||||
{
|
||||
$request = $this->container->get('request_stack')->getCurrentRequest();
|
||||
$path['_controller'] = $controller;
|
||||
$subRequest = $request->duplicate($query, null, $path);
|
||||
|
||||
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a RedirectResponse to the given URL.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function redirect(string $url, int $status = 302): RedirectResponse
|
||||
{
|
||||
return new RedirectResponse($url, $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a RedirectResponse to the given route with the given parameters.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
|
||||
{
|
||||
return $this->redirect($this->generateUrl($route, $parameters), $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function json($data, int $status = 200, array $headers = [], array $context = []): JsonResponse
|
||||
{
|
||||
if ($this->container->has('serializer')) {
|
||||
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge([
|
||||
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
|
||||
], $context));
|
||||
|
||||
return new JsonResponse($json, $status, $headers, true);
|
||||
}
|
||||
|
||||
return new JsonResponse($data, $status, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
|
||||
*
|
||||
* @param \SplFileInfo|string $file File object or path to file to be sent as response
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function file($file, string $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT): BinaryFileResponse
|
||||
{
|
||||
$response = new BinaryFileResponse($file);
|
||||
$response->setContentDisposition($disposition, null === $fileName ? $response->getFile()->getFilename() : $fileName);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a flash message to the current session for type.
|
||||
*
|
||||
* @throws \LogicException
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function addFlash(string $type, string $message)
|
||||
{
|
||||
if (!$this->container->has('session')) {
|
||||
throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
|
||||
}
|
||||
|
||||
$this->container->get('session')->getFlashBag()->add($type, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the attributes are granted against the current authentication token and optionally supplied subject.
|
||||
*
|
||||
* @throws \LogicException
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function isGranted($attributes, $subject = null): bool
|
||||
{
|
||||
if (!$this->container->has('security.authorization_checker')) {
|
||||
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
|
||||
}
|
||||
|
||||
return $this->container->get('security.authorization_checker')->isGranted($attributes, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception unless the attributes are granted against the current authentication token and optionally
|
||||
* supplied subject.
|
||||
*
|
||||
* @throws AccessDeniedException
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.')
|
||||
{
|
||||
if (!$this->isGranted($attributes, $subject)) {
|
||||
$exception = $this->createAccessDeniedException($message);
|
||||
$exception->setAttributes($attributes);
|
||||
$exception->setSubject($subject);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a rendered view.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function renderView(string $view, array $parameters = []): string
|
||||
{
|
||||
if (!$this->container->has('twig')) {
|
||||
throw new \LogicException('You can not use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
|
||||
}
|
||||
|
||||
return $this->container->get('twig')->render($view, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a view.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function render(string $view, array $parameters = [], Response $response = null): Response
|
||||
{
|
||||
$content = $this->renderView($view, $parameters);
|
||||
|
||||
if (null === $response) {
|
||||
$response = new Response();
|
||||
}
|
||||
|
||||
$response->setContent($content);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams a view.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function stream(string $view, array $parameters = [], StreamedResponse $response = null): StreamedResponse
|
||||
{
|
||||
if (!$this->container->has('twig')) {
|
||||
throw new \LogicException('You can not use the "stream" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
|
||||
}
|
||||
|
||||
$twig = $this->container->get('twig');
|
||||
|
||||
$callback = function () use ($twig, $view, $parameters) {
|
||||
$twig->display($view, $parameters);
|
||||
};
|
||||
|
||||
if (null === $response) {
|
||||
return new StreamedResponse($callback);
|
||||
}
|
||||
|
||||
$response->setCallback($callback);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a NotFoundHttpException.
|
||||
*
|
||||
* This will result in a 404 response code. Usage example:
|
||||
*
|
||||
* throw $this->createNotFoundException('Page not found!');
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function createNotFoundException(string $message = 'Not Found', \Exception $previous = null): NotFoundHttpException
|
||||
{
|
||||
return new NotFoundHttpException($message, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an AccessDeniedException.
|
||||
*
|
||||
* This will result in a 403 response code. Usage example:
|
||||
*
|
||||
* throw $this->createAccessDeniedException('Unable to access this page!');
|
||||
*
|
||||
* @throws \LogicException If the Security component is not available
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function createAccessDeniedException(string $message = 'Access Denied.', \Exception $previous = null): AccessDeniedException
|
||||
{
|
||||
if (!class_exists(AccessDeniedException::class)) {
|
||||
throw new \LogicException('You can not use the "createAccessDeniedException" method if the Security component is not available. Try running "composer require symfony/security-bundle".');
|
||||
}
|
||||
|
||||
return new AccessDeniedException($message, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a Form instance from the type of the form.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function createForm(string $type, $data = null, array $options = []): FormInterface
|
||||
{
|
||||
return $this->container->get('form.factory')->create($type, $data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a form builder instance.
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function createFormBuilder($data = null, array $options = []): FormBuilderInterface
|
||||
{
|
||||
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut to return the Doctrine Registry service.
|
||||
*
|
||||
* @throws \LogicException If DoctrineBundle is not available
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function getDoctrine(): ManagerRegistry
|
||||
{
|
||||
if (!$this->container->has('doctrine')) {
|
||||
throw new \LogicException('The DoctrineBundle is not registered in your application. Try running "composer require symfony/orm-pack".');
|
||||
}
|
||||
|
||||
return $this->container->get('doctrine');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user from the Security Token Storage.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \LogicException If SecurityBundle is not available
|
||||
*
|
||||
* @see TokenInterface::getUser()
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function getUser()
|
||||
{
|
||||
if (!$this->container->has('security.token_storage')) {
|
||||
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
|
||||
}
|
||||
|
||||
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_object($user = $token->getUser())) {
|
||||
// e.g. anonymous authentication
|
||||
return;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the validity of a CSRF token.
|
||||
*
|
||||
* @param string $id The id used when generating the token
|
||||
* @param string|null $token The actual token sent with the request that should be validated
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function isCsrfTokenValid(string $id, ?string $token): bool
|
||||
{
|
||||
if (!$this->container->has('security.csrf.token_manager')) {
|
||||
throw new \LogicException('CSRF protection is not enabled in your application. Enable it with the "csrf_protection" key in "config/packages/framework.yaml".');
|
||||
}
|
||||
|
||||
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a message to the bus.
|
||||
*
|
||||
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function dispatchMessage($message): Envelope
|
||||
{
|
||||
if (!$this->container->has('messenger.default_bus')) {
|
||||
$message = class_exists(Envelope::class) ? 'You need to define the "messenger.default_bus" configuration option.' : 'Try running "composer require symfony/messenger".';
|
||||
throw new \LogicException('The message bus is not enabled in your application. '.$message);
|
||||
}
|
||||
|
||||
return $this->container->get('messenger.default_bus')->dispatch($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Link HTTP header to the current response.
|
||||
*
|
||||
* @see https://tools.ietf.org/html/rfc5988
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
protected function addLink(Request $request, Link $link)
|
||||
{
|
||||
if (!class_exists(AddLinkHeaderListener::class)) {
|
||||
throw new \LogicException('You can not use the "addLink" method if the WebLink component is not available. Try running "composer require symfony/web-link".');
|
||||
}
|
||||
|
||||
if (null === $linkProvider = $request->attributes->get('_links')) {
|
||||
$request->attributes->set('_links', new GenericLinkProvider([$link]));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$request->attributes->set('_links', $linkProvider->withLink($link));
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\DataCollector;
|
||||
|
||||
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestDataCollector;
|
||||
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1. Use %s instead.', RequestDataCollector::class, BaseRequestDataCollector::class), E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* RequestDataCollector.
|
||||
*
|
||||
* @author Jules Pietri <jusles@heahprod.com>
|
||||
*
|
||||
* @deprecated since Symfony 4.1
|
||||
*/
|
||||
class RequestDataCollector extends BaseRequestDataCollector
|
||||
{
|
||||
}
|
@ -11,7 +11,6 @@
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass as SecurityExpressionLanguageProvidersPass;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
@ -23,17 +22,6 @@ use Symfony\Component\DependencyInjection\Reference;
|
||||
*/
|
||||
class AddExpressionLanguageProvidersPass implements CompilerPassInterface
|
||||
{
|
||||
private $handleSecurityLanguageProviders;
|
||||
|
||||
public function __construct(bool $handleSecurityLanguageProviders = true)
|
||||
{
|
||||
if ($handleSecurityLanguageProviders) {
|
||||
@trigger_error(sprintf('Registering services tagged "security.expression_language_provider" with "%s" is deprecated since Symfony 4.2, use the "%s" instead.', __CLASS__, SecurityExpressionLanguageProvidersPass::class), E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$this->handleSecurityLanguageProviders = $handleSecurityLanguageProviders;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -46,13 +34,5 @@ class AddExpressionLanguageProvidersPass implements CompilerPassInterface
|
||||
$definition->addMethodCall('addExpressionLanguageProvider', [new Reference($id)]);
|
||||
}
|
||||
}
|
||||
|
||||
// security
|
||||
if ($this->handleSecurityLanguageProviders && $container->has('security.expression_language')) {
|
||||
$definition = $container->findDefinition('security.expression_language');
|
||||
foreach ($container->findTaggedServiceIds('security.expression_language_provider', true) as $id => $attributes) {
|
||||
$definition->addMethodCall('registerProvider', [new Reference($id)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass as BaseCacheCollectorPass;
|
||||
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use "%s" instead.', CacheCollectorPass::class, BaseCacheCollectorPass::class), E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Inject a data collector to all the cache services to be able to get detailed statistics.
|
||||
*
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*
|
||||
* @deprecated since Symfony 4.2, use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass instead.
|
||||
*/
|
||||
class CacheCollectorPass extends BaseCacheCollectorPass
|
||||
{
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass as BaseCachePoolClearerPass;
|
||||
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use "%s" instead.', CachePoolClearerPass::class, BaseCachePoolClearerPass::class), E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*
|
||||
* @deprecated since version 4.2, use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass instead.
|
||||
*/
|
||||
class CachePoolClearerPass extends BaseCachePoolClearerPass
|
||||
{
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\Cache\DependencyInjection\CachePoolPass as BaseCachePoolPass;
|
||||
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use "%s" instead.', CachePoolPass::class, BaseCachePoolPass::class), E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*
|
||||
* @deprecated since version 4.2, use Symfony\Component\Cache\DependencyInjection\CachePoolPass instead.
|
||||
*/
|
||||
class CachePoolPass extends BaseCachePoolPass
|
||||
{
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass as BaseCachePoolPrunerPass;
|
||||
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use "%s" instead.', CachePoolPrunerPass::class, BaseCachePoolPrunerPass::class), E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* @author Rob Frawley 2nd <rmf@src.run>
|
||||
*
|
||||
* @deprecated since Symfony 4.2, use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass instead.
|
||||
*/
|
||||
class CachePoolPrunerPass extends BaseCachePoolPrunerPass
|
||||
{
|
||||
}
|
@ -677,27 +677,6 @@ class Configuration implements ConfigurationInterface
|
||||
->arrayNode('validation')
|
||||
->info('validation configuration')
|
||||
->{!class_exists(FullStack::class) && class_exists(Validation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
|
||||
->validate()
|
||||
->ifTrue(function ($v) { return isset($v['strict_email']) && isset($v['email_validation_mode']); })
|
||||
->thenInvalid('"strict_email" and "email_validation_mode" cannot be used together.')
|
||||
->end()
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) { return isset($v['strict_email']); })
|
||||
->then(function ($v) {
|
||||
@trigger_error('The "framework.validation.strict_email" configuration key has been deprecated in Symfony 4.1. Use the "framework.validation.email_validation_mode" configuration key instead.', E_USER_DEPRECATED);
|
||||
|
||||
return $v;
|
||||
})
|
||||
->end()
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) { return isset($v['strict_email']) && !isset($v['email_validation_mode']); })
|
||||
->then(function ($v) {
|
||||
$v['email_validation_mode'] = $v['strict_email'] ? 'strict' : 'loose';
|
||||
unset($v['strict_email']);
|
||||
|
||||
return $v;
|
||||
})
|
||||
->end()
|
||||
->children()
|
||||
->scalarNode('cache')->end()
|
||||
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && class_exists(Annotation::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
|
||||
@ -711,7 +690,6 @@ class Configuration implements ConfigurationInterface
|
||||
->end()
|
||||
->end()
|
||||
->scalarNode('translation_domain')->defaultValue('validators')->end()
|
||||
->booleanNode('strict_email')->end()
|
||||
->enumNode('email_validation_mode')->values(['html5', 'loose', 'strict'])->end()
|
||||
->arrayNode('mapping')
|
||||
->addDefaultsIfNotSet()
|
||||
|
@ -101,11 +101,9 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
|
||||
use Symfony\Component\Validator\ObjectInitializerInterface;
|
||||
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
|
||||
use Symfony\Component\WebLink\HttpHeaderSerializer;
|
||||
use Symfony\Component\Workflow;
|
||||
use Symfony\Component\Workflow\WorkflowInterface;
|
||||
@ -1072,12 +1070,6 @@ class FrameworkExtension extends Extension
|
||||
|
||||
$validatorBuilder = $container->getDefinition('validator.builder');
|
||||
|
||||
if (interface_exists(TranslatorInterface::class) && class_exists(LegacyTranslatorProxy::class)) {
|
||||
$calls = $validatorBuilder->getMethodCalls();
|
||||
$calls[1] = ['setTranslator', [new Definition(LegacyTranslatorProxy::class, [new Reference('translator')])]];
|
||||
$validatorBuilder->setMethodCalls($calls);
|
||||
}
|
||||
|
||||
$container->setParameter('validator.translation_domain', $config['translation_domain']);
|
||||
|
||||
$files = ['xml' => [], 'yml' => []];
|
||||
@ -1547,7 +1539,6 @@ class FrameworkExtension extends Extension
|
||||
$container->register($busId, MessageBus::class)->addArgument([])->addTag('messenger.bus');
|
||||
|
||||
if ($busId === $config['default_bus']) {
|
||||
$container->setAlias('message_bus', $busId)->setPublic(true)->setDeprecated(true, 'The "%alias_id%" service is deprecated, use the "messenger.default_bus" service instead.');
|
||||
$container->setAlias('messenger.default_bus', $busId)->setPublic(true);
|
||||
$container->setAlias(MessageBusInterface::class, $busId);
|
||||
} else {
|
||||
|
@ -11,11 +11,196 @@
|
||||
|
||||
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.
|
||||
* Simulates a browser and makes requests to a Kernel object.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class KernelBrowser extends Client
|
||||
class KernelBrowser 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();
|
||||
}
|
||||
}
|
||||
|
@ -11,11 +11,6 @@
|
||||
<tag name="cache.pool" clearer="cache.app_clearer" reset="reset" />
|
||||
</service>
|
||||
|
||||
<service id="cache.app.simple" class="Symfony\Component\Cache\Psr16Cache">
|
||||
<deprecated>The "Psr\SimpleCache\CacheInterface" / "%service_id%" service is deprecated since Symfony 4.3. Use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead.</deprecated>
|
||||
<argument type="service" id="cache.app" />
|
||||
</service>
|
||||
|
||||
<service id="cache.app.taggable" class="Symfony\Component\Cache\Adapter\TagAwareAdapter">
|
||||
<argument type="service" id="cache.app" />
|
||||
</service>
|
||||
@ -155,7 +150,6 @@
|
||||
<service id="cache.global_clearer" parent="cache.default_clearer" public="true" />
|
||||
<service id="cache.app_clearer" alias="cache.default_clearer" public="true" />
|
||||
<service id="Psr\Cache\CacheItemPoolInterface" alias="cache.app" />
|
||||
<service id="Psr\SimpleCache\CacheInterface" alias="cache.app.simple" />
|
||||
<service id="Symfony\Component\Cache\Adapter\AdapterInterface" alias="cache.app" />
|
||||
<service id="Symfony\Contracts\Cache\CacheInterface" alias="cache.app" />
|
||||
<service id="Symfony\Contracts\Cache\TagAwareCacheInterface" alias="cache.app.taggable" />
|
||||
|
@ -11,8 +11,5 @@
|
||||
<service id="Symfony\Contracts\Translation\TranslatorInterface" alias="translator" />
|
||||
|
||||
<service id="identity_translator" class="Symfony\Component\Translation\IdentityTranslator" />
|
||||
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector">
|
||||
<deprecated>The "%service_id%" service is deprecated since Symfony 4.2, use "identity_translator" instead.</deprecated>
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
@ -64,10 +64,6 @@
|
||||
</argument>
|
||||
</service>
|
||||
|
||||
<service id="session.save_listener" class="Symfony\Component\HttpKernel\EventListener\SaveSessionListener">
|
||||
<deprecated>The "%service_id%" service is deprecated since Symfony 4.1. Use the "session_listener" service instead.</deprecated>
|
||||
</service>
|
||||
|
||||
<!-- for BC -->
|
||||
<service id="session.storage.filesystem" alias="session.storage.mock_file" />
|
||||
</services>
|
||||
|
@ -17,7 +17,7 @@
|
||||
</service>
|
||||
<service id="Symfony\Component\Validator\Validator\ValidatorInterface" alias="validator" />
|
||||
|
||||
<service id="validator.builder" class="Symfony\Component\Validator\ValidatorBuilderInterface">
|
||||
<service id="validator.builder" class="Symfony\Component\Validator\ValidatorBuilder">
|
||||
<factory class="Symfony\Component\Validator\Validation" method="createValidatorBuilder" />
|
||||
<call method="setConstraintValidatorFactory">
|
||||
<argument type="service" id="validator.validator_factory" />
|
||||
|
@ -104,29 +104,6 @@ class ApplicationTest extends TestCase
|
||||
$this->assertSame($command, $application->find('alias'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation The "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" class is deprecated since Symfony 4.2, use "Symfony\Component\Console\Command\Command" with dependency injection instead.
|
||||
*/
|
||||
public function testBundleCommandsHaveRightContainer()
|
||||
{
|
||||
$command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', ['foo'], '', true, true, true, ['setContainer']);
|
||||
$command->setCode(function () {});
|
||||
$command->expects($this->exactly(2))->method('setContainer');
|
||||
|
||||
$application = new Application($this->getKernel([], true));
|
||||
$application->setAutoExit(false);
|
||||
$application->setCatchExceptions(false);
|
||||
$application->add($command);
|
||||
$tester = new ApplicationTester($application);
|
||||
|
||||
// set container is called here
|
||||
$tester->run(['command' => 'foo']);
|
||||
|
||||
// as the container might have change between two runs, setContainer must called again
|
||||
$tester->run(['command' => 'foo']);
|
||||
}
|
||||
|
||||
public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName()
|
||||
{
|
||||
$command = new Command('example');
|
||||
|
@ -11,13 +11,28 @@
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Fig\Link\Link;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\Form\FormConfigInterface;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Core\User\User;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
class AbstractControllerTest extends ControllerTraitTest
|
||||
class AbstractControllerTest extends TestCase
|
||||
{
|
||||
protected function createController()
|
||||
{
|
||||
@ -78,49 +93,476 @@ class AbstractControllerTest extends ControllerTraitTest
|
||||
|
||||
$controller->getParameter('foo');
|
||||
}
|
||||
}
|
||||
|
||||
class TestAbstractController extends AbstractController
|
||||
public function testForward()
|
||||
{
|
||||
use TestControllerTrait;
|
||||
$request = Request::create('/');
|
||||
$request->setLocale('fr');
|
||||
$request->setRequestFormat('xml');
|
||||
|
||||
private $throwOnUnexpectedService;
|
||||
$requestStack = new RequestStack();
|
||||
$requestStack->push($request);
|
||||
|
||||
public function __construct($throwOnUnexpectedService = true)
|
||||
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
|
||||
$kernel->expects($this->once())->method('handle')->willReturnCallback(function (Request $request) {
|
||||
return new Response($request->getRequestFormat().'--'.$request->getLocale());
|
||||
});
|
||||
|
||||
$container = new Container();
|
||||
$container->set('request_stack', $requestStack);
|
||||
$container->set('http_kernel', $kernel);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$response = $controller->forward('a_controller');
|
||||
$this->assertEquals('xml--fr', $response->getContent());
|
||||
}
|
||||
|
||||
public function testGetUser()
|
||||
{
|
||||
$this->throwOnUnexpectedService = $throwOnUnexpectedService;
|
||||
$user = new User('user', 'pass');
|
||||
$token = new UsernamePasswordToken($user, 'pass', 'default', ['ROLE_USER']);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($this->getContainerWithTokenStorage($token));
|
||||
|
||||
$this->assertSame($controller->getUser(), $user);
|
||||
}
|
||||
|
||||
public function setContainer(ContainerInterface $container)
|
||||
public function testGetUserAnonymousUserConvertedToNull()
|
||||
{
|
||||
if (!$this->throwOnUnexpectedService) {
|
||||
return parent::setContainer($container);
|
||||
$token = new AnonymousToken('default', 'anon.');
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($this->getContainerWithTokenStorage($token));
|
||||
|
||||
$this->assertNull($controller->getUser());
|
||||
}
|
||||
|
||||
$expected = self::getSubscribedServices();
|
||||
|
||||
foreach ($container->getServiceIds() as $id) {
|
||||
if ('service_container' === $id) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($expected[$id])) {
|
||||
throw new \UnexpectedValueException(sprintf('Service "%s" is not expected, as declared by %s::getSubscribedServices()', $id, AbstractController::class));
|
||||
}
|
||||
$type = substr($expected[$id], 1);
|
||||
if (!$container->get($id) instanceof $type) {
|
||||
throw new \UnexpectedValueException(sprintf('Service "%s" is expected to be an instance of "%s", as declared by %s::getSubscribedServices()', $id, $type, AbstractController::class));
|
||||
}
|
||||
}
|
||||
|
||||
return parent::setContainer($container);
|
||||
}
|
||||
|
||||
public function getParameter(string $name)
|
||||
public function testGetUserWithEmptyTokenStorage()
|
||||
{
|
||||
return parent::getParameter($name);
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($this->getContainerWithTokenStorage(null));
|
||||
|
||||
$this->assertNull($controller->getUser());
|
||||
}
|
||||
|
||||
public function fooAction()
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage The SecurityBundle is not registered in your application.
|
||||
*/
|
||||
public function testGetUserWithEmptyContainer()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer(new Container());
|
||||
|
||||
$controller->getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $token
|
||||
*
|
||||
* @return Container
|
||||
*/
|
||||
private function getContainerWithTokenStorage($token = null)
|
||||
{
|
||||
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage')->getMock();
|
||||
$tokenStorage
|
||||
->expects($this->once())
|
||||
->method('getToken')
|
||||
->willReturn($token);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.token_storage', $tokenStorage);
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
public function testJson()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer(new Container());
|
||||
|
||||
$response = $controller->json([]);
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('[]', $response->getContent());
|
||||
}
|
||||
|
||||
public function testJsonWithSerializer()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
|
||||
$serializer
|
||||
->expects($this->once())
|
||||
->method('serialize')
|
||||
->with([], 'json', ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS])
|
||||
->willReturn('[]');
|
||||
|
||||
$container->set('serializer', $serializer);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$response = $controller->json([]);
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('[]', $response->getContent());
|
||||
}
|
||||
|
||||
public function testJsonWithSerializerContextOverride()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
|
||||
$serializer
|
||||
->expects($this->once())
|
||||
->method('serialize')
|
||||
->with([], 'json', ['json_encode_options' => 0, 'other' => 'context'])
|
||||
->willReturn('[]');
|
||||
|
||||
$container->set('serializer', $serializer);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$response = $controller->json([], 200, [], ['json_encode_options' => 0, 'other' => 'context']);
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('[]', $response->getContent());
|
||||
$response->setEncodingOptions(JSON_FORCE_OBJECT);
|
||||
$this->assertEquals('{}', $response->getContent());
|
||||
}
|
||||
|
||||
public function testFile()
|
||||
{
|
||||
$container = new Container();
|
||||
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
|
||||
$container->set('http_kernel', $kernel);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(new File(__FILE__));
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileAsInline()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(new File(__FILE__), null, ResponseHeaderBag::DISPOSITION_INLINE);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition'));
|
||||
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileWithOwnFileName()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$fileName = 'test.php';
|
||||
$response = $controller->file(new File(__FILE__), $fileName);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains($fileName, $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileWithOwnFileNameAsInline()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$fileName = 'test.php';
|
||||
$response = $controller->file(new File(__FILE__), $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition'));
|
||||
$this->assertContains($fileName, $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileFromPath()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(__FILE__);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileFromPathWithCustomizedFileName()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(__FILE__, 'test.php');
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains('test.php', $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
|
||||
*/
|
||||
public function testFileWhichDoesNotExist()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file('some-file.txt', 'test.php');
|
||||
}
|
||||
|
||||
public function testIsGranted()
|
||||
{
|
||||
$authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
|
||||
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.authorization_checker', $authorizationChecker);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertTrue($controller->isGranted('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
|
||||
*/
|
||||
public function testdenyAccessUnlessGranted()
|
||||
{
|
||||
$authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
|
||||
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.authorization_checker', $authorizationChecker);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$controller->denyAccessUnlessGranted('foo');
|
||||
}
|
||||
|
||||
public function testRenderViewTwig()
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$twig->expects($this->once())->method('render')->willReturn('bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('twig', $twig);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals('bar', $controller->renderView('foo'));
|
||||
}
|
||||
|
||||
public function testRenderTwig()
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$twig->expects($this->once())->method('render')->willReturn('bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('twig', $twig);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals('bar', $controller->render('foo')->getContent());
|
||||
}
|
||||
|
||||
public function testStreamTwig()
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('twig', $twig);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
|
||||
}
|
||||
|
||||
public function testRedirectToRoute()
|
||||
{
|
||||
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
|
||||
$router->expects($this->once())->method('generate')->willReturn('/foo');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('router', $router);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
$response = $controller->redirectToRoute('foo');
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||
$this->assertSame('/foo', $response->getTargetUrl());
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testAddFlash()
|
||||
{
|
||||
$flashBag = new FlashBag();
|
||||
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
|
||||
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('session', $session);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
$controller->addFlash('foo', 'bar');
|
||||
|
||||
$this->assertSame(['bar'], $flashBag->get('foo'));
|
||||
}
|
||||
|
||||
public function testCreateAccessDeniedException()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\Security\Core\Exception\AccessDeniedException', $controller->createAccessDeniedException());
|
||||
}
|
||||
|
||||
public function testIsCsrfTokenValid()
|
||||
{
|
||||
$tokenManager = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
|
||||
$tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.csrf.token_manager', $tokenManager);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertTrue($controller->isCsrfTokenValid('foo', 'bar'));
|
||||
}
|
||||
|
||||
public function testGenerateUrl()
|
||||
{
|
||||
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
|
||||
$router->expects($this->once())->method('generate')->willReturn('/foo');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('router', $router);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals('/foo', $controller->generateUrl('foo'));
|
||||
}
|
||||
|
||||
public function testRedirect()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$response = $controller->redirect('http://dunglas.fr', 301);
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||
$this->assertSame('http://dunglas.fr', $response->getTargetUrl());
|
||||
$this->assertSame(301, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testCreateNotFoundException()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', $controller->createNotFoundException());
|
||||
}
|
||||
|
||||
public function testCreateForm()
|
||||
{
|
||||
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
|
||||
|
||||
$formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
|
||||
$formFactory->expects($this->once())->method('create')->willReturn($form);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('form.factory', $formFactory);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals($form, $controller->createForm('foo'));
|
||||
}
|
||||
|
||||
public function testCreateFormBuilder()
|
||||
{
|
||||
$formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilderInterface')->getMock();
|
||||
|
||||
$formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
|
||||
$formFactory->expects($this->once())->method('createBuilder')->willReturn($formBuilder);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('form.factory', $formFactory);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
|
||||
}
|
||||
|
||||
public function testGetDoctrine()
|
||||
{
|
||||
$doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('doctrine', $doctrine);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals($doctrine, $controller->getDoctrine());
|
||||
}
|
||||
|
||||
public function testAddLink()
|
||||
{
|
||||
$request = new Request();
|
||||
$link1 = new Link('mercure', 'https://demo.mercure.rocks');
|
||||
$link2 = new Link('self', 'https://example.com/foo');
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->addLink($request, $link1);
|
||||
$controller->addLink($request, $link2);
|
||||
|
||||
$links = $request->attributes->get('_links')->getLinks();
|
||||
$this->assertContains($link1, $links);
|
||||
$this->assertContains($link2, $links);
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\Controller;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class ControllerTest extends ControllerTraitTest
|
||||
{
|
||||
protected function createController()
|
||||
{
|
||||
return new TestController();
|
||||
}
|
||||
}
|
@ -1,534 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\Controller;
|
||||
|
||||
use Fig\Link\Link;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
|
||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\Form\FormConfigInterface;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Core\User\User;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
abstract class ControllerTraitTest extends TestCase
|
||||
{
|
||||
abstract protected function createController();
|
||||
|
||||
public function testForward()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->setLocale('fr');
|
||||
$request->setRequestFormat('xml');
|
||||
|
||||
$requestStack = new RequestStack();
|
||||
$requestStack->push($request);
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
|
||||
$kernel->expects($this->once())->method('handle')->willReturnCallback(function (Request $request) {
|
||||
return new Response($request->getRequestFormat().'--'.$request->getLocale());
|
||||
});
|
||||
|
||||
$container = new Container();
|
||||
$container->set('request_stack', $requestStack);
|
||||
$container->set('http_kernel', $kernel);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$response = $controller->forward('a_controller');
|
||||
$this->assertEquals('xml--fr', $response->getContent());
|
||||
}
|
||||
|
||||
public function testGetUser()
|
||||
{
|
||||
$user = new User('user', 'pass');
|
||||
$token = new UsernamePasswordToken($user, 'pass', 'default', ['ROLE_USER']);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($this->getContainerWithTokenStorage($token));
|
||||
|
||||
$this->assertSame($controller->getUser(), $user);
|
||||
}
|
||||
|
||||
public function testGetUserAnonymousUserConvertedToNull()
|
||||
{
|
||||
$token = new AnonymousToken('default', 'anon.');
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($this->getContainerWithTokenStorage($token));
|
||||
|
||||
$this->assertNull($controller->getUser());
|
||||
}
|
||||
|
||||
public function testGetUserWithEmptyTokenStorage()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($this->getContainerWithTokenStorage(null));
|
||||
|
||||
$this->assertNull($controller->getUser());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage The SecurityBundle is not registered in your application.
|
||||
*/
|
||||
public function testGetUserWithEmptyContainer()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer(new Container());
|
||||
|
||||
$controller->getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $token
|
||||
*
|
||||
* @return Container
|
||||
*/
|
||||
private function getContainerWithTokenStorage($token = null)
|
||||
{
|
||||
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage')->getMock();
|
||||
$tokenStorage
|
||||
->expects($this->once())
|
||||
->method('getToken')
|
||||
->willReturn($token);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.token_storage', $tokenStorage);
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
public function testJson()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer(new Container());
|
||||
|
||||
$response = $controller->json([]);
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('[]', $response->getContent());
|
||||
}
|
||||
|
||||
public function testJsonWithSerializer()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
|
||||
$serializer
|
||||
->expects($this->once())
|
||||
->method('serialize')
|
||||
->with([], 'json', ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS])
|
||||
->willReturn('[]');
|
||||
|
||||
$container->set('serializer', $serializer);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$response = $controller->json([]);
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('[]', $response->getContent());
|
||||
}
|
||||
|
||||
public function testJsonWithSerializerContextOverride()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
|
||||
$serializer
|
||||
->expects($this->once())
|
||||
->method('serialize')
|
||||
->with([], 'json', ['json_encode_options' => 0, 'other' => 'context'])
|
||||
->willReturn('[]');
|
||||
|
||||
$container->set('serializer', $serializer);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$response = $controller->json([], 200, [], ['json_encode_options' => 0, 'other' => 'context']);
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('[]', $response->getContent());
|
||||
$response->setEncodingOptions(JSON_FORCE_OBJECT);
|
||||
$this->assertEquals('{}', $response->getContent());
|
||||
}
|
||||
|
||||
public function testFile()
|
||||
{
|
||||
$container = new Container();
|
||||
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
|
||||
$container->set('http_kernel', $kernel);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(new File(__FILE__));
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileAsInline()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(new File(__FILE__), null, ResponseHeaderBag::DISPOSITION_INLINE);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition'));
|
||||
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileWithOwnFileName()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$fileName = 'test.php';
|
||||
$response = $controller->file(new File(__FILE__), $fileName);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains($fileName, $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileWithOwnFileNameAsInline()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$fileName = 'test.php';
|
||||
$response = $controller->file(new File(__FILE__), $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition'));
|
||||
$this->assertContains($fileName, $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileFromPath()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(__FILE__);
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
public function testFileFromPathWithCustomizedFileName()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file(__FILE__, 'test.php');
|
||||
|
||||
$this->assertInstanceOf(BinaryFileResponse::class, $response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
if ($response->headers->get('content-type')) {
|
||||
$this->assertSame('text/x-php', $response->headers->get('content-type'));
|
||||
}
|
||||
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
|
||||
$this->assertContains('test.php', $response->headers->get('content-disposition'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
|
||||
*/
|
||||
public function testFileWhichDoesNotExist()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
/* @var BinaryFileResponse $response */
|
||||
$response = $controller->file('some-file.txt', 'test.php');
|
||||
}
|
||||
|
||||
public function testIsGranted()
|
||||
{
|
||||
$authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
|
||||
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.authorization_checker', $authorizationChecker);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertTrue($controller->isGranted('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
|
||||
*/
|
||||
public function testdenyAccessUnlessGranted()
|
||||
{
|
||||
$authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')->getMock();
|
||||
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.authorization_checker', $authorizationChecker);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$controller->denyAccessUnlessGranted('foo');
|
||||
}
|
||||
|
||||
public function testRenderViewTwig()
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$twig->expects($this->once())->method('render')->willReturn('bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('twig', $twig);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals('bar', $controller->renderView('foo'));
|
||||
}
|
||||
|
||||
public function testRenderTwig()
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
$twig->expects($this->once())->method('render')->willReturn('bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('twig', $twig);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals('bar', $controller->render('foo')->getContent());
|
||||
}
|
||||
|
||||
public function testStreamTwig()
|
||||
{
|
||||
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('twig', $twig);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
|
||||
}
|
||||
|
||||
public function testRedirectToRoute()
|
||||
{
|
||||
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
|
||||
$router->expects($this->once())->method('generate')->willReturn('/foo');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('router', $router);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
$response = $controller->redirectToRoute('foo');
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||
$this->assertSame('/foo', $response->getTargetUrl());
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testAddFlash()
|
||||
{
|
||||
$flashBag = new FlashBag();
|
||||
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
|
||||
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('session', $session);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
$controller->addFlash('foo', 'bar');
|
||||
|
||||
$this->assertSame(['bar'], $flashBag->get('foo'));
|
||||
}
|
||||
|
||||
public function testCreateAccessDeniedException()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\Security\Core\Exception\AccessDeniedException', $controller->createAccessDeniedException());
|
||||
}
|
||||
|
||||
public function testIsCsrfTokenValid()
|
||||
{
|
||||
$tokenManager = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
|
||||
$tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('security.csrf.token_manager', $tokenManager);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertTrue($controller->isCsrfTokenValid('foo', 'bar'));
|
||||
}
|
||||
|
||||
public function testGenerateUrl()
|
||||
{
|
||||
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
|
||||
$router->expects($this->once())->method('generate')->willReturn('/foo');
|
||||
|
||||
$container = new Container();
|
||||
$container->set('router', $router);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals('/foo', $controller->generateUrl('foo'));
|
||||
}
|
||||
|
||||
public function testRedirect()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
$response = $controller->redirect('http://dunglas.fr', 301);
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||
$this->assertSame('http://dunglas.fr', $response->getTargetUrl());
|
||||
$this->assertSame(301, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testCreateNotFoundException()
|
||||
{
|
||||
$controller = $this->createController();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', $controller->createNotFoundException());
|
||||
}
|
||||
|
||||
public function testCreateForm()
|
||||
{
|
||||
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
|
||||
|
||||
$formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
|
||||
$formFactory->expects($this->once())->method('create')->willReturn($form);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('form.factory', $formFactory);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals($form, $controller->createForm('foo'));
|
||||
}
|
||||
|
||||
public function testCreateFormBuilder()
|
||||
{
|
||||
$formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilderInterface')->getMock();
|
||||
|
||||
$formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
|
||||
$formFactory->expects($this->once())->method('createBuilder')->willReturn($formBuilder);
|
||||
|
||||
$container = new Container();
|
||||
$container->set('form.factory', $formFactory);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
|
||||
}
|
||||
|
||||
public function testGetDoctrine()
|
||||
{
|
||||
$doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('doctrine', $doctrine);
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->setContainer($container);
|
||||
|
||||
$this->assertEquals($doctrine, $controller->getDoctrine());
|
||||
}
|
||||
|
||||
public function testAddLink()
|
||||
{
|
||||
$request = new Request();
|
||||
$link1 = new Link('mercure', 'https://demo.mercure.rocks');
|
||||
$link2 = new Link('self', 'https://example.com/foo');
|
||||
|
||||
$controller = $this->createController();
|
||||
$controller->addLink($request, $link1);
|
||||
$controller->addLink($request, $link2);
|
||||
|
||||
$links = $request->attributes->get('_links')->getLinks();
|
||||
$this->assertContains($link1, $links);
|
||||
$this->assertContains($link2, $links);
|
||||
}
|
||||
}
|
||||
|
||||
trait TestControllerTrait
|
||||
{
|
||||
use ControllerTrait {
|
||||
generateUrl as public;
|
||||
redirect as public;
|
||||
forward as public;
|
||||
getUser as public;
|
||||
json as public;
|
||||
file as public;
|
||||
isGranted as public;
|
||||
denyAccessUnlessGranted as public;
|
||||
redirectToRoute as public;
|
||||
addFlash as public;
|
||||
isCsrfTokenValid as public;
|
||||
renderView as public;
|
||||
render as public;
|
||||
stream as public;
|
||||
createNotFoundException as public;
|
||||
createAccessDeniedException as public;
|
||||
createForm as public;
|
||||
createFormBuilder as public;
|
||||
getDoctrine as public;
|
||||
addLink as public;
|
||||
}
|
||||
}
|
@ -0,0 +1,179 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\Controller;
|
||||
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Fig\Link\Link;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
|
||||
class TestAbstractController extends AbstractController
|
||||
{
|
||||
private $throwOnUnexpectedService;
|
||||
|
||||
public function __construct($throwOnUnexpectedService = true)
|
||||
{
|
||||
$this->throwOnUnexpectedService = $throwOnUnexpectedService;
|
||||
}
|
||||
|
||||
public function setContainer(ContainerInterface $container)
|
||||
{
|
||||
if (!$this->throwOnUnexpectedService) {
|
||||
return parent::setContainer($container);
|
||||
}
|
||||
|
||||
$expected = self::getSubscribedServices();
|
||||
|
||||
foreach ($container->getServiceIds() as $id) {
|
||||
if ('service_container' === $id) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($expected[$id])) {
|
||||
throw new \UnexpectedValueException(sprintf('Service "%s" is not expected, as declared by %s::getSubscribedServices()', $id, AbstractController::class));
|
||||
}
|
||||
$type = substr($expected[$id], 1);
|
||||
if (!$container->get($id) instanceof $type) {
|
||||
throw new \UnexpectedValueException(sprintf('Service "%s" is expected to be an instance of "%s", as declared by %s::getSubscribedServices()', $id, $type, AbstractController::class));
|
||||
}
|
||||
}
|
||||
|
||||
return parent::setContainer($container);
|
||||
}
|
||||
|
||||
public function getParameter(string $name)
|
||||
{
|
||||
return parent::getParameter($name);
|
||||
}
|
||||
|
||||
public function fooAction()
|
||||
{
|
||||
}
|
||||
|
||||
public function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
|
||||
{
|
||||
return parent::generateUrl($route, $parameters, $referenceType);
|
||||
}
|
||||
|
||||
public function forward(string $controller, array $path = [], array $query = []): Response
|
||||
{
|
||||
return parent::forward($controller, $path, $query);
|
||||
}
|
||||
|
||||
public function redirect(string $url, int $status = 302): RedirectResponse
|
||||
{
|
||||
return parent::redirect($url, $status);
|
||||
}
|
||||
|
||||
public function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
|
||||
{
|
||||
return parent::redirectToRoute($route, $parameters, $status);
|
||||
}
|
||||
|
||||
public function json($data, int $status = 200, array $headers = [], array $context = []): JsonResponse
|
||||
{
|
||||
return parent::json($data, $status, $headers, $context);
|
||||
}
|
||||
|
||||
public function file($file, string $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT): BinaryFileResponse
|
||||
{
|
||||
return parent::file($file, $fileName, $disposition);
|
||||
}
|
||||
|
||||
public function addFlash(string $type, string $message): void
|
||||
{
|
||||
parent::addFlash($type, $message);
|
||||
}
|
||||
|
||||
public function isGranted($attributes, $subject = null): bool
|
||||
{
|
||||
return parent::isGranted($attributes, $subject);
|
||||
}
|
||||
|
||||
public function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.'): void
|
||||
{
|
||||
parent::denyAccessUnlessGranted($attributes, $subject, $message);
|
||||
}
|
||||
|
||||
public function renderView(string $view, array $parameters = []): string
|
||||
{
|
||||
return parent::renderView($view, $parameters);
|
||||
}
|
||||
|
||||
public function render(string $view, array $parameters = [], Response $response = null): Response
|
||||
{
|
||||
return parent::render($view, $parameters, $response);
|
||||
}
|
||||
|
||||
public function stream(string $view, array $parameters = [], StreamedResponse $response = null): StreamedResponse
|
||||
{
|
||||
return parent::stream($view, $parameters, $response);
|
||||
}
|
||||
|
||||
public function createNotFoundException(string $message = 'Not found', \Throwable $previous = null): NotFoundHttpException
|
||||
{
|
||||
return parent::createNotFoundException($message, $previous);
|
||||
}
|
||||
|
||||
public function createAccessDeniedException(string $message = 'Access Denied.', \Throwable $previous = null): AccessDeniedException
|
||||
{
|
||||
return parent::createAccessDeniedException($message, $previous);
|
||||
}
|
||||
|
||||
public function createForm(string $type, $data = null, array $options = []): FormInterface
|
||||
{
|
||||
return parent::createForm($type, $data, $options);
|
||||
}
|
||||
|
||||
public function createFormBuilder($data = null, array $options = []): FormBuilderInterface
|
||||
{
|
||||
return parent::createFormBuilder($data, $options);
|
||||
}
|
||||
|
||||
public function getDoctrine(): ManagerRegistry
|
||||
{
|
||||
return parent::getDoctrine();
|
||||
}
|
||||
|
||||
public function getUser()
|
||||
{
|
||||
return parent::getUser();
|
||||
}
|
||||
|
||||
public function isCsrfTokenValid(string $id, ?string $token): bool
|
||||
{
|
||||
return parent::isCsrfTokenValid($id, $token);
|
||||
}
|
||||
|
||||
public function dispatchMessage($message): Envelope
|
||||
{
|
||||
return parent::dispatchMessage($message);
|
||||
}
|
||||
|
||||
public function addLink(Request $request, Link $link): void
|
||||
{
|
||||
parent::addLink($request, $link);
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
|
||||
class TestController extends Controller
|
||||
{
|
||||
use TestControllerTrait;
|
||||
}
|
@ -57,88 +57,4 @@ class AddExpressionLanguageProvidersPassTest extends TestCase
|
||||
$this->assertEquals('addExpressionLanguageProvider', $calls[0][0]);
|
||||
$this->assertEquals(new Reference('some_routing_provider'), $calls[0][1][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Registering services tagged "security.expression_language_provider" with "Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass" is deprecated since Symfony 4.2, use the "Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass" instead.
|
||||
*/
|
||||
public function testProcessForSecurity()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
|
||||
|
||||
$definition = new Definition('\stdClass');
|
||||
$definition->addTag('security.expression_language_provider');
|
||||
$container->setDefinition('some_security_provider', $definition->setPublic(true));
|
||||
|
||||
$container->register('security.expression_language', '\stdClass')->setPublic(true);
|
||||
$container->compile();
|
||||
|
||||
$calls = $container->getDefinition('security.expression_language')->getMethodCalls();
|
||||
$this->assertCount(1, $calls);
|
||||
$this->assertEquals('registerProvider', $calls[0][0]);
|
||||
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Registering services tagged "security.expression_language_provider" with "Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass" is deprecated since Symfony 4.2, use the "Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass" instead.
|
||||
*/
|
||||
public function testProcessForSecurityAlias()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
|
||||
|
||||
$definition = new Definition('\stdClass');
|
||||
$definition->addTag('security.expression_language_provider');
|
||||
$container->setDefinition('some_security_provider', $definition->setPublic(true));
|
||||
|
||||
$container->register('my_security.expression_language', '\stdClass')->setPublic(true);
|
||||
$container->setAlias('security.expression_language', 'my_security.expression_language');
|
||||
$container->compile();
|
||||
|
||||
$calls = $container->getDefinition('my_security.expression_language')->getMethodCalls();
|
||||
$this->assertCount(1, $calls);
|
||||
$this->assertEquals('registerProvider', $calls[0][0]);
|
||||
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testProcessIgnoreSecurity()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
|
||||
|
||||
$definition = new Definition('\stdClass');
|
||||
$definition->addTag('security.expression_language_provider');
|
||||
$container->setDefinition('some_security_provider', $definition->setPublic(true));
|
||||
|
||||
$container->register('security.expression_language', '\stdClass')->setPublic(true);
|
||||
$container->compile();
|
||||
|
||||
$calls = $container->getDefinition('security.expression_language')->getMethodCalls();
|
||||
$this->assertCount(0, $calls);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testProcessIgnoreSecurityAlias()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
|
||||
|
||||
$definition = new Definition('\stdClass');
|
||||
$definition->addTag('security.expression_language_provider');
|
||||
$container->setDefinition('some_security_provider', $definition->setPublic(true));
|
||||
|
||||
$container->register('my_security.expression_language', '\stdClass')->setPublic(true);
|
||||
$container->setAlias('security.expression_language', 'my_security.expression_language');
|
||||
$container->compile();
|
||||
|
||||
$calls = $container->getDefinition('my_security.expression_language')->getMethodCalls();
|
||||
$this->assertCount(0, $calls);
|
||||
}
|
||||
}
|
||||
|
@ -1,52 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheCollectorPass;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TraceableAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
|
||||
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class CacheCollectorPassTest extends TestCase
|
||||
{
|
||||
public function testProcess()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container
|
||||
->register('fs', FilesystemAdapter::class)
|
||||
->addTag('cache.pool');
|
||||
$container
|
||||
->register('tagged_fs', TagAwareAdapter::class)
|
||||
->addArgument(new Reference('fs'))
|
||||
->addTag('cache.pool');
|
||||
|
||||
$collector = $container->register('data_collector.cache', CacheDataCollector::class);
|
||||
(new CacheCollectorPass())->process($container);
|
||||
|
||||
$this->assertEquals([
|
||||
['addInstance', ['fs', new Reference('fs')]],
|
||||
['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
|
||||
], $collector->getMethodCalls());
|
||||
|
||||
$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
|
||||
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
|
||||
$this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
|
||||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
|
||||
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
|
||||
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class CachePoolClearerPassTest extends TestCase
|
||||
{
|
||||
public function testPoolRefsAreWeak()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.container_class', 'app');
|
||||
$container->setParameter('kernel.project_dir', 'foo');
|
||||
|
||||
$globalClearer = new Definition(Psr6CacheClearer::class);
|
||||
$container->setDefinition('cache.global_clearer', $globalClearer);
|
||||
|
||||
$publicPool = new Definition();
|
||||
$publicPool->addArgument('namespace');
|
||||
$publicPool->addTag('cache.pool', ['clearer' => 'clearer_alias']);
|
||||
$container->setDefinition('public.pool', $publicPool);
|
||||
|
||||
$publicPool = new Definition();
|
||||
$publicPool->addArgument('namespace');
|
||||
$publicPool->addTag('cache.pool', ['clearer' => 'clearer_alias', 'name' => 'pool2']);
|
||||
$container->setDefinition('public.pool2', $publicPool);
|
||||
|
||||
$privatePool = new Definition();
|
||||
$privatePool->setPublic(false);
|
||||
$privatePool->addArgument('namespace');
|
||||
$privatePool->addTag('cache.pool', ['clearer' => 'clearer_alias']);
|
||||
$container->setDefinition('private.pool', $privatePool);
|
||||
|
||||
$clearer = new Definition();
|
||||
$container->setDefinition('clearer', $clearer);
|
||||
$container->setAlias('clearer_alias', 'clearer');
|
||||
|
||||
$pass = new RemoveUnusedDefinitionsPass();
|
||||
foreach ($container->getCompiler()->getPassConfig()->getRemovingPasses() as $removingPass) {
|
||||
if ($removingPass instanceof RepeatedPass) {
|
||||
$pass->setRepeatedPass(new RepeatedPass([$pass]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
foreach ([new CachePoolPass(), $pass, new CachePoolClearerPass()] as $pass) {
|
||||
$pass->process($container);
|
||||
}
|
||||
|
||||
$expected = [[
|
||||
'public.pool' => new Reference('public.pool'),
|
||||
'pool2' => new Reference('public.pool2'),
|
||||
]];
|
||||
$this->assertEquals($expected, $clearer->getArguments());
|
||||
$this->assertEquals($expected, $globalClearer->getArguments());
|
||||
}
|
||||
}
|
@ -1,133 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class CachePoolPassTest extends TestCase
|
||||
{
|
||||
private $cachePoolPass;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->cachePoolPass = new CachePoolPass();
|
||||
}
|
||||
|
||||
public function testNamespaceArgumentIsReplaced()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.container_class', 'app');
|
||||
$container->setParameter('kernel.project_dir', 'foo');
|
||||
$adapter = new Definition();
|
||||
$adapter->setAbstract(true);
|
||||
$adapter->addTag('cache.pool');
|
||||
$container->setDefinition('app.cache_adapter', $adapter);
|
||||
$container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
|
||||
$cachePool = new ChildDefinition('app.cache_adapter_alias');
|
||||
$cachePool->addArgument(null);
|
||||
$cachePool->addTag('cache.pool');
|
||||
$container->setDefinition('app.cache_pool', $cachePool);
|
||||
|
||||
$this->cachePoolPass->process($container);
|
||||
|
||||
$this->assertSame('z3X945Jbf5', $cachePool->getArgument(0));
|
||||
}
|
||||
|
||||
public function testNamespaceArgumentIsNotReplacedIfArrayAdapterIsUsed()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.container_class', 'app');
|
||||
$container->setParameter('kernel.project_dir', 'foo');
|
||||
|
||||
$container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
|
||||
|
||||
$cachePool = new ChildDefinition('cache.adapter.array');
|
||||
$cachePool->addTag('cache.pool');
|
||||
$container->setDefinition('app.cache_pool', $cachePool);
|
||||
|
||||
$this->cachePoolPass->process($container);
|
||||
|
||||
$this->assertCount(0, $container->getDefinition('app.cache_pool')->getArguments());
|
||||
}
|
||||
|
||||
public function testArgsAreReplaced()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.container_class', 'app');
|
||||
$container->setParameter('cache.prefix.seed', 'foo');
|
||||
$cachePool = new Definition();
|
||||
$cachePool->addTag('cache.pool', [
|
||||
'provider' => 'foobar',
|
||||
'default_lifetime' => 3,
|
||||
]);
|
||||
$cachePool->addArgument(null);
|
||||
$cachePool->addArgument(null);
|
||||
$cachePool->addArgument(null);
|
||||
$container->setDefinition('app.cache_pool', $cachePool);
|
||||
|
||||
$this->cachePoolPass->process($container);
|
||||
|
||||
$this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
|
||||
$this->assertSame('foobar', (string) $cachePool->getArgument(0));
|
||||
$this->assertSame('tQNhcV-8xa', $cachePool->getArgument(1));
|
||||
$this->assertSame(3, $cachePool->getArgument(2));
|
||||
}
|
||||
|
||||
public function testWithNameAttribute()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.container_class', 'app');
|
||||
$container->setParameter('cache.prefix.seed', 'foo');
|
||||
$cachePool = new Definition();
|
||||
$cachePool->addTag('cache.pool', [
|
||||
'name' => 'foobar',
|
||||
'provider' => 'foobar',
|
||||
]);
|
||||
$cachePool->addArgument(null);
|
||||
$cachePool->addArgument(null);
|
||||
$cachePool->addArgument(null);
|
||||
$container->setDefinition('app.cache_pool', $cachePool);
|
||||
|
||||
$this->cachePoolPass->process($container);
|
||||
|
||||
$this->assertSame('+naTpPa4Sm', $cachePool->getArgument(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are
|
||||
*/
|
||||
public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.container_class', 'app');
|
||||
$container->setParameter('kernel.project_dir', 'foo');
|
||||
$adapter = new Definition();
|
||||
$adapter->setAbstract(true);
|
||||
$adapter->addTag('cache.pool');
|
||||
$container->setDefinition('app.cache_adapter', $adapter);
|
||||
$cachePool = new ChildDefinition('app.cache_adapter');
|
||||
$cachePool->addTag('cache.pool', ['foobar' => 123]);
|
||||
$container->setDefinition('app.cache_pool', $cachePool);
|
||||
|
||||
$this->cachePoolPass->process($container);
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPrunerPass;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
|
||||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class CachePoolPrunerPassTest extends TestCase
|
||||
{
|
||||
public function testCompilerPassReplacesCommandArgument()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->register('console.command.cache_pool_prune')->addArgument([]);
|
||||
$container->register('pool.foo', FilesystemAdapter::class)->addTag('cache.pool');
|
||||
$container->register('pool.bar', PhpFilesAdapter::class)->addTag('cache.pool');
|
||||
|
||||
$pass = new CachePoolPrunerPass();
|
||||
$pass->process($container);
|
||||
|
||||
$expected = [
|
||||
'pool.foo' => new Reference('pool.foo'),
|
||||
'pool.bar' => new Reference('pool.bar'),
|
||||
];
|
||||
$argument = $container->getDefinition('console.command.cache_pool_prune')->getArgument(0);
|
||||
|
||||
$this->assertInstanceOf(IteratorArgument::class, $argument);
|
||||
$this->assertEquals($expected, $argument->getValues());
|
||||
}
|
||||
|
||||
public function testCompilePassIsIgnoredIfCommandDoesNotExist()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$definitionsBefore = \count($container->getDefinitions());
|
||||
$aliasesBefore = \count($container->getAliases());
|
||||
|
||||
$pass = new CachePoolPrunerPass();
|
||||
$pass->process($container);
|
||||
|
||||
// the container is untouched (i.e. no new definitions or aliases)
|
||||
$this->assertCount($definitionsBefore, $container->getDefinitions());
|
||||
$this->assertCount($aliasesBefore, $container->getAliases());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Class "Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\NotFound" used for service "pool.not-found" cannot be found.
|
||||
*/
|
||||
public function testCompilerPassThrowsOnInvalidDefinitionClass()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->register('console.command.cache_pool_prune')->addArgument([]);
|
||||
$container->register('pool.not-found', NotFound::class)->addTag('cache.pool');
|
||||
|
||||
$pass = new CachePoolPrunerPass();
|
||||
$pass->process($container);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
$container->loadFromExtension('framework', [
|
||||
'validation' => [
|
||||
'strict_email' => true,
|
||||
'email_validation_mode' => 'strict',
|
||||
],
|
||||
]);
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
$container->loadFromExtension('framework', [
|
||||
'validation' => [
|
||||
'strict_email' => false,
|
||||
],
|
||||
]);
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
$container->loadFromExtension('framework', [
|
||||
'validation' => [
|
||||
'strict_email' => true,
|
||||
],
|
||||
]);
|
@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" ?>
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:framework="http://symfony.com/schema/dic/symfony"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
|
||||
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
|
||||
|
||||
<framework:config>
|
||||
<framework:validation strict-email="true" email-validation-mode="strict" />
|
||||
</framework:config>
|
||||
</container>
|
@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" ?>
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:framework="http://symfony.com/schema/dic/symfony"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
|
||||
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
|
||||
|
||||
<framework:config>
|
||||
<framework:validation strict-email="false" />
|
||||
</framework:config>
|
||||
</container>
|
@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" ?>
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:framework="http://symfony.com/schema/dic/symfony"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
|
||||
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
|
||||
|
||||
<framework:config>
|
||||
<framework:validation strict-email="true" />
|
||||
</framework:config>
|
||||
</container>
|
@ -1,4 +0,0 @@
|
||||
framework:
|
||||
validation:
|
||||
strict_email: true
|
||||
email_validation_mode: html5
|
@ -1,3 +0,0 @@
|
||||
framework:
|
||||
validation:
|
||||
strict_email: false
|
@ -1,3 +0,0 @@
|
||||
framework:
|
||||
validation:
|
||||
strict_email: true
|
@ -52,10 +52,8 @@ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
|
||||
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
|
||||
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
|
||||
use Symfony\Component\Workflow;
|
||||
|
||||
abstract class FrameworkExtensionTest extends TestCase
|
||||
@ -575,8 +573,6 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
public function testMessenger()
|
||||
{
|
||||
$container = $this->createContainerFromFile('messenger');
|
||||
$this->assertTrue($container->hasAlias('message_bus'));
|
||||
$this->assertTrue($container->getAlias('message_bus')->isPublic());
|
||||
$this->assertTrue($container->hasAlias('messenger.default_bus'));
|
||||
$this->assertTrue($container->getAlias('messenger.default_bus')->isPublic());
|
||||
$this->assertFalse($container->hasDefinition('messenger.transport.amqp.factory'));
|
||||
@ -674,8 +670,6 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
['id' => 'handle_message', 'arguments' => []],
|
||||
], $container->getParameter('messenger.bus.queries.middleware'));
|
||||
|
||||
$this->assertTrue($container->hasAlias('message_bus'));
|
||||
$this->assertSame('messenger.bus.commands', (string) $container->getAlias('message_bus'));
|
||||
$this->assertTrue($container->hasAlias('messenger.default_bus'));
|
||||
$this->assertSame('messenger.bus.commands', (string) $container->getAlias('messenger.default_bus'));
|
||||
}
|
||||
@ -777,11 +771,7 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
$this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
|
||||
$this->assertEquals([new Reference('validator.validator_factory')], $calls[0][1]);
|
||||
$this->assertSame('setTranslator', $calls[1][0]);
|
||||
if (interface_exists(TranslatorInterface::class) && class_exists(LegacyTranslatorProxy::class)) {
|
||||
$this->assertEquals([new Definition(LegacyTranslatorProxy::class, [new Reference('translator')])], $calls[1][1]);
|
||||
} else {
|
||||
$this->assertEquals([new Reference('translator')], $calls[1][1]);
|
||||
}
|
||||
$this->assertSame('setTranslationDomain', $calls[2][0]);
|
||||
$this->assertSame(['%validator.translation_domain%'], $calls[2][1]);
|
||||
$this->assertSame('addXmlMappings', $calls[3][0]);
|
||||
@ -922,39 +912,6 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
// no cache, no annotations, no static methods
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation The "framework.validation.strict_email" configuration key has been deprecated in Symfony 4.1. Use the "framework.validation.email_validation_mode" configuration key instead.
|
||||
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
|
||||
* @expectedExceptionMessage "strict_email" and "email_validation_mode" cannot be used together.
|
||||
*/
|
||||
public function testCannotConfigureStrictEmailAndEmailValidationModeAtTheSameTime()
|
||||
{
|
||||
$this->createContainerFromFile('validation_strict_email_and_validation_mode');
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation The "framework.validation.strict_email" configuration key has been deprecated in Symfony 4.1. Use the "framework.validation.email_validation_mode" configuration key instead.
|
||||
*/
|
||||
public function testEnabledStrictEmailOptionIsMappedToStrictEmailValidationMode()
|
||||
{
|
||||
$container = $this->createContainerFromFile('validation_strict_email_enabled');
|
||||
|
||||
$this->assertSame('strict', $container->getDefinition('validator.email')->getArgument(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation The "framework.validation.strict_email" configuration key has been deprecated in Symfony 4.1. Use the "framework.validation.email_validation_mode" configuration key instead.
|
||||
*/
|
||||
public function testDisabledStrictEmailOptionIsMappedToLooseEmailValidationMode()
|
||||
{
|
||||
$container = $this->createContainerFromFile('validation_strict_email_disabled');
|
||||
|
||||
$this->assertSame('loose', $container->getDefinition('validator.email')->getArgument(0));
|
||||
}
|
||||
|
||||
public function testEmailValidationModeIsPassedToEmailValidator()
|
||||
{
|
||||
$container = $this->createContainerFromFile('validation_email_validation_mode');
|
||||
|
@ -62,19 +62,6 @@ class TranslatorTest extends TestCase
|
||||
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testTransChoiceWithoutCaching()
|
||||
{
|
||||
$translator = $this->getTranslator($this->getLoader());
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocales(['en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin']);
|
||||
|
||||
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
|
||||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
||||
}
|
||||
|
||||
public function testTransWithCaching()
|
||||
{
|
||||
// prime the cache
|
||||
@ -109,31 +96,6 @@ class TranslatorTest extends TestCase
|
||||
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testTransChoiceWithCaching()
|
||||
{
|
||||
// prime the cache
|
||||
$translator = $this->getTranslator($this->getLoader(), ['cache_dir' => $this->tmpDir]);
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocales(['en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin']);
|
||||
|
||||
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
|
||||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
||||
|
||||
// do it another time as the cache is primed now
|
||||
$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
|
||||
$loader->expects($this->never())->method('load');
|
||||
|
||||
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir]);
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocales(['en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin']);
|
||||
|
||||
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
|
||||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid "invalid locale" locale.
|
||||
|
Reference in New Issue
Block a user