Passed
Push — master ( 9ccabb...39bdf1 )
by Petr
07:56
created

ConnectTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 44
c 3
b 0
f 0
dl 0
loc 87
rs 10
wmc 6
1
<?php
2
3
namespace KwTests;
4
5
6
use kalanis\kw_connect\arrays;
7
use kalanis\kw_connect\core\ConnectException;
8
use kalanis\kw_connect\core\Interfaces\IOrder;
9
use kalanis\kw_connect\records;
10
use kalanis\kw_connect\search;
11
use kalanis\kw_mapper\MapperException;
12
13
14
/**
15
 * Class ConnectTest
16
 * @package KwTests
17
 * @requires extension PDO
18
 * @requires extension pdo_sqlite
19
 */
20
class ConnectTest extends AKwTests
21
{
22
    /**
23
     * @throws ConnectException
24
     */
25
    public function testConnectorRecords()
26
    {
27
        $lib = new records\Connector($this->rows());
28
        $lib->setFiltering('target', '', 'any'); // filter type not need for this one
29
        $lib->setOrdering('name', IOrder::ORDER_DESC);
30
        $lib->setPagination(2, 2);
31
        $this->assertEquals(3, $lib->getTotalCount());
32
        $lib->setOrdering('counter', IOrder::ORDER_ASC);
33
        $lib->fetchData();
34
        $this->assertNotEmpty($lib->getFilterFactory());
35
    }
36
37
    /**
38
     * @throws ConnectException
39
     * @throws MapperException
40
     */
41
    public function testConnectorSearch()
42
    {
43
        $lib = new search\Connector(new \kalanis\kw_mapper\Search\Search(new XTestRecord()));
44
        $lib->setFiltering('target', arrays\Filters\Factory::ACTION_MULTIPLE, [ // value
45
            [ // row with another filter -> filter type, value to compare
46
                arrays\Filters\Factory::ACTION_MULTIPLE, [ // another multiple with its inner filters as array in value
47
                    [arrays\Filters\Factory::ACTION_EXACT, 'any'], // inner filters in multiple filter -> filter type, value to compare
48
                ]
49
            ],
50
        ]);
51
        $this->assertEquals(6, $lib->getTotalCount());
52
        $lib->setOrdering('name', IOrder::ORDER_ASC);
53
        $lib->setPagination(1, 4);
54
        $lib->fetchData();
55
        $this->assertEquals(4, count(iterator_to_array($lib)));
56
    }
57
58
    /**
59
     * @throws ConnectException
60
     */
61
    public function testConnectorRec2()
62
    {
63
        $lib = new records\Connector([]); // empty source
64
        $this->assertEquals(0, $lib->getTotalCount());
65
    }
66
67
    /**
68
     * @throws ConnectException
69
     */
70
    public function testConnectorData1()
71
    {
72
        $lib = new records\Connector($this->rows());
73
        $lib->setOrdering('id', IOrder::ORDER_ASC);
74
        $lib->fetchData();
75
        $this->assertEquals(5, $lib->getTotalCount());
76
        $content = iterator_to_array($lib);
77
        $this->assertEquals('emil', $content[0]->getValue('name'));
78
        $this->assertEquals('josh', $content[1]->getValue('name'));
79
        $this->assertEquals('ewan', $content[2]->getValue('name'));
80
        $this->assertEquals('kami', $content[3]->getValue('name'));
81
    }
82
83
    /**
84
     * @throws ConnectException
85
     */
86
    public function testConnectorData2()
87
    {
88
        $lib = new records\Connector($this->rows());
89
        $lib->setOrdering('id', IOrder::ORDER_DESC);
90
        $lib->fetchData();
91
        $this->assertEquals(5, $lib->getTotalCount());
92
        $content = iterator_to_array($lib);
93
        $this->assertEquals('chuck', $content[0]->getValue('name'));
94
        $this->assertEquals('kami', $content[1]->getValue('name'));
95
        $this->assertEquals('ewan', $content[2]->getValue('name'));
96
        $this->assertEquals('josh', $content[3]->getValue('name'));
97
    }
98
99
    protected function rows(): array
100
    {
101
        return [
102
            $this->loadedRec(3),
103
            $this->loadedRec(4),
104
            $this->loadedRec(5),
105
            $this->loadedRec(6),
106
            $this->loadedRec(7),
107
        ];
108
    }
109
}
110