celtric/fixtures

一个用于定义和加载静态固件的库

0.0.6 2016-09-20 12:58 UTC

This package is not auto-updated.

Last update: 2024-09-24 23:13:08 UTC


README

Build Status Latest Stable Version Latest Unstable Version License

介绍

这是一个小型库,旨在提供一个简单的方式来定义静态固件。默认情况下,它使用 YAML 文件和自定义格式(受 Alice 格式启发,该格式也部分支持使用此包中包含的 AliceStyleParser),但试图提供多个扩展点,以便任何开发者都可以轻松自定义。

这个库不是什么

此包的目的是 不是 提供动态固件(通过假数据生成)。可以添加对它的支持,但鼓励您查看 Alice 或其他固件生成器。

一个示例

我们将假设我们有以下类定义

<?php

namespace Foo\Bar;

class Person
{
    private $name;
    private $age;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }
}

以及以下固件文件: fixtures/people.yml

root_type: Foo\Bar\Person

ricard:
    name: Ricard
    age: 30

laura:
    name: Laura
    age: 29

现在我们准备好如下使用这个库

<?php

namespace Tests;

class FooTest extends \PHPUnit_Framework_TestCase
{
    /** @test */
    public function can_load_fixtures()
    {
        $this->assertEquals(new Person("Ricard", 30), $this->fixture("people.ricard"));
    }
    
    /**
     * @param string $fixtureName
     * @return mixed
     */
    private function fixture($fixtureName)
    {
        $fixtures = Fixtures::celtricStyle(__DIR__ . "/../fixtures/");
        
        return $fixtures->fixture($fixtureName);
    }
}

一个更复杂的示例

现在我们有一个稍微复杂的类定义

<?php

namespace Foo\Bar;

class Money
{
    private $amount;
    private $currency;

    public function __construct($amount, Currency $currency)
    {
        $this->amount = $amount;
        $this->currency = $currency;
    }
}

class Currency
{
    private $isoCode;

    public function __construct($isoCode)
    {
        $this->isoCode = $isoCode;
    }
}

为了定义它的固件,我们可以使用以下示例: fixtures/money.yml

root_type: Foo\Bar\Money

# We can inline the currency definition
two_euro:
    amount: 200
    currency<Foo\Bar\Currency>:
        isoCode: EUR

# Or we can reference it
three_euro:
    amount: 300
    currency: "@money.euro"

euro<Foo\Bar\Currency>:
    isoCode: EUR

开发状态

此库目前处于测试阶段。任何反馈都将受到欢迎,并鼓励您通过问题或 PR 提供反馈。