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 4eb5684
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 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
29 changes: 12 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 @@ -203,8 +193,13 @@ public function getMethods(): array
return $methods;
}

public function getMappings(): array

Check failure on line 196 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 on ubuntu-latest

Method Glpi\Inventory\Converter::getMappings() return type has no value type specified in iterable type array.

Check failure on line 196 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 on ubuntu-latest

Method Glpi\Inventory\Converter::getMappings() return type has no value type specified in iterable type array.

Check failure on line 196 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 on ubuntu-latest

Method Glpi\Inventory\Converter::getMappings() return type has no value type specified in iterable type array.

Check failure on line 196 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 on ubuntu-latest

Method Glpi\Inventory\Converter::getMappings() return type has no value type specified in iterable type array.

Check failure on line 196 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 on ubuntu-latest

Method Glpi\Inventory\Converter::getMappings() return type has no value type specified in iterable type array.

Check failure on line 196 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 on ubuntu-latest

Method Glpi\Inventory\Converter::getMappings() return type has no value type specified in iterable type array.
{
return $this->mapping;
}

/**
* Converts to inventory format 0.1
* Converts to inventory format 1.0
*
* @param array<string, mixed> $data Contents
*
Expand Down
10 changes: 10 additions & 0 deletions lib/php/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,14 @@ public function validate($json): bool
);
}
}

/**
* Get current schema version
*
* @return float
*/
public function getVersion(): float
{
return $this->build()->version;

Check failure on line 287 in lib/php/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 on ubuntu-latest

Access to an undefined property object::$version.

Check failure on line 287 in lib/php/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 on ubuntu-latest

Access to an undefined property object::$version.

Check failure on line 287 in lib/php/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 on ubuntu-latest

Access to an undefined property object::$version.

Check failure on line 287 in lib/php/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 on ubuntu-latest

Access to an undefined property object::$version.

Check failure on line 287 in lib/php/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 on ubuntu-latest

Access to an undefined property object::$version.

Check failure on line 287 in lib/php/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 on ubuntu-latest

Access to an undefined property object::$version.
}
}
9 changes: 9 additions & 0 deletions tests/Glpi/Inventory/tests/units/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,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 4eb5684

Please sign in to comment.