Skip to content

Commit 96bbccf

Browse files
committed
Added configurable base enum classes and updated testing
1 parent 7beb9c4 commit 96bbccf

13 files changed

+235
-81
lines changed

Enum/ConfigurableEnum.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Enum;
4+
5+
/**
6+
* @author Yann Eugoné <[email protected]>
7+
*/
8+
class ConfigurableEnum implements EnumInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $name;
14+
15+
/**
16+
* @var array
17+
*/
18+
private $choices;
19+
20+
/**
21+
* @param string $name
22+
* @param array $choices
23+
*/
24+
public function __construct($name, array $choices)
25+
{
26+
$this->name = $name;
27+
$this->choices = $choices;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function getName()
34+
{
35+
return $this->name;
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getChoices()
42+
{
43+
return $this->choices;
44+
}
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Enum;
4+
5+
use Symfony\Component\Translation\TranslatorInterface;
6+
7+
/**
8+
* @author Yann Eugoné <[email protected]>
9+
*/
10+
class ConfigurableTranslatedEnum extends AbstractTranslatedEnum
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $name;
16+
17+
/**
18+
* @var array
19+
*/
20+
private $values;
21+
22+
/**
23+
* @param TranslatorInterface $translator
24+
* @param string $transPattern
25+
* @param string $name
26+
* @param array $values
27+
*/
28+
public function __construct(TranslatorInterface $translator, $transPattern, $name, array $values)
29+
{
30+
parent::__construct($translator, $transPattern);
31+
32+
$this->name = $name;
33+
$this->values = $values;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getName()
40+
{
41+
return $this->name;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function getValues()
48+
{
49+
return $this->values;
50+
}
51+
}

Enum/EnumWithClassAsNameTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Enum;
4+
5+
/**
6+
* @author Yann Eugoné <[email protected]>
7+
*/
8+
trait EnumWithClassAsNameTrait
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function getName()
14+
{
15+
return static::class;
16+
}
17+
}

Tests/Enum/AbstractTranslatedEnumTest.php

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Tests\Enum;
4+
5+
use Prophecy\Prophecy\ObjectProphecy;
6+
use Symfony\Component\Translation\TranslatorInterface;
7+
use Yokai\EnumBundle\Enum\ConfigurableTranslatedEnum;
8+
9+
/**
10+
* @author Yann Eugoné <[email protected]>
11+
*/
12+
class ConfigurableTranslatedEnumTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testConstructedWithInvalidPattern()
15+
{
16+
$this->expectException('Yokai\EnumBundle\Exception\InvalidTranslatePatternException');
17+
$translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface');
18+
new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']);
19+
}
20+
21+
public function testTranslatedChoices()
22+
{
23+
/** @var ObjectProphecy|TranslatorInterface $translator */
24+
$translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface');
25+
$translator->trans('choice.something.foo', [], 'messages', null)->shouldBeCalled()->willReturn('FOO translated');
26+
$translator->trans('choice.something.bar', [], 'messages', null)->shouldBeCalled()->willReturn('BAR translated');
27+
$type = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']);
28+
29+
$expectedChoices = [
30+
'foo' => 'FOO translated',
31+
'bar' => 'BAR translated',
32+
];
33+
$this->assertEquals($expectedChoices, $type->getChoices());
34+
}
35+
36+
public function testTranslatedWithDomainChoices()
37+
{
38+
/** @var ObjectProphecy|TranslatorInterface $translator */
39+
$translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface');
40+
$translator->trans('choice.something.foo', [], 'messages', null)->shouldNotBeCalled();
41+
$translator->trans('choice.something.bar', [], 'messages', null)->shouldNotBeCalled();
42+
$translator->trans('something.foo', [], 'choices', null)->shouldBeCalled()->willReturn('FOO translated');
43+
$translator->trans('something.bar', [], 'choices', null)->shouldBeCalled()->willReturn('BAR translated');
44+
$type = new ConfigurableTranslatedEnum($translator->reveal(), 'something.%s', 'something', ['foo', 'bar']);
45+
$type->setTransDomain('choices');
46+
47+
$expectedChoices = [
48+
'foo' => 'FOO translated',
49+
'bar' => 'BAR translated',
50+
];
51+
$this->assertEquals($expectedChoices, $type->getChoices());
52+
}
53+
}

Tests/Fixtures/GenderEnum.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
namespace Yokai\EnumBundle\Tests\Fixtures;
44

55
use Yokai\EnumBundle\Enum\EnumInterface;
6+
use Yokai\EnumBundle\Enum\EnumWithClassAsNameTrait;
67

78
/**
89
* @author Yann Eugoné <[email protected]>
910
*/
1011
class GenderEnum implements EnumInterface
1112
{
13+
use EnumWithClassAsNameTrait;
14+
1215
public function getChoices()
1316
{
1417
return ['male' => 'Male', 'female' => 'Female'];
1518
}
16-
17-
public function getName()
18-
{
19-
return 'gender';
20-
}
2119
}

Tests/Fixtures/StateEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
namespace Yokai\EnumBundle\Tests\Fixtures;
44

5+
use Symfony\Component\Translation\TranslatorInterface;
56
use Yokai\EnumBundle\Enum\AbstractTranslatedEnum;
67

78
/**
89
* @author Yann Eugoné <[email protected]>
910
*/
1011
class StateEnum extends AbstractTranslatedEnum
1112
{
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function __construct(TranslatorInterface $translator)
17+
{
18+
parent::__construct($translator, 'choice.state.%s');
19+
}
20+
1221
protected function getValues()
1322
{
1423
return ['new', 'validated', 'disabled'];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Tests\Fixtures;
4+
5+
use Symfony\Component\Translation\TranslatorInterface;
6+
use Yokai\EnumBundle\Enum\ConfigurableTranslatedEnum;
7+
8+
/**
9+
* @author Yann Eugoné <[email protected]>
10+
*/
11+
class SubscriptionEnum extends ConfigurableTranslatedEnum
12+
{
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function __construct(TranslatorInterface $translator)
17+
{
18+
parent::__construct(
19+
$translator,
20+
'choice.subscription.%s',
21+
'subscription',
22+
['none', 'daily', 'weekly', 'monthly']
23+
);
24+
}
25+
}

Tests/Fixtures/TypeEnum.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22

33
namespace Yokai\EnumBundle\Tests\Fixtures;
44

5-
use Yokai\EnumBundle\Enum\EnumInterface;
5+
use Yokai\EnumBundle\Enum\ConfigurableEnum;
66

77
/**
88
* @author Yann Eugoné <[email protected]>
99
*/
10-
class TypeEnum implements EnumInterface
10+
class TypeEnum extends ConfigurableEnum
1111
{
12-
public function getChoices()
12+
public function __construct()
1313
{
14-
return ['customer' => 'Customer', 'prospect' => 'Prospect'];
15-
}
16-
17-
public function getName()
18-
{
19-
return 'type';
14+
parent::__construct('type', ['customer' => 'Customer', 'prospect' => 'Prospect']);
2015
}
2116
}

Tests/Form/Extension/EnumTypeGuesserTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ protected function setUp()
5050
{
5151
$this->enumRegistry = $this->prophesize('Yokai\EnumBundle\Registry\EnumRegistryInterface');
5252
$this->enumRegistry->has('state')->willReturn(false);
53-
$this->enumRegistry->has('gender')->willReturn(true);
54-
$this->enumRegistry->get('gender')->willReturn(new GenderEnum);
53+
$this->enumRegistry->has(GenderEnum::class)->willReturn(true);
54+
$this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum);
5555

5656
$this->metadata = new ClassMetadata(self::TEST_CLASS);
57-
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Enum(['enum' => 'gender']));
57+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Enum(['enum' => GenderEnum::class]));
5858
$this->metadataFactory = $this->prophesize('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface');
5959
$this->metadataFactory->getMetadataFor(self::TEST_CLASS)
6060
->willReturn($this->metadata);
@@ -69,7 +69,7 @@ public function testGuessType()
6969
$guess = new TypeGuess(
7070
$this->getEnumType(),
7171
[
72-
'enum' => 'gender',
72+
'enum' => GenderEnum::class,
7373
'multiple' => false,
7474
],
7575
Guess::HIGH_CONFIDENCE

0 commit comments

Comments
 (0)