Skip to content

Commit 774d89f

Browse files
committedJan 22, 2025·
minor #6757 Improve tests to remove mocks of final classes (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Improve tests to remove mocks of final classes Similar to #6753 Commits ------- 76b2eac Improve tests to remove mocks of final classes
2 parents 32eaf4f + 76b2eac commit 774d89f

15 files changed

+96
-106
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl;
4+
5+
interface IntlFormatterInterface
6+
{
7+
public function formatCurrency(int|float $amount, string $currency, array $attrs = [], ?string $locale = null): string;
8+
9+
public function formatNumber(int|float $number, array $attrs = [], string $style = 'decimal', string $type = 'default', ?string $locale = null): string;
10+
11+
/**
12+
* @param \DateTimeZone|string|bool|null $timezone
13+
*/
14+
public function formatDateTime(?\DateTimeInterface $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', /* \DateTimeZone|string|bool|null */ $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string;
15+
16+
/**
17+
* @param \DateTimeZone|string|bool|null $timezone
18+
*/
19+
public function formatDate(?\DateTimeInterface $date, ?string $dateFormat = 'medium', string $pattern = '', /* \DateTimeZone|string|bool|null */ $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string;
20+
21+
/**
22+
* @param \DateTimeZone|string|bool|null $timezone
23+
*/
24+
public function formatTime(?\DateTimeInterface $date, ?string $timeFormat = 'medium', string $pattern = '', /* \DateTimeZone|string|bool|null */ $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string;
25+
}

‎src/Field/Configurator/DateTimeConfigurator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
use Doctrine\DBAL\Types\Types;
66
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
77
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
8+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
89
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
910
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
1011
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
1112
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
1213
use EasyCorp\Bundle\EasyAdminBundle\Field\TimeField;
13-
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;
1414

1515
/**
1616
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
1717
*/
1818
final class DateTimeConfigurator implements FieldConfiguratorInterface
1919
{
20-
private IntlFormatter $intlFormatter;
20+
private IntlFormatterInterface $intlFormatter;
2121

22-
public function __construct(IntlFormatter $intlFormatter)
22+
public function __construct(IntlFormatterInterface $intlFormatter)
2323
{
2424
$this->intlFormatter = $intlFormatter;
2525
}

‎src/Field/Configurator/MoneyConfigurator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
66
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
7+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
78
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
89
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
910
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
10-
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;
1111
use Symfony\Component\Intl\Currencies;
1212
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1313

@@ -16,12 +16,12 @@
1616
*/
1717
final class MoneyConfigurator implements FieldConfiguratorInterface
1818
{
19-
private IntlFormatter $intlFormatter;
19+
private IntlFormatterInterface $intlFormatter;
2020
private PropertyAccessorInterface $propertyAccessor;
2121

2222
public const DEFAULT_DIVISOR = 100;
2323

24-
public function __construct(IntlFormatter $intlFormatter, PropertyAccessorInterface $propertyAccessor)
24+
public function __construct(IntlFormatterInterface $intlFormatter, PropertyAccessorInterface $propertyAccessor)
2525
{
2626
$this->intlFormatter = $intlFormatter;
2727
$this->propertyAccessor = $propertyAccessor;

‎src/Field/Configurator/NumberConfigurator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
66
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
7+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
78
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
89
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
910
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
10-
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;
1111

1212
/**
1313
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
1414
*/
1515
final class NumberConfigurator implements FieldConfiguratorInterface
1616
{
17-
private IntlFormatter $intlFormatter;
17+
private IntlFormatterInterface $intlFormatter;
1818

19-
public function __construct(IntlFormatter $intlFormatter)
19+
public function __construct(IntlFormatterInterface $intlFormatter)
2020
{
2121
$this->intlFormatter = $intlFormatter;
2222
}

‎src/Field/Configurator/PercentConfigurator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
66
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
7+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
78
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
89
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
910
use EasyCorp\Bundle\EasyAdminBundle\Field\PercentField;
10-
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;
1111

1212
/**
1313
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
1414
*/
1515
final class PercentConfigurator implements FieldConfiguratorInterface
1616
{
17-
private IntlFormatter $intlFormatter;
17+
private IntlFormatterInterface $intlFormatter;
1818

19-
public function __construct(IntlFormatter $intlFormatter)
19+
public function __construct(IntlFormatterInterface $intlFormatter)
2020
{
2121
$this->intlFormatter = $intlFormatter;
2222
}

‎src/Intl/IntlFormatter.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Intl;
44

5+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
56
use Twig\Error\RuntimeError;
67

78
/**
@@ -10,7 +11,7 @@
1011
*
1112
* @author Fabien Potencier <fabien@symfony.com>
1213
*/
13-
final class IntlFormatter
14+
final class IntlFormatter implements IntlFormatterInterface
1415
{
1516
private const DATE_FORMATS = [
1617
'none' => \IntlDateFormatter::NONE,
@@ -146,7 +147,7 @@ public function formatNumber($number, array $attrs = [], string $style = 'decima
146147
}
147148

148149
/**
149-
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
150+
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave unchanged
150151
*/
151152
public function formatDateTime(?\DateTimeInterface $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string
152153
{
@@ -161,15 +162,15 @@ public function formatDateTime(?\DateTimeInterface $date, ?string $dateFormat =
161162
}
162163

163164
/**
164-
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
165+
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave unchanged
165166
*/
166167
public function formatDate(?\DateTimeInterface $date, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string
167168
{
168169
return $this->formatDateTime($date, $dateFormat, 'none', $pattern, $timezone, $calendar, $locale);
169170
}
170171

171172
/**
172-
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
173+
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave unchanged
173174
*/
174175
public function formatTime(?\DateTimeInterface $date, ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string
175176
{

‎src/Provider/AdminContextProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Provider;
44

55
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
6-
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
6+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Context\AdminContextInterface;
77
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
88
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
99
use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
@@ -33,7 +33,7 @@ public function hasContext(): bool
3333
return null !== $currentRequest && $currentRequest->attributes->has(EA::CONTEXT_REQUEST_ATTRIBUTE);
3434
}
3535

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

‎tests/Context/AdminContextTest.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
1313
use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpFoundation\InputBag;
1615
use Symfony\Component\HttpFoundation\Request;
1716

1817
class AdminContextTest extends TestCase
@@ -22,12 +21,8 @@ class AdminContextTest extends TestCase
2221
*/
2322
public function testGetReferrerEmptyString()
2423
{
25-
$request = $this->createMock(Request::class);
26-
$request->query = new InputBag();
27-
$request->query->set(EA::REFERRER, '');
28-
2924
$target = new AdminContext(
30-
$request,
25+
new Request(query: [EA::REFERRER => '']),
3126
null,
3227
new I18nDto('en', 'ltr', 'en', []),
3328
new CrudControllerRegistry([], [], [], []),

‎tests/Field/AbstractFieldTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class AbstractFieldTest extends KernelTestCase
2020
protected $adminContext;
2121
protected $configurator;
2222

23-
private function getEntityDto(): EntityDto
23+
protected function getEntityDto(): EntityDto
2424
{
2525
$entityDtoMock = $this->createMock(EntityDto::class);
2626
$entityDtoMock

‎tests/Field/DateFieldTest.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,7 @@ protected function setUp(): void
1313
{
1414
parent::setUp();
1515

16-
$intlFormatterMock = $this->getMockBuilder(IntlFormatter::class)
17-
->disableOriginalConstructor()
18-
->onlyMethods(['formatDate'])
19-
->getMock();
20-
$intlFormatterMock->method('formatDate')->willReturnCallback(
21-
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); }
22-
);
23-
24-
$this->configurator = new DateTimeConfigurator($intlFormatterMock);
16+
$this->configurator = new DateTimeConfigurator(new IntlFormatter());
2517
}
2618

2719
public function testFieldWithWrongTimezone()
@@ -77,7 +69,7 @@ public function testFieldWithPredefinedFormat()
7769
$fieldDto = $this->configure($field);
7870

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

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

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

9486
public function testFieldDefaultWidget()

‎tests/Field/DateTimeFieldTest.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@ protected function setUp(): void
1212
{
1313
parent::setUp();
1414

15-
$intlFormatterMock = $this->getMockBuilder(IntlFormatter::class)
16-
->disableOriginalConstructor()
17-
->onlyMethods(['formatDateTime'])
18-
->getMock();
19-
$intlFormatterMock->method('formatDateTime')->willReturnCallback(
20-
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); }
21-
);
22-
23-
$this->configurator = new DateTimeConfigurator($intlFormatterMock);
15+
$this->configurator = new DateTimeConfigurator(new IntlFormatter());
2416
}
2517

2618
public function testFieldWithWrongTimezone()
@@ -94,7 +86,7 @@ public function testFieldWithPredefinedFormat()
9486
$fieldDto = $this->configure($field);
9587

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

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

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

111103
public function testFieldDefaultWidget()

‎tests/Field/MoneyFieldTest.php

+24-20
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,18 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field;
44

5+
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
56
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\MoneyConfigurator;
67
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
78
use EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter;
8-
use Symfony\Component\PropertyAccess\PropertyAccessor;
99

1010
class MoneyFieldTest extends AbstractFieldTest
1111
{
1212
protected function setUp(): void
1313
{
1414
parent::setUp();
1515

16-
$intlFormatterMock = $this->getMockBuilder(IntlFormatter::class)
17-
->disableOriginalConstructor()
18-
->onlyMethods(['formatCurrency'])
19-
->getMock();
20-
$intlFormatterMock->method('formatCurrency')->willReturnCallback(
21-
function ($value) { return $value.''; }
22-
);
23-
24-
$propertyAccessorMock = $this->getMockBuilder(PropertyAccessor::class)
25-
->disableOriginalConstructor()
26-
->onlyMethods(['isReadable', 'getValue'])
27-
->getMock();
28-
$propertyAccessorMock->method('isReadable')->willReturn(true);
29-
$propertyAccessorMock->method('getValue')->willReturn('USD');
30-
31-
$this->configurator = new MoneyConfigurator($intlFormatterMock, $propertyAccessorMock);
16+
$this->configurator = new MoneyConfigurator(new IntlFormatter(), static::getContainer()->get('property_accessor'));
3217
}
3318

3419
public function testFieldWithoutCurrency()
@@ -72,6 +57,25 @@ public function testFieldWithHardcodedCurrency()
7257
self::assertSame('EUR', $fieldDto->getFormTypeOption('currency'));
7358
}
7459

60+
protected function getEntityDto(): EntityDto
61+
{
62+
$reflectedClass = new \ReflectionClass(EntityDto::class);
63+
$entityDto = $reflectedClass->newInstanceWithoutConstructor();
64+
$primaryKeyNameProperty = $reflectedClass->getProperty('primaryKeyName');
65+
$primaryKeyNameProperty->setValue($entityDto, 'id');
66+
$primaryKeyValueProperty = $reflectedClass->getProperty('primaryKeyValue');
67+
$primaryKeyValueProperty->setValue($entityDto, 1);
68+
$fqcnProperty = $reflectedClass->getProperty('fqcn');
69+
$fqcnProperty->setValue($entityDto, 'App\Entity\MyEntity');
70+
$instanceProperty = $reflectedClass->getProperty('instance');
71+
$instanceProperty->setValue($entityDto, new class {
72+
public int $id = 1;
73+
public string $bar = 'USD';
74+
});
75+
76+
return $this->entityDto = $entityDto;
77+
}
78+
7579
public function testFieldWithPropertyPathCurrency()
7680
{
7781
$field = MoneyField::new('foo')->setValue(100)->setCurrencyPropertyPath('bar');
@@ -97,7 +101,7 @@ public function testFieldsDefaultsToCents()
97101
{
98102
$field = MoneyField::new('foo')->setValue(100)->setCurrency('EUR');
99103
$fieldDto = $this->configure($field);
100-
self::assertSame('1€', $fieldDto->getFormattedValue());
104+
self::assertSame('€1.00', $fieldDto->getFormattedValue());
101105
self::assertSame(100, $fieldDto->getFormTypeOption('divisor'));
102106
}
103107

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

@@ -115,7 +119,7 @@ public function testFieldWithCustomDivisor()
115119
$field = MoneyField::new('foo')->setValue(725)->setCurrency('EUR');
116120
$field->setFormTypeOption('divisor', 10000);
117121
$fieldDto = $this->configure($field);
118-
self::assertSame('0.0725€', $fieldDto->getFormattedValue());
122+
self::assertSame('€0.07', $fieldDto->getFormattedValue());
119123
self::assertSame(10000, $fieldDto->getFormTypeOption('divisor'));
120124
}
121125
}

‎tests/Field/PercentFieldTest.php

+3-16
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,11 @@
88

99
class PercentFieldTest extends AbstractFieldTest
1010
{
11-
private $intlFormatterMock;
12-
1311
protected function setUp(): void
1412
{
1513
parent::setUp();
1614

17-
$this->intlFormatterMock = $this->getMockBuilder(IntlFormatter::class)
18-
->disableOriginalConstructor()
19-
->onlyMethods(['formatNumber'])
20-
->getMock();
21-
22-
$this->configurator = new PercentConfigurator($this->intlFormatterMock);
15+
$this->configurator = new PercentConfigurator(new IntlFormatter());
2316
}
2417

2518
public function testFieldWithNullValues()
@@ -33,30 +26,24 @@ public function testFieldWithNullValues()
3326

3427
public function testFieldDefaultDecimalsAndFractional()
3528
{
36-
$this->intlFormatterMock->method('formatNumber')->with(100.9874)->willReturn('100');
37-
3829
$field = PercentField::new('foo')->setValue(100.9874)->setStoredAsFractional(false);
3930
$fieldDto = $this->configure($field);
4031
self::assertSame(0, $fieldDto->getCustomOption(PercentField::OPTION_NUM_DECIMALS));
4132
self::assertSame(0, $fieldDto->getFormTypeOption('scale'));
42-
self::assertSame('100%', $fieldDto->getFormattedValue());
33+
self::assertSame('101%', $fieldDto->getFormattedValue());
4334
}
4435

4536
public function testFieldDecimalsAndFractional()
4637
{
47-
$this->intlFormatterMock->method('formatNumber')->with(100.1345)->willReturn('100.134');
48-
4938
$field = PercentField::new('foo')->setValue(100.1345)->setStoredAsFractional(false)->setNumDecimals(3);
5039
$fieldDto = $this->configure($field);
5140
self::assertSame(3, $fieldDto->getCustomOption(PercentField::OPTION_NUM_DECIMALS));
5241
self::assertSame(3, $fieldDto->getFormTypeOption('scale'));
53-
self::assertSame('100.134%', $fieldDto->getFormattedValue());
42+
self::assertSame('100.135%', $fieldDto->getFormattedValue());
5443
}
5544

5645
public function testFieldSynmbolAndFractional()
5746
{
58-
$this->intlFormatterMock->method('formatNumber')->with(100)->willReturn('100');
59-
6047
$field = PercentField::new('foo')->setValue(100)->setSymbol(' %')->setStoredAsFractional(false);
6148
$fieldDto = $this->configure($field);
6249
self::assertSame('100 %', $fieldDto->getFormattedValue());

‎tests/Field/TimeFieldTest.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ protected function setUp(): void
1313
{
1414
parent::setUp();
1515

16-
$intlFormatterMock = $this->getMockBuilder(IntlFormatter::class)
17-
->disableOriginalConstructor()
18-
->onlyMethods(['formatDate', 'formatTime'])
19-
->getMock();
20-
$intlFormatterMock->method('formatDate')->willReturnCallback(
21-
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); }
22-
);
23-
$intlFormatterMock->method('formatTime')->willReturnCallback(
24-
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); }
25-
);
26-
27-
$this->configurator = new DateTimeConfigurator($intlFormatterMock);
16+
$this->configurator = new DateTimeConfigurator(new IntlFormatter());
2817
}
2918

3019
public function testFieldWithWrongTimezone()
@@ -80,7 +69,7 @@ public function testFieldWithPredefinedFormat()
8069
$fieldDto = $this->configure($field);
8170

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

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

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

9786
public function testFieldDefaultWidget()

‎tests/Router/AdminUrlGeneratorTest.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
66
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
7-
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
7+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Context\AdminContextInterface;
8+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Router\AdminRouteGeneratorInterface;
89
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
9-
use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistry;
10-
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminRouteGenerator;
10+
use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistryInterface;
1111
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
1212
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
1313
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1414
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpKernel\Kernel;
18+
use Symfony\Component\Routing\RouteCollection;
1819

1920
class AdminUrlGeneratorTest extends WebTestCase
2021
{
@@ -282,22 +283,20 @@ private function getAdminUrlGenerator(bool $signedUrls = false, bool $absoluteUr
282283
{
283284
self::bootKernel();
284285

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

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

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

298297
$adminContextProvider = new AdminContextProvider($requestStack);
299298

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

310-
$adminRouteGenerator = $this->getMockBuilder(AdminRouteGenerator::class)->disableOriginalConstructor()->getMock();
309+
$adminRouteGenerator = $this->getMockBuilder(AdminRouteGeneratorInterface::class)
310+
->disableOriginalConstructor()
311+
->addMethods(['usesPrettyUrls'])
312+
->getMockForAbstractClass();
313+
$adminRouteGenerator->method('generateAll')->willReturn(new RouteCollection());
314+
$adminRouteGenerator->method('findRouteName')->willReturn(null);
315+
$adminRouteGenerator->method('usesPrettyUrls')->willReturn(false);
311316

312317
return new AdminUrlGenerator($adminContextProvider, $router, $dashboardControllerRegistry, $adminRouteGenerator);
313318
}

0 commit comments

Comments
 (0)
Please sign in to comment.