lapaz / quick-brown-fox
ORM独立的关系型数据库测试数据生成器
0.3.1
2023-12-20 14:40 UTC
Requires
- php: ^7.4 || ^8.0
- ext-pdo: *
- doctrine/dbal: ^2.13.3 || ^3.1.4
- fakerphp/faker: ^1.21
Requires (Dev)
- phpunit/phpunit: ^9.5.5 || ^10.0
README
ORM独立的关系型数据库测试数据生成器。
用法
在为测试构建引导环境时定义一些通用规则
// $container points some application scope service locator $container->set(\Lapaz\QuickBrownFox\Database\SessionManager::class, function() use ($container) { $fixtures = new \Lapaz\QuickBrownFox\FixtureManager(); $fixtures->table('authors', function ($td) { $td->fixture('GoF')->define([ [ 'name' => "Erich Gamma" ], [ 'name' => "Richard Helm" ], [ 'name' => "Ralph Johnson" ], [ 'name' => "John Vlissides" ], ]); }); $fixtures->table('books', function ($td) { $td->generator('DesignPattern-N')->define(function($i) { return [ 'title' => 'Design Pattern ' . ($i + 1), 'code' => sprintf('000-000-%03d', $i), // not needed: 'author_id' => ? ]; }); }); // 'database' must be DBAL connection or PDO return $fixtures->createSessionManager($container->get('database')); });
准备新的测试会话
use PHPUnit\Framework\TestCase; class BookRepositoryTest extends TestCase { protected $session; protected function setUp() { /** @var ContainerInterface $container */ $this->session = $container->get(\Lapaz\QuickBrownFox\Database\SessionManager::class)->newSession(); } }
使用预定义的测试数据和生成器添加测试
public function testFindBook() { $this->session->into('authors')->load('GoF'); $this->session->into('books')->with('DesignPattern-N')->generate(10); $books = (new BookRepository($this->connection))->findAll(); $this->assertCount(10, $books); $this->assertEquals("Design Pattern 1", $book[0]->getTitle()); $this->assertNotNull($book[0]->getAuthor()); }
注意:如果不存在,则非空外键约束将随机解决。
您可以使用内联测试数据而不是预定义的测试数据
$this->session->into('authors')->load([ [ 'name' => "Erich Gamma" ], [ 'name' => "Richard Helm" ], [ 'name' => "Ralph Johnson" ], [ 'name' => "John Vlissides" ], ]);
以及内联生成器
$this->session->into('books')->with(function($i) { return [ 'title' => 'Design Pattern ' . ($i + 1), 'code' => sprintf('000-000-%03d', $i), ]; })->generate(10);
可以为数组形式的测试数据设置常见属性。
$this->session->into('authors')->with([ 'specialist' => true, 'rating' => function($i) { return 80 + $i * 5; }, ])->load([ [ 'name' => "Erich Gamma" ], [ 'name' => "Richard Helm" ], [ 'name' => "Ralph Johnson" ], [ 'name' => "John Vlissides" ], ]);
您可以在会话中重复添加额外数据。当下一个会话使用相同表时,这些数据将自动删除。
$this->session->into('authors')->with([ 'specialist' => false, 'rating' => function() { return mt_rand(30, 50); }, ])->generate(6)); $this->session->into('authors')->with([ 'specialist' => false, 'rating' => function() { return mt_rand(30, 50); }, ])->generate(6));
属性按从右到左的顺序覆盖。被覆盖的函数不会被评估,因此不会调用不必要的慢速计算。
$this->session->into('books')->with('DesignPattern-N')->with([ 'title' => function($i) { return 'Design Pattern -2nd Edition- ' . ($i + 1); }, ])->generate(10);
如果您不关心每个属性,可以这样做
$this->session->into('books')->generate(100);
但是,当可能存在一些属性约束时,您可以定义表级别的生成器规则
$fixtures->table('books', function ($td) { $td->defaults([ 'type' => function() { return mt_rand(1, 3); // books.type must be 1, 2 or 3 } ]); });