Skip to content

Commit

Permalink
Bump schema version; cleanup, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Jan 21, 2025
1 parent bea9040 commit b7284fb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion inventory.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$id": "https://glpi-project.org/inventory.json",
"type": "object",
"title": "GLPI Inventory Schema",
"version": "0.1",
"version": 1.0,
"definitions": {
"datetime": {
"type": "string",
Expand Down
34 changes: 17 additions & 17 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
*/
class Converter
{
public const LAST_VERSION = 0.1;

/** @var ?float */
private ?float $target_version;
/** @var float */
private float $target_version;

private Schema $schema;
/** @var bool */
Expand All @@ -40,7 +38,7 @@ class Converter

/** @var array<string, float> */
private array $mapping = [
'01' => 0.1
'01' => 1.0
];

/**
Expand Down Expand Up @@ -71,18 +69,10 @@ class Converter
*
* @param ?float $target_version JSON schema based version to target. Use last version if null.
*/
public function __construct($target_version = null)
public function __construct(?float $target_version = null)
{
$this->schema = new Schema();
if ($target_version === null) {
$target_version = self::LAST_VERSION;
}

if (!is_double($target_version)) {
throw new UnexpectedValueException('Version must be a double!');
}

$this->target_version = $target_version;
$this->target_version = $target_version ?? $this->schema->getVersion();
}

/**
Expand All @@ -92,7 +82,7 @@ public function __construct($target_version = null)
*/
public function getTargetVersion(): float
{
return $this->target_version ?? self::LAST_VERSION;
return $this->target_version;
}

/**
Expand Down Expand Up @@ -204,7 +194,17 @@ public function getMethods(): array
}

/**
* Converts to inventory format 0.1
* Get versions mapping
*
* @return array<string, float>
*/
public function getMappings(): array
{
return $this->mapping;
}

/**
* Converts to inventory format 1.0
*
* @param array<string, mixed> $data Contents
*
Expand Down
12 changes: 10 additions & 2 deletions lib/php/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*/
class Schema
{
public const LAST_VERSION = 0.1;

/** @var array<string, array<int, string>> */
private array $patterns;
/** @var array<string, array<string, string>> */
Expand Down Expand Up @@ -276,4 +274,14 @@ public function validate($json): bool
);
}
}

/**
* Get current schema version
*
* @return float
*/
public function getVersion(): float
{
return $this->build()->version; //@phpstan-ignore-line: version does exist.
}
}
17 changes: 11 additions & 6 deletions tests/Glpi/Inventory/tests/units/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ class Converter extends TestCase
public function testConstructor(): void
{
$instance = new \Glpi\Inventory\Converter();
$this->assertSame($instance::LAST_VERSION, $instance->getTargetVersion());
$this->assertSame($instance->getSchema()->getVersion(), $instance->getTargetVersion());

$ver = 156.2;
$instance = new \Glpi\Inventory\Converter($ver);
$this->assertSame($ver, $instance->getTargetVersion());

$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Version must be a double!');
new \Glpi\Inventory\Converter('abcde'); //@phpstan-ignore-line
}

/**
Expand All @@ -56,7 +52,7 @@ public function testDebug(): void
public function testGetMethods(): void
{
$expected = ['convertTo01'];
$instance = new \Glpi\Inventory\Converter(0.1);
$instance = new \Glpi\Inventory\Converter(1.0);
$this->assertSame($expected, $instance->getMethods());
}

Expand Down Expand Up @@ -802,4 +798,13 @@ public function testConvertMemoryCapacity(string $orig, int $expected): void
$instance = new \Glpi\Inventory\Converter();
$this->assertSame($expected, $instance->convertMemory($orig));
}

public function testHasLatestVersion(): void
{
$instance = new \Glpi\Inventory\Converter();
$this->assertContains(
$instance->getSchema()->getVersion(),
$instance->getMappings()
);
}
}
6 changes: 6 additions & 0 deletions tests/Glpi/Inventory/tests/units/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,10 @@ public function testFlexibleSchema(): void
$this->expectExceptionMessage('Additional properties not allowed: additional');
$this->assertTrue($instance->validate($json_additionnal));
}

public function testVersion(): void
{
$instance = new \Glpi\Inventory\Schema();
$this->assertIsFloat($instance->getVersion());
}
}

0 comments on commit b7284fb

Please sign in to comment.