gnugat / phpixture
此包已被弃用且不再维护。未建议替代包。
数组测试数据库(无框架,无数据库)
v0.3.1
2015-04-13 16:32 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- fabpot/php-cs-fixer: ~1.6
- phpspec/phpspec: ~2.2@beta
This package is not auto-updated.
Last update: 2022-02-01 12:46:26 UTC
README
通过构建数组创建测试数据
<?php require __DIR__.'/vendor/autoload.php'; $fixtures = array( 'tags' => array( 'news' => array( 'id' => 1, 'name' => 'news', 'articles' => array( '@introducing_phpixtures', '@phpixtures_v0_2_0', '@phpixtures_v0_3_0', ), ), ), 'articles' => array( 'introducing_phpixtures' => array( 'id' => 1, 'title' => 'Introducing Phpixture v0.1.0', 'content' => 'Yet another fixture library!', 'tag' => '@news', ), 'phpixtures_v0_2_0' => array( 'id' => 2, 'title' => 'Phpixture v0.2.0', 'content' => 'Added ToOne relationship management', 'tag' => '@news', ), 'phpixtures_v0_3_0' => array( 'id' => 3, 'title' => 'Phpixture v0.3.0', 'content' => 'Added ToMany relationship management', 'tag' => '@news', ), ), ); $repository = new Gnugat\Phpixture\Repository($fixtures); $articles = $repository->findAll('articles'); // array( // array( // 'id' => 1, // 'title' => 'Introducing Phpixture v0.1.0', // 'content' => 'Yet another fixture library!', // 'tag' => array( // 'id' => 1, // 'name' => 'news', // ), // ), // ... // ) $tags = $repository->findAll('tags'); // array( // 'news' => array( // 'id' => 1, // 'name' => 'news', // 'articles' => array( // array( // 'id' => 1, // 'title' => 'Introducing Phpixture v0.1.0', // 'content' => 'Yet another fixture library!', // ), // ... // ), // ), // )
安装
使用 Composer 在您的项目中安装此库
composer require gnugat/phpixture:~0.1.0@dev
常见问题解答
我们应该怎么读 Phpixture?
PHP Fixture.
为什么我应该用它而不是X?
没有原因,Phpixture 更多的是一个宠物项目/概念验证,而不是一个在生产中使用的库。但如果你发现它有用,我会很高兴!
我该如何将测试数据存储在PHP文件中?
比如说你这样存储
<?php // File: fixtures.php return $fixtures = array( 'articles' => array( 'introducing_phpixtures' => array( 'id' => 1, 'title' => 'Introducing Phpixture', 'content' => 'Yet another fixture library!', ), ), );
然后你可以这样获取它们
<?php require __DIR__.'/vendor/autoload.php'; $fixtures = include __DIR__.'/fixtures.php'; $repository = new Gnugat\Phpixture\Repository($fixtures);
我该如何将测试数据存储在YAML文件中?
比如说你这样存储
// File: fixtures.yaml
articles:
introducing_phpixtures:
id: 1
title: 'Introducing Phpixture'
content: 'Yet another fixture library!'
然后你可以这样获取它们
<?php require __DIR__.'/vendor/autoload.php'; $fixtures = Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml')); $repository = new Gnugat\Phpixture\Repository($fixtures);
注意:您需要安装 Symfony Yaml Component。
我该如何将测试数据存储在JSON文件中?
比如说你这样存储(文件:fixtures.json
)
{
'articles': {
'introducing_phpixtures': {
'id': 1,
'title': 'Introducing Phpixture',
'content': 'Yet another fixture library!'
}
}
}
然后你可以这样获取它们
<?php require __DIR__.'/vendor/autoload.php'; $fixtures = json_decode(file_get_contents(__DIR__.'/fixtures.json'), true); $repository = new Gnugat\Phpixture\Repository($fixtures);
我该如何将测试数据存储在XML文件中?
我相信你开始明白了。找到一个XML库,解析文件并转换成数组。
我该如何将测试数据存储在注释中?
嗯...我相信你会找到方法的(如果你做到了,请告诉我 :))。
我该如何从这些测试数据中创建对象?
遍历每个测试数据并实例化你的对象
<?php require __DIR__.'/vendor/autoload.php'; $fixtures = Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml')); $repository = new Gnugat\Phpixture\Repository($fixtures); $articles = array(); foreach ($repository->findAll('articles') as $articleFixture) { $article = new Article($articleFixture['title'], $articleFixture['content']); // Assuming Article#id is private, doesn't have setter and cannot be set from constructor $reflectedArticle = new ReflectionClass($article); $id = $reflectedArticle->getProperty('id'); $id->setAccessible(true); $id->setValue($articleFixture['id']); }
我该如何使用DoctrineDataFixtures创建实体?
嗯,和上面一样
use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Gnugat\Phpixture\Repository; use Symfony\Component\Yaml\Yaml; class LoadUserData implements FixtureInterface { public function load(ObjectManager $manager) { $fixtures = Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml')); $repository = new Repository($fixtures); foreach ($repository->findAll('articles') as $articleFixture) { $article = new Article($articleFixture['title'], $articleFixture['content']); // Assuming Article#id is private, doesn't have setter and cannot be set from constructor $reflectedArticle = new ReflectionClass($article); $id = $reflectedArticle->getProperty('id'); $id->setAccessible(true); $id->setValue($articleFixture['id']); $manager->persist($article); } $manager->flush(); } }
想要了解更多?
您可以使用以下方法查看当前和过去的版本:
git tag
命令- Github上的发行页面
- 列出版本之间变更的文件
最后是一些元文档