Skip to content

Commit

Permalink
Improved unit test for "Account Manager" class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jocic committed Jan 7, 2019
1 parent b538632 commit d3e1573
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 68 deletions.
128 changes: 63 additions & 65 deletions source/Jocic/GoogleAuthenticator/AccountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,7 @@ public function save($fileLocation)
$fileHandler = null;
$bytesWritten = null;

// Step 1 - Check If File Is Writable

touch($fileLocation);

if (!is_writable($fileLocation))
{
throw new \Exception("Provided file isn't writable.");
}

// Step 2 - Prepare Data
// Logic

foreach ($accounts as $account)
{
Expand All @@ -419,11 +410,15 @@ public function save($fileLocation)

// Step 3 - Save Data

$fileHandler = fopen($fileLocation, "w");

$bytesWritten = fwrite($fileHandler, $data);

fclose($fileHandler);
try
{
$fileHandler = fopen($fileLocation, "w");

$bytesWritten = fwrite($fileHandler, $data);

fclose($fileHandler);
}
catch (\Exception $e) {}

return $bytesWritten > 0;
}
Expand Down Expand Up @@ -454,54 +449,47 @@ public function load($fileLocation, $bufferSize = 1024)

$fileHandler = null;

// Step 1 - Check If File Is Readable
// Step 1 - Load Accounts

if (!is_readable($fileLocation))
try
{
throw new \Exception("Provided file isn't readable.");
}

// Step 2 - Load Accounts

$fileHandler = fopen($fileLocation, "r");

while (!feof($fileHandler))
{
$accounts .= fread($fileHandler, $bufferSize);
$fileHandler = fopen($fileLocation, "r");
while (!feof($fileHandler))
{
$accounts .= fread($fileHandler, $bufferSize);
}
fclose($fileHandler);
$accounts = unserialize($accounts);
}
catch (\Exception $e) {}

fclose($fileHandler);

$accounts = unserialize($accounts);

// Step 3 - Process Accounts
// Step 2 - Process Accounts

if (is_array($accounts))
{
$this->reset();

foreach ($accounts as $account)
{
// Check Data
// Only Add Valid Data

if (!( isset($account["account_id"])
&& isset($account["service_name"])
&& isset($account["account_name"])
&& isset($account["secret"])))
if ( isset($account["account_id"])
&& isset($account["service_name"])
&& isset($account["account_name"])
&& isset($account["secret"]))
{
return false;
$loadedAccount = new Account();

$loadedAccount->setAccountId($account["account_id"]);
$loadedAccount->setServiceName($account["service_name"]);
$loadedAccount->setAccountName($account["account_name"]);
$loadedAccount->setAccountSecret($account["secret"]);

$this->addAccount($loadedAccount);
}

// Add Account

$loadedAccount = new Account();

$loadedAccount->setAccountId($account["account_id"]);
$loadedAccount->setServiceName($account["service_name"]);
$loadedAccount->setAccountName($account["account_name"]);
$loadedAccount->setAccountSecret($account["secret"]);

$this->addAccount($loadedAccount);
}

return true;
Expand Down Expand Up @@ -535,6 +523,10 @@ public function load($fileLocation, $bufferSize = 1024)

public function removeByAccountId($accountId)
{
// Core Variables

$accounts = $this->getAccounts();

// Step 1 - Check Value

if (!is_numeric($accountId))
Expand All @@ -544,11 +536,16 @@ public function removeByAccountId($accountId)

// Step 2 - Remove Account

if (isset($this->accounts[$accountId]))
foreach ($accounts as $accountKey => $accountObject)
{
unset($this->accounts[$accountId]);

return true;
if ($accountObject->getAccountId() == $accountId)
{
unset($accounts[$accountKey]);

$this->accounts = $accounts;

return true;
}
}

return false;
Expand Down Expand Up @@ -586,7 +583,9 @@ public function removeByAccountName($accountName)
{
if ($accountObject->getAccountName() == $accountName)
{
unset($this->accounts[$accountId]);
unset($accounts[$accountId]);

$this->accounts = $accounts;

return true;
}
Expand Down Expand Up @@ -621,15 +620,7 @@ public function removeByAccountObject($accountObject)
throw new \Exception("Provided object isn't valid.");
}

// Step 2 - Check Account's Manager

if ($this->getManagerId() != $accountObject->getAccountManager()
->getManagerId())
{
throw new \Exception("Account doesn't belong to this manager.");
}

// Step 3 - Remove Account
// Step 2 - Remove Account

if (($identifier = $accountObject->getAccountId()) != null)
{
Expand Down Expand Up @@ -662,6 +653,10 @@ public function removeByAccountObject($accountObject)

public function findByAccountId($accountId)
{
// Core Variables

$accounts = $this->getAccounts();

// Step 1 - Check Value

if (!is_numeric($accountId))
Expand All @@ -671,9 +666,12 @@ public function findByAccountId($accountId)

// Step 2 - Remove Account

if (isset($this->accounts[$accountId]))
foreach ($accounts as $accountObject)
{
return $this->accounts[$accountId];
if ($accountObject->getAccountId() == $accountId)
{
return $this->accounts[$accountId];
}
}

return null;
Expand Down
84 changes: 81 additions & 3 deletions tests/elements/test-account-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,63 @@ public function testRemoveAccountMethod()

// Step 2 - Test Removal By ID

try
{
$accountManager->removeByAccountId("#");

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Provided ID isn't numeric.",
$e->getMessage());
}

$this->assertTrue($accountManager->removeAccount(1),
"Removal by \"ID\" failed.");
"Removal by \"ID\" failed - existing.");

$this->assertFalse($accountManager->removeAccount(1),
"Removal by \"ID\" failed - non-existing.");

// Step 3 - Test Removal By Name

try
{
$accountManager->removeByAccountName(1337);

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Provided ID isn't string.",
$e->getMessage());
}

$this->assertTrue($accountManager->removeAccount("D"),
"Removal by \"Name\" failed.");
"Removal by \"Name\" failed - existing.");

$this->assertFalse($accountManager->removeAccount("D"),
"Removal by \"Name\" failed - non-existing.");

// Step 4 - Test Removal By Object

try
{
$accountManager->removeByAccountObject(new Secret());

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Provided object isn't valid.",
$e->getMessage());
}

$this->assertTrue($accountManager->removeAccount($testAccounts[2]),
"Removal by \"Object\" failed.");
"Removal by \"Object\" failed - existing.");

$this->assertFalse($accountManager->removeAccount(new Account("E", "F")),
"Removal by \"Object\" failed - non-existing.");

// Step 5 - Test Invalid Removal Method

Expand Down Expand Up @@ -318,12 +363,36 @@ public function testFindAccountMethod()

// Step 2 - Test Finding By ID

try
{
$accountManager->findByAccountId("#");

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Provided ID isn't numeric.",
$e->getMessage());
}

$account = $accountManager->findAccount(1);

$this->assertSame("A", $account->getServiceName());

// Step 3 - Test Finding By Name

try
{
$accountManager->findByAccountName(1337);

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Provided ID isn't string.",
$e->getMessage());
}

$account = $accountManager->findAccount("D");

$this->assertSame("C", $account->getServiceName());
Expand All @@ -334,6 +403,10 @@ public function testFindAccountMethod()

$this->assertSame("E", $account->getServiceName());

$account = $accountManager->findAccount(new Account("E", "F"));

$this->assertSame("E", $account->getServiceName());

// Step 5 - Test Invalid Removal Method

try
Expand Down Expand Up @@ -397,6 +470,11 @@ public function testSaveLoad()
$this->assertSame($accountObject->getServiceName(),
$testAccounts[$accountKey]->getServiceName());
}

// Step 4 - Test Invalid Save & Load

$this->assertFalse($accountManager->save("/etc/shadow"));
$this->assertFalse($accountManager->load("/etc/shadow"));
}

/*********************\
Expand Down

0 comments on commit d3e1573

Please sign in to comment.