dborsatto/phpspec-data-provider-extension

PHPSpec 7+ 数据提供程序扩展

1.1.0 2023-08-31 09:50 UTC

This package is auto-updated.

Last update: 2024-08-30 01:49:43 UTC


README

此扩展允许您为规范中的示例创建数据提供程序。它是从coduo/phpspec-data-provider-extension开始的系列中最后一个分支,该系列从madisoft/phpspec-data-provider-extension分支而来。

安装

composer require dborsatto/phpspec-data-provider-extension

用法

在您的 phpspec.yml 文件中启用扩展

extensions:
  DBorsatto\PhpSpec\DataProvider\DataProviderExtension: ~

编写规范

<?php

declare(strict_types=1);

namespace spec\DBorsatto\ToString;

use PhpSpec\ObjectBehavior;

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

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

编写您的规范的类

<?php

declare(strict_types=1);

namespace DBorsatto\ToString;

class StringLibrary
{
    private $value;

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

    public function __toString(): string
    {
        $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;
        }
    }
}

运行 phpspec

$ vendor/bin/phpspec run -f pretty

您应该得到以下输出

DBorsatto\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