Skip to content

Commit

Permalink
Add tests to ModelManager and DateFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Feb 1, 2020
1 parent 82b92fc commit 521c6ca
Show file tree
Hide file tree
Showing 12 changed files with 621 additions and 4 deletions.
36 changes: 35 additions & 1 deletion src/Filter/AbstractDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,44 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
$data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateType::TYPE_EQUAL : $data['type'];

// Some types do not require a value to be set (NULL, NOT NULL).
if (!$this->typeRequiresValue($data['type']) && !$data['value']) {
if (!($data['value'] ?? false) && !$this->typeRequiresValue($data['type'])) {
return;
}

switch ($data['type']) {
case DateType::TYPE_EQUAL:
$this->active = true;

return $this->applyTypeIsEqual($queryBuilder, $field, $data);

case DateType::TYPE_GREATER_THAN:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

$this->active = true;

return $this->applyTypeIsGreaterThan($queryBuilder, $field, $data);

case DateType::TYPE_LESS_EQUAL:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

$this->active = true;

return $this->applyTypeIsLessEqual($queryBuilder, $field, $data);

case DateType::TYPE_NULL:
case DateType::TYPE_NOT_NULL:
$this->active = true;

return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);

case DateType::TYPE_GREATER_EQUAL:
case DateType::TYPE_LESS_THAN:
$this->active = true;

return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
}
}
Expand Down Expand Up @@ -111,6 +121,30 @@ public function getRenderSettings()
]];
}

/**
* @param string $field
* @param array $data
*/
protected function applyTypeIsLessEqual(ProxyQueryInterface $queryBuilder, $field, $data)
{
}

/**
* @param string $field
* @param array $data
*/
protected function applyTypeIsGreaterThan(ProxyQueryInterface $queryBuilder, $field, $data)
{
}

/**
* @param string $field
* @param array $data
*/
protected function applyTypeIsEqual(ProxyQueryInterface $queryBuilder, $field, $data)
{
}

/**
* @param string $operation
* @param string $field
Expand Down
6 changes: 6 additions & 0 deletions src/Filter/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\Form\Extension\Core\Type\DateType;

class DateFilter extends AbstractDateFilter
{
public function getFieldType()
{
return $this->getOption('field_type', DateType::class);
}

/**
* @param string $field
* @param array $data
Expand Down
6 changes: 6 additions & 0 deletions src/Filter/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class DateTimeFilter extends AbstractDateFilter
{
Expand All @@ -24,6 +25,11 @@ class DateTimeFilter extends AbstractDateFilter
*/
protected $time = true;

public function getFieldType()
{
return $this->getOption('field_type', DateTimeType::class);
}

/**
* @param string $field
* @param array $data
Expand Down
15 changes: 13 additions & 2 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ public function getExportFields($class)
*/
public function getModelInstance($class)
{
$r = new \ReflectionClass($class);
if ($r->isAbstract()) {
throw new \InvalidArgumentException(sprintf('Cannot initialize abstract class: %s', $class));
}

$constructor = $r->getConstructor();

if (null !== $constructor && (!$constructor->isPublic() || $constructor->getNumberOfRequiredParameters() > 0)) {
return $r->newInstanceWithoutConstructor();
}

return new $class();
}

Expand Down Expand Up @@ -443,7 +454,7 @@ public function modelReverseTransform($class, array $array = [])

if ($reflClass->hasMethod($setter)) {
if (!$reflClass->getMethod($setter)->isPublic()) {
throw new \RuntimeException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName()));
throw new \BadMethodCallException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName()));
}

$instance->$setter($value);
Expand All @@ -452,7 +463,7 @@ public function modelReverseTransform($class, array $array = [])
$instance->$property = $value;
} elseif ($reflClass->hasProperty($property)) {
if (!$reflClass->getProperty($property)->isPublic()) {
throw new \RuntimeException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property)));
throw new \BadMethodCallException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property)));
}

$instance->$property = $value;
Expand Down
81 changes: 81 additions & 0 deletions tests/Filter/DateTimeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Filter;

use Sonata\AdminBundle\Form\Type\Filter\DateType;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineMongoDBAdminBundle\Filter\DateTimeFilter;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

final class DateTimeFilterTest extends FilterWithQueryBuilderTest
{
public function testEmpty(): void
{
$filter = new DateTimeFilter();
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);

$builder = new ProxyQuery($this->getQueryBuilder());

$builder->getQueryBuilder()
->expects($this->never())
->method('field')
;

$filter->filter($builder, 'alias', 'field', null);
$filter->filter($builder, 'alias', 'field', 'all');
$filter->filter($builder, 'alias', 'field', []);

$this->assertFalse($filter->isActive());
}

public function testGetType(): void
{
$this->assertSame(DateTimeType::class, (new DateTimeFilter())->getFieldType());
}

/**
* @dataProvider getExamples
*/
public function testFilter(array $data, string $method): void
{
$filter = new DateTimeFilter();
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);

$builder = new ProxyQuery($this->getQueryBuilder());

$builder->getQueryBuilder()
->expects($this->once())
->method($method)
->with($data['value'] ?? null)
;

$filter->filter($builder, 'alias', 'field', $data);

$this->assertTrue($filter->isActive());
}

public function getExamples(): array
{
return [
[['type' => DateType::TYPE_EQUAL, 'value' => new \DateTime('now')], 'range'],
[['type' => DateType::TYPE_GREATER_EQUAL, 'value' => new \DateTime('now')], 'gte'],
[['type' => DateType::TYPE_GREATER_THAN, 'value' => new \DateTime('now')], 'gt'],
[['type' => DateType::TYPE_LESS_EQUAL, 'value' => new \DateTime('now')], 'lte'],
[['type' => DateType::TYPE_LESS_THAN, 'value' => new \DateTime('now')], 'lt'],
[['type' => DateType::TYPE_NULL], 'equals'],
[['type' => DateType::TYPE_NOT_NULL], 'notEqual'],
[['value' => new \DateTime('now')], 'range'],
];
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/Document/AbstractDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document;

abstract class AbstractDocument
{
}
29 changes: 29 additions & 0 deletions tests/Fixtures/Document/AssociatedDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document;

class AssociatedDocument
{
private $plainField;
private $embeddedDocument;

public function __construct(int $plainField, EmbeddedDocument $embeddedDocument)
{
$this->plainField = $plainField;
$this->embeddedDocument = $embeddedDocument;
}

public function getPlainField(): int
{
return $this->plainField;
}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/Document/ContainerDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document;

class ContainerDocument
{
private $plainField;
private $associatedDocument;
private $embeddedDocument;

public function __construct(AssociatedDocument $associatedDocument, EmbeddedDocument $embeddedDocument)
{
$this->associatedDocument = $associatedDocument;
$this->embeddedDocument = $embeddedDocument;
}

public function getAssociatedDocument(): AssociatedDocument
{
return $this->associatedDocument;
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Document/EmbeddedDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document;

class EmbeddedDocument
{
/**
* @var bool
*/
protected $plainField;
}
19 changes: 19 additions & 0 deletions tests/Fixtures/Document/ProtectedDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document;

class ProtectedDocument
{
private function __construct()
{
}
}
38 changes: 38 additions & 0 deletions tests/Fixtures/Document/SimpleDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document;

class SimpleDocument
{
private $schmeckles;
private $multiWordProperty;

public function getSchmeckles()
{
return $this->schmeckles;
}

public function setSchmeckles($value): void
{
$this->schmeckles = $value;
}

public function getMultiWordProperty()
{
return $this->multiWordProperty;
}

public function setMultiWordProperty($value): void
{
$this->multiWordProperty = $value;
}
}
Loading

0 comments on commit 521c6ca

Please sign in to comment.