Skip to content

Commit 24395c3

Browse files
committed
Merge pull request #51 from fre5h/fix-legacy-form-helper
Fix checking symfony version in LegacyFormHelper
2 parents 782aec3 + f57f1db commit 24395c3

File tree

3 files changed

+80
-61
lines changed

3 files changed

+80
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Provides support of **ENUM type** for Doctrine in Symfony applications.
2626

2727
| Bundle Version (X.Y) | PHP | Symfony | Doctrine | Comment |
2828
|:--------------------:|:-------:|:------------------:|:--------:|:------------------------------------------|
29-
| 4.2 | >= 5.4 | >= 2.6, >= 3.0 | >= 2.2 | Actual version |
29+
| 4.3 | >= 5.4 | >= 2.6, >= 3.0 | >= 2.2 | Actual version |
3030
| 3.3 | >= 5.4 | >= 2.3 and <= 2.8 | >= 2.2 | |
3131
| 2.6 | 5.3 | >= 2.3 and <= 2.7 | >= 2.2 | Frozen version, no longer being supported |
3232

Tests/Util/LegacyFormHelperTest.php

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,63 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Fresh\DoctrineEnumBundle\Tests\Util {
11+
namespace Fresh\DoctrineEnumBundle\Tests\Util;
1212

13-
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
14-
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper;
13+
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper;
1514

15+
/**
16+
* LegacyFormHelperTest
17+
*
18+
* @author Jaik Dean <jaik@fluoresce.co>
19+
* @author Artem Genvald <genvaldartem@gmail.com>
20+
*/
21+
class LegacyFormHelperTest extends \PHPUnit_Framework_TestCase
22+
{
1623
/**
17-
* LegacyFormHelperTest
18-
*
19-
* @author Jaik Dean <jaik@fluoresce.co>
20-
*
21-
* @coversClass \Fresh\DoctrineEnumBundle\Util\LegacyFormHelper
24+
* @dataProvider dataProviderForIsLegacyTest
2225
*/
23-
class LegacyFormHelperTest extends \PHPUnit_Framework_TestCase
26+
public function testIsLegacy($majorVersion, $expectedLegacyStatus)
2427
{
25-
/**
26-
* Test that the helper identifies whether we’re running a legacy
27-
* version of Symfony.
28-
*/
29-
public function testIsLegacy()
30-
{
31-
global $LegacyFormHelperTest_legacySymfonyVersion;
32-
33-
$LegacyFormHelperTest_legacySymfonyVersion = true;
34-
$this->assertEquals(true, LegacyFormHelper::isLegacy());
35-
36-
$LegacyFormHelperTest_legacySymfonyVersion = false;
37-
$this->assertEquals(false, LegacyFormHelper::isLegacy());
38-
}
39-
40-
/**
41-
* Test that the correct form field type is returned for current and legacy
42-
* versions of Symfony.
43-
*/
44-
public function testGetType()
45-
{
46-
global $LegacyFormHelperTest_legacySymfonyVersion;
47-
$formType = 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
28+
$this->assertEquals($expectedLegacyStatus, LegacyFormHelper::isLegacy($majorVersion));
29+
}
4830

49-
$LegacyFormHelperTest_legacySymfonyVersion = true;
50-
$this->assertEquals('choice', LegacyFormHelper::getType($formType));
31+
public function dataProviderForIsLegacyTest()
32+
{
33+
return [
34+
[1, true],
35+
[2, true],
36+
[3, false],
37+
];
38+
}
5139

52-
$LegacyFormHelperTest_legacySymfonyVersion = false;
53-
$this->assertEquals($formType, LegacyFormHelper::getType($formType));
54-
}
40+
/**
41+
* @dataProvider dataProviderForGetTypeTest
42+
*/
43+
public function testGetType($majorVersion, $expectedFormType)
44+
{
45+
$this->assertEquals(
46+
$expectedFormType,
47+
LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\ChoiceType', $majorVersion)
48+
);
5549
}
56-
}
5750

58-
namespace Fresh\DoctrineEnumBundle\Util {
51+
/**
52+
* @return array
53+
*/
54+
public function dataProviderForGetTypeTest()
55+
{
56+
return [
57+
[2, 'choice'],
58+
[3, 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'],
59+
];
60+
}
5961

60-
function method_exists($class, $method)
62+
/**
63+
* @expectedException \InvalidArgumentException
64+
* @expectedExceptionMessage Form type with class "Symfony\Component\Form\Extension\Core\Type\TextType" can not be found. Please check for typos or add it to the map in LegacyFormHelper
65+
*/
66+
public function testExceptionForGetUnsupportedType()
6167
{
62-
global $LegacyFormHelperTest_legacySymfonyVersion;
63-
return !$LegacyFormHelperTest_legacySymfonyVersion;
68+
LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType', 2);
6469
}
6570
}

Util/LegacyFormHelper.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,53 @@
1010

1111
namespace Fresh\DoctrineEnumBundle\Util;
1212

13+
use Symfony\Component\HttpKernel\Kernel;
14+
1315
/**
14-
* This class was based on LegacyFormHelper in FOSUserBundle:
15-
* https://github.com/FriendsOfSymfony/FOSUserBundle/blob/c533a233b52c1d3843e816a35677561330ddbc74/Util/LegacyFormHelper.php
16+
* LegacyFormHelper
17+
*
18+
* This class based on LegacyFormHelper in FOSUserBundle:
19+
* @see https://github.com/FriendsOfSymfony/FOSUserBundle/blob/c533a233b52c1d3843e816a35677561330ddbc74/Util/LegacyFormHelper.php
1620
*
1721
* @internal
1822
*
1923
* @author Gabor Egyed <gabor.egyed@gmail.com>
2024
* @author Jaik Dean <jaik@fluoresce.co>
25+
* @author Artem Genvald <genvaldartem@gmail.com>
2126
*/
2227
final class LegacyFormHelper
2328
{
24-
private static $map = array(
29+
const MINIMUM_MAJOR_VERSION = 3;
30+
31+
/**
32+
* @var array $map Mapping form type classes to legacy form types
33+
* @static
34+
*/
35+
private static $map = [
2536
'Symfony\Component\Form\Extension\Core\Type\ChoiceType' => 'choice',
26-
);
37+
];
2738

2839
/**
2940
* Get a form field type compatible with the current version of Symfony
3041
*
31-
* @param string $class
42+
* @param string $class Class
43+
* @param int $majorVersion Major version
44+
*
3245
* @return string Class or type name
3346
*/
34-
public static function getType($class)
47+
public static function getType($class, $majorVersion = Kernel::MAJOR_VERSION)
3548
{
36-
if (!self::isLegacy()) {
49+
if (!self::isLegacy($majorVersion)) {
3750
return $class;
3851
}
3952

4053
if (!isset(self::$map[$class])) {
41-
throw new \InvalidArgumentException(sprintf('Form type with class "%s" can not be found. Please check for typos or add it to the map in LegacyFormHelper', $class));
54+
throw new \InvalidArgumentException(
55+
sprintf(
56+
'Form type with class "%s" can not be found. Please check for typos or add it to the map in LegacyFormHelper',
57+
$class
58+
)
59+
);
4260
}
4361

4462
return self::$map[$class];
@@ -47,18 +65,14 @@ public static function getType($class)
4765
/**
4866
* Check whether to use legacy form behaviour from Symfony <3.0
4967
*
68+
* @param int $majorVersion Major version
69+
*
70+
* @static
71+
*
5072
* @return bool
5173
*/
52-
public static function isLegacy()
53-
{
54-
return !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
55-
}
56-
57-
private function __construct()
58-
{
59-
}
60-
61-
private function __clone()
74+
public static function isLegacy($majorVersion = Kernel::MAJOR_VERSION)
6275
{
76+
return $majorVersion < self::MINIMUM_MAJOR_VERSION;
6377
}
6478
}

0 commit comments

Comments
 (0)