Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
refactor(test): mutualize helper method
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry authored and DIOHz0r committed Apr 13, 2018
1 parent 890ab63 commit 153ca96
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 124 deletions.
16 changes: 15 additions & 1 deletion tests/src/Flyvemdm/Tests/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,18 @@ protected function agentFromInvitation(

return $this->enrollFromInvitation($user, $input);
}
}

/**
* @return object PluginFlyvemdmFleet mocked
*
* @param array $input input data
*/
protected function createFleet($input) {
$fleet = $this->newMockInstance(\PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add($input);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}
}
21 changes: 4 additions & 17 deletions tests/suite-integration/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,10 @@ public function testMoveBetweenFleets() {
$this->boolean($agent->isNewItem())
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));

$fleet = $this->createFleet();
$fleet = $this->createFleet([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => __CLASS__ . '::'. __FUNCTION__,
]);
$fleetFk = $fleet::getForeignKeyField();

// add the agent in the fleet
Expand Down Expand Up @@ -789,22 +792,6 @@ public function testMoveBetweenFleets() {

}

/**
* @return object PluginFlyvemdmFleet mocked
* @tags createFleet
*/
private function createFleet() {
$fleet = $this->newMockInstance(\PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => $this->getUniqueString(),
]);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}

/**
* Lock or unlock device and check the expected status
* @param \PluginFlyvemdmAgent $agent
Expand Down
20 changes: 4 additions & 16 deletions tests/suite-integration/PluginFlyvemdmFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public function testApplyPolicy() {

// Applya a policy on a file
$deployPolicyData = $this->getFileDeploymentPolicy();
$fleet = $this->createFleet();
$fleet = $this->createFleet([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => __CLASS__ . '::'. __FUNCTION__,
]);

$fleet_policy = $this->ApplyAddFilePolicy($deployPolicyData, $file, $fleet, $fileDestination);
$this->boolean($fleet_policy->isNewItem())
Expand Down Expand Up @@ -136,21 +139,6 @@ private function getFileRemovalPolicy() {
return $policyData;
}

/**
* @return object PluginFlyvemdmFleet mocked
*/
private function createFleet() {
$fleet = $this->newMockInstance(PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => $this->getUniqueString(),
]);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}

/**
* @param PluginFlyvemdmPolicy $policyData
* @param FlyvemdmFile $file
Expand Down
20 changes: 4 additions & 16 deletions tests/suite-integration/PluginFlyvemdmPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public function testCreateApplication() {
$policyDataRemove = new \PluginFlyvemdmPolicy();
$this->boolean($policyDataRemove->getFromDBBySymbol('removeApp'))->isTrue();

$fleet = $this->createFleet();
$fleet = $this->createFleet([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => __CLASS__ . '::'. __FUNCTION__,
]);
$task = $this->applyAddPackagePolicy($policyDataDeploy, $package, $fleet);
$this->boolean($task->isNewItem())->isFalse();

Expand All @@ -112,21 +115,6 @@ public function testCreateApplication() {
$this->boolean($task->isNewItem())->isTrue();
}

/**
* @return object \PluginFlyvemdmFleet mocked
*/
private function createFleet() {
$fleet = $this->newMockInstance(\PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => 'a fleet',
]);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}

/**
* @param \PluginFlyvemdmPolicy $policyData
* @param \PluginFlyvemdmPackage $package
Expand Down
20 changes: 4 additions & 16 deletions tests/suite-integration/PluginFlyvemdmPolicyDeployFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public function testApplyPolicy() {
$policyDataDeploy = new \PluginFlyvemdmPolicy();
$this->boolean($policyDataDeploy->getFromDBBySymbol('deployFile'))->isTrue();

$fleet = $this->createFleet();
$fleet = $this->createFleet([
'entities_id' => $this->defaultEntity,
'name' => __CLASS__ . '::'. __FUNCTION__,
]);

$fleetFk = \PluginFlyvemdmFleet::getForeignKeyField();
$policyFk = \PluginFlyvemdmPolicy::getForeignKeyField();
Expand Down Expand Up @@ -170,21 +173,6 @@ public function testApplyPolicy() {
]))->isTrue();
}

/**
* @return object PluginFlyvemdmFleet mocked
*/
private function createFleet() {
$fleet = $this->newMockInstance(\PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add([
'entities_id' => $this->defaultEntity,
'name' => 'a fleet',
]);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}

/**
* @param \PluginFlyvemdmPolicy $policyData
* @param PluginFlyvemdmFile $file
Expand Down
20 changes: 4 additions & 16 deletions tests/suite-integration/PluginFlyvemdmPolicyRemoveFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public function testDeployRemoveFilePolicy() {
$this->boolean($file->isNewItem())->isFalse($mysqlError);

// Create a fleet
$fleet = $this->createFleet();
$fleet = $this->createFleet([
'entities_id' => $this->defaultEntity,
'name' => __CLASS__ . '::'. __FUNCTION__,
]);

$policyData = new \PluginFlyvemdmPolicy();
$this->boolean($policyData->getFromDBBySymbol('removeFile'))->isTrue();
Expand Down Expand Up @@ -106,19 +109,4 @@ public function testDeployRemoveFilePolicy() {
$this->boolean($task->isNewItem())->isFalse();

}

/**
* @return object PluginFlyvemdmFleet mocked
*/
private function createFleet() {
$fleet = $this->newMockInstance(\PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add([
'entities_id' => $this->defaultEntity,
'name' => 'a fleet',
]);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}
}
30 changes: 12 additions & 18 deletions tests/suite-integration/PluginFlyvemdmTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function testApplyPolicy() {
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));

// Create a fleet
$fleet = $this->createFleet();
$fleet = $this->createFleet([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => __FUNCTION__
]);

// Move the agent to the fleet
$this->boolean($agent->update([
Expand Down Expand Up @@ -158,7 +161,10 @@ public function testApplyPolicy() {

// Test tassk status is created when an agent joins a fleet having policies
// Create a 2nd fleet
$fleet2 = $this->createFleet();
$fleet2 = $this->createFleet([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => __FUNCTION__
]);

// Apply a policy
$policy = new \PluginFlyvemdmPolicy();
Expand Down Expand Up @@ -191,7 +197,10 @@ public function testApplyPolicy() {
}

// Create a 3rd fleet
$fleet3 = $this->createFleet();
$fleet3 = $this->createFleet([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => __CLASS__ . '::'. __FUNCTION__,
]);

// Apply a policy
$policy = new \PluginFlyvemdmPolicy();
Expand Down Expand Up @@ -228,19 +237,4 @@ public function testApplyPolicy() {
$this->string($row['status'])->isEqualTo('canceled');
}
}

/**
* @return object PluginFlyvemdmFleet mocked
*/
private function createFleet() {
$fleet = $this->newMockInstance(\PluginFlyvemdmFleet::class, '\MyMock');
$fleet->getMockController()->post_addItem = function () {};
$fleet->add([
'entities_id' => $_SESSION['glpiactive_entity'],
'name' => $this->getUniqueString(),
]);
$this->boolean($fleet->isNewItem())->isFalse();

return $fleet;
}
}
24 changes: 0 additions & 24 deletions tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,4 @@ private function createAppInDB() {

return $file;
}

/**
* Create an application (directly in DB) because we are not uploading any file
* @return \PluginFlyvemdmPackage
*/
private function createFleetInDB() {
global $DB;

$uniqid = 'packages_fleet_' . uniqid();
$table_file = \PluginFlyvemdmFleet::getTable();
$query = "INSERT INTO `$table_file` (
`name`
) VALUES (
'" . $uniqid . "'
)";
$result = $DB->query($query);
$this->boolean($result)->isTrue();

$itemType = new \PluginFlyvemdmFleet();
$itemType->getFromDB($DB->insert_id());
$this->boolean($itemType->isNewItem())->isFalse();

return $itemType;
}
}

0 comments on commit 153ca96

Please sign in to comment.