Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Import knp features #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,30 @@ Get a registry for this category later:
$dictionaries = $registry->filterByCategory('product.toy')->all();
```

### Extended dictionary

You can create an extended dictionary:
```yaml
knp_dictionary:
dictionaries:
europe:
type: 'key_value'
content:
fr: France
de: Germany

world:
type: 'key_value'
extends: europe
content:
us: USA
ca: Canada
```
The dictionary `world` will now contains its own values in addition
to the `europe` values.

*Note*: You must define the initial dictionary *BEFORE* the extended one.

## Transformers
For now, this bundle is only able to resolve your **class constants**:

Expand Down
90 changes: 90 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/CombinedDictionarySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace spec\Knp\DictionaryBundle\Dictionary;

use Phpspec\ObjectBehavior;
use Knp\DictionaryBundle\Dictionary;
use Knp\DictionaryBundle\Dictionary\CombinedDictionary;
use Webmozart\Assert\Assert;

class CombinedDictionarySpec extends ObjectBehavior
{
function let(
Dictionary $dictionary1,
Dictionary $dictionary2,
Dictionary $dictionary3
) {
$this->beConstructedWith('combined_dictionary', [
$dictionary1,
$dictionary2,
$dictionary3
]);
}

function it_is_initializable()
{
$this->shouldHaveType(CombinedDictionary::class);
}

function it_is_a_dictionary()
{
$this->shouldImplement(Dictionary::class);
}

function it_access_to_value_like_an_array(
$dictionary1,
$dictionary2,
$dictionary3
) {
$dictionary1->offsetExists('foo1')->willReturn(true);
$dictionary1->offsetGet('foo1')->willReturn('foo10');

$dictionary1->offsetExists('bar1')->willReturn(false);
$dictionary2->offsetExists('bar1')->willReturn(true);
$dictionary2->offsetGet('bar1')->willReturn('bar10');

$dictionary1->offsetExists('baz1')->willReturn(false);
$dictionary2->offsetExists('baz1')->willReturn(false);
$dictionary3->offsetExists('baz1')->willReturn(true);
$dictionary3->offsetGet('baz1')->willReturn('baz10');

Assert::eq($this['foo1']->getWrappedObject(), 'foo10');
Assert::eq($this['bar1']->getWrappedObject(), 'bar10');
Assert::eq($this['baz1']->getWrappedObject(), 'baz10');
}

function it_getvalues_should_return_dictionaries_values(
$dictionary1,
$dictionary2,
$dictionary3
) {
$dictionary1->getValues()->willReturn([
'foo1' => 'foo10',
'foo2' => 'foo20',
]);

$dictionary2->getValues()->willReturn([
'bar1' => 'bar10',
'bar2' => 'bar20',
]);

$dictionary3->getValues()->willReturn([
'foo2' => 'baz20',
'bar2' => 'baz20',
]);

$this->getValues()->shouldReturn([
'foo1' => 'foo10',
'foo2' => 'baz20',
'bar1' => 'bar10',
'bar2' => 'baz20',
]);
}

function its_getname_should_return_dictionary_name()
{
$this->getName()->shouldReturn('combined_dictionary');
}
}
51 changes: 51 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/ExtendedDictionarySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace spec\Knp\DictionaryBundle\Dictionary;

use Knp\DictionaryBundle\Dictionary\ExtendedDictionary;
use Knp\DictionaryBundle\Dictionary;
use Phpspec\ObjectBehavior;
use Webmozart\Assert\Assert;

class ExtendedDictionarySpec extends ObjectBehavior
{
function let(Dictionary $initialDictionary, Dictionary $extendedDictionary) {
$initialDictionary->getValues()->willReturn(['foo1' => 'foo1', 'foo2' => 'foo2']);
$extendedDictionary->getValues()->willReturn(['bar1' => 'bar1', 'bar2' => 'bar2']);

$this->beConstructedWith('foo', $initialDictionary, $extendedDictionary);
}

function it_is_initializable()
{
$this->shouldHaveType(ExtendedDictionary::class);
}

function it_is_a_dictionary()
{
$this->shouldImplement(Dictionary::class);
}

function it_access_to_value_like_an_array()
{
Assert::eq($this['foo1']->getWrappedObject(), 'foo1');
Assert::eq($this['foo2']->getWrappedObject(), 'foo2');
Assert::eq($this['bar1']->getWrappedObject(), 'bar1');
Assert::eq($this['bar2']->getWrappedObject(), 'bar2');
}

function its_getvalues_should_return_dictionary_values()
{
$this->getValues()->shouldReturn([
'foo1' => 'foo1',
'foo2' => 'foo2',
'bar1' => 'bar1',
'bar2' => 'bar2',
]);
}

function its_getname_should_return_dictionary_name()
{
$this->getName()->shouldReturn('foo');
}
}
76 changes: 76 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/Factory/CombinedSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace spec\Knp\DictionaryBundle\Dictionary\Factory;

use Phpspec\ObjectBehavior;
use Knp\DictionaryBundle\Dictionary;
use Knp\DictionaryBundle\Dictionary\DictionaryRegistry;
use Knp\DictionaryBundle\Dictionary\Factory;
use Knp\DictionaryBundle\Dictionary\Factory\Combined;

class CombinedSpec extends ObjectBehavior
{
function let(DictionaryRegistry $registry)
{
$this->beConstructedWith($registry);
}

function it_is_initializable()
{
$this->shouldHaveType(Combined::class);
}

function it_is_a_factory()
{
$this->shouldHaveType(Factory::class);
}

function it_supports_specific_config()
{
$this->supports(['type' => 'combined'])->shouldReturn(true);
}

function it_creates_a_dictionary(
$registry,
Dictionary $dictionary1,
Dictionary $dictionary2,
Dictionary $dictionary3
) {
$dictionary1->getValues()->willReturn([
'foo1' => 'foo10',
'foo2' => 'foo20',
]);

$dictionary2->getValues()->willReturn([
'bar1' => 'bar10',
'bar2' => 'bar20',
]);

$dictionary3->getValues()->willReturn([
'foo2' => 'baz20',
'bar2' => 'baz20',
]);

$registry->get('dictionary1')->willReturn($dictionary1);
$registry->get('dictionary2')->willReturn($dictionary2);
$registry->get('dictionary3')->willReturn($dictionary3);

$config = [
'type' => 'combined',
'dictionaries' => [
'dictionary1',
'dictionary2',
'dictionary3',
]
];

$dictionary = $this->create('combined_dictionary', $config);

$dictionary->getValues()->shouldReturn([
'foo1' => 'foo10',
'foo2' => 'baz20',
'bar1' => 'bar10',
'bar2' => 'baz20',
]);
}
}
59 changes: 59 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/Factory/ExtendedSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace spec\Knp\DictionaryBundle\Dictionary\Factory;

use PhpSpec\ObjectBehavior;
use Knp\DictionaryBundle\Dictionary\Factory;
use Knp\DictionaryBundle\Dictionary\Factory\FactoryAggregate;
use Knp\DictionaryBundle\Dictionary;
use Knp\DictionaryBundle\Dictionary\DictionaryRegistry;

class ExtendedSpec extends ObjectBehavior
{
function let(
FactoryAggregate $factoryAggregate,
DictionaryRegistry $registry
) {
$this->beConstructedWith($factoryAggregate, $registry);
}

function it_is_initializable()
{
$this->shouldHaveType(Factory\Extended::class);
}

function it_is_a_factory()
{
$this->shouldHaveType(Factory::class);
}

function it_supports_specific_config()
{
$this->supports(['extends' => 'my_dictionary'])->shouldReturn(true);
}

function it_creates_a_dictionary(
$factoryAggregate,
$registry,
Dictionary $initialDictionary,
Dictionary $extendsDictionary
) {
$config = [
'content' => ['bar1', 'bar2', 'bar3'],
];

$initialDictionary->getValues()->willReturn(['foo1', 'foo2']);
$extendsDictionary->getValues()->willReturn(['bar1', 'bar2', 'bar3']);

$factoryAggregate->create('yolo', $config)->willReturn($extendsDictionary);
$registry->get('initial_dictionary')->willReturn($initialDictionary);

$config = array_merge($config, ['extends' => 'initial_dictionary']);
$factoryAggregate->supports($config)->willReturn(true);

$dictionary = $this->create('yolo', $config);

$dictionary->getName()->shouldBe('yolo');
$dictionary->getValues()->shouldBe(['foo1', 'foo2', 'bar1', 'bar2', 'bar3']);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Knp\DictionaryBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down Expand Up @@ -42,6 +44,10 @@ public function getConfigTreeBuilder()
->end()
->children()
->scalarNode('type')->defaultValue('value')->end()
->scalarNode('extends')->end()
->arrayNode('dictionaries')
->normalizeKeys(false)->prototype('scalar')->end()
->end()
->scalarNode('category')->defaultNull()->end()
->arrayNode('content')
->prototype('scalar')->end()
Expand Down
Loading