Question

Previously when creating fixture documents to build a website (either for testing or for sites without a CMS). I've always done it the way shown in the example documentation. So a fixtures page would have a lot of repeating code. Like this (and this is a short one!):

class codeExampleFixtures  extends AbstractFixture implements OrderedFixtureInterface
{
/**
 * {@inheritDoc}
 */
public function load(ObjectManager $manager)
{
    /*** Begin data fixture ***/
    $code1 = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';        
    // The $exampleX variable can be left empty if not needed.
    $example1 = '<p>In this example. The code is different for the example itself.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
    $lang1 = 'html'; // choices are 'html', 'css' and 'js'

    // Do not edit anything else in this fixture.
    $codeExample1 = new codeExample();
    $codeExample1->setlang($lang1);
    $codeExample1->setcodeExample($code1);
    $exampleFixture1 = (empty($example1)) ? $code1 : $example1;
    $codeExample1->settextExample($exampleFixture);
    $manager->persist($codeExample1);
    $this->setReference('ce1', $codeExample1);
    /*** End data fixture ***/

    /*** Begin data fixture ***/
    $code2 = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';        
    // The $exampleX variable can be left empty if not needed.
    $example2 = Null;
    $lang2 = 'css'; // choices are 'html', 'css' and 'js'

    // Do not edit anything else in this fixture.
    $codeExample2 = new codeExample();
    $codeExample2->setlang($lang2);
    $codeExample2->setcodeExample($code2);
    $exampleFixture2 = (empty($example2)) ? $code2 : $example2;
    $codeExample2->settextExample($exampleFixture2);
    $manager->persist($codeExample2);
    $this->setReference('ce2', $codeExample2);
    /*** End data fixture ***/

    $manager->flush(); //Upload the data to the database.
}

I decided to refactor it and I think my results might be useful to other people doing similar things. I'm sure there are ways to improve it (I'd be happy to hear them!) and I'm sure there will be plenty of people who have already thought of doing similar things themselves but if it is useful to you, please feel free to use it :)

Was it helpful?

Solution 2

Firstly I added a method to the class called createFixture():

public function createFixture($code, $example, $lang) {
    $codeExample = new codeExample();
    $codeExample->setlang($lang);
    $codeExample->setcodeExample($code);
    $exampleFixture = (empty($example)) ? $code : $example;
    $codeExample->settextExample($exampleFixture);
    return $codeExample;
}

Then all that was left was to put the fixtures into it. As you can see, the repeated code is drastically reduced:

/*** Begin data fixture ***/
    $fixture = $this->createFixture(
        "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
        incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
        exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
        fugiat nulla pariatur.</p>", 
        "<p>In this example. The code is different from the example itself.</p>
        <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
        fugiat nulla pariatur.</p>", 
        "html"
    );
    /* in order to reference this later, specify a simple and uniqe key for this fixture */
    $this->setReference('ce1', $fixture);
    $manager->persist($fixture);
    /*** End data fixture ***/

    /*** Begin data fixture ***/
    $fixture = $this->createFixture(
        "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
        incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
        exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
        fugiat nulla pariatur.</p>", 
        "", 
        "css"
    );
    /* in order to reference this later, specify a simple and uniqe key for this fixture */
    $this->setReference('ce2', $fixture);
    $manager->persist($fixture);
    /*** End data fixture ***/

Obviously you'd have to rewrite this a little for each different type of fixture but the principle is the same.

My full fixtures file can be found here: https://gist.github.com/alexward1981/6827216

OTHER TIPS

you should really take a look at Jordi Boggiano's (the guy who wrote composer) nelmio/alice!

Get an idea of how simple it is to set up fixtures without repeating yourself endless times in fixture classes using Alice by diving into this blog article.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top