diff --git a/argumentsExt/Argument/ArgumentOrganiser.php b/argumentsExt/Argument/ArgumentOrganiser.php index 9b5f94a..34aba7e 100644 --- a/argumentsExt/Argument/ArgumentOrganiser.php +++ b/argumentsExt/Argument/ArgumentOrganiser.php @@ -63,12 +63,13 @@ public function organiseArguments(ReflectionFunctionAbstract $function, array $m foreach ($annotations as $annotation) { if ($annotation instanceof StepInjectorArgument && - in_array($annotation->getArgument(), $paramsKeys) + in_array($argument = $annotation->getArgument(), $paramsKeys) ) { /* @var StepInjectorArgument $annotation */ foreach ($this->stepArgumentHolders as $hooker) { if ($hooker->doesHandleStepArgument($annotation)) { - $match[$annotation->getArgument()] + + $match[$argument] = $match[strval(++$i)] = $hooker->getStepArgumentValueFor($annotation) ; diff --git a/argumentsExt/Resolver/ArgumentsResolver.php b/argumentsExt/Resolver/ArgumentsResolver.php index 94217c3..994f896 100644 --- a/argumentsExt/Resolver/ArgumentsResolver.php +++ b/argumentsExt/Resolver/ArgumentsResolver.php @@ -49,7 +49,7 @@ public function __construct($stepArgumentHolders, Reader $reader) */ public function resolve(\ReflectionMethod $function, array $arguments) { - // No `@StepArgumentInjectorArgument` annotation found + // No `@StepInjectorArgument` annotation found if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) { return $arguments; } @@ -62,12 +62,12 @@ public function resolve(\ReflectionMethod $function, array $arguments) $annotations = $this->reader->getMethodAnnotations($function); foreach ($annotations as $annotation) { if ($annotation instanceof StepInjectorArgument && - in_array($annotation->getArgument(), $paramsKeys) + in_array($argument = $annotation->getArgument(), $paramsKeys) ) { /* @var StepArgumentInjectorArgument $annotation */ foreach ($this->stepArgumentHolders as $hooker) { if ($hooker->doesHandleStepArgument($annotation)) { - $arguments[$annotation->getArgument()] = $hooker->getStepArgumentValueFor($annotation); + $arguments[$argument] = $hooker->getStepArgumentValueFor($annotation); } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a62fc3b..b9de83c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,5 +14,8 @@ tests + + testsSAI + diff --git a/testsSAI/Annotation/ScenarioStateArgumentTest.php b/testsSAI/Annotation/ScenarioStateArgumentTest.php deleted file mode 100644 index 8ce684a..0000000 --- a/testsSAI/Annotation/ScenarioStateArgumentTest.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Gorghoa\ScenarioStateBehatExtension\Annotation; - -/** - * @author Vincent Chalamon - */ -class ScenarioStateArgumentTest extends \PHPUnit_Framework_TestCase -{ - /** - * @dataProvider getArguments - * - * @param array $arguments - * @param string $name - * @param string $argument - */ - public function testWithValue(array $arguments, $name, $argument) - { - $annotation = new ScenarioStateArgument($arguments); - $this->assertEquals($name, $annotation->name); - $this->assertEquals($argument, $annotation->argument); - } - - /** - * @return array - */ - public function getArguments() - { - return [ - [ - ['value' => 'foo'], - 'foo', - 'foo', - ], - [ - ['name' => 'foo'], - 'foo', - 'foo', - ], - [ - ['name' => 'foo', 'argument' => 'bar'], - 'foo', - 'bar', - ], - ]; - } -} diff --git a/testsSAI/Argument/ArgumentOrganiserTest.php b/testsSAI/Argument/ArgumentOrganiserTest.php new file mode 100644 index 0000000..fc76a7f --- /dev/null +++ b/testsSAI/Argument/ArgumentOrganiserTest.php @@ -0,0 +1,115 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gorghoa\StepArgumentInjectorBehatExtension\Argument; + +use Doctrine\Common\Annotations\Reader; +use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; +use Prophecy\Prophecy\ObjectProphecy; +use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser; + +/** + * @author Vincent Chalamon + * @author Rodrigue Villetard + */ +class ArgumentOrganiserTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ArgumentOrganiser + */ + private $organiser; + + /** + * @var ObjectProphecy|ArgumentOrganiser + */ + private $organiserMock; + + /** + * @var ObjectProphecy + */ + private $initializerMock; + + /** + * @var ObjectProphecy|\ReflectionMethod + */ + private $functionMock; + + /** + * @var ObjectProphecy|Reader + */ + private $readerMock; + + /** + * @var StepArgumentHolder + */ + private $holderMock; + + protected function setUp() + { + $this->organiserMock = $this->prophesize(BehatArgumentOrganiser::class); + $this->functionMock = $this->prophesize(\ReflectionMethod::class); + $this->readerMock = $this->prophesize(Reader::class); + $this->holderMock = $this->prophesize(StepArgumentHolder::class); + + $this->organiser = new ArgumentOrganiser( + $this->organiserMock->reveal(), + [$this->holderMock->reveal()], + $this->readerMock->reveal() + ); + } + + /** + * @return ObjectProphecy + */ + private function annotationMockFactory() + { + return $this->prophesize(StepInjectorArgument::class); + } + + public function testOrganiseArguments() + { + $this->functionMock->getParameters()->willReturn([ + (object) ['name' => 'scenarioBanana'], // argument with injector annotation and **a service hold** value + (object) ['name' => 'gorilla'], // argument with injector annotation but **no service hold** value + (object) ['name' => 'foo'], // argument not handled by this extension + ])->shouldBeCalledTimes(1); + + $annot1 = $this->annotationMockFactory(); + $annot1->getArgument()->willReturn('scenarioBanana')->shouldBeCalledTimes(1); + $annot1->reveal(); + + $annot2 = $this->annotationMockFactory(); + $annot2->getArgument()->willReturn('gorilla')->shouldBeCalledTimes(1); + $annot2->reveal(); + + $this->readerMock->getMethodAnnotations($this->functionMock->reveal())->willReturn([ + $annot1, + $annot2, + ])->shouldBeCalledTimes(1); + + $this->holderMock->doesHandleStepArgument($annot1)->willReturn(true); + $this->holderMock->doesHandleStepArgument($annot2)->willReturn(false); + + $this->holderMock->getStepArgumentValueFor($annot1)->willReturn('yammyBanana')->shouldBeCalledTimes(1); + $this->holderMock->getStepArgumentValueFor($annot2)->shouldNotBeCalled(); + + $this->holderMock->getStepArgumentValueFor($annot2); + + $this->organiserMock->organiseArguments($this->functionMock->reveal(), [ + 0 => 'scenarioBanana', + 1 => 'gorilla', + 'scenarioBanana' => 'yammyBanana', + 2 => 'yammyBanana', + ])->shouldBeCalledTimes(1); + + $this->organiser->organiseArguments($this->functionMock->reveal(), ['scenarioBanana', 'gorilla']); + } +} diff --git a/testsSAI/Argument/ScenarioStateArgumentOrganiserTest.php b/testsSAI/Argument/ScenarioStateArgumentOrganiserTest.php deleted file mode 100644 index 575a245..0000000 --- a/testsSAI/Argument/ScenarioStateArgumentOrganiserTest.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Gorghoa\ScenarioStateBehatExtension\Argument; - -use Behat\Testwork\Argument\ArgumentOrganiser; -use Doctrine\Common\Annotations\Reader; -use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; -use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; -use Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface; -use Prophecy\Prophecy\ObjectProphecy; - -/** - * @author Vincent Chalamon - */ -class ArgumentOrganiserTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var ArgumentOrganiser - */ - private $organiser; - - /** - * @var ObjectProphecy|ArgumentOrganiser - */ - private $organiserMock; - - /** - * @var ObjectProphecy|ScenarioStateInitializer - */ - private $initializerMock; - - /** - * @var ObjectProphecy|ScenarioStateInterface - */ - private $storeMock; - - /** - * @var ObjectProphecy|\ReflectionMethod - */ - private $functionMock; - - /** - * @var ObjectProphecy|Reader - */ - private $readerMock; - - /** - * @var ObjectProphecy|ScenarioStateArgument - */ - private $annotationMock; - - protected function setUp() - { - $this->organiserMock = $this->prophesize(ArgumentOrganiser::class); - $this->initializerMock = $this->prophesize(ScenarioStateInitializer::class); - $this->storeMock = $this->prophesize(ScenarioStateInterface::class); - $this->functionMock = $this->prophesize(\ReflectionMethod::class); - $this->readerMock = $this->prophesize(Reader::class); - $this->annotationMock = $this->prophesize(ScenarioStateArgument::class); - - $this->organiser = new ArgumentOrganiser( - $this->organiserMock->reveal(), - $this->initializerMock->reveal(), - $this->readerMock->reveal() - ); - } - - public function testOrganiseArguments() - { - $this->functionMock->getParameters()->willReturn([ - (object) ['name' => 'scenarioBanana'], - (object) ['name' => 'gorilla'], - (object) ['name' => 'foo'], - ])->shouldBeCalledTimes(1); - - $this->initializerMock->getStore()->willReturn($this->storeMock->reveal())->shouldBeCalledTimes(1); - $this->readerMock->getMethodAnnotations($this->functionMock->reveal())->willReturn([ - $this->annotationMock->reveal(), - $this->annotationMock->reveal(), - ])->shouldBeCalledTimes(1); - $this->annotationMock->getArgument() - ->willReturn('scenarioBanana', 'scenarioBanana', 'gorilla', 'gorilla') - ->shouldBeCalled(); - $this->annotationMock->getName() - ->willReturn('scenarioBanana', 'scenarioBanana', 'scenarioBanana', 'scenarioGorilla', 'scenarioGorilla', 'scenarioGorilla') - ->shouldBeCalled(); - $this->storeMock->hasStateFragment('scenarioBanana')->willReturn(true)->shouldBeCalledTimes(1); - $this->storeMock->hasStateFragment('scenarioGorilla')->willReturn(true)->shouldBeCalledTimes(1); - $this->storeMock->hasStateFragment('foo')->shouldNotBeCalled(); - $this->storeMock->getStateFragment('scenarioBanana')->willReturn('Yummy banana!')->shouldBeCalledTimes(2); - $this->storeMock->getStateFragment('scenarioGorilla')->willReturn('Bonobo')->shouldBeCalledTimes(2); - $this->storeMock->getStateFragment('foo')->shouldNotBeCalled(); - - $this->organiserMock->organiseArguments($this->functionMock->reveal(), [ - 0 => 'scenarioBanana', - 1 => 'gorilla', - 'scenarioBanana' => 'Yummy banana!', - 2 => 'Yummy banana!', - 'gorilla' => 'Bonobo', - 3 => 'Bonobo', - ])->shouldBeCalledTimes(1); - - $this->organiser->organiseArguments($this->functionMock->reveal(), ['scenarioBanana', 'gorilla']); - } -} diff --git a/testsSAI/Resolver/ArgumentsResolverTest.php b/testsSAI/Resolver/ArgumentsResolverTest.php index 3badd3f..82e1443 100644 --- a/testsSAI/Resolver/ArgumentsResolverTest.php +++ b/testsSAI/Resolver/ArgumentsResolverTest.php @@ -1,7 +1,7 @@ * @@ -9,38 +9,42 @@ * file that was distributed with this source code. */ -namespace Gorghoa\ScenarioStateBehatExtension\Resolver; +namespace Gorghoa\StepArgumentInjectorBehatExtension\Resolver; use Doctrine\Common\Annotations\Reader; -use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; -use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; -use Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface; +use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; +use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder; /** * @author Vincent Chalamon + * @author Rodrigue Villetard */ class ArgumentsResolverTest extends \PHPUnit_Framework_TestCase { public function testResolve() { - $initializerMock = $this->prophesize(ScenarioStateInitializer::class); - $storeMock = $this->prophesize(ScenarioStateInterface::class); $readerMock = $this->prophesize(Reader::class); $functionMock = $this->prophesize(\ReflectionMethod::class); $parameterMock = $this->prophesize(\ReflectionParameter::class); - $annotationMock = $this->prophesize(ScenarioStateArgument::class); + $annotationMock = $this->prophesize(StepInjectorArgument::class); $functionMock->getParameters()->willReturn([$parameterMock, $parameterMock])->shouldBeCalledTimes(2); $parameterMock->getName()->willReturn('lorem', 'foo', 'lorem', 'foo')->shouldBeCalledTimes(4); - $initializerMock->getStore()->willReturn($storeMock)->shouldBeCalledTimes(1); - $readerMock->getMethodAnnotation($functionMock, ScenarioStateArgument::class)->willReturn($annotationMock)->shouldBeCalledTimes(1); + + $holderMock1 = $this->prophesize(StepArgumentHolder::class); + $holderMock2 = $this->prophesize(StepArgumentHolder::class); + + $readerMock->getMethodAnnotation($functionMock, StepInjectorArgument::class)->willReturn($annotationMock)->shouldBeCalledTimes(1); $readerMock->getMethodAnnotations($functionMock)->willReturn([$this->prophesize(\stdClass::class), $annotationMock])->shouldBeCalledTimes(1); - $annotationMock->getArgument()->willReturn('lorem')->shouldBeCalledTimes(2); - $annotationMock->getName()->willReturn('ipsum')->shouldBeCalledTimes(2); - $storeMock->hasStateFragment('ipsum')->willReturn(true)->shouldBeCalledTimes(1); - $storeMock->getStateFragment('ipsum')->willReturn('pouet')->shouldBeCalledTimes(1); + $annotationMock->getArgument()->willReturn('lorem')->shouldBeCalledTimes(1); + + $holderMock1->doesHandleStepArgument($annotationMock)->willReturn(true)->shouldBeCalledTimes(1); + $holderMock1->getStepArgumentValueFor($annotationMock)->willReturn(true)->shouldBeCalledTimes(1); + + $holderMock2->doesHandleStepArgument($annotationMock)->willReturn(false)->shouldBeCalledTimes(1); + $holderMock2->getStepArgumentValueFor($annotationMock)->shouldNotBeCalled(); - $resolver = new ArgumentsResolver($initializerMock->reveal(), $readerMock->reveal()); + $resolver = new ArgumentsResolver([$holderMock1->reveal(), $holderMock2->reveal()], $readerMock->reveal()); $resolver->resolve($functionMock->reveal(), ['lorem' => 'pouet', 'foo' => 'bar']); } }