summaryrefslogtreecommitdiffstats
path: root/tests/TestDataHelper.php
blob: 99acc1cb3bc50fa5a329e2266eb4bf5f6d7d3547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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),true);
    }

    private function correctNoEndRecType($type){
        $noPos = strpos($type, "#no");
        if($noPos){
            $type = substr_replace($type, "", $noPos + 1);
        }
        return $type;
    }

    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) {
            $hVal = isset($helperObj[$key]) ? $helperObj[$key] : "";
            $sVal = isset($schedObj[$key]) ? $schedObj[$key] : "";
            if ($key == "rec_type") {
                $sVal = $this->correctNoEndRecType($sVal);
                $hVal = $this->correctNoEndRecType($hVal);
            } elseif ($key == "event_length" || $key == "event_pid") {
                $sVal = $sVal == 0 ? "" : $sVal;
                $hVal = $hVal == 0 ? "" : $hVal;
            }
            if ($hVal != $sVal) {
                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;
    }

    public function writeObjectToFile($obj, $name, $bunch="")
    {
        ob_start();
        var_dump($obj);
        $output = ob_get_clean();
        if ($bunch)
            $bunch = "/$bunch";
        $file = dirname(__FILE__) . "/" . $this->_dataFolder . $bunch . "/" . $name;
        file_put_contents($file, $output);
    }

    public function prepateDataForHelper($data, $helper)
    {
        foreach ($data as &$event) {
            if (!isset($event["end_date"]) && isset($event["recurring_type"]) && isset($event["length"])) {
                $event["end_date"] = $helper->getRecurringEndDateStr($event["recurring_type"], $event["start_date"], $event["length"]);
            }
        }

        return $data;
    }

    public function saveDataWithHelper($data, $helper)
    {
        foreach($data as $event){
            $helper->saveData($event);
        }
    }
}