test(unit test): Added new tests, and method to delete from database

Issue: #195Signed-off-by: Stuart McCulloch Anderson's avatarStuart McCulloch Anderson <stuart@nxfifteen.me.uk>
parent e9164d45
<?php
/**
* Copyright (c) 2017. Stuart McCulloch Anderson
*/
/*******************************************************************************
* This file is part of NxFIFTEEN Fitness Core.
* https://nxfifteen.me.uk
*
* Copyright (c) 2017, Stuart McCulloch Anderson
*
* Released under the MIT license
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
namespace Core;
namespace Core;
require_once(dirname(__FILE__) . "/../autoloader.php");
......@@ -247,6 +255,30 @@ namespace Core;
}
}
/**
* Delete setting value
* Function to store/change setting values. Values can be stored in the database or held in memory.
*
* @param string $key Setting to query
* @param bool $query_db Boolean to store in database or not
*
* @return bool was data stored correctly
*/
public function del($key, $query_db = true)
{
if (array_key_exists($key, $this->settings)) {
unset($this->settings[$key]);
}
if ($query_db) {
if ($this->database->has($this->get("db_prefix", false) . "settings", array("var" => $key))) {
return $this->database->delete($this->get("db_prefix", false) . "settings", array("var" => $key));
}
}
return true;
}
/**
* Return user setting value
* Queries user settings for value. Default value can be provided, if not NULL is returned.
......@@ -324,12 +356,41 @@ namespace Core;
}
}
/**
* Set user setting value
* Function to store/change setting values. Values are stored in the database.
*
* @param string $fuid User fuid
* @param string $key Setting to query
* @param string $value Value to store
*
* @return bool was data stored correctly
*/
public function delUser($fuid, $key)
{
if (array_key_exists($key . "_" . $fuid, $this->settings)) {
unset($this->settings[$key . "_" . $fuid]);
}
if ($this->database->has($this->get("db_prefix", false) . "settings_users", array(
"AND" => array(
"fuid" => $fuid,
"var" => $key
)
))
) {
return $this->database->delete($this->get("db_prefix", false) . "settings_users",
array("fuid" => $fuid, "var" => $key));
}
return true;
}
/**
* Set class database store
* Takes medoo paramater and stores for access within the class
*
* @param Medoo $database Application database connection
*
*/
public function setDatabase($database)
{
......
<?php
/**
* Copyright (c) 2017. Stuart McCulloch Anderson
*/
namespace Core\Tests;
/*******************************************************************************
* This file is part of NxFIFTEEN Fitness Core.
* https://nxfifteen.me.uk
*
* Copyright (c) 2017, Stuart McCulloch Anderson
*
* Released under the MIT license
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
namespace Core\Tests;
use Core\Config;
use Medoo\Medoo;
......@@ -27,13 +35,59 @@ namespace Core\Tests;
$this->configClass = new Config();
}
/**
* @param $value
*
* @return bool
*/
private function convertNumberToBool($value)
{
if (is_numeric($value)) {
if ($value == 1) {
return true;
} else {
return false;
}
}
return $value;
}
/**
* @return Medoo
*/
private function setUpDatabase()
{
return new medoo(array(
'database_type' => 'mysql',
'database_name' => $this->configClass->get("db_name"),
'server' => $this->configClass->get("db_server"),
'username' => $this->configClass->get("db_username"),
'password' => $this->configClass->get("db_password"),
'charset' => 'utf8'
));
}
/**
* @covers \Core\Config::set
*/
public function testSetNoDB()
{
$storeValue = rand(0, 1000);
$this->assertTrue($this->configClass->set('testSetNoDB', $storeValue, false));
$this->assertTrue($this->configClass->set('test' . __METHOD__ . 'DB' . $storeValue, $storeValue, false));
}
/**
* @covers \Core\Config::del
*/
public function testDelNoDB()
{
$storeValue = rand(0, 1000);
$this->configClass->set('test' . __METHOD__ . 'DB' . $storeValue, $storeValue, false);
$dbAction = $this->convertNumberToBool($this->configClass->del('test' . __METHOD__ . 'DB' . $storeValue,
false));
$this->assertTrue($dbAction);
}
/**
......@@ -42,8 +96,8 @@ namespace Core\Tests;
public function testGetNoDB()
{
$storeValue = rand(0, 1000);
$this->configClass->set('testSetNoDB', $storeValue, false);
$settingsValue = $this->configClass->get('testSetNoDB', 'Not Stored', false);
$this->configClass->set('test' . __METHOD__ . 'DB' . $storeValue, $storeValue, false);
$settingsValue = $this->configClass->get('test' . __METHOD__ . 'DB' . $storeValue, 'Not Stored', false);
$this->assertSame($storeValue, $settingsValue);
}
......@@ -52,26 +106,14 @@ namespace Core\Tests;
*/
public function testSetInDB()
{
$this->configClass->setDatabase(new medoo(array(
'database_type' => 'mysql',
'database_name' => $this->configClass->get("db_name"),
'server' => $this->configClass->get("db_server"),
'username' => $this->configClass->get("db_username"),
'password' => $this->configClass->get("db_password"),
'charset' => 'utf8'
)));
$this->configClass->setDatabase($this->setUpDatabase());
$storeValue = rand(0, 1000);
$dbAction = $this->configClass->set('testSetNoDB', $storeValue, true);
if (is_numeric($dbAction)) {
if ($dbAction == 1) {
$dbAction = true;
} else {
$dbAction = false;
}
}
$dbAction = $this->convertNumberToBool($this->configClass->set('test' . __METHOD__ . 'DB' . $storeValue,
$storeValue, true));
$this->assertTrue($dbAction);
$this->configClass->del('test' . __METHOD__ . 'DB' . $storeValue, true);
}
/**
......@@ -79,21 +121,31 @@ namespace Core\Tests;
*/
public function testGetInDB()
{
$this->configClass->setDatabase(new medoo(array(
'database_type' => 'mysql',
'database_name' => $this->configClass->get("db_name"),
'server' => $this->configClass->get("db_server"),
'username' => $this->configClass->get("db_username"),
'password' => $this->configClass->get("db_password"),
'charset' => 'utf8'
)));
$this->configClass->setDatabase($this->setUpDatabase());
$storeValue = rand(0, 1000);
$this->configClass->set('testSetNoDB', $storeValue, true);
$this->configClass->set('test' . __METHOD__ . 'DB' . $storeValue, $storeValue, true);
$settingsValue = $this->configClass->get('testSetNoDB', 'Not Stored', true);
$settingsValue = $this->configClass->get('test' . __METHOD__ . 'DB' . $storeValue, 'Not Stored', true);
$this->assertSame($storeValue, $settingsValue);
$this->configClass->del('test' . __METHOD__ . 'DB' . $storeValue, true);
}
/**
* @covers \Core\Config::del
*/
public function testDelInDB()
{
$this->configClass->setDatabase($this->setUpDatabase());
$storeValue = rand(0, 1000);
$this->configClass->set('test' . __METHOD__ . 'DB' . $storeValue, $storeValue, true);
$dbAction = $this->convertNumberToBool($this->configClass->del('test' . __METHOD__ . 'DB' . $storeValue,
true));
$this->assertTrue($dbAction);
}
/**
......@@ -103,25 +155,30 @@ namespace Core\Tests;
{
$ownerFuid = $this->configClass->get("ownerFuid");
$this->configClass->setDatabase(new medoo(array(
'database_type' => 'mysql',
'database_name' => $this->configClass->get("db_name"),
'server' => $this->configClass->get("db_server"),
'username' => $this->configClass->get("db_username"),
'password' => $this->configClass->get("db_password"),
'charset' => 'utf8'
)));
$this->configClass->setDatabase($this->setUpDatabase());
$storeValue = rand(0, 1000);
$dbAction = $this->configClass->setUser($ownerFuid, 'testSetNoDB', $storeValue);
if (is_numeric($dbAction)) {
if ($dbAction == 1) {
$dbAction = true;
} else {
$dbAction = false;
}
}
$dbAction = $this->convertNumberToBool($this->configClass->setUser($ownerFuid,
'test' . __METHOD__ . 'DB' . $storeValue, $storeValue));
$this->assertTrue($dbAction);
$this->configClass->delUser($ownerFuid, 'test' . __METHOD__ . 'DB' . $storeValue);
}
/**
* @covers \Core\Config::del
*/
public function testUserDelNoDB()
{
$ownerFuid = $this->configClass->get("ownerFuid");
$this->configClass->setDatabase($this->setUpDatabase());
$storeValue = rand(0, 1000);
$this->configClass->setUser($ownerFuid, 'test' . __METHOD__ . 'DB' . $storeValue, $storeValue);
$dbAction = $this->convertNumberToBool($this->configClass->delUser($ownerFuid,
'test' . __METHOD__ . 'DB' . $storeValue));
$this->assertTrue($dbAction);
}
......@@ -132,21 +189,16 @@ namespace Core\Tests;
{
$ownerFuid = $this->configClass->get("ownerFuid");
$this->configClass->setDatabase(new medoo(array(
'database_type' => 'mysql',
'database_name' => $this->configClass->get("db_name"),
'server' => $this->configClass->get("db_server"),
'username' => $this->configClass->get("db_username"),
'password' => $this->configClass->get("db_password"),
'charset' => 'utf8'
)));
$this->configClass->setDatabase($this->setUpDatabase());
$storeValue = rand(0, 1000);
$this->configClass->setUser($ownerFuid, 'testSetNoDB', $storeValue, true);
$this->configClass->setUser($ownerFuid, 'test' . __METHOD__ . 'DB' . $storeValue, $storeValue);
$settingsValue = $this->configClass->getUser($ownerFuid, 'testSetNoDB', 'Not Stored');
$settingsValue = $this->configClass->getUser($ownerFuid, 'test' . __METHOD__ . 'DB' . $storeValue,
'Not Stored');
$this->assertSame($storeValue, $settingsValue);
}
$this->configClass->delUser($ownerFuid, 'test' . __METHOD__ . 'DB' . $storeValue);
}
}
<?php
/**
* Copyright (c) 2017. Stuart McCulloch Anderson
*/
/*******************************************************************************
* This file is part of NxFIFTEEN Fitness Core.
* https://nxfifteen.me.uk
*
* Copyright (c) 2017, Stuart McCulloch Anderson
*
* Released under the MIT license
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
namespace Core\Tests;
namespace Core\Tests;
use Core\Analytics\UserAnalytics;
use PHPUnit\Framework\TestCase;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment