Passed
Branchdev (6beeb4)
by Plamen
02:31
created

TableTest::testprepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
5
class TableTest extends TestCase
6
{
7
    /**
8
     * @covers Table::prepare
9
     * @covers Table::confirmValue
10
     */
11
    public function testprepare()
12
    {
13
        $_SERVER['REQUEST_URI'] = 'test.json';
14
15
        //Table::prepare(true);
16
17
        Table::prepare();
18
        $this->assertSame(Table::confirmValue('t.slug', 'test.json'), true);
19
        $this->assertSame(Table::$pageExt, 'json');
20
21
        $_SERVER['REQUEST_URI'] = null;
22
23
        Table::prepare();
24
        $this->assertSame(Table::confirmValue('t.slug', ''), true);
25
        $this->assertSame(Table::$pageExt, '');
26
    }
27
28
    /**
29
     * @covers Table::prepare
30
     * @covers Table::create
31
     * @covers Table::error
32
     * @covers Table::config
33
     */
34
    public function testcreate()
35
    {
36
        $_SERVER['REQUEST_URI'] = 'test/users';
37
38
        Table::prepare();
39
40
        $this->assertSame(Table::confirmValue('t.page', 1), true);
41
        $this->assertSame(Table::confirmValue('t.tables', []), true);
42
43
        $items = 'users';
44
        $orderBy = 'id';
45
46
        Table::create($items, $orderBy, 'a');
47
        $this->assertSame(Table::confirmValue('t.items', $items), true);
48
        $this->assertSame(Table::confirmValue('t.order.col', $orderBy), true);
49
        $err = Table::confirmValue('errors.0', 'Invalid orderDir (Asc/Desc): a');
50
        $this->assertSame($err, true);
51
52
        Table::create($items, $orderBy, 'asc', 2);
53
        $this->assertSame(Table::confirmValue('t.items', $items), true);
54
        $this->assertSame(Table::confirmValue('t.order.col', $orderBy), true);
55
        $this->assertSame(Table::confirmValue('t.order.dir', 'asc'), true);
56
        $err1 = Table::confirmValue('errors.∞', 'Invalid paging (<10): 2');
57
        $this->assertSame($err1, true);
58
    }
59
60
    /**
61
     * @covers Table::execute
62
     * @covers Table::request
63
     * @covers Table::filter
64
     * @covers Table::filterByAll
65
     * @covers Table::orderCol
66
     * @covers Table::orderDir
67
     * @covers Table::setExport
68
     * @covers Table::page
69
     */
70
    public function testexecute()
71
    {
72
        $_SERVER['REQUEST_URI'] = 'test/users';
73
74
        Table::prepare();
75
76
        $items = 'users';
77
        $orderBy = 'id';
78
        Table::create($items, $orderBy);
79
80
        $query = 'SELECT `id` FROM `table`';
81
        Table::execute($query);
82
        $this->assertSame(Table::$export, false);
83
    }
84
85
    /**
86
     * @covers Table::load
87
     * @covers Thead::load
88
     * @covers Thead::filterValues
89
     * @covers Thead::tagAttributes
90
     * @covers Thead::ths
91
     * @covers Thead::thAttributes
92
     * @covers Thead::thTag
93
     */
94
    public function testload()
95
    {
96
        $_SERVER['REQUEST_URI'] = 'test/articles';
97
98
        Table::prepare();
99
100
        $items = 'articles';
101
        $orderBy = 'id';
102
        Table::create($items, $orderBy);
103
        Table::$cols = [['id']];
104
        $query = 'SELECT `id` FROM `articles`';
105
        Table::execute($query);
106
107
        Table::load();
108
        $regex = '/(.*)$/m';
109
        $this->expectOutputRegex($regex);
110
    }
111
112
    /**
113
     * @codeCoverageIgnore
114
     */
115
    public static function prepare($classMethod, $setOrCheck = false)
116
    {
117
        self::$pageExtension = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_EXTENSION) ?: null;
118
        $continuePreparation = $setOrCheck === false;
119
        if ($setOrCheck === true) {
120
            if (!in_array($classMethod, self::$preparePassed) && empty(self::$pageExtension)) {
121
                die('ERROR (in a '.__CLASS__.' call): Please call the '.$classMethod.'(); method before the page headers sent.');
122
            }
123
        } elseif (!empty($setOrCheck)) {
124
            self::$pageExtension = $setOrCheck;
125
        }
126
        if (!in_array($classMethod, self::$preparePassed)) {
127
            self::$preparePassed[] = $classMethod;
128
        }
129
130
        return $continuePreparation;
131
    }
132
133
    public static $rustart;
134
    public static $pageExtension;
135
    public static $preparePassed = [];
136
137
    /**
138
     * @codeCoverageIgnore
139
     */
140
    public function setUp(): void
141
    {
142
        Table::$helperClass = __CLASS__;
143
        parent::setUp();
144
    }
145
}
146