Skip to content
This repository has been archived by the owner on Feb 22, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed May 7, 2014
0 parents commit d23978f
Show file tree
Hide file tree
Showing 14 changed files with 752 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
bin/
composer.lock

14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

php:
- 5.3
- 5.4
- 5.5

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar update

script:
- bin/phpspec r -v
- bin/behat -v
22 changes: 22 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Michal Dabrowski, Norbert Orzechowicz

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#PhpSpec data provider extension

This extension allows you to create data providers for examples in specs.

> At the moment this extension is just a proof of concept, use it at own risk.
## Installation

```
require: {
"coduo/phpspec-data-provider-extension": "dev-master"
}
```

## Usage

Enable exntesion in phpspec.yml file

```
extensions:
- Coduo\PhpSpec\DataProviderExtension
```

Write a spec:

```php
<?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
<?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
```
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "coduo/phpspec-data-provider-extension",
"type": "library",
"keywords": ["phpspec", "dataprovider", "extension", "coduo", "data", "provider"],
"license": "MIT",
"authors": [
{
"name": "Norbert Orzechowicz",
"email": "[email protected]"
},
{
"name": "Michał Dąbrowski",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"phpspec/phpspec": "2.0.*"
},
"require-dev": {
"behat/behat": "2.4.*@stable",
"symfony/filesystem": "~2.3",
"bossa/phpspec2-expect": "dev-master"
},
"autoload": {
"psr-0": { "": "src/"}
},
"config": {
"bin-dir": "bin"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
126 changes: 126 additions & 0 deletions features/bootstrap/Console/ApplicationTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;

/**
* A console application tester heavily inspired by/proudly stolen from \Symfony\Component\Console\Tester\ApplicationTester.
*/
class ApplicationTester
{
/**
* @var Application $application
*/
private $application;

/**
* @var StringInput $input
*/
private $input;

/**
* @var StreamOutput $output
*/
private $output;

/**
* @var resource $inputStream
*/
private $inputStream;

/**
* @var int
*/
private $result;

/**
* @param Application $application
*/
public function __construct(Application $application)
{
$this->application = $application;
}

/**
* @param string $input
*
* @return integer
*/
public function run($input)
{
$this->input = new StringInput($input);

$this->output = new StreamOutput(fopen('php://memory', 'w', false));

$inputStream = $this->getInputStream();
rewind($inputStream);
$this->application->getHelperSet()
->get('dialog')
->setInputStream($inputStream);

$this->result = $this->application->run($this->input, $this->output);
}

/**
* @param boolean
*
* @return string
*/
public function getDisplay($normalize = false)
{
rewind($this->output->getStream());

$display = stream_get_contents($this->output->getStream());

if ($normalize) {
$display = str_replace(PHP_EOL, "\n", $display);
}

return $display;
}

/**
* @return InputInterface
*/
public function getInput()
{
return $this->input;
}

/**
* @return OutputInterface
*/
public function getOutput()
{
return $this->output;
}

/**
* @param string $input
*/
public function putToInputStream($input)
{
fputs($this->getInputStream(), $input);
}

/**
* @return resource
*/
private function getInputStream()
{
if (null === $this->inputStream) {
$this->inputStream = fopen('php://memory', 'r+', false);
}

return $this->inputStream;
}

/**
* @return int
*/
public function getResult()
{
return $this->result;
}
}
20 changes: 20 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Behat\Behat\Context\BehatContext;

/**
* Features context.
*/
class FeatureContext extends BehatContext
{
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters)
{
$this->useContext('phpspec', new PHPSpecContext());
}
}
Loading

0 comments on commit d23978f

Please sign in to comment.