Completed
Push — master ( 141805...6a8432 )
by Adam
02:45
created

testBuildGridWithEachCallbackAndModifyColumnValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
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->each(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->each(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
    private function getSampleGrid()
83
    {
84
        $grid = new Grid($this->gridHelper);
85
        $grid->addColumn('website', [
86
            'title' => 'Website',
87
            'decorators' => [
88
                new \Boduch\Grid\Decorators\Url()
89
            ]
90
        ]);
91
        $grid->addColumn('id', [
92
            'title' => 'ID',
93
            'clickable' => function ($row) {
94
                return '<a href="http://4programmers.net">' . $row['id'] . '</a>';
95
            }
96
        ]);
97
98
        $collection = collect([
99
            ['id' => 1, 'website' => 'http://4programmers.net']
100
        ]);
101
102
        $source = new \Boduch\Grid\Source\CollectionSource($collection);
103
        $grid->setSource($source);
104
105
        return $grid;
106
    }
107
}
108