1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\View; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Symfony\Component\Form\FormView; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Povilas Margaiatis <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class ListViewTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private $pagerViewMock; |
14
|
|
|
private $formViewMock; |
15
|
|
|
private $routerViewMock; |
16
|
|
|
private $headerRowMock; |
17
|
|
|
private $bodyRowMock; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->pagerViewMock = $this->createMock(PagerView::class); |
22
|
|
|
$this->formViewMock = $this->createMock(FormView::class); |
23
|
|
|
$this->routerViewMock = $this->createMock(RouterView::class); |
24
|
|
|
$this->headerRowMock = $this->createMock(RowView::class); |
25
|
|
|
$this->bodyRowMock = $this->createMock(RowView::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetHeaderRow(): void |
29
|
|
|
{ |
30
|
|
|
$listView = $this->getListView(); |
31
|
|
|
$this->assertEquals($this->headerRowMock, $listView->getHeaderRow()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetBodyRowsPaged(): void |
35
|
|
|
{ |
36
|
|
|
$this->pagerViewMock->expects($this->once()) |
37
|
|
|
->method('getData') |
38
|
|
|
->willReturn([ |
39
|
|
|
['field1' => 'foo', 'field2' => 'bar'], |
40
|
|
|
['field1' => 'foo', 'field2' => 'bar'] |
41
|
|
|
]); |
42
|
|
|
$this->bodyRowMock->expects($this->exactly(2)) |
43
|
|
|
->method('init') |
44
|
|
|
->with( |
45
|
|
|
$this->isInstanceOf(ListView::class), |
46
|
|
|
'body', |
47
|
|
|
$this->equalTo(['field1' => 'foo', 'field2' => 'bar']) |
48
|
|
|
); |
49
|
|
|
$view = $this->getListView(); |
50
|
|
|
$this->assertCount(2, $view->getBodyRows(true)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetBodyRowsNotPaged(): void |
54
|
|
|
{ |
55
|
|
|
$firstPage = [ |
56
|
|
|
['field1' => 'foo', 'field2' => 'bar'], |
57
|
|
|
['field1' => 'foo', 'field2' => 'bar'] |
58
|
|
|
]; |
59
|
|
|
$secondPage = [ |
60
|
|
|
['field1' => 'foo', 'field2' => 'bar'], |
61
|
|
|
['field1' => 'foo', 'field2' => 'bar'] |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$this->pagerViewMock->expects($this->exactly(2)) |
65
|
|
|
->method('getData') |
66
|
|
|
->willReturnOnConsecutiveCalls($firstPage, $secondPage); |
67
|
|
|
$this->pagerViewMock->expects($this->exactly(2)) |
68
|
|
|
->method('iterateNextPage') |
69
|
|
|
->willReturnOnConsecutiveCalls(true, false); |
70
|
|
|
$this->bodyRowMock->expects($this->exactly(4)) |
71
|
|
|
->method('init') |
72
|
|
|
->with( |
73
|
|
|
$this->isInstanceOf(ListView::class), |
74
|
|
|
'body', |
75
|
|
|
$this->equalTo(['field1' => 'foo', 'field2' => 'bar']) |
76
|
|
|
); |
77
|
|
|
$view = $this->getListView(); |
78
|
|
|
$this->assertCount(4, $view->getBodyRows(false)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGetPager(): void |
82
|
|
|
{ |
83
|
|
|
$this->assertEquals($this->pagerViewMock, $this->getListView()->getPager()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGetRouter(): void |
87
|
|
|
{ |
88
|
|
|
$this->assertEquals($this->routerViewMock, $this->getListView()->getRouter()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetFilter(): void |
92
|
|
|
{ |
93
|
|
|
$this->assertEquals($this->formViewMock, $this->getListView()->getFilter()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGetFieldViews(): void |
97
|
|
|
{ |
98
|
|
|
$fieldViews = ['view1', 'view2']; |
99
|
|
|
$this->assertEquals($fieldViews, $this->getListView($fieldViews)->getFieldViews()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $fieldViews |
104
|
|
|
* |
105
|
|
|
* @return ListView |
106
|
|
|
*/ |
107
|
|
|
private function getListView(array $fieldViews = []): ListView |
108
|
|
|
{ |
109
|
|
|
$this->headerRowMock->expects($this->once()) |
110
|
|
|
->method('init') |
111
|
|
|
->with($this->isInstanceOf(ListView::class), 'header', null); |
112
|
|
|
|
113
|
|
|
return new ListView( |
114
|
|
|
$this->pagerViewMock, |
115
|
|
|
$this->formViewMock, |
116
|
|
|
$this->routerViewMock, |
117
|
|
|
$this->headerRowMock, |
118
|
|
|
$this->bodyRowMock, |
119
|
|
|
$fieldViews |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|