afurculita / phpspec-data-provider-extension
PhpSpec extension that allows you to create data providers for examples in specs
Installs: 127
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 7
pkg:composer/afurculita/phpspec-data-provider-extension
Requires
- php: >=5.4.0
- phpspec/phpspec: ^2
Requires (Dev)
- behat/behat: ~3.0
- bossa/phpspec2-expect: ^1
- symfony/console: ~2.3.2|~3.0
- symfony/filesystem: ~2.3|~3.0
- symfony/process: ~2.3|~3.0
This package is not auto-updated.
Last update: 2016-06-07 21:15:15 UTC
README
This extension allows you to create data providers for examples in specs.
Installation
composer require coduo/phpspec-data-provider-extension
Usage
Enable extension in phpspec.yml file
extensions:
  - Coduo\PhpSpec\DataProvider\DataProviderExtension
Write a spec:
<?php
namespace spec\Coduo\ToString;
use PhpSpec\ObjectBehavior;
class StringSpec 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)')
        );
    }
}
Write class for spec:
<?php
namespace Coduo\ToString;
class String
{
    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;
        }
    }
}
Run php spec
$ console bin/phpspec run -f pretty
You should get following output:
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