Skip to content

Commit

Permalink
Migrations to phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher authored Dec 17, 2024
1 parent 543b61e commit b56a78b
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class CommonITILSatisfaction extends DbTestCase
*/
protected function getTestedClass(): string
{
$test_class = static::class;
$test_class = str_replace('Test', '', static::class);
// Rule class has the same name as the test class but in the global namespace
return substr(strrchr($test_class, '\\'), 1);
}
Expand All @@ -57,7 +57,7 @@ public function testGetItemtype()
$tested_class = $this->getTestedClass();
$itemtype = $tested_class::getItemtype();
// Verify the itemtype is a subclass of CommonITILObject
$this->boolean(is_a($itemtype, \CommonITILObject::class, true))->isTrue();
$this->assertTrue(is_a($itemtype, \CommonITILObject::class, true));
}

public function testGetSurveyUrl()
Expand All @@ -75,7 +75,7 @@ public function testGetSurveyUrl()
'entities_id' => $root_entity_id,
'solvedate' => $_SESSION['glpi_currenttime'],
]);
$this->integer($items_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $items_id);

$this->login(); // Authorized user required to update entity config

Expand All @@ -84,25 +84,25 @@ public function testGetSurveyUrl()
$config_suffix = $itemtype === 'Ticket' ? '' : ('_' . strtolower($itemtype));
$inquest_url = "[ITEMTYPE],[ITEMTYPE_NAME],[{$tag_prefix}_ID],[{$tag_prefix}_NAME],[{$tag_prefix}_CREATEDATE],
[{$tag_prefix}_SOLVEDATE],[{$tag_prefix}_PRIORITY]";
$this->boolean(
$this->assertTrue(
$entity->update([
'id' => $root_entity_id,
'inquest_URL' . $config_suffix => $inquest_url,
'inquest_config' . $config_suffix => \CommonITILSatisfaction::TYPE_EXTERNAL,
])
)->isTrue();
);

$expected = "{$itemtype},{$item->getTypeName(1)},{$items_id},{$item->fields['name']},{$item->getField('date')},
{$item->getField('solvedate')},{$item->getField('priority')}";
$generated = \Entity::generateLinkSatisfaction($item);
$this->string($generated)->isEqualTo($expected);
$this->assertEquals($expected, $generated);
}

public function testGetIndexName()
{
$index_name = $this->getTestedClass()::getIndexName();
$this->boolean(is_string($index_name))->isTrue();
$this->string($index_name)->isNotEmpty();
$this->assertTrue(is_string($index_name));
$this->assertNotEmpty($index_name);
}

public function testGetLogTypeID()
Expand All @@ -118,7 +118,7 @@ public function testGetLogTypeID()
'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true),
'solvedate' => $_SESSION['glpi_currenttime'],
]);
$this->integer($items_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $items_id);

// Add a satisfaction
/** @var \CommonITILSatisfaction $satisfaction */
Expand All @@ -131,14 +131,14 @@ public function testGetLogTypeID()
'type' => \CommonITILSatisfaction::TYPE_INTERNAL,
'date' => $_SESSION['glpi_currenttime'],
]);
$this->integer($satisfaction_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $satisfaction_id);

$log_type = $satisfaction->getLogTypeID();
$this->boolean(is_array($log_type))->isTrue();
$this->array($log_type)->size->isEqualTo(2);
$this->string($log_type[0])->isEqualTo($itilobject_type);
$this->assertIsArray($log_type);
$this->assertCount(2, $log_type);
$this->assertEquals($itilobject_type, $log_type[0]);

$this->integer($log_type[1])->isEqualTo($items_id);
$this->assertEquals($items_id, $log_type[1]);
}

public function testDateAnsweredSetOnAnswer()
Expand All @@ -156,11 +156,11 @@ public function testDateAnsweredSetOnAnswer()
'solvedate' => $_SESSION['glpi_currenttime'],
'users_id_recipient' => getItemByTypeName('User', TU_USER, true),
]);
$this->integer($items_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $items_id);

$this->boolean($item->addTeamMember('User', getItemByTypeName('User', TU_USER, true), [
$this->assertTrue($item->addTeamMember('User', getItemByTypeName('User', TU_USER, true), [
'role' => Team::ROLE_REQUESTER,
]))->isTrue();
]));

// Add a satisfaction
/** @var \CommonITILSatisfaction $satisfaction */
Expand All @@ -173,17 +173,17 @@ public function testDateAnsweredSetOnAnswer()
'type' => \CommonITILSatisfaction::TYPE_INTERNAL,
'date' => $_SESSION['glpi_currenttime'],
]);
$this->integer($satisfaction_id)->isGreaterThan(0);
$this->variable($satisfaction->fields['date_answered'])->isNull();
$this->assertGreaterThan(0, $satisfaction_id);
$this->assertNull($satisfaction->fields['date_answered']);

$this->login();

$this->boolean($satisfaction->update([
$this->assertTrue($satisfaction->update([
$itilobject_type::getForeignKeyField() => $items_id, // These items don't use `id` as the index field...
'satisfaction' => 5
]))->isTrue();
]));

$this->variable($satisfaction->fields['date_answered'])->isNotNull();
$this->string($satisfaction->fields['date_answered'])->isEqualTo($_SESSION['glpi_currenttime']);
$this->assertNotNull($satisfaction->fields['date_answered']);
$this->assertEquals($_SESSION['glpi_currenttime'], $satisfaction->fields['date_answered']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
namespace tests\units;

// Force import because of atoum autoloader not working
require_once 'CommonITILSatisfaction.php';
require_once __DIR__ . '/../abstracts/CommonITILSatisfaction.php';

class ChangeSatisfaction extends CommonITILSatisfaction
class ChangeSatisfactionTest extends CommonITILSatisfaction
{
}
24 changes: 13 additions & 11 deletions tests/functional/Cluster.php → phpunit/functional/ClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
use Item_Cluster;
use NetworkEquipment;

class Cluster extends DbTestCase
class ClusterTest extends DbTestCase
{
protected function getClusterByItemProvider(): iterable
{
Expand Down Expand Up @@ -129,17 +129,19 @@ protected function getClusterByItemProvider(): iterable
];
}

/**
* @dataProvider getClusterByItemProvider
*/
public function testgetClusterByItem(CommonDBTM $item, ?int $expected): void
public function testgetClusterByItem(): void
{
$result = \Cluster::getClusterByItem($item);
if ($result === null) {
$this->variable($result)->isNull();
} else {
$this->object($result)->isInstanceOf(\Cluster::class);
$this->integer($result->getID())->isEqualTo($expected);
foreach ($this->getClusterByItemProvider() as $row) {
$item = $row["item"];
$expected = $row["expected"];

$result = \Cluster::getClusterByItem($item);
if ($result === null) {
$this->assertNull($result);
} else {
$this->assertInstanceOf(\Cluster::class, $result);
$this->assertEquals($expected, $result->getID());
}
}
}
}
Loading

0 comments on commit b56a78b

Please sign in to comment.