Skip to content

Commit

Permalink
Improve tests to remove mocks of final classes
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Jan 22, 2025
1 parent 4a9fb6c commit 3e35b6a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 93 deletions.
4 changes: 2 additions & 2 deletions src/Provider/AdminContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace EasyCorp\Bundle\EasyAdminBundle\Provider;

use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Context\AdminContextInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
Expand Down Expand Up @@ -33,7 +33,7 @@ public function hasContext(): bool
return null !== $currentRequest && $currentRequest->attributes->has(EA::CONTEXT_REQUEST_ATTRIBUTE);
}

public function getContext(bool $throw = false): ?AdminContext
public function getContext(bool $throw = false): ?AdminContextInterface
{
$currentRequest = $this->requestStack->getCurrentRequest();

Expand Down
6 changes: 1 addition & 5 deletions tests/Context/AdminContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ class AdminContextTest extends TestCase
*/
public function testGetReferrerEmptyString()
{
$request = $this->createMock(Request::class);
$request->query = new InputBag();
$request->query->set(EA::REFERRER, '');

$target = new AdminContext(
$request,
new Request(query: [EA::REFERRER => '']),
null,
new I18nDto('en', 'ltr', 'en', []),
new CrudControllerRegistry([], [], [], []),
Expand Down
2 changes: 1 addition & 1 deletion tests/Field/AbstractFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class AbstractFieldTest extends KernelTestCase
protected $adminContext;
protected $configurator;

private function getEntityDto(): EntityDto
protected function getEntityDto(): EntityDto
{
$entityDtoMock = $this->createMock(EntityDto::class);
$entityDtoMock
Expand Down
16 changes: 4 additions & 12 deletions tests/Field/DateFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\DateTimeConfigurator;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;

class DateFieldTest extends AbstractFieldTest
{
protected function setUp(): void
{
parent::setUp();

$intlFormatterMock = $this->getMockBuilder(IntlFormatterInterface::class)
->disableOriginalConstructor()
->onlyMethods(['formatDate'])
->getMock();
$intlFormatterMock->method('formatDate')->willReturnCallback(
static function ($value, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null) { return sprintf('value: %s | dateFormat: %s | pattern: %s | timezone: %s | calendar: %s | locale: %s', $value instanceof \DateTimeInterface ? $value->format('Y-m-d H:i:s') : $value, $dateFormat, $pattern, $timezone, $calendar, $locale); }
);

$this->configurator = new DateTimeConfigurator($intlFormatterMock);
$this->configurator = new DateTimeConfigurator(new IntlFormatter());
}

public function testFieldWithWrongTimezone()
Expand Down Expand Up @@ -77,7 +69,7 @@ public function testFieldWithPredefinedFormat()
$fieldDto = $this->configure($field);

$this->assertSame(DateTimeField::FORMAT_LONG, $fieldDto->getCustomOption(DateField::OPTION_DATE_PATTERN));
$this->assertSame('value: 2006-01-02 15:04:05 | dateFormat: long | pattern: | timezone: | calendar: gregorian | locale: ', $fieldDto->getFormattedValue());
$this->assertSame('January 2, 2006', $fieldDto->getFormattedValue());
}

public function testFieldWithCustomPattern()
Expand All @@ -88,7 +80,7 @@ public function testFieldWithCustomPattern()
$fieldDto = $this->configure($field);

$this->assertSame('HH:mm:ss ZZZZ a', $fieldDto->getCustomOption(DateField::OPTION_DATE_PATTERN));
$this->assertSame('value: 2006-01-02 15:04:05 | dateFormat: | pattern: HH:mm:ss ZZZZ a | timezone: | calendar: gregorian | locale: ', $fieldDto->getFormattedValue());
$this->assertSame('15:04:05 GMT PM', $fieldDto->getFormattedValue());
}

public function testFieldDefaultWidget()
Expand Down
16 changes: 4 additions & 12 deletions tests/Field/DateTimeFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\DateTimeConfigurator;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;

class DateTimeFieldTest extends AbstractFieldTest
{
protected function setUp(): void
{
parent::setUp();

$intlFormatterMock = $this->getMockBuilder(IntlFormatterInterface::class)
->disableOriginalConstructor()
->onlyMethods(['formatDateTime'])
->getMock();
$intlFormatterMock->method('formatDateTime')->willReturnCallback(
static function ($value, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null) { return sprintf('value: %s | dateFormat: %s | timeFormat: %s | pattern: %s | timezone: %s | calendar: %s | locale: %s', $value->format('Y-m-d'), $dateFormat, $timeFormat, $pattern, $timezone, $calendar, $locale); }
);

$this->configurator = new DateTimeConfigurator($intlFormatterMock);
$this->configurator = new DateTimeConfigurator(new IntlFormatter());
}

public function testFieldWithWrongTimezone()
Expand Down Expand Up @@ -94,7 +86,7 @@ public function testFieldWithPredefinedFormat()
$fieldDto = $this->configure($field);

$this->assertSame(DateTimeField::FORMAT_LONG, $fieldDto->getCustomOption(DateTimeField::OPTION_DATE_PATTERN));
$this->assertSame('value: 2015-01-16 | dateFormat: long | timeFormat: none | pattern: | timezone: | calendar: gregorian | locale: ', $fieldDto->getFormattedValue());
$this->assertSame('January 16, 2015', $fieldDto->getFormattedValue());
}

public function testFieldWithCustomPattern()
Expand All @@ -105,7 +97,7 @@ public function testFieldWithCustomPattern()
$fieldDto = $this->configure($field);

$this->assertSame('HH:mm:ss ZZZZ a', $fieldDto->getCustomOption(DateTimeField::OPTION_DATE_PATTERN));
$this->assertSame('value: 2015-01-16 | dateFormat: | timeFormat: | pattern: HH:mm:ss ZZZZ a | timezone: | calendar: gregorian | locale: ', $fieldDto->getFormattedValue());
$this->assertSame('00:00:00 GMT AM', $fieldDto->getFormattedValue());
}

public function testFieldDefaultWidget()
Expand Down
46 changes: 25 additions & 21 deletions tests/Field/MoneyFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\MoneyConfigurator;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;

class MoneyFieldTest extends AbstractFieldTest
{
protected function setUp(): void
{
parent::setUp();

$intlFormatterMock = $this->getMockBuilder(IntlFormatterInterface::class)
->disableOriginalConstructor()
->onlyMethods(['formatCurrency'])
->getMock();
$intlFormatterMock->method('formatCurrency')->willReturnCallback(
function ($value) { return $value.''; }
);

$propertyAccessorMock = $this->getMockBuilder(PropertyAccessor::class)
->disableOriginalConstructor()
->onlyMethods(['isReadable', 'getValue'])
->getMock();
$propertyAccessorMock->method('isReadable')->willReturn(true);
$propertyAccessorMock->method('getValue')->willReturn('USD');

$this->configurator = new MoneyConfigurator($intlFormatterMock, $propertyAccessorMock);
$this->configurator = new MoneyConfigurator(new IntlFormatter(), static::getContainer()->get('property_accessor'));
}

public function testFieldWithoutCurrency()
Expand Down Expand Up @@ -72,6 +57,25 @@ public function testFieldWithHardcodedCurrency()
self::assertSame('EUR', $fieldDto->getFormTypeOption('currency'));
}

protected function getEntityDto(): EntityDto
{
$reflectedClass = new \ReflectionClass(EntityDto::class);
$entityDto = $reflectedClass->newInstanceWithoutConstructor();
$primaryKeyNameProperty = $reflectedClass->getProperty('primaryKeyName');
$primaryKeyNameProperty->setValue($entityDto, 'id');
$primaryKeyValueProperty = $reflectedClass->getProperty('primaryKeyValue');
$primaryKeyValueProperty->setValue($entityDto, 1);
$fqcnProperty = $reflectedClass->getProperty('fqcn');
$fqcnProperty->setValue($entityDto, 'App\Entity\MyEntity');
$instanceProperty = $reflectedClass->getProperty('instance');
$instanceProperty->setValue($entityDto, new class {
public int $id = 1;
public string $bar = 'USD';
});

return $this->entityDto = $entityDto;
}

public function testFieldWithPropertyPathCurrency()
{
$field = MoneyField::new('foo')->setValue(100)->setCurrencyPropertyPath('bar');
Expand All @@ -97,7 +101,7 @@ public function testFieldsDefaultsToCents()
{
$field = MoneyField::new('foo')->setValue(100)->setCurrency('EUR');
$fieldDto = $this->configure($field);
self::assertSame('1€', $fieldDto->getFormattedValue());
self::assertSame('€1.00', $fieldDto->getFormattedValue());
self::assertSame(100, $fieldDto->getFormTypeOption('divisor'));
}

Expand All @@ -106,7 +110,7 @@ public function testFieldCents()
$field = MoneyField::new('foo')->setValue(100)->setCurrency('EUR');
$field->setStoredAsCents(false);
$fieldDto = $this->configure($field);
self::assertSame('100€', $fieldDto->getFormattedValue());
self::assertSame('€100.00', $fieldDto->getFormattedValue());
self::assertSame(1, $fieldDto->getFormTypeOption('divisor'));
}

Expand All @@ -115,7 +119,7 @@ public function testFieldWithCustomDivisor()
$field = MoneyField::new('foo')->setValue(725)->setCurrency('EUR');
$field->setFormTypeOption('divisor', 10000);
$fieldDto = $this->configure($field);
self::assertSame('0.0725€', $fieldDto->getFormattedValue());
self::assertSame('€0.07', $fieldDto->getFormattedValue());
self::assertSame(10000, $fieldDto->getFormTypeOption('divisor'));
}
}
21 changes: 4 additions & 17 deletions tests/Field/PercentFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\PercentConfigurator;
use EasyCorp\Bundle\EasyAdminBundle\Field\PercentField;
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;

class PercentFieldTest extends AbstractFieldTest
{
private $intlFormatterMock;

protected function setUp(): void
{
parent::setUp();

$this->intlFormatterMock = $this->getMockBuilder(IntlFormatterInterface::class)
->disableOriginalConstructor()
->onlyMethods(['formatNumber'])
->getMock();

$this->configurator = new PercentConfigurator($this->intlFormatterMock);
$this->configurator = new PercentConfigurator(new IntlFormatter());
}

public function testFieldWithNullValues()
Expand All @@ -33,30 +26,24 @@ public function testFieldWithNullValues()

public function testFieldDefaultDecimalsAndFractional()
{
$this->intlFormatterMock->method('formatNumber')->with(100.9874)->willReturn('100');

$field = PercentField::new('foo')->setValue(100.9874)->setStoredAsFractional(false);
$fieldDto = $this->configure($field);
self::assertSame(0, $fieldDto->getCustomOption(PercentField::OPTION_NUM_DECIMALS));
self::assertSame(0, $fieldDto->getFormTypeOption('scale'));
self::assertSame('100%', $fieldDto->getFormattedValue());
self::assertSame('101%', $fieldDto->getFormattedValue());
}

public function testFieldDecimalsAndFractional()
{
$this->intlFormatterMock->method('formatNumber')->with(100.1345)->willReturn('100.134');

$field = PercentField::new('foo')->setValue(100.1345)->setStoredAsFractional(false)->setNumDecimals(3);
$fieldDto = $this->configure($field);
self::assertSame(3, $fieldDto->getCustomOption(PercentField::OPTION_NUM_DECIMALS));
self::assertSame(3, $fieldDto->getFormTypeOption('scale'));
self::assertSame('100.134%', $fieldDto->getFormattedValue());
self::assertSame('100.135%', $fieldDto->getFormattedValue());
}

public function testFieldSynmbolAndFractional()
{
$this->intlFormatterMock->method('formatNumber')->with(100)->willReturn('100');

$field = PercentField::new('foo')->setValue(100)->setSymbol(' %')->setStoredAsFractional(false);
$fieldDto = $this->configure($field);
self::assertSame('100 %', $fieldDto->getFormattedValue());
Expand Down
19 changes: 4 additions & 15 deletions tests/Field/TimeFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\DateTimeConfigurator;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TimeField;
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;

class TimeFieldTest extends AbstractFieldTest
{
protected function setUp(): void
{
parent::setUp();

$intlFormatterMock = $this->getMockBuilder(IntlFormatterInterface::class)
->disableOriginalConstructor()
->onlyMethods(['formatDate', 'formatTime'])
->getMock();
$intlFormatterMock->method('formatDate')->willReturnCallback(
static function ($value, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null) { return sprintf('value: %s | dateFormat: %s | pattern: %s | timezone: %s | calendar: %s | locale: %s', $value instanceof \DateTimeInterface ? $value->format('Y-m-d H:i:s') : $value, $dateFormat, $pattern, $timezone, $calendar, $locale); }
);
$intlFormatterMock->method('formatTime')->willReturnCallback(
static function ($value, ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null) { return sprintf('value: %s | timeFormat: %s | pattern: %s | timezone: %s | calendar: %s | locale: %s', $value instanceof \DateTimeInterface ? $value->format('Y-m-d H:i:s') : $value, $timeFormat, $pattern, $timezone, $calendar, $locale); }
);

$this->configurator = new DateTimeConfigurator($intlFormatterMock);
$this->configurator = new DateTimeConfigurator(new IntlFormatter());
}

public function testFieldWithWrongTimezone()
Expand Down Expand Up @@ -80,7 +69,7 @@ public function testFieldWithPredefinedFormat()
$fieldDto = $this->configure($field);

$this->assertSame(DateTimeField::FORMAT_LONG, $fieldDto->getCustomOption(TimeField::OPTION_TIME_PATTERN));
$this->assertSame('value: 2006-01-02 15:04:05 | timeFormat: long | pattern: | timezone: | calendar: gregorian | locale: ', $fieldDto->getFormattedValue());
$this->assertSame('3:04:05 PM UTC', $fieldDto->getFormattedValue());
}

public function testFieldWithCustomPattern()
Expand All @@ -91,7 +80,7 @@ public function testFieldWithCustomPattern()
$fieldDto = $this->configure($field);

$this->assertSame('HH:mm:ss ZZZZ a', $fieldDto->getCustomOption(TimeField::OPTION_TIME_PATTERN));
$this->assertSame('value: 2006-01-02 15:04:05 | timeFormat: | pattern: HH:mm:ss ZZZZ a | timezone: | calendar: gregorian | locale: ', $fieldDto->getFormattedValue());
$this->assertSame('15:04:05 GMT PM', $fieldDto->getFormattedValue());
}

public function testFieldDefaultWidget()
Expand Down
19 changes: 11 additions & 8 deletions tests/Router/AdminUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Context\AdminContextInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Router\AdminRouteGeneratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminRouteGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
Expand Down Expand Up @@ -282,22 +283,20 @@ private function getAdminUrlGenerator(bool $signedUrls = false, bool $absoluteUr
{
self::bootKernel();

$adminContext = $this->getMockBuilder(AdminContext::class)->disableOriginalConstructor()->getMock();
$adminContext = $this->getMockBuilder(AdminContextInterface::class)->disableOriginalConstructor()->getMock();
$adminContext->method('getDashboardRouteName')->willReturn('admin');
$adminContext->method('getSignedUrls')->willReturn($signedUrls);
$adminContext->method('getAbsoluteUrls')->willReturn($absoluteUrls);
$adminContext->method('getRequest')->willReturn(new Request(['foo' => 'bar']));

$request = new Request();
$request->query->set('foo', 'bar');
$request->attributes->set(EA::CONTEXT_REQUEST_ATTRIBUTE, $adminContext);
$request = new Request(query: ['foo' => 'bar'], attributes: [EA::CONTEXT_REQUEST_ATTRIBUTE => $adminContext]);

$requestStack = new RequestStack();
$requestStack->push($request);

$adminContextProvider = new AdminContextProvider($requestStack);

$dashboardControllerRegistry = $this->getMockBuilder(DashboardControllerRegistry::class)->disableOriginalConstructor()->getMock();
$dashboardControllerRegistry = $this->getMockBuilder(DashboardControllerRegistryInterface::class)->disableOriginalConstructor()->getMock();
$dashboardControllerRegistry->method('getRouteByControllerFqcn')->willReturnMap([
['App\Controller\Admin\SecureDashboardController', 'secure_admin'],
]);
Expand All @@ -307,7 +306,11 @@ private function getAdminUrlGenerator(bool $signedUrls = false, bool $absoluteUr
$container = Kernel::MAJOR_VERSION >= 6 ? static::getContainer() : self::$container;
$router = $container->get('router');

$adminRouteGenerator = $this->getMockBuilder(AdminRouteGenerator::class)->disableOriginalConstructor()->getMock();
$adminRouteGenerator = $this->getMockBuilder(AdminRouteGeneratorInterface::class)
->disableOriginalConstructor()
->addMethods(['usesPrettyUrls'])
->getMock();
$adminRouteGenerator->method('usesPrettyUrls')->willReturn(false);

return new AdminUrlGenerator($adminContextProvider, $router, $dashboardControllerRegistry, $adminRouteGenerator);
}
Expand Down

0 comments on commit 3e35b6a

Please sign in to comment.