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

Commit

Permalink
Implemented test doubles injection after data provider arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Jul 13, 2015
1 parent 23edd1a commit b8b98e7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
62 changes: 62 additions & 0 deletions features/use_data_providers_in_examples.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,65 @@ class String
And I should see "✔ 3) it convert input value into string"
And I should see "✔ 4) it convert input value into string"

Scenario: Positive match with Coduo matcher with trailing phpspec's test double arguments
Given the PhpSpecDataProviderExtension is enabled
When I write a spec "spec/Coduo/Date/DateRangeSpec.php" with following code
"""
<?php
namespace spec\Coduo\Date;
use PhpSpec\ObjectBehavior;
use DateTime;
class DateRangeSpec extends ObjectBehavior
{
/**
* @dataProvider positiveConversionExamples
*/
function it_returns_a_formatted_date_range($inputValue, DateTime $date)
{
$this->beConstructedWith($inputValue, $date);
$date->format('Ymd')->willReturn('2035-10-28');
$this->getFormattedRange()->shouldBeLike('1985-10-26 - 2035-10-28');
}
public function positiveConversionExamples()
{
return array(
array(new \DateTime('1985-10-26')),
);
}
}
"""
And I write a class "src/Coduo/Date/DateRange.php" with following code
"""
<?php
namespace Coduo\Date;
class DateRange
{
private $start;
private $end;
public function __construct($start, $end)
{
$this->start = $start;
$this->end = $end;
}
public function getFormattedRange()
{
return $this->start->format('Y-m-d') . ' - ' . $this->end->format('Ymd');
}
}
"""
And I run phpspec
Then it should pass
And I should see "✔ returns a formatted date range"
And I should see "✔ 1) it returns a formatted date range"

Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ public function prepare(ExampleNode $example, SpecificationInterface $context,
{
$exampleNum = $this->getExampleNumber($example->getTitle());
$providedData = $this->getDataFromProvider($example);

if (! array_key_exists($exampleNum, $providedData)) {
return ;
}

$data = $providedData[$exampleNum];

foreach ($example->getFunctionReflection()->getParameters() as $position => $parameter) {
if (!isset($data[$position])) {
continue;
}
$collaborators->set($parameter->getName(), $data[$position]);
}
}
Expand Down Expand Up @@ -85,7 +90,7 @@ private function haveValidDataProvider(ExampleNode $example)

$exampleParamsCount = count($example->getFunctionReflection()->getParameters());
foreach ($providedData as $dataRow) {
if (!is_array($dataRow) || count($dataRow) != $exampleParamsCount) {
if (!is_array($dataRow)) {
return false;
}
}
Expand Down

0 comments on commit b8b98e7

Please sign in to comment.