gos/fixture

此包已被弃用且不再维护。未建议替代包。

提供处理简单样例数据的解决方案

dev-master / 1.0.x-dev 2014-10-01 15:40 UTC

This package is auto-updated.

Last update: 2020-08-17 22:12:13 UTC


README

#Gos Fixtures 组件#

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight

该项目目前正在开发中,请小心使用。

创建样例可能很痛苦,尤其是当你为小型实体编写时。你会尽可能快地创建它们并赋予特定值。

以下是从 doctrine-data-fixture 的一个示例。

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;

class LoadUserRoleData extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
        $adminRole = new Role();
        $adminRole->setName('admin');

        $anonymousRole = new Role;
        $anonymousRole->setName('anonymous');

        $manager->persist($adminRole);
        $manager->persist($anonymousRole);
        $manager->flush();

        // store reference to admin role for User relation to Role
        $this->addReference('admin-role', $adminRole);
    }
}

如果你讨厌将数据存储在 PHP 数组中,并且不想花时间创建专用组件,那么这个就是为你准备的。将它们存储在 YAML 文件中,并轻松获取你的数据!

如何使用

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;

class LoadUserRoleData extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
    	//Instanciate the fixture component with the path of folder who contains our YAML data fixture
        //File are retrieve via Symfony Finder Component, so your do path/*/*/folder and all think who is
        //interpreted by the finder
        $fixture = new Fixture('path/to/my/fixture/folder', $this);

        //If you want split / order your fixture you can also add dynamically some folder
        $this->fixture->addDirectory('path/to/my/fixture/another_folder');

        //Now load your specific file
        $fixture->load('myDataFixture.yml');

        //And now you can fetch your data
        foreach($this->fixture->fetch() as $data){
        	//attach to your entity with PropertyAccess or by the hand.
        }

    }
}

以下是一个 YAML 数据的示例

database:
    name:
        - 'fr'
        - 'en'
        - 'it'
        - 'es'
    locale:
        - 'fr_FR'
        - 'en_US'
        - 'it_IT'
        - 'es_ES'
    status:
        - 'active'
        - 'active'
        - 'active'
        - 'inactive'
    default:
        - true

####处理一对多与 ArrayCollection####

collection:
    scope: [ "roles" ]
database:
	username:
    	- 'alice'
        - 'bob'
        - 'peter'
    roles: #this is a one to Many
        - 'client'
        - 'editor'
        - 'admin'

####检索引用####

database:
	user:
    	- &alice

路线图

实际上,此组件并不涵盖你可能会遇到的所有功能。你不能从 YAML 创建引用,集合也尚未完全支持,目前它们只是将数组转换为 ArrayCollection,因为我们尚未遇到这种情况,但我们会,所以这是我们的计划。

[] 直接从 YAML 生成引用 [] 完全支持集合

具体示例

public function load(ObjectManager $manager)
{
    $fixture = new Fixture('src/*/*/DataFixtures/YML/');
    $this->localeManager = $this->container->get('gos.i18n_bundle.locale_entity.manager');

    $this->fixture->load('LocaleData.yml', $this);

    foreach ($this->fixture->fetch() as $data) {
        $locale = $this->localeManager->create($data);
        $this->setReference($locale->getName(), $locale);
    }

    $this->localeManager->save();
}

你也可以看到

运行测试

需要 PHPUnit 3.5 或更高版本以及 Mock_Object 包。要设置和运行测试,请按照以下步骤操作

  • 转到项目的根目录
  • 运行:composer install --dev
  • 运行:phpunit

许可证

该项目受 MIT 许可证保护,有关更多信息,请参阅项目中的 LICENSE 文件。