Skip to content

Commit

Permalink
fix(migration): fix type error when input is not an array (#262)
Browse files Browse the repository at this point in the history
* fix(migration): fix type error when input is not an array

* test: add test for data migrator

* test: add case for collection input
  • Loading branch information
EdieLemoine authored Aug 1, 2024
1 parent 0f2cdb9 commit 218a3e2
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Migration/Util/DataMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Facade\Logger;

class DataMigrator implements DataMigratorInterface
{
Expand Down Expand Up @@ -33,6 +34,18 @@ public function transform($input, $map): array
$input = $input->toArray();
}

if (! is_array($input)) {
Logger::warning(
sprintf(
'%s expects an array or Collection as input, got %s instead.',
__METHOD__,
gettype($input)
)
);

return [];
}

$newSettings = [];

/** @var \MyParcelNL\PrestaShop\Migration\Util\MigratableValue $migratable */
Expand Down
94 changes: 94 additions & 0 deletions tests/Unit/Migration/Util/DataMigratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Migration\Util;

use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Tests\Uses\UsesMockPsPdkInstance;
use Psr\Log\LoggerInterface;
use function expect;
use function MyParcelNL\Pdk\Tests\usesShared;

usesShared(new UsesMockPsPdkInstance());

test('gets value', function () {
/** @var \MyParcelNL\PrestaShop\Migration\Util\DataMigrator $migrator */
$migrator = Pdk::get(DataMigrator::class);

$result = $migrator->getValue('other_key', [
'key' => 'value',
'other_key' => 'other_value',
'another_key' => 'another_value',
]);

expect($result)->toBe('other_value');
});

test('migrates value', function (string $inputType) {
/** @var \MyParcelNL\PrestaShop\Migration\Util\DataMigrator $migrator */
$migrator = Pdk::get(DataMigrator::class);

$transformationMap = [
new MigratableValue(
'key',
'new_key',
new TransformValue(function ($value) {
return "new_$value";
})
),
new MigratableValue(
'other_key',
'some_key',
new CastValue('int')
),
new MigratableValue(
'key_not_present',
'new_key_not_present',
new CastValue('string')
),
];

$input = [
'key' => 'value',
'other_key' => '123',
'another_key' => 'another_value',
];

$result = $migrator->transform(
'collection' === $inputType
? new Collection($input)
: $input,
$transformationMap
);

expect($result)->toBe([
'new_key' => 'new_value',
'some_key' => 123,
'new_key_not_present' => '',
]);
})->with([
'array',
'collection',
]);

test('handles invalid input', function () {
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockLogger $logger */
$logger = Pdk::get(LoggerInterface::class);
/** @var \MyParcelNL\PrestaShop\Migration\Util\DataMigrator $migrator */
$migrator = Pdk::get(DataMigrator::class);

$transformationMap = [];

/** @noinspection PhpParamsInspection */
$result = $migrator->transform(null, $transformationMap);

expect($logger->getLogs())
->toHaveCount(1)
->and($logger->getLogs()[0]['message'])
->toContain('transform expects an array or Collection as input, got NULL instead.')
->and($result)
->toBe([]);
});

0 comments on commit 218a3e2

Please sign in to comment.