diff options
author | sten <stenmarsh938@gmail.com> | 2016-03-17 12:54:26 +0300 |
---|---|---|
committer | sten <stenmarsh938@gmail.com> | 2016-03-17 12:54:26 +0300 |
commit | 61650522ee83433656535a48dde9be2d9b86bd35 (patch) | |
tree | be9b527234c88394c2d2ad92c27f7dd72a4610f3 /tests | |
parent | 888ede15fc86db7001fa4d28bc2a686ed41315a3 (diff) | |
download | scheduler-helper-php-61650522ee83433656535a48dde9be2d9b86bd35.zip scheduler-helper-php-61650522ee83433656535a48dde9be2d9b86bd35.tar.gz scheduler-helper-php-61650522ee83433656535a48dde9be2d9b86bd35.tar.bz2 |
initialLoad
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Data_getData/DayRecurrings/source.json | 19 | ||||
-rw-r--r-- | tests/Data_getData/DayRecurrings/target.json | 34 | ||||
-rw-r--r-- | tests/Logger.php | 33 | ||||
-rw-r--r-- | tests/SchedulerHelperTest.php | 76 | ||||
-rw-r--r-- | tests/TestBaseHelper.php | 102 | ||||
-rw-r--r-- | tests/TestConfig.php | 20 | ||||
-rw-r--r-- | tests/TestDataHelper.php | 83 |
7 files changed, 367 insertions, 0 deletions
diff --git a/tests/Data_getData/DayRecurrings/source.json b/tests/Data_getData/DayRecurrings/source.json new file mode 100644 index 0000000..345ba0f --- /dev/null +++ b/tests/Data_getData/DayRecurrings/source.json @@ -0,0 +1,19 @@ +{ + "settings": { + "start_date": "2016-03-09 00:00:00", + "end_date": "2016-03-13 23:00:00" + }, + "data": [ + { + "start_date": "2016-03-09 00:00:00", + "end_date": "2016-03-15 00:00:00", + "text": "Rec Ev", + "id": "497", + "event_pid": "", + "event_length": "300", + "rec_pattern": "day_2___", + "rec_type": "day_2___#3", + "!nativeeditor_status": "" + } + ] +}
\ No newline at end of file diff --git a/tests/Data_getData/DayRecurrings/target.json b/tests/Data_getData/DayRecurrings/target.json new file mode 100644 index 0000000..3d7c4d3 --- /dev/null +++ b/tests/Data_getData/DayRecurrings/target.json @@ -0,0 +1,34 @@ +{ + "data": [ + { + "start_date": "2016-03-09 00:00:00", + "end_date": "2016-03-09 00:05:00", + "event_pid": "497", + "text": "Rec Ev", + "id": "497", + "_timed": true, + "rec_type": "day_2___#3", + "event_length": "300" + }, + { + "start_date": "2016-03-11 00:00:00", + "end_date": "2016-03-11 00:05:00", + "event_pid": "497", + "text": "Rec Ev", + "id": "497", + "_timed": true, + "rec_type": "day_2___#3", + "event_length": "300" + }, + { + "start_date": "2016-03-13 00:00:00", + "end_date": "2016-03-13 00:05:00", + "event_pid": "497", + "text": "Rec Ev", + "id": "497", + "_timed": true, + "rec_type": "day_2___#3", + "event_length": "300" + } + ] +}
\ No newline at end of file diff --git a/tests/Logger.php b/tests/Logger.php new file mode 100644 index 0000000..115b624 --- /dev/null +++ b/tests/Logger.php @@ -0,0 +1,33 @@ +<?php + +class Logger +{ + private $_stepNumber = 0; + + public function logStart($name = NULL) + { + $this->_stepNumber = 0; + print "Test started. $name\n"; + } + + public function logStep(){ + print "Step ".(++$this->_stepNumber)."\n"; + } + + public function logEnd($name = NULL){ + $this->_stepNumber = 0; + print "Test finished. $name\n"; + } + + public function info($msg){ + print "INFO: $msg\n"; + } + + public function warning($msg){ + print "WARNING: $msg\n"; + } + + public function error($msg){ + print "ERROR: $msg\n"; + } +}
\ No newline at end of file diff --git a/tests/SchedulerHelperTest.php b/tests/SchedulerHelperTest.php new file mode 100644 index 0000000..42a1111 --- /dev/null +++ b/tests/SchedulerHelperTest.php @@ -0,0 +1,76 @@ +<?php +require_once "Logger.php"; +require_once "TestBaseHelper.php"; +require_once "TestDataHelper.php"; +require_once "TestConfig.php"; +use DHTMLX_Scheduler\Helper; + + +class SchedulerHelperTest extends \PHPUnit_Framework_TestCase +{ + private $_baseHelper; + private $_logger; + + public function __construct() + { + $this->_logger = new Logger(); + + $this->_baseHelper = new TestBaseHelper(array( + "dbsm" => TestConfig::DBSM, + "host" => TestConfig::HOST, + "db_name" => TestConfig::DB_NAME, + "user" => TestConfig::USER, + "password" => TestConfig::PASSWORD, + "table_name" => TestConfig::TEMP_TABLE_NAME, + "fields" => TestConfig::$fields + )); + } + + public function testGetData() + { + $testName = "getData"; + + $this->_logger->logStart($testName); + $schedHelper = new Helper( + array( + "dbsm" => TestConfig::DBSM, + "host" => TestConfig::HOST, + "db_name" => TestConfig::DB_NAME, + "user" => TestConfig::USER, + "password" => TestConfig::PASSWORD, + "table_name" => TestConfig::TEMP_TABLE_NAME + ) + ); + $schedHelper->setFieldsNames(array( + $schedHelper::FLD_ID => "id", + )); + + + $dataHelp = new TestDataHelper($testName); + $dataPacks = $dataHelp->getTestDataList(); + + if($dataPacks) { + for ($i = 0; $i < count($dataPacks); $i++) { + $this->_logger->logStep($testName); + $this->_logger->info("$dataPacks[$i] bunch processing...."); + $source = $dataHelp->getTestSourceData($dataPacks[$i]); + $target = $dataHelp->getTestTargetData($dataPacks[$i]); + + if(!$source || !$target){ + $this->_logger->warning("There is no data. Bunch is skipped"); + continue; + } + + $this->_baseHelper->resetTable(); + $this->_baseHelper->insertDataFromJSON($source->{'data'}); + + $helperData = $schedHelper->getData($source->settings->start_date, $source->settings->end_date); + var_dump($helperData); exit; + $this->assertTrue($dataHelp->compareDataBunches($helperData, $target->data, TestConfig::$fields), + "Helper and Scheduler data has difference"); + } + } + + $this->_logger->logEnd($testName); + } +}
\ No newline at end of file diff --git a/tests/TestBaseHelper.php b/tests/TestBaseHelper.php new file mode 100644 index 0000000..953e4c6 --- /dev/null +++ b/tests/TestBaseHelper.php @@ -0,0 +1,102 @@ +<?php + +class TestBaseHelper +{ + private $_dbsm, $_host, $_db_name, $_user, $_password, $_table_name; + private $_PDO = null; + + private $_fields; + + public function __construct($configs = array()) + { + $this->_dbsm = isset($configs["dbsm"]) ? $configs["dbsm"] : "mysql"; + $this->_host = isset($configs["host"]) ? $configs["host"] : "localhost"; + $this->_db_name = $configs["db_name"]; + $this->_user = $configs["user"]; + $this->_password = $configs["password"]; + $this->_table_name = $configs["table_name"]; + $this->_fields = $configs["fields"]; + } + + private function getConnection() + { + $PDO_options = array( + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC + ); + + $dsn = $this->_dbsm . ":host=" . $this->_host . ";dbname=" . $this->_db_name; + $this->_PDO = ($this->_PDO) ? $this->_PDO : new PDO($dsn, $this->_user, $this->_password,$PDO_options); + return $this->_PDO; + } + + private function closeConnection() + { + unset($this->_PDO); + $this->_PDO = null; + } + + public function dropTable() + { + $conn = $this->getConnection(); + $conn->prepare("DROP TABLE IF EXISTS `$this->_table_name`;")->execute(); + $this->closeConnection(); + } + + public function resetTable() + { + $this->dropTable(); + $conn = $this->getConnection(); + $sql = "CREATE TABLE `$this->_table_name`("; + $f = $this->_fields; + $fieldsCount = count($f); + $elNumb = 1; + + foreach($f as $name=>$type) { + $sql .= "`$name` $type"; + if ($elNumb++ != $fieldsCount) + $sql .= ","; + } + $sql .= ");"; + + $conn->prepare($sql)->execute(); + $this->closeConnection(); + } + + public function insertDataFromJSON($data){ + $f = $this->_fields; + $fieldsCount = count($f); + + $sql = "INSERT INTO `$this->_table_name`("; + + $elNumb = 1; + foreach($f as $name=>$type) { + $sql .= "`$name`"; + if ($elNumb++ != $fieldsCount) + $sql .= ","; + } + + $sql.= ") VALUES "; + + $evsCount = count($data); + for($i = 0; $i < $evsCount; $i++) { + $sql .= "("; + $event = $data[$i]; + $elNumb = 1; + foreach($f as $name=>$type) { + $sql .= isset($event->{$name}) && !is_null($event->{$name}) ? "'".$event->{$name}."'":"''"; + + if ($elNumb++ != $fieldsCount) + $sql .= ","; + } + $sql.=")"; + if($i + 1 != $evsCount) + $sql.=","; + } + $sql .= ";"; + + $conn = $this->getConnection(); + $conn->prepare($sql)->execute(); + $this->closeConnection(); + } +}
\ No newline at end of file diff --git a/tests/TestConfig.php b/tests/TestConfig.php new file mode 100644 index 0000000..f7f0985 --- /dev/null +++ b/tests/TestConfig.php @@ -0,0 +1,20 @@ +<?php + +class TestConfig +{ + const DBSM = "mysql"; + const HOST = "localhost"; + const USER = "root"; + const PASSWORD = "root"; + const DB_NAME = "scheduler"; + const TEMP_TABLE_NAME = "temp_table_20160315"; + public static $fields = array( + "id" => "INT(11) NOT NULL", + "start_date" => "DATETIME NOT NULL", + "end_date" => "DATETIME NOT NULL", + "text" => "VARCHAR(255) NOT NULL", + "rec_type" => "VARCHAR(50) DEFAULT NULL", + "event_length" => " BIGINT(20) DEFAULT NULL", + "event_pid" => "INT(11) DEFAULT NULL" + ); +}
\ No newline at end of file diff --git a/tests/TestDataHelper.php b/tests/TestDataHelper.php new file mode 100644 index 0000000..7f9f8d9 --- /dev/null +++ b/tests/TestDataHelper.php @@ -0,0 +1,83 @@ +<?php +require_once "TestConfig.php"; + +class TestDataHelper +{ + const DATA_FOLDER_PREFIX = "Data_"; + const SOURCE_NAME = "source.json"; + const TARGET_NAME = "target.json"; + + private $_dataFolder; + + private function getJSONDataFromFile($name, $type) + { + $file = dirname(__FILE__) . "/" . $this->_dataFolder . "/" . $name . "/" . $type; + if (!file_exists($file)) return null; + + return json_decode(file_get_contents($file)); + } + + public function __construct($testName) + { + $this->_dataFolder = self::DATA_FOLDER_PREFIX . $testName; + } + + public function getTestDataList() + { + $dir = dirname(__FILE__) . "/" . $this->_dataFolder; + if (!file_exists($dir)) return null; + $folderItems = scandir($dir); + $folders = array(); + foreach ($folderItems as $item) { + if ($item === '.' || $item === '..') continue; + if (is_dir($dir . "/" . $item)) + array_push($folders, $item); + } + return $folders; + } + + public function getTestSourceData($name) + { + return $this->getJSONDataFromFile($name, self::SOURCE_NAME); + } + + public function getTestTargetData($name) + { + return $this->getJSONDataFromFile($name, self::TARGET_NAME); + } + + public function compareDataObjects($helperObj, $schedObj, $fields) + { + foreach($fields as $key=>$value){ + print($helperObj[$key]." _ ".$schedObj -> {$key}."\n"); + if($helperObj[$key] !== $schedObj -> {$key}) + return false; + } + return true; + } + + public function compareDataBunches($helperData, $schedData, $fields) + { + $helpLength = count($helperData); + $schedLength = count($schedData); + + if($helpLength != $schedLength) return false; + + for($i = 0; $i < $helpLength; $i++){ + $objHasSame = false; + for($j = 0; $j < $schedLength; $j++){ + if($this->compareDataObjects($helperData[$i], $schedData[$j], $fields)) { + array_splice($schedData, $j, 1); + $schedLength = count($schedData); + $objHasSame = true; + break; + } + } + if(!$objHasSame){ + return false; + } + } + + return true; + } +}
\ No newline at end of file |