Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDTT-38: Fix for CI failure. #24

Merged
merged 11 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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
Expand All @@ -47,4 +47,4 @@ jobs:
- name: Send code coverage report to Codecov.io
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{ secrets.CODECOV_TOKEN }}
34 changes: 29 additions & 5 deletions src/Test/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@

class DefaultTest extends Test
{

/**
* @inheritDoc
*/
public function execute(array $sourceData, array $destinationData): bool
{
$sourceFields = explode('/', $this->getSourceField());
$destinationFields = explode('/', $this->getDestinationField());

Check warning on line 18 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L17-L18

Added lines #L17 - L18 were not covered by tests

if (!$this->issetField($sourceData, $sourceFields)) {

Check warning on line 20 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L20

Added line #L20 was not covered by tests
throw new ExecutionException("Source field could not be found in the source data.");
}

if (!$this->issetField($destinationData, $destinationFields)) {

Check warning on line 24 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L24

Added line #L24 was not covered by tests
throw new ExecutionException("Destination field could not be found in the destination data.");
}

/** @var string|int $sourceValue */
$sourceValue = array_reduce(
$sourceFields,
static fn (array $carry, string $key) => $carry[$key],
$sourceData
);

Check warning on line 33 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L29-L33

Added lines #L29 - L33 were not covered by tests
/** @var string|int $destinationValue */
$destinationValue = array_reduce(
$destinationFields,
static fn (array $carry, string $key) => $carry[$key],
$destinationData
);

Check warning on line 39 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L35-L39

Added lines #L35 - L39 were not covered by tests

if ($this->getTransform() !== null) {
$sourceValue = $this->getTransform()->process($sourceValue);
Expand All @@ -46,27 +45,52 @@
Assert::assertSame(
$sourceValue,
$destinationValue,
sprintf("Source: `%s`\nDestination: `%s`", $sourceValue, $destinationValue)

Check warning on line 48 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L48

Added line #L48 was not covered by tests
);

return true;
}

/**
* @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>>|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.
*
* Used a combination of type hinting and explicit checks to assist PHPStan
* in understanding the types involved and resolving static analysis errors.
*/
private function issetField(mixed $data, array $fields): bool

Check warning on line 73 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L73

Added line #L73 was not covered by tests
{
// Check if $data is an array
if (!is_array($data)) {
sadeesh-sdet marked this conversation as resolved.
Show resolved Hide resolved
return false;

Check warning on line 77 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L76-L77

Added lines #L76 - L77 were not covered by tests
}

// Get the next key to check
$key = array_shift($fields);

Check warning on line 81 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L81

Added line #L81 was not covered by tests

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

Check warning on line 85 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L84-L85

Added lines #L84 - L85 were not covered by tests
}

$test = isset($data[$key]);
// Check if the key exists in the data array
if (!array_key_exists($key, $data)) {
sadeesh-sdet marked this conversation as resolved.
Show resolved Hide resolved
return false;

Check warning on line 90 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L89-L90

Added lines #L89 - L90 were not covered by tests
}

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

Check warning on line 94 in src/Test/DefaultTest.php

View check run for this annotation

Codecov / codecov/patch

src/Test/DefaultTest.php#L94

Added line #L94 was not covered by tests
}
}
Loading