Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
increase mutation coverage tests (#77)
Browse files Browse the repository at this point in the history
* add exception tests

* add unit test for parseTableNode

* fix lint issue

* remove unused spy in exception unit tests

* close mockery after tests

* remove final keyword for testability

* add _beforeStep unit tests

* suppress PHPMD.StaticAccess on Mockery::close()

* downgrade msi to 70

* fix lint warnings
  • Loading branch information
edno authored Feb 18, 2023
1 parent fbce4d0 commit 3477c36
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@
<rule ref="Generic.Commenting.DocComment">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PSR2.ControlStructures.ControlStructureSpacing">
<properties>
<property name="maxLineLength" value="120" />
</properties>
</rule>
</ruleset>
31 changes: 16 additions & 15 deletions src/GherkinParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @category Test
* @package GherkinParam
* @author Gregory Heitz <edno@edno.io>
* @license https://git.io/Juy0k Apache Licence
* @license https://git.io/Juy0k Apache License
* @link https://packagist.org/packages/edno/codeception-gherkin-param
*/

Expand All @@ -33,6 +33,7 @@
use \Codeception\Step;
use \Codeception\Lib\ModuleContainer;
use \Codeception\Extension\GherkinParamException;
use \Codeception\Exception\Warning;

/**
* GherkinParam extension main class
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -341,26 +342,26 @@ final protected function getValueFromArray(string $param)
/**
* Parse a table node by mapping its parameters
*
* @param \Behat\Gherkin\Node\TableNode<mixed> $tablenode table node
* @param \Behat\Gherkin\Node\TableNode<mixed> $tableNode table node
*
* @return \Behat\Gherkin\Node\TableNode<mixed> 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);
// issue TableNode does not support `null` values in table
$table[$i][$j] = $val;
}
}
$prop->setValue($tablenode, $table);
$prop->setValue($tableNode, $table);
$prop->setAccessible(false);

return $tablenode;
return $tableNode;
}

/**
Expand Down
35 changes: 31 additions & 4 deletions tests/unit/GherkinParamExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,7 +80,7 @@ public function testMapParametersToValuesWithExceptionOnIsArray(): void
->mapParametersToValues(
[0,1,2,3,4],
[[0],[1],[2],[3],[4]],
"test"
'test'
);
}
);
Expand All @@ -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]}}');
}
);
}
}
8 changes: 8 additions & 0 deletions tests/unit/GherkinParamNullableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ protected function _before(): void
->shouldAllowMockingProtectedMethods();
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function _after(): void
{
Mockery::close();
}

public function testMapParametersToValuesWithExceptionOnIsArray(): void
{
$param = $this
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/GherkinParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use \Behat\Gherkin\Node\TableNode;
use \Codeception\Step;

/**
* Happy path unit tests
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
class GherkinParamTest extends \Codeception\Test\Unit
{

/**
* @var \UnitTester
*/
protected $module;
protected $fixture;

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function _before(): void
{
$moduleInstance = $this->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);
}
}

0 comments on commit 3477c36

Please sign in to comment.