mockApplication(); } public function testEmptyListShown() { ob_start(); $this->getListView([ 'dataProvider' => new ArrayDataProvider(['allModels' => []]), 'emptyText' => 'Nothing at all', ])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE('
Nothing at all
', $out); } public function testEmpty() { ob_start(); $this->getListView([ 'dataProvider' => new ArrayDataProvider(['allModels' => []]), 'emptyText' => false, ])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE('
', $out); } public function testEmptyListNotShown() { ob_start(); $this->getListView([ 'dataProvider' => new ArrayDataProvider(['allModels' => []]), 'showOnEmpty' => true, ])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE(<<<'HTML'
HTML , $out); } /** * @param array $options * @return ListView */ private function getListView($options = []) { return new ListView(array_merge([ 'id' => 'w0', 'dataProvider' => $this->getDataProvider(), ], $options)); } /** * @return DataProviderInterface */ private function getDataProvider() { return new ArrayDataProvider([ 'allModels' => [ ['id' => 1, 'login' => 'silverfire'], ['id' => 2, 'login' => 'samdark'], ['id' => 3, 'login' => 'cebe'], ], ]); } public function testSimplyListView() { ob_start(); $this->getListView()->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE(<<<'HTML'
Showing 1-3 of 3 items.
0
1
2
HTML , $out); } public function testWidgetOptions() { ob_start(); $this->getListView(['options' => ['class' => 'test-passed'], 'separator' => ''])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE(<<<'HTML'
Showing 1-3 of 3 items.
0
1
2
HTML , $out); } public function itemViewOptions() { return [ [ null, '
Showing 1-3 of 3 items.
0
1
2
', ], [ function ($model, $key, $index, $widget) { return "Item #{$index}: {$model['login']} - Widget: " . $widget->className(); }, '
Showing 1-3 of 3 items.
Item #0: silverfire - Widget: yii\widgets\ListView
Item #1: samdark - Widget: yii\widgets\ListView
Item #2: cebe - Widget: yii\widgets\ListView
', ], [ '@yiiunit/data/views/widgets/ListView/item', '
Showing 1-3 of 3 items.
Item #0: silverfire - Widget: yii\widgets\ListView
Item #1: samdark - Widget: yii\widgets\ListView
Item #2: cebe - Widget: yii\widgets\ListView
', ], ]; } /** * @dataProvider itemViewOptions * @param mixed $itemView * @param string $expected */ public function testItemViewOptions($itemView, $expected) { ob_start(); $this->getListView(['itemView' => $itemView])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE($expected, $out); } public function itemOptions() { return [ [ null, '
Showing 1-3 of 3 items.
0
1
2
', ], [ function ($model, $key, $index, $widget) { return [ 'tag' => 'span', 'data' => [ 'test' => 'passed', 'key' => $key, 'index' => $index, 'id' => $model['id'], ], ]; }, '
Showing 1-3 of 3 items.
0 1 2
', ], ]; } /** * @dataProvider itemOptions * @param mixed $itemOptions * @param string $expected */ public function testItemOptions($itemOptions, $expected) { ob_start(); $this->getListView(['itemOptions' => $itemOptions])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE($expected, $out); } public function testBeforeAndAfterItem() { $before = function ($model, $key, $index, $widget) { $widget = get_class($widget); return ""; }; $after = function ($model, $key, $index, $widget) { if ($model['id'] === 1) { return null; } $widget = get_class($widget); return ""; }; ob_start(); $this->getListView([ 'beforeItem' => $before, 'afterItem' => $after, ])->run(); $out = ob_get_clean(); $this->assertEqualsWithoutLE(<<
Showing 1-3 of 3 items.
0
1
2
HTML , $out ); } /** * @see https://github.com/yiisoft/yii2/pull/14596 */ public function testShouldTriggerInitEvent() { $initTriggered = false; $this->getListView([ 'on init' => function () use (&$initTriggered) { $initTriggered = true; }, 'dataProvider' => new ArrayDataProvider(['allModels' => []]), ]); $this->assertTrue($initTriggered); } }