Skip to content

Commit c975799

Browse files
committed
[Tests][Twig] Upgraded & fixed FileSizeExtensionTest
1 parent 2ab00c4 commit c975799

File tree

2 files changed

+59
-101
lines changed

2 files changed

+59
-101
lines changed

tests/lib/MVC/Symfony/Templating/Twig/Extension/FileSizeExtensionTest.php

Lines changed: 39 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,127 +14,83 @@
1414
use Symfony\Contracts\Translation\TranslatorInterface;
1515
use Twig\Test\IntegrationTestCase;
1616

17-
/**
18-
* Class FileSizeExtensionTest.
19-
*/
20-
class FileSizeExtensionTest extends IntegrationTestCase
17+
final class FileSizeExtensionTest extends IntegrationTestCase
2118
{
22-
/**
23-
* @param string $locale
24-
*/
25-
protected $locale;
26-
27-
/**
28-
* @param array $suffixes
29-
*/
30-
protected $suffixes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'];
31-
32-
/**
33-
* @param TranslatorInterface|MockObject
34-
*/
35-
protected $translatorMock;
36-
37-
/**
38-
* @param ConfigResolverInterface|MockObject
39-
*/
40-
protected $configResolverInterfaceMock;
41-
42-
/**
43-
* @param LocaleConverterInterface|MockObject
44-
*/
45-
protected $localeConverterInterfaceMock;
46-
47-
/**
48-
* @param string $locale
49-
* @param string $defaultLocale
50-
*/
19+
protected string $locale = '';
20+
21+
/** @var string[] */
22+
protected array $suffixes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'];
23+
24+
protected TranslatorInterface & MockObject $translatorMock;
25+
26+
protected ConfigResolverInterface & MockObject $configResolverInterfaceMock;
27+
28+
protected LocaleConverterInterface & MockObject $localeConverterInterfaceMock;
29+
5130
protected function setConfigurationLocale(
52-
$locale,
53-
$defaultLocale
54-
) {
31+
string $locale,
32+
string $defaultLocale
33+
): void {
5534
locale_set_default($defaultLocale);
5635
$this->locale = $locale;
5736
}
5837

59-
/**
60-
* @return string $locale
61-
*/
62-
public function getLocale()
38+
public function getLocale(): string
6339
{
64-
return [$this->locale];
40+
return $this->locale;
6541
}
6642

67-
/**
68-
* @return array
69-
*/
70-
protected function getExtensions()
43+
protected function getExtensions(): array
7144
{
7245
return [
7346
new FileSizeExtension($this->getTranslatorInterfaceMock(), $this->suffixes, $this->getConfigResolverInterfaceMock(), $this->getLocaleConverterInterfaceMock()),
7447
];
7548
}
7649

77-
protected function getFixturesDir(): string
50+
protected static function getFixturesDirectory(): string
7851
{
7952
return __DIR__ . '/_fixtures/functions/ibexa_file_size';
8053
}
8154

82-
/**
83-
* @return ConfigResolverInterface|MockObject
84-
*/
85-
protected function getConfigResolverInterfaceMock()
55+
protected function getConfigResolverInterfaceMock(): ConfigResolverInterface & MockObject
8656
{
8757
$configResolverInterfaceMock = $this->createMock(ConfigResolverInterface::class);
88-
$configResolverInterfaceMock->expects(self::any())
58+
$configResolverInterfaceMock->expects(self::atLeastOnce())
8959
->method('getParameter')
9060
->with('languages')
91-
->will(self::returnValue($this->getLocale()));
61+
->willReturn([$this->getLocale()]);
9262

9363
return $configResolverInterfaceMock;
9464
}
9565

96-
/**
97-
* @return LocaleConverterInterface|MockObject
98-
*/
99-
protected function getLocaleConverterInterfaceMock()
66+
protected function getLocaleConverterInterfaceMock(): LocaleConverterInterface & MockObject
10067
{
10168
$this->localeConverterInterfaceMock = $this->createMock(LocaleConverterInterface::class);
102-
$this->localeConverterInterfaceMock->expects(self::any())
69+
$this->localeConverterInterfaceMock->expects(self::atLeastOnce())
10370
->method('convertToPOSIX')
104-
->will(
105-
self::returnValueMap(
106-
[
107-
['fre-FR', 'fr-FR'],
108-
['eng-GB', 'en-GB'],
109-
]
110-
)
71+
->willReturnMap(
72+
[
73+
['fre-FR', 'fr-FR'],
74+
['eng-GB', 'en-GB'],
75+
]
11176
);
11277

11378
return $this->localeConverterInterfaceMock;
11479
}
11580

116-
/**
117-
* @return TranslatorInterface|MockObject
118-
*/
119-
protected function getTranslatorInterfaceMock()
81+
protected function getTranslatorInterfaceMock(): TranslatorInterface & MockObject
12082
{
121-
$that = $this;
12283
$this->translatorMock = $this->createMock(TranslatorInterface::class);
12384
$this->translatorMock
124-
->expects(self::any())->method('trans')->will(
125-
self::returnCallback(
126-
static function ($suffixes) use ($that) {
127-
foreach ($that->getLocale() as $value) {
128-
if ($value === 'fre-FR') {
129-
return $suffixes . ' French version';
130-
} elseif ($value === 'eng-GB') {
131-
return $suffixes . ' English version';
132-
} else {
133-
return $suffixes . ' wrong local so we take the default one which is en-GB here';
134-
}
135-
}
136-
}
137-
)
85+
->expects(self::atLeastOnce())
86+
->method('trans')->willReturnCallback(
87+
function ($suffixes): string {
88+
return match ($this->getLocale()) {
89+
'fre-FR' => $suffixes . ' French version',
90+
'eng-GB' => $suffixes . ' English version',
91+
default => $suffixes . ' wrong locale so we take the default one which is en-GB here',
92+
};
93+
}
13894
);
13995

14096
return $this->translatorMock;

tests/lib/MVC/Symfony/Templating/Twig/Extension/_fixtures/functions/ibexa_file_size/ibexa_file_size.test

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@
1414
{{ 910565875123441600000|ibexa_file_size( 2 ) }}
1515
{{ 910565632767581700000000000|ibexa_file_size( 4 ) }}
1616
--DATA--
17-
$this->setConfigurationLocale( array( 'wrong local' ), 'eng-GB' );
18-
return array()
17+
$this->setConfigurationLocale('wrong locale', 'eng-GB');
18+
return []
1919
--EXPECT--
20-
10 B wrong local so we take the default one which is en-GB here
21-
1 kB wrong local so we take the default one which is en-GB here
22-
5 kB wrong local so we take the default one which is en-GB here
23-
12 kB wrong local so we take the default one which is en-GB here
24-
152 kB wrong local so we take the default one which is en-GB here
25-
26.15126 MB wrong local so we take the default one which is en-GB here
26-
123.1231 MB wrong local so we take the default one which is en-GB here
27-
456.5 GB wrong local so we take the default one which is en-GB here
28-
789.78979 TB wrong local so we take the default one which is en-GB here
29-
789.7897897898 PB wrong local so we take the default one which is en-GB here
30-
789.79 EB wrong local so we take the default one which is en-GB here
31-
789789789.7898 EB wrong local so we take the default one which is en-GB here
20+
10 B wrong locale so we take the default one which is en-GB here
21+
1 kB wrong locale so we take the default one which is en-GB here
22+
5 kB wrong locale so we take the default one which is en-GB here
23+
12 kB wrong locale so we take the default one which is en-GB here
24+
152 kB wrong locale so we take the default one which is en-GB here
25+
26.15126 MB wrong locale so we take the default one which is en-GB here
26+
123.1231 MB wrong locale so we take the default one which is en-GB here
27+
456.5 GB wrong locale so we take the default one which is en-GB here
28+
789.78979 TB wrong locale so we take the default one which is en-GB here
29+
789.7897897898 PB wrong locale so we take the default one which is en-GB here
30+
789.79 EB wrong locale so we take the default one which is en-GB here
31+
789789789.7898 EB wrong locale so we take the default one which is en-GB here
3232
--DATA--
33-
return array()
33+
return []
3434
--CONFIG--
35-
$this->locale = "fre-FR"; return array();
35+
$this->setConfigurationLocale('fre-FR', 'eng-GB');
36+
return []
3637
--EXPECT--
3738
10 B French version
3839
1 kB French version
@@ -47,9 +48,10 @@ $this->locale = "fre-FR"; return array();
4748
789,79 EB French version
4849
789789789,7898 EB French version
4950
--DATA--
50-
return array()
51+
return []
5152
--CONFIG--
52-
$this->locale = "eng-GB"; return array();
53+
$this->setConfigurationLocale('eng-GB', 'eng-GB');
54+
return [];
5355
--EXPECT--
5456
10 B English version
5557
1 kB English version

0 commit comments

Comments
 (0)