Skip to content

Commit

Permalink
Add support for SimplePager and fix psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 9, 2021
1 parent 6aa5134 commit 003952c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
31 changes: 30 additions & 1 deletion src/Builder/DatagridBuilder.php
Expand Up @@ -18,17 +18,22 @@
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
use Sonata\AdminBundle\Datagrid\Datagrid;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Datagrid\SimplePager;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\FieldDescription\TypeGuesserInterface;
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
use Sonata\AdminBundle\Guesser\TypeGuesserInterface as DeprecatedTypeGuesserInterface;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;

/**
* @final since sonata-project/doctrine-mongodb-admin-bundle 3.5.
*
* @phpstan-implements DatagridBuilderInterface<ProxyQueryInterface>
*/
class DatagridBuilder implements DatagridBuilderInterface
{
Expand Down Expand Up @@ -169,7 +174,7 @@ public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionIn

public function getBaseDatagrid(AdminInterface $admin, array $values = [])
{
$pager = new Pager();
$pager = $this->getPager($admin->getPagerType());

$defaultOptions = [];
if ($this->csrfTokenEnabled) {
Expand All @@ -180,4 +185,28 @@ public function getBaseDatagrid(AdminInterface $admin, array $values = [])

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

/**
* Get pager by pagerType.
*
* @throws \RuntimeException If invalid pager type is set
*
* @return PagerInterface<ProxyQueryInterface>
*/
private function getPager(string $pagerType): PagerInterface
{
switch ($pagerType) {
case Pager::TYPE_DEFAULT:
return new Pager();

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

return $simplePager;

default:
throw new \RuntimeException(sprintf('Unknown pager type "%s".', $pagerType));
}
}
}
38 changes: 31 additions & 7 deletions tests/Builder/DatagridBuilderTest.php
Expand Up @@ -15,11 +15,13 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\Datagrid;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\Pager;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Datagrid\SimplePager;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionCollection;
use Sonata\AdminBundle\FieldDescription\TypeGuesserInterface;
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
Expand Down Expand Up @@ -75,28 +77,50 @@ protected function setUp(): void
$this->typeGuesser
);

$this->admin = $this->createMock(AdminInterface::class);
$this->admin = $this->createMock(AbstractAdmin::class); // NEXT_MAJOR: Use AdminInterface
$this->admin
->method('getModelManager')
->willReturn($this->modelManager);
}

public function testGetBaseDatagrid(): void
/**
* @phpstan-param class-string $pager
*
* @dataProvider getBaseDatagridData
*/
public function testGetBaseDatagrid(string $pagerType, string $pager): void
{
$proxyQuery = $this->createStub(ProxyQueryInterface::class);
$fieldDescription = new FieldDescriptionCollection();
$formBuilder = $this->createStub(FormBuilderInterface::class);

$this->admin->method('getPagerType')->willReturn($pagerType);
$this->admin->method('createQuery')->willReturn($proxyQuery);
$this->admin->method('getList')->willReturn($fieldDescription);

$this->formFactory->method('createNamedBuilder')->willReturn($formBuilder);

$this->assertInstanceOf(
Datagrid::class,
$datagrid = $this->datagridBuilder->getBaseDatagrid($this->admin)
);
$this->assertInstanceOf(Pager::class, $datagrid->getPager());
$datagrid = $this->datagridBuilder->getBaseDatagrid($this->admin);

$this->assertInstanceOf(Datagrid::class, $datagrid);
$this->assertInstanceOf($pager, $datagrid->getPager());
}

/**
* @phpstan-return iterable<array-key, array{string, class-string}>
*/
public function getBaseDatagridData(): iterable
{
return [
'simple' => [
Pager::TYPE_SIMPLE,
SimplePager::class,
],
'default' => [
Pager::TYPE_DEFAULT,
Pager::class,
],
];
}

public function testFixFieldDescription(): void
Expand Down

0 comments on commit 003952c

Please sign in to comment.