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

increase mutation coverage tests #77

Merged
merged 10 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Fixed Show fixed Hide fixed
}

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();
Fixed Show fixed Hide fixed
}

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

protected function _before(): void
{
$moduleInstance = $this->getModule(
'Codeception\Extension\GherkinParam'
);

$this->module = Mockery::spy($moduleInstance);
Fixed Show fixed Hide fixed
}

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

public function testBeforeStepCallsGetValueFromParamWhenStepParamIsString(): void

Check warning

Code scanning / Phpcs (reported by Codacy)

Line exceeds 80 characters; contains 85 characters

Line exceeds 80 characters; contains 85 characters
{
$step = new Step\Action('test', ['test']);

$this->module->_beforeStep($step);

$this->module->shouldHaveReceived('getValueFromParam', ['test']);
}

public function testBeforeStepCallsParseTableNodeWhenStepParamIsTableNode(): void

Check warning

Code scanning / Phpcs (reported by Codacy)

Line exceeds 80 characters; contains 85 characters

Line exceeds 80 characters; contains 85 characters
{
$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

Check warning

Code scanning / Phpcs (reported by Codacy)

Line exceeds 80 characters; contains 87 characters

Line exceeds 80 characters; contains 87 characters
{
$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);
}
}