moave/phpspec-data-provider-extension

1.0.3 2015-10-27 14:27 UTC

This package is not auto-updated.

Last update: 2024-09-19 19:39:47 UTC


README

# PhpSpec 数据提供者扩展

Build Status

此扩展允许您为规范中的示例创建数据提供者。

安装

composer require coduo/phpspec-data-provider-extension

用法

在 phpspec.yml 文件中启用扩展

extensions:
  Coduo\PhpSpec\DataProvider\DataProviderExtension: ~

编写规范

<?php

namespace spec\Coduo\ToString;

use PhpSpec\ObjectBehavior;

class StringLibrarySpec extends ObjectBehavior
{
    /**
     *  @dataProvider positiveConversionExamples
     */
    function it_convert_input_value_into_string($inputValue, $expectedValue)
    {
        $this->beConstructedWith($inputValue);
        $this->__toString()->shouldReturn($expectedValue);
    }

    public function positiveConversionExamples()
    {
        return array(
            array(1, '1'),
            array(1.1, '1.1'),
            array(new \DateTime, '\DateTime'),
            array(array('foo', 'bar'), 'Array(2)')
        );
    }
}

为规范编写类

<?php

namespace Coduo\ToString;

class StringLibrary
{
    private $value;

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

    public function __toString()
    {
        $type = gettype($this->value);
        switch ($type) {
            case 'array':
                return sprintf('Array(%d)', count($this->value));
            case 'object':
                return sprintf("\\%s", get_class($this->value));
            default:
                return (string) $this->value;
        }
    }
}

运行 php spec

$ console bin/phpspec run -f pretty

您应该得到以下输出

Coduo\ToString\String

  12  ✔ convert input value into string
  12  ✔ 1) it convert input value into string
  12  ✔ 2) it convert input value into string
  12  ✔ 3) it convert input value into string
  12  ✔ 4) it convert input value into string


1 specs
5 examples (5 passed)
13ms