From 97df012b8868e2d5ca9a761f57dd9d3e5eb5a9cc Mon Sep 17 00:00:00 2001 From: SonataCI Date: Mon, 29 Aug 2022 07:05:39 +0000 Subject: [PATCH 1/2] DevKit updates --- bin/console | 2 +- phpunit.xml.dist | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/console b/bin/console index ced2b14b..8144c33d 100755 --- a/bin/console +++ b/bin/console @@ -30,6 +30,6 @@ set_time_limit(0); require dirname(__DIR__) . '/vendor/autoload.php'; -$kernel = new AppKernel(); +$kernel = new AppKernel('test', false); $application = new Application($kernel); $application->run(new ArgvInput()); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 655e06a1..ed00999d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -38,5 +38,8 @@ It's auto-generated by sonata-project/dev-kit package. + + + From a43cb2b8a93b346a21a437c46b28a8bae03bd8bf Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Mon, 29 Aug 2022 09:18:15 +0200 Subject: [PATCH 2/2] Fix AppKernel --- composer.json | 2 +- src/Builder/DatagridBuilder.php | 7 +++--- src/Datagrid/Pager.php | 5 ++++- src/Datagrid/ProxyQuery.php | 6 ++--- src/Datagrid/ProxyQueryInterface.php | 4 ++++ src/Filter/AbstractDateFilter.php | 6 +++++ src/Filter/Filter.php | 3 +++ src/Filter/ModelFilter.php | 6 +++++ src/Model/ModelManager.php | 6 +++-- tests/App/AppKernel.php | 18 +++++++-------- tests/App/config/config_sonata_block_v4.yaml | 2 ++ tests/App/config/config_v4.yml | 1 + tests/App/config/config_v5.yml | 1 + tests/App/public/index.php | 2 +- tests/Datagrid/ProxyQueryTest.php | 23 +++++++++++--------- tests/Filter/CallbackFilterTest.php | 3 +++ tests/Filter/FilterTest.php | 13 ++++++----- tests/Functional/BaseFunctionalTestCase.php | 6 ----- tests/Functional/BasePantherTestCase.php | 6 ----- tests/custom_bootstrap.php | 8 ++++--- 20 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 tests/App/config/config_sonata_block_v4.yaml diff --git a/composer.json b/composer.json index 2b5a6dcf..58254ce9 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "doctrine/mongodb-odm": "^2.1", "doctrine/mongodb-odm-bundle": "^4.3", "doctrine/persistence": "^2.0 || ^3.0", - "sonata-project/admin-bundle": "^4.16", + "sonata-project/admin-bundle": "^4.18", "sonata-project/exporter": "^2.0 || ^3.0", "sonata-project/form-extensions": "^1.7.1", "symfony/config": "^4.4 || ^5.4 || ^6.0", diff --git a/src/Builder/DatagridBuilder.php b/src/Builder/DatagridBuilder.php index f73d0aac..0cc65c62 100644 --- a/src/Builder/DatagridBuilder.php +++ b/src/Builder/DatagridBuilder.php @@ -29,7 +29,7 @@ use Symfony\Component\Form\FormFactoryInterface; /** - * @phpstan-implements DatagridBuilderInterface + * @phpstan-implements DatagridBuilderInterface> * @psalm-suppress DeprecatedInterface * * @see https://github.com/sonata-project/SonataAdminBundle/pull/7519 @@ -132,6 +132,7 @@ public function getBaseDatagrid(AdminInterface $admin, array $values = []): Data if (!$query instanceof ProxyQueryInterface) { throw new \TypeError(sprintf('The admin query MUST implement %s.', ProxyQueryInterface::class)); } + /** @phpstan-var ProxyQueryInterface $query */ return new Datagrid($query, $admin->getList(), $pager, $formBuilder, $values); } @@ -139,7 +140,7 @@ public function getBaseDatagrid(AdminInterface $admin, array $values = []): Data /** * Get pager by pagerType. * - * @return PagerInterface + * @return PagerInterface> * * @throws \RuntimeException If invalid pager type is set */ @@ -150,7 +151,7 @@ private function getPager(string $pagerType): PagerInterface return new Pager(); case AdminPager::TYPE_SIMPLE: - /** @var SimplePager $simplePager */ + /** @var SimplePager> $simplePager */ $simplePager = new SimplePager(); return $simplePager; diff --git a/src/Datagrid/Pager.php b/src/Datagrid/Pager.php index 7f1d1c75..92fbbacf 100644 --- a/src/Datagrid/Pager.php +++ b/src/Datagrid/Pager.php @@ -21,7 +21,7 @@ * @author Jonathan H. Wage * @author Kévin Dunglas * - * @phpstan-extends BasePager + * @phpstan-extends BasePager> */ final class Pager extends BasePager { @@ -70,6 +70,9 @@ public function init(): void } } + /** + * @param ProxyQueryInterface $query + */ private function computeResultsCount(ProxyQueryInterface $query): int { $countQuery = clone $query; diff --git a/src/Datagrid/ProxyQuery.php b/src/Datagrid/ProxyQuery.php index 59b296da..3e6af0e3 100644 --- a/src/Datagrid/ProxyQuery.php +++ b/src/Datagrid/ProxyQuery.php @@ -19,6 +19,9 @@ /** * This class try to unify the query usage with Doctrine. + * + * @phpstan-template-covariant T of object + * @phpstan-implements ProxyQueryInterface */ final class ProxyQuery implements ProxyQueryInterface { @@ -57,9 +60,6 @@ public function __clone() $this->queryBuilder = clone $this->queryBuilder; } - /** - * @return Iterator - */ public function execute() { // always clone the original queryBuilder. diff --git a/src/Datagrid/ProxyQueryInterface.php b/src/Datagrid/ProxyQueryInterface.php index 2a9e910a..9ca5337d 100644 --- a/src/Datagrid/ProxyQueryInterface.php +++ b/src/Datagrid/ProxyQueryInterface.php @@ -16,6 +16,10 @@ use Doctrine\ODM\MongoDB\Query\Builder; use Sonata\AdminBundle\Datagrid\ProxyQueryInterface as BaseProxyQueryInterface; +/** + * @phpstan-template-covariant T of object + * @phpstan-extends BaseProxyQueryInterface + */ interface ProxyQueryInterface extends BaseProxyQueryInterface { public function getQueryBuilder(): Builder; diff --git a/src/Filter/AbstractDateFilter.php b/src/Filter/AbstractDateFilter.php index 7e39d130..72cb4876 100644 --- a/src/Filter/AbstractDateFilter.php +++ b/src/Filter/AbstractDateFilter.php @@ -123,12 +123,18 @@ final protected function getOperator(int $type): string return $choices[$type]; } + /** + * @param ProxyQueryInterface $queryBuilder + */ private function applyType(ProxyQueryInterface $queryBuilder, string $operation, string $field, \DateTimeInterface $value): void { $queryBuilder->getQueryBuilder()->field($field)->$operation($value); $this->setActive(true); } + /** + * @param ProxyQueryInterface $query + */ private function filterRange(ProxyQueryInterface $query, string $field, FilterData $data): void { $value = $data->getValue(); diff --git a/src/Filter/Filter.php b/src/Filter/Filter.php index 4704a267..300444d8 100644 --- a/src/Filter/Filter.php +++ b/src/Filter/Filter.php @@ -31,5 +31,8 @@ final public function apply(BaseProxyQueryInterface $query, FilterData $filterDa $this->filter($query, $field, $filterData); } + /** + * @param ProxyQueryInterface $query + */ abstract protected function filter(ProxyQueryInterface $query, string $field, FilterData $data): void; } diff --git a/src/Filter/ModelFilter.php b/src/Filter/ModelFilter.php index d42f3070..323f6062 100644 --- a/src/Filter/ModelFilter.php +++ b/src/Filter/ModelFilter.php @@ -70,6 +70,9 @@ protected function filter(ProxyQueryInterface $query, string $field, FilterData } } + /** + * @param ProxyQueryInterface $query + */ protected function handleMultiple(ProxyQueryInterface $query, string $field, FilterData $data): void { if (0 === \count($data->getValue())) { @@ -90,6 +93,9 @@ protected function handleMultiple(ProxyQueryInterface $query, string $field, Fil $this->setActive(true); } + /** + * @param ProxyQueryInterface $query + */ protected function handleScalar(ProxyQueryInterface $query, string $field, FilterData $data): void { $id = self::fixIdentifier($data->getValue()->getId()); diff --git a/src/Model/ModelManager.php b/src/Model/ModelManager.php index 543207d2..e82436d6 100755 --- a/src/Model/ModelManager.php +++ b/src/Model/ModelManager.php @@ -121,10 +121,12 @@ public function getDocumentManager($class): DocumentManager public function createQuery(string $class, string $alias = 'o'): BaseProxyQueryInterface { $repository = $this->getDocumentManager($class)->getRepository($class); - \assert($repository instanceof DocumentRepository); - return new ProxyQuery($repository->createQueryBuilder()); + /** @phpstan-var ProxyQuery $proxyQuery */ + $proxyQuery = new ProxyQuery($repository->createQueryBuilder()); + + return $proxyQuery; } public function supportsQuery(object $query): bool diff --git a/tests/App/AppKernel.php b/tests/App/AppKernel.php index 9797f774..fb00fa53 100644 --- a/tests/App/AppKernel.php +++ b/tests/App/AppKernel.php @@ -16,6 +16,7 @@ use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle; use Knp\Bundle\MenuBundle\KnpMenuBundle; use Sonata\AdminBundle\SonataAdminBundle; +use Sonata\BlockBundle\Cache\HttpCacheHandler; use Sonata\BlockBundle\SonataBlockBundle; use Sonata\Doctrine\Bridge\Symfony\SonataDoctrineBundle; use Sonata\DoctrineMongoDBAdminBundle\SonataDoctrineMongoDBAdminBundle; @@ -31,20 +32,10 @@ use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; -/** - * @psalm-suppress PropertyNotSetInConstructor - * - * @see https://github.com/psalm/psalm-plugin-symfony/pull/220 - */ final class AppKernel extends Kernel { use MicroKernelTrait; - public function __construct() - { - parent::__construct('test', true); - } - public function registerBundles(): iterable { return [ @@ -87,6 +78,9 @@ protected function configureRoutes($routes): void $routes->import(sprintf('%s/config/routes.yaml', $this->getProjectDir())); } + /** + * @psalm-suppress DeprecatedClass + */ protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { if (interface_exists(AuthenticatorFactoryInterface::class)) { @@ -97,6 +91,10 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $loader->load(__DIR__.'/config/security_v4.yml'); } + if (class_exists(HttpCacheHandler::class)) { + $loader->load(__DIR__.'/config/config_sonata_block_v4.yaml'); + } + $loader->load(__DIR__.'/config/services.php'); } diff --git a/tests/App/config/config_sonata_block_v4.yaml b/tests/App/config/config_sonata_block_v4.yaml new file mode 100644 index 00000000..b83465cc --- /dev/null +++ b/tests/App/config/config_sonata_block_v4.yaml @@ -0,0 +1,2 @@ +sonata_block: + http_cache: false diff --git a/tests/App/config/config_v4.yml b/tests/App/config/config_v4.yml index 63ee0a0a..0e27768a 100644 --- a/tests/App/config/config_v4.yml +++ b/tests/App/config/config_v4.yml @@ -8,6 +8,7 @@ framework: handler_id: session.handler.native_file storage_id: session.storage.mock_file name: MOCKSESSID + http_method_override: false test: true translator: enabled: true diff --git a/tests/App/config/config_v5.yml b/tests/App/config/config_v5.yml index 251b9661..4bdab0c4 100644 --- a/tests/App/config/config_v5.yml +++ b/tests/App/config/config_v5.yml @@ -7,6 +7,7 @@ framework: session: storage_factory_id: session.storage.factory.mock_file name: MOCKSESSID + http_method_override: false test: true translator: enabled: true diff --git a/tests/App/public/index.php b/tests/App/public/index.php index 656a94b8..0ba1219c 100644 --- a/tests/App/public/index.php +++ b/tests/App/public/index.php @@ -16,7 +16,7 @@ require __DIR__.'/../../../vendor/autoload.php'; -$kernel = new AppKernel(); +$kernel = new AppKernel('test', false); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); diff --git a/tests/Datagrid/ProxyQueryTest.php b/tests/Datagrid/ProxyQueryTest.php index 59c28827..1cff8db8 100644 --- a/tests/Datagrid/ProxyQueryTest.php +++ b/tests/Datagrid/ProxyQueryTest.php @@ -119,8 +119,8 @@ public function testExecuteAllowsSorting(): void $proxyQuery->setSortBy([], ['fieldName' => 'name']); $proxyQuery->setSortOrder('DESC'); - /** @var array{array{name: string}} $result */ - $result = $proxyQuery->execute()->toArray(); + /** @var iterable $result */ + $result = $proxyQuery->execute(); static::assertSame(['B', 'A'], $this->getNames($result)); } @@ -143,23 +143,26 @@ public function testExecuteAllowsSortingWithEmbedded(): void $proxyQuery->setSortBy([['fieldName' => 'embeddedDocument']], ['fieldName' => 'position']); $proxyQuery->setSortOrder('DESC'); - /** @var array{array{name: string}} $result */ - $result = $proxyQuery->execute()->toArray(); + /** @var iterable $result */ + $result = $proxyQuery->execute(); static::assertSame(['B', 'A'], $this->getNames($result)); } /** - * @param array $results + * @param iterable $results * * @return string[] */ - private function getNames(array $results): array + private function getNames(iterable $results): array { - return array_values(array_map( - static fn (array $result): string => $result['name'], - $results - )); + $names = []; + + foreach ($results as $result) { + $names[] = $result['name']; + } + + return $names; } private function createConfiguration(): Configuration diff --git a/tests/Filter/CallbackFilterTest.php b/tests/Filter/CallbackFilterTest.php index b9a8ca7a..f61e53f7 100644 --- a/tests/Filter/CallbackFilterTest.php +++ b/tests/Filter/CallbackFilterTest.php @@ -80,6 +80,9 @@ public function testFilterMethodNotEmpty(): void static::assertTrue($filter->isActive()); } + /** + * @param ProxyQueryInterface $proxyQuery + */ public function customCallback(ProxyQueryInterface $proxyQuery, string $field, FilterData $data): bool { return $data->hasValue(); diff --git a/tests/Filter/FilterTest.php b/tests/Filter/FilterTest.php index f2f5dee8..44e68e6e 100644 --- a/tests/Filter/FilterTest.php +++ b/tests/Filter/FilterTest.php @@ -29,12 +29,15 @@ public function getDefaultOptions(): array return ['option1' => 2]; } - public function getRenderSettings(): array + /** + * @return array + */ + public function getFormOptions(): array { - return ['sonata_type_filter_default', [ - 'type' => $this->getFieldType(), - 'options' => $this->getFieldOptions(), - ]]; + return [ + 'field_type' => $this->getFieldType(), + 'field_options' => $this->getFieldOptions(), + ]; } protected function filter(ProxyQueryInterface $query, string $field, FilterData $data): void diff --git a/tests/Functional/BaseFunctionalTestCase.php b/tests/Functional/BaseFunctionalTestCase.php index b979a727..8fd1fc88 100644 --- a/tests/Functional/BaseFunctionalTestCase.php +++ b/tests/Functional/BaseFunctionalTestCase.php @@ -13,7 +13,6 @@ namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Functional; -use Sonata\DoctrineMongoDBAdminBundle\Tests\App\AppKernel; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -29,9 +28,4 @@ protected function setUp(): void $this->client = static::createClient(); $this->client->followRedirects(); } - - protected static function getKernelClass(): string - { - return AppKernel::class; - } } diff --git a/tests/Functional/BasePantherTestCase.php b/tests/Functional/BasePantherTestCase.php index de1dd238..d60a2f44 100644 --- a/tests/Functional/BasePantherTestCase.php +++ b/tests/Functional/BasePantherTestCase.php @@ -13,7 +13,6 @@ namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Functional; -use Sonata\DoctrineMongoDBAdminBundle\Tests\App\AppKernel; use Symfony\Component\Panther\Client; use Symfony\Component\Panther\PantherTestCase; @@ -32,9 +31,4 @@ protected function setUp(): void 'request_timeout_in_ms' => 60000, ]); } - - protected static function getKernelClass(): string - { - return AppKernel::class; - } } diff --git a/tests/custom_bootstrap.php b/tests/custom_bootstrap.php index 697f686d..ff953bc4 100644 --- a/tests/custom_bootstrap.php +++ b/tests/custom_bootstrap.php @@ -15,21 +15,23 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\Filesystem\Filesystem; -$application = new Application(new AppKernel()); +$kernel = new AppKernel($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG']); +$application = new Application($kernel); $application->setAutoExit(false); -// Load fixtures of the AppTestBundle $input = new ArrayInput([ 'command' => 'doctrine:mongodb:fixtures:load', '--no-interaction' => false, ]); $application->run($input, new NullOutput()); -// Install Assets $input = new ArrayInput([ 'command' => 'assets:install', 'target' => __DIR__.'/App/public', '--symlink' => true, ]); $application->run($input, new NullOutput()); + +(new Filesystem())->remove([$kernel->getCacheDir()]);