SaveTest.php 4.49 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 2/2/16
 * Time: 8:47 AM
 */

Marc Egger's avatar
Marc Egger committed
9
namespace IMATHUZH\Qfq\Tests\Unit\Core;
10

Carsten  Rose's avatar
Carsten Rose committed
11
12

use IMATHUZH\Qfq\Core\Helper\HelperFormElement;
Marc Egger's avatar
Marc Egger committed
13
14
15
16
use IMATHUZH\Qfq\Core\Save;
use IMATHUZH\Qfq\Tests\Unit\Core\Database\AbstractDatabaseTest;

require_once(__DIR__ . '/Database/AbstractDatabaseTest.php');
17

18
19
20
/**
 * Class SaveTest
 */
21
22
class SaveTest extends AbstractDatabaseTest {

23
    /**
Marc Egger's avatar
Marc Egger committed
24
     * @throws \CodeException
Carsten  Rose's avatar
Carsten Rose committed
25
     * @throws \DbException
Marc Egger's avatar
Marc Egger committed
26
27
     * @throws \UserFormException
     * @throws \UserReportException
28
     */
29
    public function testUpdateRecord() {
30

31
32
        $save = new Save([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), array());
//        $db = new Database();
33
34
35
36

        $values = ['name' => 'Doe', 'firstName' => 'John'];
        $id = $save->insertRecord('Person', $values);

37
38
        $values = ['name' => 'Doe', 'firstName' => 'Big John'];
        $save->updateRecord('Person', $values, $id);
39

40
        $sql = "SELECT name, firstName FROM Person WHERE id = ? ";
41
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, [$id]);
42

43
        $this->assertEquals($values, $result[0]);
44
45
46

    }

47
    /**
Marc Egger's avatar
Marc Egger committed
48
     * @throws \CodeException
Carsten  Rose's avatar
Carsten Rose committed
49
     * @throws \DbException
Marc Egger's avatar
Marc Egger committed
50
51
     * @throws \UserFormException
     * @throws \UserReportException
52
     */
53
    public function testInsertRecord() {
54

55
56
        $save = new Save([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), array());
//        $db = new Database();
57
58
59
60
61

        $values = ['name' => 'Doe', 'firstName' => 'John'];
        $id = $save->insertRecord('Person', $values);

        $sql = "SELECT name, firstName FROM Person WHERE id = ? ";
62

63
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, [$id]);
64
65
66

        $this->assertEquals($values, $result[0]);

Carsten  Rose's avatar
Carsten Rose committed
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
130
131
132
133
134
135
136
137
138
139
140
    }

    /**
     * Very basic test: Fake a form with two text inputs.
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
     */
    public function testSaveBasicText() {
        $formSpec = [
            F_DB_INDEX => DB_INDEX_DEFAULT,
            F_TABLE_NAME => 'Person',
            F_MULTI_SQL => '',
            F_PRIMARY_KEY => F_PRIMARY_KEY_DEFAULT,
        ];

        $feSpecNative = [
            [
                FE_ID => 1,
                FE_TYPE => FE_TYPE_TEXT,
                FE_NAME => 'name',
            ],
            [
                FE_ID => 2,
                FE_TYPE => FE_TYPE_TEXT,
                FE_NAME => 'firstName',
            ]
        ];

        // Clean
        $this->dbArray[DB_INDEX_DEFAULT]->sql('DELETE FROM Person WHERE name LIKE "DoeTmp"');

        // Prepare Setup
        $tableDefinition = $this->dbArray[DB_INDEX_DEFAULT]->getTableDefinition('Person');
        $this->store->fillStoreTableDefaultColumnType($tableDefinition);

        // Init FE
        $feSpecNative = HelperFormElement::formElementSetDefault($feSpecNative, $formSpec);
        // Fake POST value
        $this->store::setStore(['name' => 'DoeTmp', 'firstName' => 'John'], STORE_FORM, true);

        // ------------- Check -------------
        // 1) Expect INSERT into Person
        $save = new Save($formSpec, [], $feSpecNative, $feSpecNative);
        $save->process();

        // Get newly save record
        $sql = "SELECT name, firstName FROM Person WHERE name like 'DoeTmp'";
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_IMPLODE_ALL);

        // Compare
        $this->assertEquals('DoeTmpJohn', $result);


//        // ------------- Check -------------
//        $id=$this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id FROM Person WHERE name="DoeTmp"');
//        // Fake POST value
//        $this->store::setStore(['name' => 'DoeTmp', 'firstName' => 'JohnUpdate'], STORE_FORM, true);
//        $this->store::setVar(SIP_RECORD_ID, $id,STORE_SIP, true);
//
//        // 1) Expect UPDATE Person
//        $save = new Save($formSpec, [], $feSpecNative, $feSpecNative);
//        $save->process();
//
//        // Get newly save record
//        $sql = "SELECT name, firstName FROM Person WHERE name like 'DoeTmp'";
//        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_IMPLODE_ALL);
//
//        // Compare
//        $this->assertEquals('DoeTmpJohnUpdate' , $result);


141
142
143
    }

    /**
Marc Egger's avatar
Marc Egger committed
144
145
146
147
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
148
149
150
151
     */
    protected function setUp() {
        parent::setUp();

Marc Egger's avatar
Marc Egger committed
152
        $this->executeSQLFile(__DIR__ . '/Database/fixtures/Generic.sql', true);
153
154
155
    }

}