Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

 

Dude Where's My Data?

Developer Chris Barrett from Little Giant shows you how to generate data to test and develop more efficiently through SilverStripe seeder.

Read post

Forms are painful. Testing large datasets is painful.

Submitting the same form 50 times is 50 times as painful. SilverStripe seeder can help you generate data to test and develop more efficiently.

A simple example
Let’s say that you've developed a new blog page with pagination and tags. How would you go about testing it? You would obviously create enough blog posts for the pagination to work. If you had 10 posts per page and wanted to test at least 5 pages, to catch any potential edge cases, then you would need 49-51 blog posts. So you go into the CMS or database and start creating...

Ha kidding! If you're like me, you're never going to go to the CMS and manually create 51 blog posts. You’re going to reduce the page length to 1 and create 2 blog posts to check that it looks like it works.

Now that you've tested pagination, proceed to create 2 tags and check that the correct tag will show the correct post.

At this point, you're either happy testing your pagination with a page length of 1 and 2 blog posts or you're looking for a better way to do it. Perhaps you can create a small script that will create the blog posts and tags, something along the lines of...

    function createTestData()
    {
        $tags = [];
        $tag1 = new BlogTag();
        $tag1->Title = 'Test Tag 1';
        $tag1->write();
        $tags[] = $tag1;

        $tag2 = new BlogTag();
        $tag2->Title = 'Test Tag 2';
        $tag2->write();
        $tags[] = $tag2;

        for ($i = 0; $i < 51; $i++) {
            $post = new BlogPost();
            $post->Title = "Test Blog Post {$i}";
            $post->Content = "Test Blog Content {$i}";
            $post->Tags()->add($tags[array_rand($tags)]);
            $post->write();
            $post->publish('Staging', 'Live');
        }
    }

Sweet, you've got your blog posts and you didn't have to submit a form 53 times. But you’ve just written 20 lines of code that you're going to throw away... and if there are multiple people working and testing this site, then they might all go and do the same thing, if at all.

A brave new (formless) world
Luckily, there’s a better way and it can be shared among team members easily.

Encapsulated in a simple module, silverstripe-seeder, we have a declarative approach to generating the data you need. Gone are the days of setting the page length to 1 and creating 2 pages! Gone are the days of slamming the keyboard with random keys to fill in form fields!

Awesome!
silverstripe-seeder allows you to declare how you want to generate the data via simple YAML configuration

Seeder:
  create:
    BlogTag:
      count: 10
    BlogPost:
      count: 51
      fields:
        Tags: 'random()'

Once you have declared the YAML (at Little Giant we would make the above only for development environments) you can run framework/sake seed flush=1 in the root of your project. The seeder will create 10 tags and 51 posts, each with a random tag attached. The post's title and content, and any other fields it has declared, will be automatically filled with appropriate test data.

And more awesome!
Extending this, you could create a seeder.yml file containing most, if not all of your site's data needs. For example:

---
Name: seeder
---

Seeder:
  create:
    HomePage:
      fields:
        URLSegment: home
        Content: '<p>Welcome to my new website, I haven't submitted a single form HUZZAH to set this up</p>''
      ...
    ContactPage: ...
    Blog: ...
---
Name: seeder-dev
Only:
  environment: 'dev'
---

Seeder:
  create:
    BlogTag:
      count: 10
    BlogPost:
      count: 51
      fields:
        Parent: object(BlogPage)
        Tags: random()

would create the home page, contact page, blog page and then, if you're in dev, some tags and posts. Commit that to your repo and go drink a coffee with all the time you just saved yourself (and your team).

How do I get it?
Go run composer install littlegiant/silverstripe-seeder in your project root now.

Features and bugs
What if it doesn't meet your needs? Great, let me know! 

Hopefully you'll also love seeder, and it will provide you an easy way to test your code!

About the author
Chris Barrett

Chris Barrett is a developer at Little Giant - a digitally led agency based in Auckland, New Zealand.

Post your comment

Comments

  • Hi Chris,

    thanks for this article. I'm a bit confused. Composer should be used be running 'composer require littlegiant/silverstripe-seeder'. (require instead of install) Or am i wrong?

    Posted by Thomas Apfelbacher, 05/12/2015 4:29am (4 years ago)

RSS feed for comments on this page | RSS feed for all comments

Like what you have read?

Sign up for our weekly blog digest sent to your inbox.

Subscribe