Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to phpunit #18721

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ protected function testGetAvailableParameters($values, $parameters): void
$values_keys = array_keys($values);
$parameters_keys = array_column($parameters, 'key');

// Remove "flat" arrays (requester.user, requester.groups, ...)
// Remove "flat" arrays (requester.user, requester.groups, ...)
$parameters_keys = array_map(function ($parameter) {
$properties = explode('.', $parameter);
return array_shift($properties);
}, $parameters_keys);
$parameters_keys = array_values(array_unique($parameters_keys));

$this->array($parameters_keys)->isEqualTo($values_keys);
$this->assertEquals($values_keys, $parameters_keys);
}
}
2 changes: 1 addition & 1 deletion phpunit/functional/DBmysqlIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ public static function resultProvider(): iterable
#[DataProvider('resultProvider')]
public function testAutoUnsanitize(array $db_data, array $result): void
{
//PHPUnit cannot mack native functions.
//PHPUnit cannot mock native functions.
$mysqli_result = $this->createMock(\mysqli_result::class);
$mysqli_result->method('fetch_assoc')->willReturn($db_data);
$mysqli_result->method('data_seek')->willReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class AssetParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\AssetParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class AssetParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -47,19 +51,22 @@ public function testGetValues(): void
'entities_id' => $test_entity_id
]);

$parameters = $this->newTestedInstance();
$parameters = new AssetParameters();
$values = $parameters->getValues(getItemByTypeName('Computer', 'pc_testGetValues'));
$this->array($values)->isEqualTo([
'id' => getItemByTypeName('Computer', 'pc_testGetValues', true),
'name' => 'pc_testGetValues',
'itemtype' => 'Computer',
'serial' => 'abcd1234',
'entity' => [
'id' => $test_entity_id,
'name' => '_test_child_2',
'completename' => 'Root entity > _test_root_entity > _test_child_2',
]
]);
$this->assertEquals(
[
'id' => getItemByTypeName('Computer', 'pc_testGetValues', true),
'name' => 'pc_testGetValues',
'itemtype' => 'Computer',
'serial' => 'abcd1234',
'entity' => [
'id' => $test_entity_id,
'name' => '_test_child_2',
'completename' => 'Root entity > _test_root_entity > _test_child_2',
]
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class ChangeParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\ChangeParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class ChangeParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -61,51 +65,54 @@ public function testGetValues(): void

$changes_id = getItemByTypeName('Change', 'change_testGetValues', true);

$parameters = $this->newTestedInstance();
$parameters = new ChangeParameters();
$values = $parameters->getValues(getItemByTypeName('Change', 'change_testGetValues'));
$this->array($values)->isEqualTo([
'id' => $changes_id,
'ref' => "#$changes_id",
'link' => '<a href="/glpi/front/change.form.php?id=' . $changes_id . '" title="change_testGetValues">change_testGetValues</a>',
'name' => 'change_testGetValues',
'content' => '<p>change_testGetValues content</p>',
'date' => '2021-07-19 17:11:28',
'solvedate' => null,
'closedate' => null,
'status' => 'New',
'urgency' => 'Medium',
'impact' => 'Medium',
'priority' => 'Medium',
'entity' => [
'id' => $test_entity_id,
'name' => '_test_child_2',
'completename' => 'Root entity > _test_root_entity > _test_child_2',
],
'itilcategory' => [
'id' => $itilcategories_id,
'name' => 'category_testGetValues',
'completename' => 'category_testGetValues',
],
'requesters' => [
'users' => [],
'groups' => [],
],
'observers' => [
'users' => [],
'groups' => [
[
'id' => $observer_groups_id,
'name' => '_test_group_1',
'completename' => '_test_group_1',
$this->assertEquals(
[
'id' => $changes_id,
'ref' => "#$changes_id",
'link' => '<a href="/glpi/front/change.form.php?id=' . $changes_id . '" title="change_testGetValues">change_testGetValues</a>',
'name' => 'change_testGetValues',
'content' => '<p>change_testGetValues content</p>',
'date' => '2021-07-19 17:11:28',
'solvedate' => null,
'closedate' => null,
'status' => 'New',
'urgency' => 'Medium',
'impact' => 'Medium',
'priority' => 'Medium',
'entity' => [
'id' => $test_entity_id,
'name' => '_test_child_2',
'completename' => 'Root entity > _test_root_entity > _test_child_2',
],
'itilcategory' => [
'id' => $itilcategories_id,
'name' => 'category_testGetValues',
'completename' => 'category_testGetValues',
],
'requesters' => [
'users' => [],
'groups' => [],
],
'observers' => [
'users' => [],
'groups' => [
[
'id' => $observer_groups_id,
'name' => '_test_group_1',
'completename' => '_test_group_1',
],
],
],
'assignees' => [
'users' => [],
'groups' => [],
'suppliers' => [],
],
],
'assignees' => [
'users' => [],
'groups' => [],
'suppliers' => [],
],
]);
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,24 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class EntityParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\EntityParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class EntityParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
$parameters = $this->newTestedInstance();
$parameters = new EntityParameters();
$values = $parameters->getValues(getItemByTypeName('Entity', '_test_child_2'));
$this->array($values)->isEqualTo([
'id' => getItemByTypeName('Entity', '_test_child_2', true),
'name' => '_test_child_2',
'completename' => 'Root entity > _test_root_entity > _test_child_2',
]);
$this->assertEquals(
[
'id' => getItemByTypeName('Entity', '_test_child_2', true),
'name' => '_test_child_2',
'completename' => 'Root entity > _test_root_entity > _test_child_2',
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class GroupParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\GroupParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class GroupParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -46,13 +50,16 @@ public function testGetValues(): void
'entities_id' => $test_entity_id
]);

$parameters = $this->newTestedInstance();
$parameters = new GroupParameters();
$values = $parameters->getValues(getItemByTypeName('Group', 'group_testGetValues'));
$this->array($values)->isEqualTo([
'id' => getItemByTypeName('Group', 'group_testGetValues', true),
'name' => 'group_testGetValues',
'completename' => 'group_testGetValues',
]);
$this->assertEquals(
[
'id' => getItemByTypeName('Group', 'group_testGetValues', true),
'name' => 'group_testGetValues',
'completename' => 'group_testGetValues',
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class ITILCategoryParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\ITILCategoryParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class ITILCategoryParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -46,13 +50,16 @@ public function testGetValues(): void
'entities_id' => $test_entity_id
]);

$parameters = $this->newTestedInstance();
$parameters = new ITILCategoryParameters();
$values = $parameters->getValues(getItemByTypeName('ITILCategory', 'itilcategory_testGetValues'));
$this->array($values)->isEqualTo([
'id' => getItemByTypeName('ITILCategory', 'itilcategory_testGetValues', true),
'name' => 'itilcategory_testGetValues',
'completename' => 'itilcategory_testGetValues',
]);
$this->assertEquals(
[
'id' => getItemByTypeName('ITILCategory', 'itilcategory_testGetValues', true),
'name' => 'itilcategory_testGetValues',
'completename' => 'itilcategory_testGetValues',
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class KnowbaseItemParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\KnowbaseItemParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class KnowbaseItemParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -48,14 +52,17 @@ public function testGetValues(): void

$kbi_id = getItemByTypeName('KnowbaseItem', 'kbi_testGetValues', true);

$parameters = $this->newTestedInstance();
$parameters = new KnowbaseItemParameters();
$values = $parameters->getValues(getItemByTypeName('KnowbaseItem', 'kbi_testGetValues'));
$this->array($values)->isEqualTo([
'id' => $kbi_id,
'name' => 'kbi_testGetValues',
'answer' => "test answer' \"testGetValues",
'link' => '<a href="/glpi/front/knowbaseitem.form.php?id=' . $kbi_id . '" title="kbi_testGetValues">kbi_testGetValues</a>',
]);
$this->assertEquals(
[
'id' => $kbi_id,
'name' => 'kbi_testGetValues',
'answer' => "test answer' \"testGetValues",
'link' => '<a href="/glpi/front/knowbaseitem.form.php?id=' . $kbi_id . '" title="kbi_testGetValues">kbi_testGetValues</a>',
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class LocationParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\LocationParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class LocationParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -52,13 +56,16 @@ public function testGetValues(): void
'locations_id' => getItemByTypeName('Location', 'location_testGetValues_parent', true)
]);

$parameters = $this->newTestedInstance();
$parameters = new LocationParameters();
$values = $parameters->getValues(getItemByTypeName('Location', 'location_testGetValues'));
$this->array($values)->isEqualTo([
'id' => getItemByTypeName('Location', 'location_testGetValues', true),
'name' => 'location_testGetValues',
'completename' => 'location_testGetValues_parent > location_testGetValues',
]);
$this->assertEquals(
[
'id' => getItemByTypeName('Location', 'location_testGetValues', true),
'name' => 'location_testGetValues',
'completename' => 'location_testGetValues_parent > location_testGetValues',
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

namespace tests\units\Glpi\ContentTemplates\Parameters;

class OLAParameters extends AbstractParameters
use Glpi\ContentTemplates\Parameters\OLAParameters;

include_once __DIR__ . '/../../../../abstracts/AbstractParameters.php';

class OLAParametersTest extends AbstractParameters
{
public function testGetValues(): void
{
Expand All @@ -49,15 +53,18 @@ public function testGetValues(): void
'definition_time' => 'hour',
]);

$parameters = $this->newTestedInstance();
$parameters = new OLAParameters();
$values = $parameters->getValues(getItemByTypeName('OLA', 'ola_testGetValues'));
$this->array($values)->isEqualTo([
'id' => getItemByTypeName('OLA', 'ola_testGetValues', true),
'name' => 'ola_testGetValues',
'type' => 'Time to own',
'duration' => '4',
'unit' => 'hours',
]);
$this->assertEquals(
[
'id' => getItemByTypeName('OLA', 'ola_testGetValues', true),
'name' => 'ola_testGetValues',
'type' => 'Time to own',
'duration' => '4',
'unit' => 'hours',
],
$values
);

$this->testGetAvailableParameters($values, $parameters->getAvailableParameters());
}
Expand Down
Loading
Loading