Skip to content

Commit

Permalink
chore: beautify identifier generator proxy, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shpran committed Nov 6, 2024
1 parent 094800e commit 8638c1e
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
interface IdentifierGeneratorInterface
{
public const OPTION_RESOURCE = 'resource';
public const OPTION_RESOURCE_ID = 'resource_id';
public const OPTION_RESOURCE_ID = 'resourceId';

public function generate(array $options = []): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,26 @@ public function __construct(Ontology $ontology)
public function addIdentifierGenerator(IdentifierGeneratorInterface $idGenerator, string $resourceType): void
{
if (isset($this->idGenerators[$resourceType])) {
throw new InvalidArgumentException('Id generator for type already defined');
throw new InvalidArgumentException(
sprintf(
'Id generator for type %s already defined',
$resourceType
)
);
}

$this->idGenerators[$resourceType] = $idGenerator;
}

public function generate(array $options = []): string
{
$this->assertRequiredOptionsProvided($options);
$resourceType = $this->getResourceType($options);

return $this->getIdGenerator($resourceType)->generate($options);
}

private function assertRequiredOptionsProvided(array $options): void
{
if (!isset($options[self::OPTION_RESOURCE]) && !isset($options[self::OPTION_RESOURCE_ID])) {
throw new InvalidArgumentException(
Expand All @@ -56,7 +69,10 @@ public function generate(array $options = []): string
)
);
}
}

private function getResourceType(array $options): string
{
if (
isset($options[self::OPTION_RESOURCE])
&& !$options[self::OPTION_RESOURCE] instanceof core_kernel_classes_Resource
Expand All @@ -71,12 +87,21 @@ public function generate(array $options = []): string
}

$resource = $options[self::OPTION_RESOURCE] ?? $this->ontology->getResource($options[self::OPTION_RESOURCE_ID]);
$resourceType = $resource->getRootId();

return $resource->getRootId();
}

private function getIdGenerator(string $resourceType): IdentifierGeneratorInterface
{
if (!isset($this->idGenerators[$resourceType])) {
throw new InvalidArgumentException('ID generator for resource type not defined');
throw new InvalidArgumentException(
sprintf(
'ID generator for resource type %s not defined',
$resourceType
)
);
}

return $this->idGenerators[$resourceType]->generate($options);
return $this->idGenerators[$resourceType];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA.
*/

declare(strict_types=1);

namespace oat\tao\test\unit\models\classes\IdentifierGenerator\Generator;

use core_kernel_classes_Resource;
use InvalidArgumentException;
use oat\generis\model\data\Ontology;
use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface;
use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorProxy;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class IdentifierGeneratorProxyTest extends TestCase
{
/** @var Ontology|MockObject */
private Ontology $ontology;

private IdentifierGeneratorProxy $sut;

protected function setUp(): void
{
$this->ontology = $this->createMock(Ontology::class);
$this->sut = new IdentifierGeneratorProxy($this->ontology);
}

public function testNoResourceOrResourceIdProvided(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
sprintf(
'Option "%s" or "%s" is required to generate ID',
IdentifierGeneratorInterface::OPTION_RESOURCE,
IdentifierGeneratorInterface::OPTION_RESOURCE_ID
)
);

$this->sut->generate();
}

public function testInvalidResourceOptionProvided(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
sprintf(
'Option "%s" must be an instance of %s',
IdentifierGeneratorInterface::OPTION_RESOURCE,
core_kernel_classes_Resource::class
)
);

$this->sut->generate(['resource' => 'invalidResource']);
}

public function testMissedResourceType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('ID generator for resource type missedResourceType not defined');

$resource = $this->createMock(core_kernel_classes_Resource::class);
$resource
->expects($this->once())
->method('getRootId')
->willReturn('missedResourceType');

$this->sut->generate([IdentifierGeneratorInterface::OPTION_RESOURCE => $resource]);
}

public function testSuccess(): void
{
$resource = $this->createMock(core_kernel_classes_Resource::class);

$options = [IdentifierGeneratorInterface::OPTION_RESOURCE => $resource];

$resource
->expects($this->once())
->method('getRootId')
->willReturn('resourceType');

$generator = $this->createMock(IdentifierGeneratorInterface::class);

$generator
->expects($this->once())
->method('generate')
->with($options)
->willReturn('identifier');

$this->sut->addIdentifierGenerator($generator, 'resourceType');

$this->assertEquals('identifier', $this->sut->generate($options));
}
}

0 comments on commit 8638c1e

Please sign in to comment.