Skip to content

Commit

Permalink
Fix AppKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 29, 2022
1 parent 97df012 commit 9e293e3
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 37 deletions.
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
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
3 changes: 3 additions & 0 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
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
5 changes: 4 additions & 1 deletion src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public function createQuery(string $class, string $alias = 'o'): BaseProxyQueryI

\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
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()]);

0 comments on commit 9e293e3

Please sign in to comment.