Skip to content

Commit

Permalink
Throw exception with invalid JIRA-issue config
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliot-Tm committed Jun 17, 2024
1 parent e0e134d commit 25229ef
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/Exception/InvalidConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Exception;

final class InvalidConfigException extends \InvalidArgumentException
{
}
30 changes: 21 additions & 9 deletions src/Service/Registrar/JIRA/IssueConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Aeliot\TodoRegistrar\Service\Registrar\JIRA;

use Aeliot\TodoRegistrar\Exception\InvalidConfigException;

class IssueConfig
{
private bool $addTagToLabels;
Expand All @@ -26,16 +28,21 @@ class IssueConfig
*/
public function __construct(array $config)
{
$config = $this->normalizeConfig($config);
if (array_key_exists('issueType', $config) && array_key_exists('type', $config)) {
throw new InvalidConfigException(
'Conflicting config. Both properties "issueType" and "type" added to config of issue'
);
}

$this->addTagToLabels = $config['addTagToLabels'];
$this->assignee = $config['assignee'];
$this->components = $config['components'];
$this->issueType = $config['type'];
$this->labels = $config['labels'];
$this->priority = $config['priority'];
$this->projectKey = $config['projectKey'];
$this->tagPrefix = $config['tagPrefix'];
$config = $this->normalizeConfig($config);
foreach ((new \ReflectionClass($this))->getProperties() as $property) {
$key = $property->getName();
if (!array_key_exists($key, $config)) {
throw new InvalidConfigException("Undefined property of issue config: {$key}");
}

$this->$key = $config[$key];
}
}

public function isAddTagToLabels(): bool
Expand Down Expand Up @@ -104,6 +111,11 @@ public function normalizeConfig(array $config): array
$config['components'] = (array) $config['components'];
$config['labels'] = (array) $config['labels'];

if (array_key_exists('type', $config)) {
$config['issueType'] = $config['type'];
unset($config['type']);
}

return $config;
}
}
59 changes: 59 additions & 0 deletions tests/Unit/Service/Registrar/IssueConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,72 @@

namespace Aeliot\TodoRegistrar\Test\Unit\Service\Registrar;

use Aeliot\TodoRegistrar\Exception\InvalidConfigException;
use Aeliot\TodoRegistrar\Service\Registrar\JIRA\IssueConfig;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(IssueConfig::class)]
final class IssueConfigTest extends TestCase
{
public static function getDataForTestIssueTypeAlias(): iterable
{
yield [
[
'projectKey' => 'any string',
'issueType' => 'Bug',
]
];

yield [
[
'projectKey' => 'any string',
'type' => 'Bug',
]
];
}

public static function getDataForTestThrowExceptionWhenMissedRequiredProperty(): iterable
{
$fullConfig = [
'projectKey' => 'any string',
'type' => 'any string',
];

foreach (array_keys($fullConfig) as $key) {
$config = $fullConfig;
unset($config[$key]);
yield [$config];
}
}

#[DataProvider('getDataForTestIssueTypeAlias')]
public function testIssueTypeAlias(array $values): void
{
$config = new IssueConfig($values);
self::assertSame('Bug', $config->getIssueType());
}

public function testThrowExceptionWithIssueTypeDuplicatedByAlias(): void
{
$this->expectException(InvalidConfigException::class);
$values = [
'projectKey' => 'any string',
'type' => 'any string',
'issueType' => 'any string',
];
new IssueConfig($values);

}

#[DataProvider('getDataForTestThrowExceptionWhenMissedRequiredProperty')]
public function testThrowExceptionWhenMissedRequiredProperty(array $values): void
{
$this->expectException(InvalidConfigException::class);
new IssueConfig($values);
}

public function testValueAssigning(): void
{
$values = [
Expand Down

0 comments on commit 25229ef

Please sign in to comment.