diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 0735d3b..ce65940 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -25,7 +25,7 @@ jobs: run: ./vendor/bin/codecept run --quiet --no-colors --no-interaction --coverage --coverage-xml - name: Run mutation tests - run: ./vendor/bin/infection --min-covered-msi=75 --threads=2 --no-progress --no-interaction --log-verbosity=none + run: ./vendor/bin/infection --min-covered-msi=70 --threads=2 --no-progress --no-interaction --log-verbosity=none env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} diff --git a/phpcs.xml.dist b/phpcs.xml.dist index c8041c4..80eeebc 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -42,4 +42,9 @@ */tests/* + + + + + diff --git a/src/GherkinParam.php b/src/GherkinParam.php index 5133899..1d4d387 100644 --- a/src/GherkinParam.php +++ b/src/GherkinParam.php @@ -10,7 +10,7 @@ * @category Test * @package GherkinParam * @author Gregory Heitz - * @license https://git.io/Juy0k Apache Licence + * @license https://git.io/Juy0k Apache License * @link https://packagist.org/packages/edno/codeception-gherkin-param */ @@ -33,6 +33,7 @@ use \Codeception\Step; use \Codeception\Lib\ModuleContainer; use \Codeception\Extension\GherkinParamException; +use \Codeception\Exception\Warning; /** * GherkinParam extension main class @@ -84,7 +85,7 @@ class GherkinParam extends \Codeception\Module * * @var boolean * true: if parameter invalid then replacement value will be null - * false: default behaviour, ie replacement value is parameter {{name}} + * false: default behavior, ie replacement value is parameter {{name}} */ private bool $_nullable = false; @@ -138,7 +139,7 @@ final public function onReconfigure(): void * * @SuppressWarnings(PHPMD.StaticAccess) */ - final protected function getValueFromParam(string $param) + protected function getValueFromParam(string $param) { $variable = null; @@ -216,7 +217,7 @@ final protected function getValueFromParam(string $param) * * @return mixed Returns value if exists, else parameter {{name}} */ - final protected function mapParametersToValues( + protected function mapParametersToValues( array $matches, array $values, string $param @@ -248,7 +249,7 @@ final protected function mapParametersToValues( * * @return mixed Returns parameter's value if exists, else null */ - final protected function getValueFromConfigParam( + protected function getValueFromConfigParam( string $param ) { $value = null; @@ -279,11 +280,11 @@ final protected function getValueFromConfigParam( * * @return mixed Returns parameter's value if exists, else null */ - final protected function getValueFromArrayParam(string $param) + protected function getValueFromArrayParam(string $param) { try { return $this->getValueFromArray($param); - } catch (RuntimeException | TypeError $exception) { + } catch (Warning | RuntimeException | TypeError $exception) { if ($this->_throwException) { throw new GherkinParamException(); } @@ -302,7 +303,7 @@ final protected function getValueFromArrayParam(string $param) * * @SuppressWarnings(PHPMD.StaticAccess) */ - final protected function getValueFromFixture(string $param) + protected function getValueFromFixture(string $param) { try { return Fixtures::get($param); @@ -325,7 +326,7 @@ final protected function getValueFromFixture(string $param) * * @SuppressWarnings(PHPMD.StaticAccess) */ - final protected function getValueFromArray(string $param) + protected function getValueFromArray(string $param) { $value = null; @@ -341,15 +342,15 @@ final protected function getValueFromArray(string $param) /** * Parse a table node by mapping its parameters * - * @param \Behat\Gherkin\Node\TableNode $tablenode table node + * @param \Behat\Gherkin\Node\TableNode $tableNode table node * * @return \Behat\Gherkin\Node\TableNode Returns valued table node */ - final protected function parseTableNode(TableNode $tablenode) + protected function parseTableNode(TableNode $tableNode) { - $prop = new ReflectionProperty(get_class($tablenode), 'table'); + $prop = new ReflectionProperty(get_class($tableNode), 'table'); $prop->setAccessible(true); - $table = $prop->getValue($tablenode); + $table = $prop->getValue($tableNode); foreach ($table as $i => $row) { foreach ($row as $j => $cell) { $val = $this->getValueFromParam($cell); @@ -357,10 +358,10 @@ final protected function parseTableNode(TableNode $tablenode) $table[$i][$j] = $val; } } - $prop->setValue($tablenode, $table); + $prop->setValue($tableNode, $table); $prop->setAccessible(false); - return $tablenode; + return $tableNode; } /** diff --git a/tests/unit/GherkinParamExceptionTest.php b/tests/unit/GherkinParamExceptionTest.php index f291f34..270c4d4 100644 --- a/tests/unit/GherkinParamExceptionTest.php +++ b/tests/unit/GherkinParamExceptionTest.php @@ -28,9 +28,14 @@ protected function _before(): void $this->module = Mockery::mock($moduleInstance) ->shouldAllowMockingProtectedMethods() ->makePartial(); + } - $this->module = Mockery::spy($moduleInstance) - ->shouldAllowMockingProtectedMethods(); + /** + * @SuppressWarnings(PHPMD.StaticAccess) + */ + protected function _after(): void + { + Mockery::close(); } public function testGetValueFromParamWithExceptionFromConfig(): void @@ -75,7 +80,7 @@ public function testMapParametersToValuesWithExceptionOnIsArray(): void ->mapParametersToValues( [0,1,2,3,4], [[0],[1],[2],[3],[4]], - "test" + 'test' ); } ); @@ -90,9 +95,31 @@ public function testMapParametersToValuesWithExceptionOnIsSet(): void ->mapParametersToValues( [0,1,2,3,4], [], - "test" + 'test' ); } ); + } + + public function testGetValueFromFixtureWithExceptionOnIsSet(): void + { + $this->assertThrows( + GherkinParamException::class, function () { + $this + ->module + ->getValueFromFixture('{{test}}'); + } + ); + } + + public function testGetValueFromArrayParamWithExceptionOnIsSet(): void + { + $this->assertThrows( + GherkinParamException::class, function () { + $this + ->module + ->getValueFromArrayParam('{{test[1]}}'); + } + ); } } diff --git a/tests/unit/GherkinParamNullableTest.php b/tests/unit/GherkinParamNullableTest.php index f1b4b48..1adaef2 100644 --- a/tests/unit/GherkinParamNullableTest.php +++ b/tests/unit/GherkinParamNullableTest.php @@ -27,6 +27,14 @@ protected function _before(): void ->shouldAllowMockingProtectedMethods(); } + /** + * @SuppressWarnings(PHPMD.StaticAccess) + */ + protected function _after(): void + { + Mockery::close(); + } + public function testMapParametersToValuesWithExceptionOnIsArray(): void { $param = $this diff --git a/tests/unit/GherkinParamTest.php b/tests/unit/GherkinParamTest.php new file mode 100644 index 0000000..6fa3869 --- /dev/null +++ b/tests/unit/GherkinParamTest.php @@ -0,0 +1,80 @@ +getModule( + 'Codeception\Extension\GherkinParam' + ); + + $this->module = Mockery::spy($moduleInstance); + } + + /** + * @SuppressWarnings(PHPMD.StaticAccess) + */ + protected function _after(): void + { + Mockery::close(); + } + + public function testBeforeStepCallsGetValueFromParamWhenStepParamIsString(): void + { + $step = new Step\Action('test', ['test']); + + $this->module->_beforeStep($step); + + $this->module->shouldHaveReceived('getValueFromParam', ['test']); + } + + public function testBeforeStepCallsParseTableNodeWhenStepParamIsTableNode(): void + { + $param = new TableNode([1 => ['foo', 'bar', 'baz']]); + $step = new Step\Action('test', [$param]); + + $this->module->_beforeStep($step); + + $this->module->shouldHaveReceived('parseTableNode', [$param]); + } + + public function testBeforeStepIteratesGetValueFromParamWhenStepParamIsArray(): void + { + $param = ['foo', 'bar', 'baz']; + $step = new Step\Action('test', [$param]); + + $this->module->_beforeStep($step); + + $this->module->shouldHaveReceived('getValueFromParam', ['foo']); + $this->module->shouldHaveReceived('getValueFromParam', ['bar']); + $this->module->shouldHaveReceived('getValueFromParam', ['baz']); + } + + public function testParseTableNodeReturnsValuedTableNode(): void + { + $input = new TableNode([1 => ['foo', 'bar', 'baz']]); + + $tableNode = $this->module + ->parseTableNode($input); + + $this->assertEquals($input, $tableNode); + } +}