Completed
Push — master ( 6a8432...b81c5a )
by Adam
05:46
created

GridTest::testAddColumnWithDecorators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use Boduch\Grid\Grid;
4
5
class GridTest extends GridBuilderTestCase
6
{
7
    public function testAddColumn()
8
    {
9
        $grid = new Grid($this->gridHelper);
10
        $grid->addColumn('name', [
11
            'title' => 'First name'
12
        ]);
13
        $grid->addColumn('sex', [
14
            'title' => 'Sex',
15
            'sortable' => true
16
        ]);
17
18
        $this->assertInstanceOf(\Boduch\Grid\Column::class, $grid->getColumns()['name']);
19
        $this->assertEquals('First name', $grid->getColumns()['name']->getTitle());
20
21
        $this->assertInstanceOf(\Boduch\Grid\Column::class, $grid->getColumns()['sex']);
22
        $this->assertEquals('Sex', $grid->getColumns()['sex']->getTitle());
23
        $this->assertTrue($grid->getColumns()['sex']->isSortable());
24
    }
25
26
    public function testAddColumnWithDecorators()
27
    {
28
        $grid = new Grid($this->gridHelper);
29
        $grid->addColumn('name', [
30
            'title' => 'First name',
31
            'clickable' => function () {
32
                return '';
33
            },
34
            'decorators' => [
35
                new \Boduch\Grid\Decorators\Url()
36
            ]
37
        ]);
38
39
        $column = $grid->getColumns()['name'];
40
41
        $this->assertEquals(2, count($column->getDecorators()));
42
        $this->assertInstanceOf(\Boduch\Grid\Decorators\DecoratorInterface::class, $column->getDecorators()[0]);
43
        $this->assertInstanceOf(\Boduch\Grid\Decorators\DecoratorInterface::class, $column->getDecorators()[1]);
44
    }
45
46
    public function testRenderColumnWithDecorator()
47
    {
48
        $grid = $this->getSampleGrid();
49
        $rows = $grid->getRows();
50
51
        $this->assertInstanceOf(\Boduch\Grid\Rows::class, $rows);
52
        $this->assertInstanceOf(\Boduch\Grid\Row::class, $rows[0]);
53
54
        $this->assertEquals("<a href=\"http://4programmers.net\">\nhttp://4programmers.net\n</a>\n", (string) $rows[0]->getValue('website'));
55
        $this->assertEquals("<a href=\"http://4programmers.net\">1</a>", (string) $rows[0]->getValue('id'));
56
    }
57
58
    public function testBuildGridWithEachCallbackAndModifyColumnValue()
59
    {
60
        $grid = $this->getSampleGrid();
61
        $grid->after(function (\Boduch\Grid\Row $row) {
62
            $row->get('website')->setValue('');
63
        });
64
65
        $rows = $grid->getRows();
66
67
        $this->assertEquals('', (string) $rows[0]->getValue('website'));
68
    }
69
70
    public function testBuildGridAndAddRowClass()
71
    {
72
        $grid = $this->getSampleGrid();
73
        $grid->after(function (\Boduch\Grid\Row $row) {
74
            $row->class = 'foo';
75
        });
76
77
        $rows = $grid->getRows();
78
79
        $this->assertEquals('foo', (string) $rows[0]->class);
80
    }
81
82
    public function tesAutoescapeCellOutput()
83
    {
84
        $grid = new Grid($this->gridHelper);
85
        $grid->addColumn('xss', [
86
            'title' => 'xss'
87
        ]);
88
89
        $collection = collect([
90
            ['xss' => '<xss>']
91
        ]);
92
93
        $source = new \Boduch\Grid\Source\CollectionSource($collection);
94
        $grid->setSource($source);
95
96
        $rows = $grid->getRows();
97
98
        $this->assertEquals('&lt;xss&gt;', (string) $rows[0]->xss);
99
    }
100
101
    private function getSampleGrid()
102
    {
103
        $grid = new Grid($this->gridHelper);
104
        $grid->addColumn('website', [
105
            'title' => 'Website',
106
            'decorators' => [
107
                new \Boduch\Grid\Decorators\Url()
108
            ]
109
        ]);
110
        $grid->addColumn('id', [
111
            'title' => 'ID',
112
            'clickable' => function ($row) {
113
                return '<a href="http://4programmers.net">' . $row['id'] . '</a>';
114
            }
115
        ]);
116
117
        $collection = collect([
118
            ['id' => 1, 'website' => 'http://4programmers.net']
119
        ]);
120
121
        $source = new \Boduch\Grid\Source\CollectionSource($collection);
122
        $grid->setSource($source);
123
124
        return $grid;
125
    }
126
}
127