大空塾/蓝图捆绑包

该捆绑包提供了一种管理 Doctrine ORM 测试数据的方法

安装次数: 7,644

依赖者: 0

建议者: 0

安全: 0

星标: 4

关注者: 2

分支: 0

开放性问题: 0

类型:symfony-bundle

1.1.0 2013-12-06 18:10 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:00:59 UTC


README

该捆绑包提供了一种管理 Doctrine ORM 测试数据的方法。

安装

将以下行添加到您的 composer.json 文件中

{
    "require": {
        "dakatsuka/blueprint-bundle": "1.1.0"
    },
}

然后执行

$ php composer.phar install

并在 AppKernel.php 中导入 BlueprintBundle

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
    $bundles[] = new Dakatsuka\BlueprintBundle\DakatsukaBlueprintBundle();
}

使用方法

src/Acme/BlogBundle/Tests/Blueprints/post.php

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

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');
});

如何使用

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());

技巧

嵌套蓝图(需要 required cascade={"persist"} 选项)

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));
});

贡献

  1. 分支
  2. 创建您的功能分支(git checkout -b my-new-feature
  3. 提交您的更改(git commit -am '添加一些功能'
  4. 推送到分支(git push origin my-new-feature
  5. 创建新的 Pull Request

测试

$ make phpunit
$ make test

版权

版权(C)2013 大空塾,MIT 许可下发布。