Skip to content

Commit

Permalink
- fix for list strategy factories - correct handling of empty lists d…
Browse files Browse the repository at this point in the history
…uring merge and hydrate

- refinement of exceptions thrown by provided strategies
  • Loading branch information
Arthur Mogliev committed May 9, 2021
1 parent 8c6fe7d commit 4ee4a98
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 189 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions composer.lock.7.1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions composer.lock.7.2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,46 @@
$valueStrategy->shouldReceive('extract')->with($source[2])->andReturn($destination[2])->twice();

$strategy = $factory($container, 'test', ['type' => $type]);
\expect($strategy->extract(new \ArrayObject()))->toBe([]);
\expect($strategy->extract(new \ArrayObject($source)))->toBe($destination);
\expect($strategy->extract(new \ArrayObject(\array_combine([3, 4, 5], $source))))->toBe($destination);
});
\it('creates list strategy that hydrates array of hydrated items', function ()
\it('creates list strategy that hydrates to empty array', function ()
{
$source = [1, 2];
$destination = new \ArrayObject();
$newDestination = [\mock(), \mock()];

$type = Example\TestClass::class;
$container = \mock(ContainerInterface::class);
$metadataProvider = \mock(DT\ClassMetadataProviderInterface::class);
$strategyPluginManager = \mock(DT\Strategy\PluginManager::class);
$valueStrategyMetadata = ['test_strategy_name', ['test_option_name' => 'test_option_value']];
$valueStrategy = \mock(DT\Strategy\StrategyInterface::class);
$factory = new OAGC\Strategy\Factory\NoArgObjectList();

$container->shouldReceive('get')->with(DT\ClassMetadataProviderInterface::class)->andReturn($metadataProvider)->once();
$container->shouldReceive('get')->with(DT\Strategy\PluginManager::class)->andReturn($strategyPluginManager)->once();
$metadataProvider->shouldReceive('getClassStrategy')->with($type, '')->andReturn($valueStrategyMetadata)->once();
$strategyPluginManager->shouldReceive('get')->with(...$valueStrategyMetadata)->andReturn($valueStrategy)->once();
$valueStrategy->shouldReceive('hydrate')->withArgs(
function ($a, &$b) use ($type, &$source, &$newDestination)
{
$sourceIndex = \array_search($a, $source, true);
$result = (($sourceIndex !== false) && ($b instanceof $type));
if ($result)
{
$b = $newDestination[$sourceIndex];
}
return $result;
}
)->times(\count($source));

$strategy = $factory($container, 'test', ['type' => $type]);
$strategy->hydrate($source, $destination);
\expect($destination->getArrayCopy())->toBe($newDestination);
});
\it('creates list strategy that hydrates to array with items', function ()
{
$source = [1, 2];
$destination = new \ArrayObject([\mock(), \mock(), \mock()]);
Expand Down Expand Up @@ -133,7 +169,44 @@ function ($a, &$b) use ($type, &$source, &$newDestination)
$strategy->hydrate($source, $destination);
\expect($destination->getArrayCopy())->toBe(\array_combine([3, 4], $newDestination));
});
\it('creates list strategy that merges array of merged items', function ()
\it('creates list strategy that merges to empty array', function ()
{
$source = [1, 2];
$destination = [];
$extractions = [6, 7];
$newDestination = [8, 9];

$type = Example\TestClass::class;
$container = \mock(ContainerInterface::class);
$metadataProvider = \mock(DT\ClassMetadataProviderInterface::class);
$strategyPluginManager = \mock(DT\Strategy\PluginManager::class);
$valueStrategyMetadata = ['test_strategy_name', ['test_option_name' => 'test_option_value']];
$valueStrategy = \mock(DT\Strategy\StrategyInterface::class);
$factory = new OAGC\Strategy\Factory\NoArgObjectList();

$container->shouldReceive('get')->with(DT\ClassMetadataProviderInterface::class)->andReturn($metadataProvider)->once();
$container->shouldReceive('get')->with(DT\Strategy\PluginManager::class)->andReturn($strategyPluginManager)->once();
$metadataProvider->shouldReceive('getClassStrategy')->with($type, '')->andReturn($valueStrategyMetadata)->once();
$strategyPluginManager->shouldReceive('get')->with(...$valueStrategyMetadata)->andReturn($valueStrategy)->once();
$valueStrategy->shouldReceive('extract')->with(\Mockery::type($type))->andReturnValues($extractions)->times(\count($source));
$valueStrategy->shouldReceive('merge')->withArgs(
function ($a, &$b) use ($type, &$source, &$extractions, &$newDestination)
{
$sourceIndex = \array_search($a, $source, true);
$result = (($sourceIndex !== false) && ($b === $extractions[$sourceIndex]));
if ($result)
{
$b = $newDestination[$sourceIndex];
}
return $result;
}
)->times(\count($source));

$strategy = $factory($container, 'test', ['type' => $type]);
$strategy->merge($source, $destination);
\expect($destination)->toBe($newDestination);
});
\it('creates list strategy that merges to array with items', function ()
{
$source = [1, 2];
$destination = [3, 4, 5];
Expand Down
25 changes: 23 additions & 2 deletions spec/OpenAPIGenerator/Common/Strategy/Factory/ScalarList.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@

$strategy = $factory($container, 'test', ['type' => OAGC\Validator\Scalar::TYPE_INT]);
$list = [1, 2, 3];
\expect($strategy->extract(new \ArrayObject()))->toBe([]);
\expect($strategy->extract(new \ArrayObject($list)))->toBe($list);
\expect($strategy->extract(new \ArrayObject([3 => 1, 4 => 2, 5 => 3])))->toBe($list);
});
\it('creates list strategy that hydrates scalar array', function ()
\it('creates list strategy that hydrates to empty scalar array', function ()
{
$container = \mock(ContainerInterface::class);
$factory = new OAGC\Strategy\Factory\ScalarList();

$strategy = $factory($container, 'test', ['type' => OAGC\Validator\Scalar::TYPE_INT]);
$destination = new \ArrayObject();
$strategy->hydrate([4, 5], $destination);
\expect($destination->getArrayCopy())->toBe([0 => 4, 1 => 5]);
});
\it('creates list strategy that hydrates to scalar array with items', function ()
{
$container = \mock(ContainerInterface::class);
$factory = new OAGC\Strategy\Factory\ScalarList();
Expand All @@ -54,7 +65,17 @@
$strategy->hydrate([4, 5], $destination);
\expect($destination->getArrayCopy())->toBe([3 => 4, 4 => 5]);
});
\it('creates list strategy that merges scalar array', function ()
\it('creates list strategy that merges to empty scalar array', function ()
{
$container = \mock(ContainerInterface::class);
$factory = new OAGC\Strategy\Factory\ScalarList();

$strategy = $factory($container, 'test', ['type' => OAGC\Validator\Scalar::TYPE_INT]);
$destination = [];
$strategy->merge([4, 5], $destination);
\expect($destination)->toBe([0 => 4, 1 => 5]);
});
\it('creates list strategy that merges to scalar array with items', function ()
{
$container = \mock(ContainerInterface::class);
$factory = new OAGC\Strategy\Factory\ScalarList();
Expand Down

0 comments on commit 4ee4a98

Please sign in to comment.