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

Commit

Permalink
Merge pull request #7 from omissis/feature/test-doubles-after-data-pr…
Browse files Browse the repository at this point in the history
…ovider-arguments

Test doubles injected after data provider arguments
  • Loading branch information
Norbert Orzechowicz committed Jul 14, 2015
2 parents 857848f + b8b98e7 commit 3114035
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
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"

2 changes: 1 addition & 1 deletion spec/Coduo/PhpSpec/DataProvider/Annotation/ParserSpec.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace spec\Coduo\PhpSpec\Annotation\DataProvider;
namespace spec\Coduo\PhpSpec\DataProvider\Annotation;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
Expand Down
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 3114035

Please sign in to comment.