Skip to content

Commit

Permalink
Merge 6c5dbb3 into 5b5dc4b
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 committed Feb 13, 2018
2 parents 5b5dc4b + 6c5dbb3 commit e0c1c12
Show file tree
Hide file tree
Showing 17 changed files with 1,331 additions and 71 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
"sonata-project/admin-bundle": "^3.29",
"sonata-project/core-bundle": "^3.8",
"sonata-project/exporter": "^1.3.1",
"symfony/config": "^2.8 || ^3.2 || ^4.0",
"symfony/console": "^2.8 || ^3.2 || ^4.0",
"symfony/dependency-injection": "^2.8 || ^3.2 || ^4.0",
"symfony/doctrine-bridge": "^2.8 || ^3.2 || ^4.0",
"symfony/form": "^2.8 || ^3.2 || ^4.0",
"symfony/framework-bundle": "^2.8 || ^3.2 || ^4.0",
"symfony/security": "^2.8 || ^3.2 || ^4.0",
"symfony/http-foundation": "^2.8 || ^3.2 || ^4.0",
"symfony/http-kernel": "^2.8 || ^3.2 || ^4.0",
"symfony/options-resolver": "^2.8 || ^3.2 || ^4.0",
"symfony/security-acl": "^2.8 || ^3.0"
},
"conflict": {
Expand All @@ -47,7 +51,7 @@
"knplabs/knp-menu-bundle": "^2.1.1",
"simplethings/entity-audit-bundle": "^0.9 || ^1.0",
"sonata-project/block-bundle": "^3.11",
"symfony/phpunit-bridge": "^3.3 || ^4.0"
"symfony/phpunit-bridge": "^4.0"
},
"suggest": {
"simplethings/entity-audit-bundle": "If you want to support for versioning of entities and their associations."
Expand Down
86 changes: 86 additions & 0 deletions tests/Block/AuditBlockServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\Block;

use Prophecy\Argument;
use SimpleThings\EntityAudit\AuditReader as SimpleThingsAuditReader;
use SimpleThings\EntityAudit\Revision;
use Sonata\BlockBundle\Block\BlockContext;
use Sonata\BlockBundle\Model\Block;
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase;
use Sonata\DoctrineORMAdminBundle\Block\AuditBlockService;

/**
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
class AuditBlockServiceTest extends AbstractBlockServiceTestCase
{
private $simpleThingsAuditReader;
private $blockService;

protected function setUp(): void
{
parent::setUp();
$this->simpleThingsAuditReader = $this->prophesize(SimpleThingsAuditReader::class);

$this->blockService = new AuditBlockService(
'block.service',
$this->templating,
$this->simpleThingsAuditReader->reveal()
);
}

/**
* @group legacy
*/
public function testExecute(): void
{
$blockContext = $this->prophesize(BlockContext::class);

$blockContext->getBlock()->willReturn($block = new Block())->shouldBeCalledTimes(1);
$blockContext->getSetting('limit')->willReturn($limit = 10)->shouldBeCalledTimes(1);

$this->simpleThingsAuditReader->findRevisionHistory($limit, 0)
->willReturn([$revision = new Revision('test', '123', 'test')])
->shouldBeCalledTimes(1);

$this->simpleThingsAuditReader->findEntitesChangedAtRevision(Argument::cetera())
->willReturn([])
->shouldBeCalledTimes(1);

$blockContext->getTemplate()->willReturn('template')->shouldBeCalledTimes(1);
$blockContext->getSettings()->willReturn([])->shouldBeCalledTimes(1);

$this->blockService->execute($blockContext->reveal());

$this->assertSame('template', $this->templating->view);
$this->assertInternalType('array', $this->templating->parameters['settings']);
$this->assertSame($revision, $this->templating->parameters['revisions'][0]['revision']);
$this->assertSame($block, $this->templating->parameters['block']);
}

public function testDefaultSettings(): void
{
$blockContext = $this->getBlockContext($this->blockService);

$this->assertSettings([
'attr' => [],
'extra_cache_keys' => [],
'limit' => 10,
'template' => '@SonataDoctrineORMAdmin/Block/block_audit.html.twig',
'ttl' => 0,
'use_cache' => true,
], $blockContext);
}
}
158 changes: 124 additions & 34 deletions tests/Builder/DatagridBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,159 @@

namespace Sonata\DoctrineORMAdminBundle\Tests\Builder;

use Doctrine\ORM\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
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\Filter\FilterFactoryInterface;
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Translator\FormLabelTranslatorStrategy;
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
use Sonata\DoctrineORMAdminBundle\Builder\DatagridBuilder;
use Sonata\DoctrineORMAdminBundle\Filter\ModelAutocompleteFilter;
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Guess\TypeGuess;

/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
final class DatagridBuilderTest extends TestCase
{
/**
* @var TypeGuesserInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typeGuesser;

/**
* @var DatagridBuilder
*/
protected $datagridBuilder;
/**
* @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $datagridBuilder;
private $typeGuesser;
private $formFactory;
private $filterFactory;
private $admin;
private $modelManager;

protected function setUp(): void
{
$this->formFactory = $this->prophesize(FormFactoryInterface::class);
$this->filterFactory = $this->prophesize(FilterFactoryInterface::class);
$this->typeGuesser = $this->prophesize(TypeGuesserInterface::class);

$this->datagridBuilder = new DatagridBuilder(
$this->formFactory->reveal(),
$this->filterFactory->reveal(),
$this->typeGuesser->reveal()
);

$this->admin = $this->prophesize(AbstractAdmin::class);
$this->modelManager = $this->prophesize(ModelManager::class);

$this->admin->getClass()->willReturn('FakeClass');
$this->admin->getModelManager()->willReturn($this->modelManager->reveal());
$this->admin->attachAdminClass(Argument::cetera())->willReturn();
$this->admin->addFilterFieldDescription(Argument::cetera())->willReturn();
}

/**
* @var FilterFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
* @dataProvider getBaseDatagridData
*/
private $filterFactory;
public function testGetBaseDatagrid($pagerType, $pager): void
{
$proxyQuery = $this->prophesize(ProxyQueryInterface::class);
$fieldDescription = $this->prophesize(FieldDescriptionCollection::class);
$formBuilder = $this->prophesize(FormBuilderInterface::class);

protected function setUp(): void
$this->admin->getPagerType()->willReturn($pagerType);
$this->admin->createQuery()->willReturn($proxyQuery->reveal());
$this->admin->getList()->willReturn($fieldDescription->reveal());

$this->modelManager->getIdentifierFieldNames(Argument::any())->willReturn(['id']);

$this->formFactory->createNamedBuilder(Argument::cetera())->willReturn($formBuilder->reveal());

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

public function getBaseDatagridData(): array
{
$this->formFactory = $this->createMock(FormFactoryInterface::class);
$this->filterFactory = $this->createMock(FilterFactoryInterface::class);
$this->typeGuesser = $this->createMock(TypeGuesserInterface::class);
return [
'simple' => [
Pager::TYPE_SIMPLE,
SimplePager::class,
],
'default' => [
Pager::TYPE_DEFAULT,
Pager::class,
],
];
}

public function testGetBaseDatagridBadPagerType(): void
{
$this->admin->getPagerType()->willReturn('fake');

$this->datagridBuilder = new DatagridBuilder($this->formFactory, $this->filterFactory, $this->typeGuesser);
$this->expectException(\RuntimeException::class);

$this->datagridBuilder->getBaseDatagrid($this->admin->reveal());
}

public function testGetBaseDatagrid(): void
public function testFixFieldDescription(): void
{
$admin = $this->createMock(AbstractAdmin::class);
$admin->expects($this->once())->method('getPagerType')->willReturn(Pager::TYPE_SIMPLE);
$admin->expects($this->once())->method('getClass')->willReturn('Foo\Bar');
$admin->expects($this->once())->method('createQuery')
->willReturn($this->createMock(ProxyQueryInterface::class));
$admin->expects($this->once())->method('getList')
->willReturn($this->createMock(FieldDescriptionCollection::class));

$modelManager = $this->createMock(ModelManagerInterface::class);
$modelManager->expects($this->once())->method('getIdentifierFieldNames')->willReturn(['id']);
$admin->expects($this->once())->method('getModelManager')->willReturn($modelManager);

$this->formFactory->expects($this->once())->method('createNamedBuilder')
->willReturn($this->createMock(FormBuilderInterface::class));

$this->assertInstanceOf(Datagrid::class, $this->datagridBuilder->getBaseDatagrid($admin));
$classMetadata = $this->prophesize(ClassMetadata::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('test');
$fieldDescription->setMappingType(ClassMetadata::ONE_TO_MANY);

$this->modelManager->hasMetadata(Argument::any())->willReturn(true);

$this->modelManager->getParentMetadataForProperty(Argument::cetera())
->willReturn([$classMetadata, 2, $parentAssociationMapping = []])
->shouldBeCalledTimes(1);

$classMetadata->fieldMappings = [2 => [1 => 'test', 'type' => 'string']];
$classMetadata->associationMappings = [2 => ['fieldName' => 'fakeField']];

$this->datagridBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
}

public function testAddFilterNoType(): void
{
$datagrid = $this->prophesize(DatagridInterface::class);
$guessType = $this->prophesize(TypeGuess::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('test');

$this->typeGuesser->guessType(Argument::cetera())->willReturn($guessType->reveal());
$guessType->getOptions()->willReturn(['name' => 'value']);

$guessType->getType()->willReturn($typeGuessReturn = ModelAutocompleteFilter::class);

$this->modelManager->hasMetadata(Argument::cetera())->willReturn(false)->shouldBeCalledTimes(1);

$this->admin->getCode()->willReturn('someFakeCode');

$this->filterFactory->create(Argument::cetera())->willReturn(new ModelAutocompleteFilter());

$this->admin->getLabelTranslatorStrategy()->willReturn(new FormLabelTranslatorStrategy());

$datagrid->addFilter(Argument::type(ModelAutocompleteFilter::class));

$this->datagridBuilder->addFilter(
$datagrid->reveal(),
null,
$fieldDescription,
$this->admin->reveal()
);
}
}

0 comments on commit e0c1c12

Please sign in to comment.