Skip to content

Commit

Permalink
Merge pull request #24 from sadeesh-sdet/main
Browse files Browse the repository at this point in the history
MDTT-38: Fix for CI failure.
  • Loading branch information
hussainweb authored Nov 16, 2023
2 parents 53acfe1 + 5a5b5ad commit 0b960e0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ jobs:
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Security check installed dependencies
uses: symfonycorp/security-checker-action@v2
uses: symfonycorp/security-checker-action@v4

- name: Check PSR2 code style (PHP_CodeSniffer)
run: composer php-cs

- name: Analyse PHP Code (PHPStan)
run: composer phpstan

- name: Validate code quality
run: composer phpunit-with-coverage
# As codecov throwing lot of errors, disabling the respective check temporarily
# Reference discussion: https://github.com/axelerant/mdtt/pull/24#pullrequestreview-1632092133
# - name: Validate code quality
# run: composer phpunit-with-coverage

- name: Send code coverage report to Codecov.io
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
# - name: Send code coverage report to Codecov.io
# uses: codecov/codecov-action@v2
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
64 changes: 58 additions & 6 deletions src/Test/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Mdtt\Exception\ExecutionException;
use PHPUnit\Framework\Assert;
use InvalidArgumentException;

class DefaultTest extends Test
{

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -53,20 +53,72 @@ public function execute(array $sourceData, array $destinationData): bool
}

/**
* @param array<string, numeric-string|array<string, numeric-string>>|numeric-string $data
* @param array<string> $fields
* Recursively checks if an offset exists in a nested array.
*
* This function is designed to handle nested arrays and verify the existence of
* a specified offset (string key) within them. It performs recursive checks on
* nested arrays to ensure that the offset exists at each level.
*
* @param array<string, numeric-string|array<string, numeric-string>> $data
* The data array to check for the offset in.
* @param array<string> $fields
* An array of string keys representing the path to the desired offset.
*
* @return bool
* Returns `true` if the specified offset exists in the nested array,
* `false` otherwise.
*
* @throws InvalidArgumentException
* If the input data structure is not as expected.
*/
private function issetField(mixed $data, array $fields): bool
private function issetField(array $data, array $fields): bool
{
// Get the next key to check
$key = array_shift($fields);

// If there are no more keys to check, the offset exists
if ($key === null) {
return true;
}

$test = isset($data[$key]);
// Check if the key exists in the data array
if (!array_key_exists($key, $data)) {
return false;
}

// Make sure that the key value is an array of strings
if (!is_array($data[$key]) || !$this->isArrayOfStrings($data[$key])) {
throw new InvalidArgumentException("Data structure is not as expected.");
}

// Recursively check the next level
return $this->issetField($data[$key], $fields);
}

/**
* Checks if an iterable value is an array containing only strings.
*
* @param iterable<string> $value
* The array to check.
*
* @return bool
* Returns `true` if the iterable value is an array containing only strings, `false` otherwise.
*
* @throws InvalidArgumentException
* If the input value is not an array or contains non-string elements..
*/
private function isArrayOfStrings(iterable $value): bool
{
if (!is_array($value)) {
throw new InvalidArgumentException("Input must be an array.");
}

foreach ($value as $item) {
if (!is_string($item)) {
return false;
}
}

return $test && $this->issetField($data[$key], $fields);
return true;
}
}

0 comments on commit 0b960e0

Please sign in to comment.