|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the StepArgumentInjectorBehatExtension project. |
| 5 | + * |
| 6 | + * (c) Rodrigue Villetard <rodrigue.villetard@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Gorghoa\StepArgumentInjectorBehatExtension\Argument; |
| 13 | + |
| 14 | +use Doctrine\Common\Annotations\Reader; |
| 15 | +use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; |
| 16 | +use Prophecy\Prophecy\ObjectProphecy; |
| 17 | +use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Vincent Chalamon <vincentchalamon@gmail.com> |
| 21 | + * @author Rodrigue Villetard <rodrigue.villetard@gmail.com> |
| 22 | + */ |
| 23 | +class ArgumentOrganiserTest extends \PHPUnit_Framework_TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ArgumentOrganiser |
| 27 | + */ |
| 28 | + private $organiser; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ObjectProphecy|ArgumentOrganiser |
| 32 | + */ |
| 33 | + private $organiserMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ObjectProphecy |
| 37 | + */ |
| 38 | + private $initializerMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var ObjectProphecy|\ReflectionMethod |
| 42 | + */ |
| 43 | + private $functionMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ObjectProphecy|Reader |
| 47 | + */ |
| 48 | + private $readerMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var StepArgumentHolder |
| 52 | + */ |
| 53 | + private $holderMock; |
| 54 | + |
| 55 | + protected function setUp() |
| 56 | + { |
| 57 | + $this->organiserMock = $this->prophesize(BehatArgumentOrganiser::class); |
| 58 | + $this->functionMock = $this->prophesize(\ReflectionMethod::class); |
| 59 | + $this->readerMock = $this->prophesize(Reader::class); |
| 60 | + $this->holderMock = $this->prophesize(StepArgumentHolder::class); |
| 61 | + |
| 62 | + $this->organiser = new ArgumentOrganiser( |
| 63 | + $this->organiserMock->reveal(), |
| 64 | + [$this->holderMock->reveal()], |
| 65 | + $this->readerMock->reveal() |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return ObjectProphecy |
| 71 | + */ |
| 72 | + private function annotationMockFactory() |
| 73 | + { |
| 74 | + return $this->prophesize(StepInjectorArgument::class); |
| 75 | + } |
| 76 | + |
| 77 | + public function testOrganiseArguments() |
| 78 | + { |
| 79 | + $this->functionMock->getParameters()->willReturn([ |
| 80 | + (object) ['name' => 'scenarioBanana'], // argument with injector annotation and **a service hold** value |
| 81 | + (object) ['name' => 'gorilla'], // argument with injector annotation but **no service hold** value |
| 82 | + (object) ['name' => 'foo'], // argument not handled by this extension |
| 83 | + ])->shouldBeCalledTimes(1); |
| 84 | + |
| 85 | + $annot1 = $this->annotationMockFactory(); |
| 86 | + $annot1->getArgument()->willReturn('scenarioBanana')->shouldBeCalledTimes(1); |
| 87 | + $annot1->reveal(); |
| 88 | + |
| 89 | + $annot2 = $this->annotationMockFactory(); |
| 90 | + $annot2->getArgument()->willReturn('gorilla')->shouldBeCalledTimes(1); |
| 91 | + $annot2->reveal(); |
| 92 | + |
| 93 | + $this->readerMock->getMethodAnnotations($this->functionMock->reveal())->willReturn([ |
| 94 | + $annot1, |
| 95 | + $annot2, |
| 96 | + ])->shouldBeCalledTimes(1); |
| 97 | + |
| 98 | + $this->holderMock->doesHandleStepArgument($annot1)->willReturn(true); |
| 99 | + $this->holderMock->doesHandleStepArgument($annot2)->willReturn(false); |
| 100 | + |
| 101 | + $this->holderMock->getStepArgumentValueFor($annot1)->willReturn('yammyBanana')->shouldBeCalledTimes(1); |
| 102 | + $this->holderMock->getStepArgumentValueFor($annot2)->shouldNotBeCalled(); |
| 103 | + |
| 104 | + $this->holderMock->getStepArgumentValueFor($annot2); |
| 105 | + |
| 106 | + $this->organiserMock->organiseArguments($this->functionMock->reveal(), [ |
| 107 | + 0 => 'scenarioBanana', |
| 108 | + 1 => 'gorilla', |
| 109 | + 'scenarioBanana' => 'yammyBanana', |
| 110 | + 2 => 'yammyBanana', |
| 111 | + ])->shouldBeCalledTimes(1); |
| 112 | + |
| 113 | + $this->organiser->organiseArguments($this->functionMock->reveal(), ['scenarioBanana', 'gorilla']); |
| 114 | + } |
| 115 | +} |
0 commit comments