diff --git a/src/Impact/Embodied/AbstractEmbodiedImpact.php b/src/Impact/Embodied/AbstractEmbodiedImpact.php index 279e9339..c39ce339 100644 --- a/src/Impact/Embodied/AbstractEmbodiedImpact.php +++ b/src/Impact/Embodied/AbstractEmbodiedImpact.php @@ -89,6 +89,7 @@ abstract protected function getVersion(): string; */ final public function getUnit(int $type, bool $short = true): ?string { + // TODO: This methos is probably never called now. Check and drop. switch ($type) { case Type::IMPACT_GWP: return $short ? 'gCO2eq' : __('grams of carbon dioxyde equivalent', 'carbon'); diff --git a/src/Impact/Embodied/Engine.php b/src/Impact/Embodied/Engine.php index ab250976..4bd58a23 100644 --- a/src/Impact/Embodied/Engine.php +++ b/src/Impact/Embodied/Engine.php @@ -57,7 +57,7 @@ public static function getAvailableBackends(): array } /** - * Get an instance of the engine to calculate imapcts for the given itemtype + * Get an instance of the engine to calculate impacts for the given itemtype * * Returns null if no engine found * diff --git a/src/Impact/Embodied/Internal/AbstractAsset.php b/src/Impact/Embodied/Internal/AbstractAsset.php index 06c43046..6c8eabc1 100644 --- a/src/Impact/Embodied/Internal/AbstractAsset.php +++ b/src/Impact/Embodied/Internal/AbstractAsset.php @@ -64,8 +64,8 @@ protected function doEvaluation(): array return []; } - /** @var CommonDBTM $model */ - $model = getItemForItemtype($glpi_model_itemtype); + /** @var CommonDBTM|false $model */ + $model = getItemForItemtype('GlpiPlugin\\Carbon\\' . $glpi_model_itemtype); $model->getFromDBByCrit([ $glpi_model_fk => $this->item->fields[$glpi_model_fk], ]); diff --git a/tests/src/Impact/Embodied/AbstractEmbodiedImpactTest.php b/tests/src/Impact/Embodied/AbstractCommonEmbodiedImpactTest.php similarity index 92% rename from tests/src/Impact/Embodied/AbstractEmbodiedImpactTest.php rename to tests/src/Impact/Embodied/AbstractCommonEmbodiedImpactTest.php index 1a458c07..05726850 100644 --- a/tests/src/Impact/Embodied/AbstractEmbodiedImpactTest.php +++ b/tests/src/Impact/Embodied/AbstractCommonEmbodiedImpactTest.php @@ -39,19 +39,22 @@ use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(AbstractEmbodiedImpact::class)] -class AbstractEmbodiedImpactTest extends DbTestCase +class AbstractCommonEmbodiedImpactTest extends DbTestCase { protected static string $itemtype = ''; protected static string $itemtype_type = ''; protected static string $itemtype_model = ''; - public function testGetItemsToEvaluate() + public static function setUpBeforeClass(): void { if (static::$itemtype === '' || static::$itemtype_type === '' || static::$itemtype_model === '') { // Ensure that the inherited test class is properly implemented for this test - $this->fail('Itemtype properties not set in ' . static::class); + self::fail('Itemtype properties not set in ' . static::class); } + } + public function test_GetItemsToEvaluate_evaluates_asset_when_no_embodied_impact_exists() + { // Test the asset is evaluable when no embodied impact is in the DB $glpi_asset_type = $this->createItem(static::$itemtype_type); $asset_type = $this->createItem('GlpiPlugin\\Carbon\\' . static::$itemtype_type, [ @@ -64,7 +67,10 @@ public function testGetItemsToEvaluate() $asset->getTableField('id') => $asset->getID(), ]); $this->assertEquals(1, $iterator->count()); + } + public function test_GetItemsToEvaluate_does_not_evaluates_asset_when_embodied_impact_exists() + { // Test the asset is no longer evaluable when there is embodied impact in the DB $glpi_asset_type = $this->createItem(static::$itemtype_type); $asset_type = $this->createItem('GlpiPlugin\\Carbon\\' . static::$itemtype_type, [ @@ -82,8 +88,11 @@ public function testGetItemsToEvaluate() $asset::getTableField('id') => $asset->getID(), ]); $this->assertEquals(0, $iterator->count()); + } - // Test the asset is evaluable when there is embodied impact in the DB but recamculate is set + public function test_GetItemsToEvaluate_evaluates_asset_when_embodied_impact_exists_and_marked_for_recalculation() + { + // Test the asset is evaluable when there is embodied impact in the DB but recalculate is set $glpi_asset_type = $this->createItem(static::$itemtype_type); $asset_type = $this->createItem('GlpiPlugin\\Carbon\\' . static::$itemtype_type, [ getForeignKeyFieldForItemType(static::$itemtype_type) => $glpi_asset_type->getID(), diff --git a/tests/src/Impact/Embodied/Boavizta/AbstractEmbodiedImpactTest.php b/tests/src/Impact/Embodied/Boavizta/AbstractEmbodiedImpactTest.php new file mode 100644 index 00000000..9e7d7dc4 --- /dev/null +++ b/tests/src/Impact/Embodied/Boavizta/AbstractEmbodiedImpactTest.php @@ -0,0 +1,45 @@ +. + * + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\Carbon\Tests\Impact\Embodied\Boavizta; + +use GlpiPlugin\Carbon\Impact\Embodied\AbstractEmbodiedImpact; +use GlpiPlugin\Carbon\Tests\Impact\Embodied\AbstractCommonEmbodiedImpactTest; +use PHPUnit\Framework\Attributes\CoversClass; + +#[CoversClass(AbstractEmbodiedImpact::class)] +class AbstractEmbodiedImpactTest extends AbstractCommonEmbodiedImpactTest +{ + protected static string $itemtype = ''; + protected static string $itemtype_type = ''; + protected static string $itemtype_model = ''; +} diff --git a/tests/src/Impact/Embodied/Internal/AbstractEmbodiedImpactTest.php b/tests/src/Impact/Embodied/Internal/AbstractEmbodiedImpactTest.php new file mode 100644 index 00000000..500ba651 --- /dev/null +++ b/tests/src/Impact/Embodied/Internal/AbstractEmbodiedImpactTest.php @@ -0,0 +1,45 @@ +. + * + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\Carbon\Tests\Impact\Embodied\Internal; + +use GlpiPlugin\Carbon\Impact\Embodied\AbstractEmbodiedImpact; +use GlpiPlugin\Carbon\Tests\Impact\Embodied\AbstractCommonEmbodiedImpactTest; +use PHPUnit\Framework\Attributes\CoversClass; + +#[CoversClass(AbstractEmbodiedImpact::class)] +class AbstractEmbodiedImpactTest extends AbstractCommonEmbodiedImpactTest +{ + protected static string $itemtype = ''; + protected static string $itemtype_type = ''; + protected static string $itemtype_model = ''; +} diff --git a/tests/units/Impact/Embodied/Boavizta/ComputerTest.php b/tests/units/Impact/Embodied/Boavizta/ComputerTest.php index 6f071483..c91074e0 100644 --- a/tests/units/Impact/Embodied/Boavizta/ComputerTest.php +++ b/tests/units/Impact/Embodied/Boavizta/ComputerTest.php @@ -36,7 +36,7 @@ use ComputerModel as GlpiComputerModel; use ComputerType as GlpiComputerType; use GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Computer as BoaviztaComputer; -use GlpiPlugin\Carbon\Tests\Impact\Embodied\AbstractEmbodiedImpactTest; +use GlpiPlugin\Carbon\Tests\Impact\Embodied\Boavizta\AbstractEmbodiedImpactTest; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(BoaviztaComputer::class)] diff --git a/tests/units/Impact/Embodied/Boavizta/MonitorTest.php b/tests/units/Impact/Embodied/Boavizta/MonitorTest.php index 93aaa6f3..3ab862f8 100644 --- a/tests/units/Impact/Embodied/Boavizta/MonitorTest.php +++ b/tests/units/Impact/Embodied/Boavizta/MonitorTest.php @@ -33,7 +33,7 @@ namespace GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Tests; use GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Monitor as BoaviztaMonitor; -use GlpiPlugin\Carbon\Tests\Impact\Embodied\AbstractEmbodiedImpactTest; +use GlpiPlugin\Carbon\Tests\Impact\Embodied\Boavizta\AbstractEmbodiedImpactTest; use Monitor as GlpiMonitor; use MonitorModel as glpiMonitorModel; use MonitorType as GlpiMonitorType; diff --git a/tests/units/Impact/Embodied/Internal/ComputerTest.php b/tests/units/Impact/Embodied/Internal/ComputerTest.php new file mode 100644 index 00000000..78b12a42 --- /dev/null +++ b/tests/units/Impact/Embodied/Internal/ComputerTest.php @@ -0,0 +1,48 @@ +. + * + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\Carbon\Impact\Embodied\Internal\Tests; + +use Computer as GlpiComputer; +use ComputerModel as GlpiComputerModel; +use ComputerType as GlpiComputerType; +use GlpiPlugin\Carbon\Impact\Embodied\Internal\Computer as InternalComputer; +use GlpiPlugin\Carbon\Tests\Impact\Embodied\Internal\AbstractEmbodiedImpactTest; +use PHPUnit\Framework\Attributes\CoversClass; + +#[CoversClass(InternalComputer::class)] +class ComputerTest extends AbstractEmbodiedImpactTest +{ + protected static string $itemtype = GlpiComputer::class; + protected static string $itemtype_type = GlpiComputerType::class; + protected static string $itemtype_model = GlpiComputerModel::class; +}