Skip to content

Commit

Permalink
Fix support for hyphens (nelmio#431)
Browse files Browse the repository at this point in the history
nelmio#355 introduced a BC which is removing support for the usage of hyphens (`-`) in fixture names. This commit revert that change and add tests for it.
  • Loading branch information
theofidry authored Jul 15, 2016
1 parent 1aabda5 commit 2592adf
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 54 deletions.
3 changes: 2 additions & 1 deletion src/Nelmio/Alice/Instances/Populator/Populator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function populate(Fixture $fixture)

$value = $property->requiresUnique() ?
$this->generateUnique($fixture, $property) :
$this->processor->process($property, $fixture->getSetProperties(), $fixture->getValueForCurrent());
$this->processor->process($property, $fixture->getSetProperties(), $fixture->getValueForCurrent())
;

foreach ($this->setters as $setter) {
if ($setter->canSet($fixture, $object, $key, $value)) {
Expand Down
18 changes: 10 additions & 8 deletions src/Nelmio/Alice/Instances/Processor/Methods/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@

class Reference implements MethodInterface
{
private static $regex = '/^[\',\"]?'
.'(?:(?<multi>\d+)x\ )?'
.'@(?<reference>[\p{L}\d\_\.\*\/\-]+)'
.'(?<sequence>\{(?P<from>\d+)\.\.(?P<to>\d+)\})?'
.'(?:\->(?<property>[\p{L}\d_.*\/-]+))?'
.'[\',\"]?$'
.'/xi'
;

/**
* @var Collection
*/
Expand All @@ -36,16 +45,9 @@ public function setObjects(Collection $objects)
*/
public function canProcess(ProcessableInterface $processable)
{
$multiPart = '(?:(?<multi>\d+)x +)';
$referencePart = '@(?<reference>\{?[^\{\}\-\>\']+\}?)';
$sequencePart = '(?<sequence>\{(?<from>\d+)\.\.(?<to>\d+)\})';
$propertyPart = '(?:\->(?<property>[a-z0-9_-]+))';

$regex = "/^\\'?{$multiPart}?{$referencePart}{$sequencePart}?{$propertyPart}?\\'?$/i";

return
is_string($processable->getValue())
&& $processable->valueMatches($regex)
&& $processable->valueMatches(static::$regex)
;
}

Expand Down
92 changes: 47 additions & 45 deletions tests/Nelmio/Alice/Fixtures/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,35 @@ protected function createLoader(array $options = [])

public function testLoadCreatesInstances()
{
$res = $this->loadData([
$objects = $this->loadData([
self::USER => [
'alice-user' => [],
'bob' => [],
],
]);
$user = $res['bob'];

$this->assertInstanceOf(self::USER, $user);
$this->assertCount(2, $objects);

$alice = $objects['alice-user'];
$bob = $objects['bob'];

$this->assertInstanceOf(User::class, $alice);
$this->assertInstanceOf(User::class, $bob);
}

public function testGetReference()
{
$res = $this->loadData([
$objects = $this->loadData([
self::USER => [
'alice-user' => [],
'bob' => [],
],
]);
$user = $res['bob'];

$this->assertSame($user, $this->loader->getReference('bob'));
$this->assertCount(2, $objects);

$this->assertSame($this->loader->getReference('alice-user'), $objects['alice-user']);
$this->assertSame($this->loader->getReference('bob'), $objects['bob']);
}

/**
Expand All @@ -91,12 +100,7 @@ public function testGetReference()
*/
public function testGetBadReference()
{
$this->loadData([
self::USER => [
'bob' => [],
],
]);

$this->loadData([]);
$this->loader->getReference('foo');
}

Expand Down Expand Up @@ -190,6 +194,7 @@ public function testLoadEmptyFile()
public function testLoadSequencedItems()
{
$object = $this->createLoader()->load($file = __DIR__.'/../support/fixtures/sequenced_items.yml');

$this->assertArrayHasKey('group1', $object);
$this->assertInstanceOf(self::GROUP, $object['group1']);
$counter = 1;
Expand Down Expand Up @@ -321,23 +326,38 @@ public function testLoadAddsReferencesToAdders()

public function testLoadParsesReferences()
{
$res = $this->loadData([
$objects = $this->loadData([
self::USER => [
'user1' => [
'username' => 'alice',
],
'user-2' => [
'username' => 'bob',
],
],
self::GROUP => [
'a' => [
'members' => ['@user1']
'members' => [
'@user1',
'@user-2',
]
],
],
]);
/** @var Group $group */
$group = $res['a'];
$group = $objects['a'];

$this->assertInstanceOf(self::USER, current($group->getMembers()));
$this->assertEquals('alice', current($group->getMembers())->username);
$this->assertInstanceOf(Group::class, $group);

$members = $group->getMembers();

$this->assertCount(2, $members);
foreach ($members as $member) {
$this->assertInstanceOf(User::class, $member);
}

$this->assertEquals('alice', $members[0]->username);
$this->assertEquals('bob', $members[1]->username);
}

public function testLoadParsesReferencesInQuotes()
Expand All @@ -363,21 +383,24 @@ public function testLoadParsesReferencesInQuotes()

public function testLoadParsesPropertyReferences()
{
$res = $this->loadData([
$objects = $this->loadData([
self::USER => [
'user1' => [
'user-1' => [
'username' => 'alice',
],
'user2' => [
'username' => '@user1->username',
'username' => '@user-1->username',
],
]
]);

$this->assertInstanceOf(self::USER, $res['user1']);
$this->assertInstanceOf(self::USER, $res['user2']);
$this->assertEquals('alice', $res['user1']->username);
$this->assertEquals($res['user1']->username, $res['user2']->username);
$user1 = $objects['user-1'];
$user2 = $objects['user2'];

$this->assertInstanceOf(User::class, $user1);
$this->assertInstanceOf(User::class, $user2);
$this->assertEquals('alice', $user1->username);
$this->assertEquals($user1->username, $user2->username);
}

public function testLoadParsesPropertyReferencesGetter()
Expand Down Expand Up @@ -2002,27 +2025,6 @@ public function provideSpecialCharactersData()
],
];

$return['with chinese characters'] = [
'data' => [
self::USER => [
'汉字' => [
'username' => 'alice',
],
'汉字汉' => [
'username' => '@汉字->username',
],
'汉字汉字' => [
'username' => '@汉字汉->username',
],
]
],
'keys' => [
'汉字',
'汉字汉',
'汉字汉字',
],
];

return $return;
}
}
Expand Down
Loading

0 comments on commit 2592adf

Please sign in to comment.