Skip to content

Commit 2dba0ec

Browse files
committed
refactor(Impact\Embodied\AbstractembodiedImpact): split method for tests
1 parent 25eb02e commit 2dba0ec

File tree

7 files changed

+137
-22
lines changed

7 files changed

+137
-22
lines changed

src/Impact/Embodied/AbstractEmbodiedImpact.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use DBmysql;
3737
use DbUtils;
3838
use CommonDBTM;
39+
use DBmysqlIterator;
3940
use GlpiPlugin\Carbon\DataTracking\AbstractTracked;
4041
use GlpiPlugin\Carbon\EmbodiedImpact;
4142
use GlpiPlugin\Carbon\Impact\Type;
@@ -99,7 +100,13 @@ public function setLimit(int $limit)
99100
$this->limit = $limit;
100101
}
101102

102-
public function evaluateItems(): int
103+
/**
104+
* Get an iterator of items to evaluate
105+
*
106+
* @param array $crit criterias
107+
* @return DBmysqlIterator
108+
*/
109+
public function getEvaluateItems(array $crit = []): DBmysqlIterator
103110
{
104111
/** @var DBmysql $DB */
105112
global $DB;
@@ -112,6 +119,14 @@ public function evaluateItems(): int
112119
throw new \LogicException('Itemtype does not inherits from ' . CommonDBTM::class);
113120
}
114121

122+
$crit[EmbodiedImpact::getTableField('id')] = null;
123+
$iterator = $DB->request($this->getEvaluableQuery($crit, false));
124+
125+
return $iterator;
126+
}
127+
128+
public function evaluateItems(DBmysqlIterator $iterator): int
129+
{
115130
/**
116131
* Huge quantity of SQL queries will be executed
117132
* We NEED to check memory usage to avoid running out of memory
@@ -128,10 +143,7 @@ public function evaluateItems(): int
128143
$attempts_count = 0;
129144
/** @var int $count count of successfully evaluated assets */
130145
$count = 0;
131-
$crit = [
132-
EmbodiedImpact::getTableField('id') => null,
133-
];
134-
$iterator = $DB->request($this->getEvaluableQuery($crit, false));
146+
135147
foreach ($iterator as $row) {
136148
if ($this->evaluateItem($row['id'])) {
137149
$count++;

src/Impact/Embodied/EmbodiedImpactInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
namespace GlpiPlugin\Carbon\Impact\Embodied;
3535

3636
use CommonDBTM;
37+
use DBmysqlIterator;
3738
use GlpiPlugin\Carbon\Engine\V1\EngineInterface;
3839

3940
interface EmbodiedImpactInterface
@@ -73,9 +74,10 @@ public function getEvaluableQuery(array $crit = [], bool $entity_restrict = true
7374
/**
7475
* Start the evaluation of all items
7576
*
77+
* @param DBmysqlIterator $iterator assets to evaluate providing their IDs
7678
* @return int count of successfully evaluated assets
7779
*/
78-
public function evaluateItems(): int;
80+
public function evaluateItems(DBmysqlIterator $iterator): int;
7981

8082
/**
8183
* Evaluate all impacts of the asset

src/Impact/Embodied/Engine.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public static function getEngineFromItemtype(string $itemtype, ?RestApiClientInt
6363
{
6464
$embodied_impact_namespace = Config::getEmbodiedImpactEngine();
6565
$embodied_impact_class = $embodied_impact_namespace . '\\' . $itemtype;
66-
if (!class_exists($embodied_impact_class) || !is_subclass_of($embodied_impact_class, AbstractEmbodiedImpact::class)) {
66+
$must_implement = AbstractEmbodiedImpact::class;
67+
if (!class_exists($embodied_impact_class) || !is_subclass_of($embodied_impact_class, $must_implement)) {
6768
return self::getInternalEngineFromItemtype($itemtype);
6869
}
6970

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\Tests\Impact\Engine;
34+
35+
use DBmysql;
36+
use GlpiPlugin\Carbon\EmbodiedImpact;
37+
use GlpiPlugin\Carbon\Impact\Embodied\AbstractEmbodiedImpact;
38+
use GlpiPlugin\Carbon\Tests\DbTestCase;
39+
use PHPUnit\Framework\Attributes\CoversClass;
40+
41+
#[CoversClass(AbstractEmbodiedImpact::class)]
42+
class AbstractEmbodiedImpactTest extends DbTestCase
43+
{
44+
protected static string $itemtype = '';
45+
protected static string $itemtype_type = '';
46+
protected static string $itemtype_model = '';
47+
48+
public function testGetEvaluableItems()
49+
{
50+
/** @var DBmysql $DB */
51+
global $DB;
52+
53+
if (static::$itemtype === '' || static::$itemtype_type === '' || static::$itemtype_model === '') {
54+
// Ensure that the inherited test class is properly implemented for this test
55+
$this->fail('Itemtype properties not set in ' . static::class);
56+
}
57+
58+
// Test the asset is evaluable when no embodied impact is in the DB
59+
$glpi_asset_type = $this->createItem(static::$itemtype_type);
60+
$asset_type = $this->createItem('GlpiPlugin\\Carbon\\' . static::$itemtype_type, [
61+
getForeignKeyFieldForItemType(static::$itemtype_type) => $glpi_asset_type->getID(),
62+
]);
63+
$asset = $this->createItem(static::$itemtype, [
64+
getForeignKeyFieldForItemType(static::$itemtype_type) => $glpi_asset_type->getID(),
65+
]);
66+
$instance = new ('GlpiPlugin\\Carbon\\Impact\\Embodied\\Boavizta\\' . static::$itemtype)();
67+
$iterator = $instance->getEvaluateItems([
68+
$asset->getTableField('id') => $asset->getID(),
69+
]);
70+
$this->assertEquals(1, $iterator->count());
71+
72+
// Test the asset is no longer evaluable when no embodied impact is in the DB
73+
$glpi_asset_type = $this->createItem(static::$itemtype_type);
74+
$asset_type = $this->createItem('GlpiPlugin\\Carbon\\' . static::$itemtype_type, [
75+
getForeignKeyFieldForItemType(static::$itemtype_type) => $glpi_asset_type->getID(),
76+
]);
77+
$asset = $this->createItem(static::$itemtype, [
78+
getForeignKeyFieldForItemType(static::$itemtype_type) => $glpi_asset_type->getID(),
79+
]);
80+
$instance = new ('GlpiPlugin\\Carbon\\Impact\\Embodied\\Boavizta\\' . static::$itemtype)();
81+
$embodied_impact = $this->createItem(EmbodiedImpact::class, [
82+
'itemtype' => $asset->getType(),
83+
'items_id' => $asset->getID(),
84+
]);
85+
$iterator = $instance->getEvaluateItems([
86+
$asset::getTableField('id') => $asset->getID(),
87+
]);
88+
$this->assertEquals(0, $iterator->count());
89+
90+
}
91+
}

tests/src/Impact/History/CommonAsset.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
namespace GlpiPlugin\Carbon\Tests\Impact\History;
3434

3535
use GlpiPlugin\Carbon\Tests\DbTestCase;
36-
use Computer as GlpiComputer;
3736
use Infocom;
3837

3938
class CommonAsset extends DbTestCase

tests/units/Impact/Embodied/Boavizta/ComputerTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,35 @@
3232

3333
namespace GlpiPlugin\Carbon\Impact\Engine\Boavizta\Tests;
3434

35-
use Computer;
35+
use Computer as GlpiComputer;
3636
use ComputerType as GlpiComputerType;
37+
use ComputerModel as GlpiComputerModel;
3738
use DBmysql;
3839
use GlpiPlugin\Carbon\ComputerType;
3940
use GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Computer as BoaviztaComputer;
40-
use GlpiPlugin\Carbon\Tests\DbTestCase;
41+
use GlpiPlugin\Carbon\Tests\Impact\Engine\AbstractEmbodiedImpactTest;
4142
use PHPUnit\Framework\Attributes\CoversClass;
4243

43-
#[CoversClass(Computer::class)]
44-
class ComputerTest extends DbTestCase
44+
#[CoversClass(BoaviztaComputer::class)]
45+
class ComputerTest extends AbstractEmbodiedImpactTest
4546
{
47+
protected static string $itemtype = GlpiComputer::class;
48+
protected static string $itemtype_type = GlpiComputerType::class;
49+
protected static string $itemtype_model = GlpiComputerModel::class;
50+
4651
public function testGetEvaluableQuery()
4752
{
4853
/** @var DBmysql $DB */
4954
global $DB;
5055

56+
// Test a computer is evaluable
5157
$glpi_computer_type = $this->createItem(GlpiComputerType::class);
5258
$computer_type = $this->createItem(ComputerType::class, [
5359
'computertypes_id' => $glpi_computer_type->getID()
5460
]);
55-
$computer = $this->createItem(Computer::class, [
61+
$computer = $this->createItem(GlpiComputer::class, [
5662
'computertypes_id' => $glpi_computer_type->getID()
5763
]);
58-
5964
$instance = new BoaviztaComputer();
6065
$request = $instance->getEvaluableQuery([
6166
'glpi_computers.id' => $computer->getID(),
@@ -67,15 +72,15 @@ public function testGetEvaluableQuery()
6772
$iterator = $DB->request($request);
6873
$this->assertEquals(1, $iterator->count());
6974

75+
// Test a computer is not evaluable
7076
$glpi_computer_type = $this->createItem(GlpiComputerType::class);
7177
$computer_type = $this->createItem(ComputerType::class, [
7278
'computertypes_id' => $glpi_computer_type->getID(),
7379
'is_ignore' => 1,
7480
]);
75-
$computer = $this->createItem(Computer::class, [
81+
$computer = $this->createItem(GlpiComputer::class, [
7682
'computertypes_id' => $glpi_computer_type->getID()
7783
]);
78-
7984
$instance = new BoaviztaComputer();
8085
$request = $instance->getEvaluableQuery([
8186
'glpi_computers.id' => $computer->getID(),

tests/units/Impact/Embodied/Boavizta/MonitorTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@
3232

3333
namespace GlpiPlugin\Carbon\Impact\Engine\Boavizta\Tests;
3434

35-
use Monitor;
35+
use Monitor as GlpiMonitor;
3636
use MonitorType as GlpiMonitorType;
37+
use MonitorModel as glpiMonitorModel;
3738
use DBmysql;
3839
use GlpiPlugin\Carbon\MonitorType;
3940
use GlpiPlugin\Carbon\Impact\Embodied\Boavizta\Monitor as BoaviztaMonitor;
40-
use GlpiPlugin\Carbon\Tests\DbTestCase;
41+
use GlpiPlugin\Carbon\Tests\Impact\Engine\AbstractEmbodiedImpactTest;
4142
use PHPUnit\Framework\Attributes\CoversClass;
4243

43-
#[CoversClass(Monitor::class)]
44-
class MonitorTest extends DbTestCase
44+
#[CoversClass(BoaviztaMonitor::class)]
45+
class MonitorTest extends AbstractEmbodiedImpactTest
4546
{
47+
protected static string $itemtype = GlpiMonitor::class;
48+
protected static string $itemtype_type = GlpiMonitorType::class;
49+
protected static string $itemtype_model = GlpiMonitorModel::class;
50+
4651
public function testGetEvaluableQuery()
4752
{
4853
/** @var DBmysql $DB */
@@ -52,7 +57,7 @@ public function testGetEvaluableQuery()
5257
$monitor_type = $this->createItem(MonitorType::class, [
5358
'monitortypes_id' => $glpi_monitor_type->getID()
5459
]);
55-
$monitor = $this->createItem(Monitor::class, [
60+
$monitor = $this->createItem(GlpiMonitor::class, [
5661
'monitortypes_id' => $glpi_monitor_type->getID()
5762
]);
5863

@@ -72,7 +77,7 @@ public function testGetEvaluableQuery()
7277
'monitortypes_id' => $glpi_monitor_type->getID(),
7378
'is_ignore' => 1,
7479
]);
75-
$monitor = $this->createItem(Monitor::class, [
80+
$monitor = $this->createItem(GlpiMonitor::class, [
7681
'monitortypes_id' => $glpi_monitor_type->getID()
7782
]);
7883

0 commit comments

Comments
 (0)