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

Unique option not working with templates (issue #291) #359

Closed
wants to merge 12 commits into from
7 changes: 6 additions & 1 deletion src/Nelmio/Alice/Fixtures/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ public function extendTemplate(Fixture $template)

foreach ($template->properties as $property) {
if (!isset($this->spec[$property->getName()])) {
$this->addProperty($property->getName(), $property->getValue());
$name = $property->getName();
if ($property->requiresUnique()) {
$name .= ' (unique)';
}

$this->addProperty($name, $property->getValue());
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Nelmio/Alice/FixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ class FixturesTest extends \PHPUnit_Framework_TestCase
const GROUP = 'Nelmio\Alice\support\models\Group';
const CONTACT = 'Nelmio\Alice\support\models\Contact';

public function testLoadUniqueFromTemplateFixtures()
{
$objectManagerMock = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
$metadataFactory = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory');
$metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');

$objectManagerMock->expects($this->any())
->method('getMetadataFactory')
->will($this->returnValue($metadataFactory));

$metadataFactory->expects($this->any())
->method('getAllMetadata')
->will($this->returnValue([$metadata, $metadata, $metadata]));

$objects = Fixtures::load(__DIR__.'/support/fixtures/unique_with_template.yml', $objectManagerMock, ['providers' => [$this]]);

$this->assertCount(26, $objects);

$names = [];
foreach($objects as $object) {
$this->assertFalse(in_array($object->username, $names), sprintf('duplicate value %s', $object->username));
$names[] = $object->username;
}
}

public function testLoadLoadsYamlFilesAndDoctrinePersister()
{
$om = $this->getDoctrineManagerMock(14);
Expand Down
6 changes: 6 additions & 0 deletions tests/Nelmio/Alice/support/fixtures/unique_with_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\Nelmio\Alice\support\models\User:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a more complete test for that one, e.g. add another non unique field to the template. It would be also good to have another example with two level deep, i.e.

  • template1: 2 fields with unique, 1 without
  • template2: extends template 1, override 1 unique field to make it not unique, add another unique field
  • 3: extends template 1

Don't hesitate to create dedicated entities for it if needed

user_template (template):
username (unique): <regexify('[A-Z]{1}')>

user_{1..26} (extends user_template):
email: <email()>