diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 9b48c658f1..cbd1861f63 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index f3d6a875e0..2acfc3cf3e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -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; @@ -31,17 +30,10 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer private $debug; /** - * @param string $phpArrayFile The PHP file where annotations are cached - * @param string $excludeRegexp - * @param bool $debug + * @param string $phpArrayFile The PHP file where annotations are cached */ - 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; diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php index 535161b119..1811084963 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php @@ -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; } diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php index 6591c94009..d9cb48e70d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php @@ -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 + * @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; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Client.php b/src/Symfony/Bundle/FrameworkBundle/Client.php deleted file mode 100644 index f33808db04..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Client.php +++ /dev/null @@ -1,206 +0,0 @@ - - * - * 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 = <<boot(); -$profilerCode - -\$request = unserialize($request); -EOF; - - return $code.$this->getHandleScript(); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index 5d20431c95..5feacb6492 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -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; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php deleted file mode 100644 index 7b01a55ef8..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php +++ /dev/null @@ -1,60 +0,0 @@ - - * - * 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 - * - * @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; - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php index 11c189d4b1..bfd88b09d5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php @@ -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(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php index 7fdd1dbca2..aa26607cf4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php @@ -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; diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index 8b05a83fe1..a570669aa1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -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)); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php deleted file mode 100644 index b6708db544..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 - */ -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); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php deleted file mode 100644 index eec990e088..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php +++ /dev/null @@ -1,413 +0,0 @@ - - * - * 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 - * - * @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)); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/DataCollector/RequestDataCollector.php b/src/Symfony/Bundle/FrameworkBundle/DataCollector/RequestDataCollector.php deleted file mode 100644 index 3e82f27e2c..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/DataCollector/RequestDataCollector.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * 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 - * - * @deprecated since Symfony 4.1 - */ -class RequestDataCollector extends BaseRequestDataCollector -{ -} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.php index 7c6c85f64b..47195b4830 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.php @@ -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)]); - } - } } } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php deleted file mode 100644 index 7be7464f71..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * 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 - * - * @deprecated since Symfony 4.2, use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass instead. - */ -class CacheCollectorPass extends BaseCacheCollectorPass -{ -} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolClearerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolClearerPass.php deleted file mode 100644 index c43cb769db..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolClearerPass.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * 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 - * - * @deprecated since version 4.2, use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass instead. - */ -class CachePoolClearerPass extends BaseCachePoolClearerPass -{ -} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php deleted file mode 100644 index bc7ab8bcc1..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * 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 - * - * @deprecated since version 4.2, use Symfony\Component\Cache\DependencyInjection\CachePoolPass instead. - */ -class CachePoolPass extends BaseCachePoolPass -{ -} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPrunerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPrunerPass.php deleted file mode 100644 index d20c93a024..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPrunerPass.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * 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 - * - * @deprecated since Symfony 4.2, use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass instead. - */ -class CachePoolPrunerPass extends BaseCachePoolPrunerPass -{ -} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index dd8cd93fee..fe5106dd5c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -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() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 995b1ca9f4..ffd8d66476 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -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 { diff --git a/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php b/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php index b05b60def1..1a386d45c0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php +++ b/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php @@ -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 */ -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 = <<boot(); +$profilerCode + +\$request = unserialize($request); +EOF; + + return $code.$this->getHandleScript(); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml index a5060e2cd6..b24b25c8c0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml @@ -11,11 +11,6 @@ - - The "Psr\SimpleCache\CacheInterface" / "%service_id%" service is deprecated since Symfony 4.3. Use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead. - - - @@ -155,7 +150,6 @@ - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml index 4d2d488edb..e39a1c4d95 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml @@ -11,8 +11,5 @@ - - The "%service_id%" service is deprecated since Symfony 4.2, use "identity_translator" instead. - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index 020e29e721..b94d4464c2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -64,10 +64,6 @@ - - The "%service_id%" service is deprecated since Symfony 4.1. Use the "session_listener" service instead. - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml index 582f3951ac..871feb9dcf 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml @@ -17,7 +17,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php index c9f93b96ea..fe4d124b09 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php @@ -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'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index cd86432e78..fca2b25fa7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -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 -{ - use TestControllerTrait; - - private $throwOnUnexpectedService; - - public function __construct($throwOnUnexpectedService = true) + public function testForward() { - $this->throwOnUnexpectedService = $throwOnUnexpectedService; + $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 setContainer(ContainerInterface $container) + public function testGetUser() { - if (!$this->throwOnUnexpectedService) { - return parent::setContainer($container); + $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')); + } - $expected = self::getSubscribedServices(); + public function testFileAsInline() + { + $controller = $this->createController(); - 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)); - } + /* @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')); } - - return parent::setContainer($container); + $this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition')); + $this->assertContains(basename(__FILE__), $response->headers->get('content-disposition')); } - public function getParameter(string $name) + public function testFileWithOwnFileName() { - return parent::getParameter($name); + $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 fooAction() + 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); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php deleted file mode 100644 index feff96bc44..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * 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(); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php deleted file mode 100644 index d4f0c24440..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php +++ /dev/null @@ -1,534 +0,0 @@ - - * - * 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; - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TestAbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TestAbstractController.php new file mode 100644 index 0000000000..3a9666464b --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TestAbstractController.php @@ -0,0 +1,179 @@ + + * + * 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); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TestController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TestController.php deleted file mode 100644 index 595dfd8d32..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TestController.php +++ /dev/null @@ -1,10 +0,0 @@ -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); - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CacheCollectorPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CacheCollectorPassTest.php deleted file mode 100644 index d7613d5ae5..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CacheCollectorPassTest.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * 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'); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolClearerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolClearerPassTest.php deleted file mode 100644 index 494cf6f094..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolClearerPassTest.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * 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()); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php deleted file mode 100644 index 747e29ffef..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php +++ /dev/null @@ -1,133 +0,0 @@ - - * - * 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); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPrunerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPrunerPassTest.php deleted file mode 100644 index 58362df6ed..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPrunerPassTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * 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); - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_and_validation_mode.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_and_validation_mode.php deleted file mode 100644 index 414b323efe..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_and_validation_mode.php +++ /dev/null @@ -1,8 +0,0 @@ -loadFromExtension('framework', [ - 'validation' => [ - 'strict_email' => true, - 'email_validation_mode' => 'strict', - ], -]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_disabled.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_disabled.php deleted file mode 100644 index b5a8b7bba6..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_disabled.php +++ /dev/null @@ -1,7 +0,0 @@ -loadFromExtension('framework', [ - 'validation' => [ - 'strict_email' => false, - ], -]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_enabled.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_enabled.php deleted file mode 100644 index caa47d7470..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/validation_strict_email_enabled.php +++ /dev/null @@ -1,7 +0,0 @@ -loadFromExtension('framework', [ - 'validation' => [ - 'strict_email' => true, - ], -]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_and_validation_mode.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_and_validation_mode.xml deleted file mode 100644 index a88a37f26b..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_and_validation_mode.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_disabled.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_disabled.xml deleted file mode 100644 index fa41396161..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_disabled.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_enabled.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_enabled.xml deleted file mode 100644 index ec5c6b94e6..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/validation_strict_email_enabled.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_and_validation_mode.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_and_validation_mode.yml deleted file mode 100644 index 64e658ba69..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_and_validation_mode.yml +++ /dev/null @@ -1,4 +0,0 @@ -framework: - validation: - strict_email: true - email_validation_mode: html5 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_disabled.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_disabled.yml deleted file mode 100644 index b5be5f598b..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_disabled.yml +++ /dev/null @@ -1,3 +0,0 @@ -framework: - validation: - strict_email: false diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_enabled.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_enabled.yml deleted file mode 100644 index 1c805f9b92..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/validation_strict_email_enabled.yml +++ /dev/null @@ -1,3 +0,0 @@ -framework: - validation: - strict_email: true diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 067a9fc977..65d1b8c7fa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -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->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'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index 43d858e452..330b3cc211 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -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.