Blueprint Bundle
The bundle provides a way to manage test data for the Doctrine ORM., (*1)
Installation
Add this lines to your composer.json:, (*2)
{
"require": {
"dakatsuka/blueprint-bundle": "1.1.0"
},
}
And then execute:, (*3)
$ php composer.phar install
And import a BlueprintBundle to AppKernel.php:, (*4)
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Dakatsuka\BlueprintBundle\DakatsukaBlueprintBundle();
}
Usage
src/Acme/BlogBundle/Tests/Blueprints/post.php:, (*5)
namespace Acme\BlogBundle\Tests\Blueprints;
use Dakatsuka\BlueprintBundle\Blueprint;
Blueprint::register('post', 'Acme\BlogBundle\Entity\Post', function($post, $blueprint) {
$post->setTitle('Title'.$blueprint->sequence());
$post->setBody('BodyBodyBody');
});
src/Acme/BlogBundle/Tests/Blueprints/comment.php:, (*6)
namespace Acme\BlogBundle\Tests\Blueprints;
use Dakatsuka\BlueprintBundle\Blueprint;
Blueprint::register('comment', 'Acme\BlogBundle\Entity\Comment', function($comment, $blueprint) {
$comment->setPost($blueprint->create('post'));
$comment->setBody('CommentCommentComment');
});
How to use:, (*7)
static::$kernel = static::createKernel();
static::$kernel->boot();
static::$container = static::$kernel->getContainer();
$blueprint = static::$container->get('dakatsuka.blueprint');
$blueprint->loadFromDirectory(static::$kernel->getRootDir() . '/../src/Acme/BlogBundle/Tests/Blueprints');
$post = $blueprint->create('post');
$this->assertEquals('Title1', $post->getTitle());
$this->assertEquals('BodyBodyBody', $post->getBody());
$comment = $blueprint->create('comment');
$this->assertEquals('CommentCommentComment', $comment->getBody());
$this->assertEquals('Title2', $comment->getPost()->getTitle());
// optional
$comment2 = $blueprint->create('comment', array('post' => $post));
$this->assertSame($post, $comment2->getPost());
Tips
Nested blueprint (required cascade={"persist"} option):, (*8)
Blueprint::register('post', 'Acme\BlogBundle\Entity\Post', function($post, $blueprint) {
$post->setTitle('Title'.$blueprint->sequence());
$post->setBody('BodyBodyBody');
$post->getComments()->add($blueprint->build('comment', array('post' => $post));
$post->getComments()->add($blueprint->build('comment', array('post' => $post));
$post->getComments()->add($blueprint->build('comment', array('post' => $post));
});
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
)
- Commit your changes (
git commit -am 'Add some feature'
)
- Push to the branch (
git push origin my-new-feature
)
- Create new Pull Request
Test
$ make phpunit
$ make test
Copyright
Copyright (C) 2013 Dai Akatsuka, released under the MIT License., (*9)