Skip to content

Commit cd7ca68

Browse files
committed
added tests
1 parent 020e556 commit cd7ca68

27 files changed

+504
-67
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tests export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.idea
2+
/var
23
/vendor
34
/composer.lock

.travis.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ cache:
66

77
php:
88
- 7.1
9+
- 7.2
10+
- 7.3
911

1012
install:
1113
- composer install --no-interaction --prefer-dist
1214

1315
script:
14-
- php vendor/bin/phpstan analyse ./src --level 7
15-
- php vendor/bin/phpcs --standard=PSR12 --ignore=./src/Resources ./src
16+
- php vendor/bin/phpstan analyse ./src ./tests --level 7
17+
- php vendor/bin/phpcs --standard=PSR12 --ignore=./src/Resources ./src ./tests
18+
- php vendor/bin/phpunit

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FreezyBee/DataGridBundle
2+
========================
3+
4+
[![Build Status](https://travis-ci.org/FreezyBee/DataGridBundle.svg?branch=master)](https://travis-ci.org/FreezyBee/DataGridBundle)
5+
[![Coverage Status](https://coveralls.io/repos/github/FreezyBee/DataGridBundle/badge.svg?branch=master)](https://coveralls.io/github/FreezyBee/DataGridBundle)
6+
7+
8+
Requirements
9+
------------
10+
11+
FreezyBee/DataGridBundle requires PHP 7.1 or higher
12+
13+
- [DataTables](https://datatables.net)
14+
- [flatpickr](https://flatpickr.js.org)
15+
16+
17+
Installation
18+
------------
19+
20+
```sh
21+
composer require freezy-bee/datagrid-bundle
22+
```

composer.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,33 @@
1414
"FreezyBee\\DataGridBundle\\": "src"
1515
}
1616
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"FreezyBee\\DataGridBundle\\Tests\\": "tests"
20+
}
21+
},
1722
"require": {
1823
"php": "^7.1",
24+
"ext-json": "*",
1925
"doctrine/orm": "^2.6",
2026
"symfony/config": "^4.0",
2127
"symfony/dependency-injection": "^4.0",
22-
"symfony/http-kernel": "^4.0",
2328
"symfony/property-access": "^4.0",
2429
"symfony/templating": "^4.0",
30+
"symfony/yaml": "^4.0",
2531
"twig/twig": "^2.0"
2632
},
2733
"config": {
2834
"sort-packages": true
2935
},
3036
"require-dev": {
37+
"doctrine/doctrine-bundle": "^1.10",
3138
"phpstan/phpstan-shim": "^0.11",
32-
"squizlabs/php_codesniffer": "^3.4"
39+
"phpunit/phpunit": "^7.5",
40+
"squizlabs/php_codesniffer": "^3.4",
41+
"symfony/framework-bundle": "^4.2",
42+
"symfony/panther": "^0.2",
43+
"symfony/phpunit-bridge": "^4.2",
44+
"symfony/twig-bundle": "^4.2"
3345
}
3446
}

phpunit.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
4+
bootstrap="./tests/bootstrap.php"
5+
>
6+
<php>
7+
<env name="KERNEL_CLASS" value="FreezyBee\DataGridBundle\Tests\App\Kernel"/>
8+
</php>
9+
10+
<testsuites>
11+
<testsuite name="My Test Suite">
12+
<directory>./tests/*</directory>
13+
</testsuite>
14+
</testsuites>
15+
</phpunit>

src/Column/ActionColumn.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ public function getActions(): array
3737
{
3838
return $this->actions;
3939
}
40+
41+
/**
42+
* @return bool
43+
*/
44+
public function hasActions(): bool
45+
{
46+
return count($this->actions) > 0;
47+
}
4048
}

src/Controller/DataGridController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use FreezyBee\DataGridBundle\DataGridFactory;
88
use Symfony\Component\HttpFoundation\JsonResponse;
99
use Symfony\Component\HttpFoundation\Request;
10-
use Symfony\Component\Routing\Annotation\Route;
1110

1211
/**
1312
* @author Jakub Janata <[email protected]>
@@ -26,7 +25,6 @@ public function __construct(DataGridFactory $dataGridFactory)
2625
}
2726

2827
/**
29-
* @Route("/api/grid/{name}", name="datagrid_ajax")
3028
* @param string $name
3129
* @param Request $request
3230
* @return JsonResponse

src/DataGrid.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace FreezyBee\DataGridBundle;
66

77
use FreezyBee\DataGridBundle\Column\ActionColumn;
8-
use FreezyBee\DataGridBundle\Column\Column;
98
use FreezyBee\DataGridBundle\DataSource\DataSourceInterface;
109
use Symfony\Component\HttpFoundation\JsonResponse;
1110
use Symfony\Component\HttpFoundation\Request;
@@ -22,33 +21,27 @@ class DataGrid
2221
/** @var DataSourceInterface */
2322
private $dataSource;
2423

25-
/** @var Column[] */
26-
private $columns;
27-
28-
/** @var ActionColumn|null */
29-
private $actionColumn;
24+
/** @var DataGridConfig */
25+
private $config;
3026

3127
/** @var string */
3228
private $name;
3329

3430
/**
3531
* @param EngineInterface $engine
3632
* @param DataSourceInterface $dataSource
37-
* @param array $columns
38-
* @param ActionColumn|null $actionColumn
33+
* @param DataGridConfig $config
3934
* @param string $name
4035
*/
4136
public function __construct(
4237
EngineInterface $engine,
4338
DataSourceInterface $dataSource,
44-
array $columns,
45-
?ActionColumn $actionColumn,
39+
DataGridConfig $config,
4640
string $name
4741
) {
4842
$this->engine = $engine;
4943
$this->dataSource = $dataSource;
50-
$this->columns = $columns;
51-
$this->actionColumn = $actionColumn;
44+
$this->config = $config;
5245
$this->name = $name;
5346
}
5447

@@ -65,13 +58,13 @@ public function ajax(Request $request): JsonResponse
6558
// sort
6659
$orderByIndex = $query['order'][0]['column'] ?? null;
6760
if ($orderByIndex !== null) {
68-
$this->dataSource->applySort($this->columns[$orderByIndex], $query['order'][0]['dir']);
61+
$this->dataSource->applySort($this->config->getColumns()[$orderByIndex], $query['order'][0]['dir']);
6962
}
7063

7164
// filters
7265
foreach ($query['columns'] as $index => $ajaxColumn) {
7366
$value = $ajaxColumn['search']['value'];
74-
$column = $this->columns[$index] ?? null;
67+
$column = $this->config->getColumns()[$index] ?? null;
7568

7669
if ($value !== '' && $column !== null && $column->isFilterable()) {
7770
$this->dataSource->applyFilter($column, $value);
@@ -88,17 +81,17 @@ public function ajax(Request $request): JsonResponse
8881
$data = [];
8982
foreach ($items as $item) {
9083
$row = [];
91-
foreach ($this->columns as $column) {
84+
foreach ($this->config->getColumns() as $column) {
9285
if ($column instanceof ActionColumn) {
9386
continue;
9487
}
9588
$row[] = $column->renderContent($item, $this->engine);
9689
}
9790

98-
if ($this->actionColumn !== null) {
91+
if ($this->config->getActionColumn()->hasActions()) {
9992
$row[] = $this->engine->render('@FreezyBeeDataGrid/action.html.twig', [
10093
'item' => $item,
101-
'actions' => $this->actionColumn->getActions()
94+
'actions' => $this->config->getActionColumn()->getActions()
10295
]);
10396
}
10497
$data[] = $row;
@@ -117,10 +110,20 @@ public function ajax(Request $request): JsonResponse
117110
*/
118111
public function render(): string
119112
{
113+
$sortIndex = 0;
114+
if ($this->config->getDefaultSortColumnName() !== null) {
115+
$sortIndex = array_search($this->config->getDefaultSortColumnName(), $this->config->getColumns(), true);
116+
}
117+
120118
return $this->engine->render('@FreezyBeeDataGrid/grid.html.twig', [
121119
'name' => $this->name,
122-
'columns' => $this->columns,
123-
'actionColumn' => $this->actionColumn
120+
'columns' => $this->config->getColumns(),
121+
'actionColumn' => $this->config->getActionColumn(),
122+
'default' => [
123+
'perPage' => $this->config->getDefaultPerPage(), $this->config->getColumns(),
124+
'sortIndex' => $sortIndex,
125+
'sortDir' => $this->config->getDefaultSortColumnDirection() ?? 'desc',
126+
]
124127
]);
125128
}
126129
}

src/DataGridBuilder.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ class DataGridBuilder
2626
/** @var ActionColumn */
2727
private $actionColumn;
2828

29+
/** @var string|null */
30+
private $defaultSortColumnName;
31+
32+
/** @var string|null */
33+
private $defaultSortColumnDirection;
34+
35+
/** @var int */
36+
private $defaultPerPage = 10;
37+
2938
/**
3039
*/
3140
public function __construct()
@@ -43,13 +52,6 @@ public function setDataSource($dataSource): self
4352
return $this;
4453
}
4554

46-
/**
47-
* @return array|QueryBuilder|null
48-
*/
49-
public function getDataSource()
50-
{
51-
return $this->dataSource;
52-
}
5355

5456
/**
5557
* @param string $name
@@ -97,27 +99,50 @@ public function addAction(string $route, string $label, array $params = []): Act
9799
}
98100

99101
/**
102+
* @param string $name
100103
* @param Column $column
101104
* @return Column
102105
*/
103-
public function add(Column $column): Column
106+
public function add(string $name, Column $column): Column
104107
{
105108
return $this->columns[] = $column;
106109
}
107110

108111
/**
109-
* @return Column[]
112+
* @param string $name
113+
* @param string $direction
114+
* @return DataGridBuilder
115+
*/
116+
public function setDefaultSort(string $name, string $direction): self
117+
{
118+
$this->defaultSortColumnName = $name;
119+
$this->defaultSortColumnDirection = $direction;
120+
return $this;
121+
}
122+
123+
/**
124+
* @param int $defaultPerPage
125+
* @return DataGridBuilder
110126
*/
111-
public function getColumns(): array
127+
public function setDefaultPerPage(int $defaultPerPage): self
112128
{
113-
return $this->columns;
129+
$this->defaultPerPage = $defaultPerPage;
130+
return $this;
114131
}
115132

116133
/**
117-
* @return ActionColumn
134+
* @internal
135+
* @return DataGridConfig
118136
*/
119-
public function getActionColumn(): ActionColumn
137+
public function generateConfig(): DataGridConfig
120138
{
121-
return $this->actionColumn;
139+
return new DataGridConfig(
140+
$this->dataSource,
141+
$this->columns,
142+
$this->actionColumn,
143+
$this->defaultSortColumnName,
144+
$this->defaultSortColumnDirection,
145+
$this->defaultPerPage
146+
);
122147
}
123148
}

0 commit comments

Comments
 (0)