Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevKit updates for 4.x branch #779

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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());
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ It's auto-generated by sonata-project/dev-kit package.
<ini name="precision" value="8" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
<server name="PANTHER_WEB_SERVER_DIR" value="./tests/App/public/" />
<env name="KERNEL_CLASS" value="\Sonata\DoctrineMongoDBAdminBundle\Tests\App\AppKernel" />
<server name="APP_ENV" value="test" force="true" />
<server name="APP_DEBUG" value="false" />
</php>
</phpunit>
7 changes: 4 additions & 3 deletions src/Builder/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use Symfony\Component\Form\FormFactoryInterface;

/**
* @phpstan-implements DatagridBuilderInterface<ProxyQueryInterface>
* @phpstan-implements DatagridBuilderInterface<ProxyQueryInterface<object>>
* @psalm-suppress DeprecatedInterface
*
* @see https://github.com/sonata-project/SonataAdminBundle/pull/7519
Expand Down Expand Up @@ -132,14 +132,15 @@ 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<object> $query */

return new Datagrid($query, $admin->getList(), $pager, $formBuilder, $values);
}

/**
* Get pager by pagerType.
*
* @return PagerInterface<ProxyQueryInterface>
* @return PagerInterface<ProxyQueryInterface<object>>
*
* @throws \RuntimeException If invalid pager type is set
*/
Expand All @@ -150,7 +151,7 @@ private function getPager(string $pagerType): PagerInterface
return new Pager();

case AdminPager::TYPE_SIMPLE:
/** @var SimplePager<ProxyQueryInterface> $simplePager */
/** @var SimplePager<ProxyQueryInterface<object>> $simplePager */
$simplePager = new SimplePager();

return $simplePager;
Expand Down
5 changes: 4 additions & 1 deletion src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author Jonathan H. Wage <[email protected]>
* @author Kévin Dunglas <[email protected]>
*
* @phpstan-extends BasePager<ProxyQueryInterface>
* @phpstan-extends BasePager<ProxyQueryInterface<object>>
*/
final class Pager extends BasePager
{
Expand Down Expand Up @@ -70,6 +70,9 @@ public function init(): void
}
}

/**
* @param ProxyQueryInterface<object> $query
*/
private function computeResultsCount(ProxyQueryInterface $query): int
{
$countQuery = clone $query;
Expand Down
6 changes: 3 additions & 3 deletions src/Datagrid/ProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

/**
* This class try to unify the query usage with Doctrine.
*
* @phpstan-template-covariant T of object
* @phpstan-implements ProxyQueryInterface<T>
*/
final class ProxyQuery implements ProxyQueryInterface
{
Expand Down Expand Up @@ -57,9 +60,6 @@ public function __clone()
$this->queryBuilder = clone $this->queryBuilder;
}

/**
* @return Iterator<object>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about this alternative @VincentLanglet ? the typehint for execute is already on the parent and it is already iterable so I don't think we need to add this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a solution until doctrine/mongodb-odm#2457 indeed.

*/
public function execute()
{
// always clone the original queryBuilder.
Expand Down
4 changes: 4 additions & 0 deletions src/Datagrid/ProxyQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
*/
interface ProxyQueryInterface extends BaseProxyQueryInterface
{
public function getQueryBuilder(): Builder;
Expand Down
6 changes: 6 additions & 0 deletions src/Filter/AbstractDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,18 @@ final protected function getOperator(int $type): string
return $choices[$type];
}

/**
* @param ProxyQueryInterface<object> $queryBuilder
*/
private function applyType(ProxyQueryInterface $queryBuilder, string $operation, string $field, \DateTimeInterface $value): void
{
$queryBuilder->getQueryBuilder()->field($field)->$operation($value);
$this->setActive(true);
}

/**
* @param ProxyQueryInterface<object> $query
*/
private function filterRange(ProxyQueryInterface $query, string $field, FilterData $data): void
{
$value = $data->getValue();
Expand Down
3 changes: 3 additions & 0 deletions src/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ final public function apply(BaseProxyQueryInterface $query, FilterData $filterDa
$this->filter($query, $field, $filterData);
}

/**
* @param ProxyQueryInterface<object> $query
*/
abstract protected function filter(ProxyQueryInterface $query, string $field, FilterData $data): void;
}
6 changes: 6 additions & 0 deletions src/Filter/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ protected function filter(ProxyQueryInterface $query, string $field, FilterData
}
}

/**
* @param ProxyQueryInterface<object> $query
*/
protected function handleMultiple(ProxyQueryInterface $query, string $field, FilterData $data): void
{
if (0 === \count($data->getValue())) {
Expand All @@ -90,6 +93,9 @@ protected function handleMultiple(ProxyQueryInterface $query, string $field, Fil
$this->setActive(true);
}

/**
* @param ProxyQueryInterface<object> $query
*/
protected function handleScalar(ProxyQueryInterface $query, string $field, FilterData $data): void
{
$id = self::fixIdentifier($data->getValue()->getId());
Expand Down
6 changes: 4 additions & 2 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> $proxyQuery */
$proxyQuery = new ProxyQuery($repository->createQueryBuilder());

return $proxyQuery;
}

public function supportsQuery(object $query): bool
Expand Down
18 changes: 8 additions & 10 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 [
Expand Down Expand Up @@ -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)) {
Expand All @@ -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');
}

Expand Down
2 changes: 2 additions & 0 deletions tests/App/config/config_sonata_block_v4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sonata_block:
http_cache: false
1 change: 1 addition & 0 deletions tests/App/config/config_v4.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/App/config/config_v5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/App/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 13 additions & 10 deletions tests/Datagrid/ProxyQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{name: string}> $result */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you typehint the ProxyQuery as ProxyQuery<array{name: string}> you won't have to typehint the result.

$result = $proxyQuery->execute();

static::assertSame(['B', 'A'], $this->getNames($result));
}
Expand All @@ -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<array{name: string}> $result */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

$result = $proxyQuery->execute();

static::assertSame(['B', 'A'], $this->getNames($result));
}

/**
* @param array<array{name: string}> $results
* @param iterable<array{name: string}> $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
Expand Down
3 changes: 3 additions & 0 deletions tests/Filter/CallbackFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function testFilterMethodNotEmpty(): void
static::assertTrue($filter->isActive());
}

/**
* @param ProxyQueryInterface<object> $proxyQuery
*/
public function customCallback(ProxyQueryInterface $proxyQuery, string $field, FilterData $data): bool
{
return $data->hasValue();
Expand Down
13 changes: 8 additions & 5 deletions tests/Filter/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ public function getDefaultOptions(): array
return ['option1' => 2];
}

public function getRenderSettings(): array
/**
* @return array<string, mixed>
*/
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
Expand Down
6 changes: 0 additions & 6 deletions tests/Functional/BaseFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,9 +28,4 @@ protected function setUp(): void
$this->client = static::createClient();
$this->client->followRedirects();
}

protected static function getKernelClass(): string
{
return AppKernel::class;
}
}
6 changes: 0 additions & 6 deletions tests/Functional/BasePantherTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,9 +31,4 @@ protected function setUp(): void
'request_timeout_in_ms' => 60000,
]);
}

protected static function getKernelClass(): string
{
return AppKernel::class;
}
}
8 changes: 5 additions & 3 deletions tests/custom_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);