Skip to content

Commit 1dfd4fb

Browse files
committed
Add new test
1 parent ffc0f98 commit 1dfd4fb

File tree

6 files changed

+125
-74
lines changed

6 files changed

+125
-74
lines changed

.php-cs-fixer.dist.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use CodeIgniter\CodingStandard\CodeIgniter4;
1313
use Nexus\CsConfig\Factory;
1414
use Nexus\CsConfig\Fixer\Comment\NoCodeSeparatorCommentFixer;
15-
use Nexus\CsConfig\Fixer\Comment\SpaceAfterCommentStartFixer;
15+
// use Nexus\CsConfig\Fixer\Comment\SpaceAfterCommentStartFixer;
1616
use Nexus\CsConfig\FixerGenerator;
1717
use PhpCsFixer\Finder;
1818

@@ -48,7 +48,7 @@
4848
'customFixers' => FixerGenerator::create('vendor/nexusphp/cs-config/src/Fixer', 'Nexus\\CsConfig\\Fixer'),
4949
'customRules' => [
5050
NoCodeSeparatorCommentFixer::name() => true,
51-
SpaceAfterCommentStartFixer::name() => true,
51+
// SpaceAfterCommentStartFixer::name() => true,
5252
],
5353
];
5454

composer.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"minimum-stability": "dev",
1616
"prefer-stable": true,
1717
"require": {
18-
"php": "^7.4 || ^8.0 || ^8.1",
18+
"php": "^7.4 || ^8.0 || ^8.1 || ^8.2",
1919
"ext-json": "*",
2020
"codeigniter4/framework": "^4.2",
2121
"codeigniter4/shield": "dev-develop",
@@ -37,8 +37,9 @@
3737
"qossmic/deptrac-shim": "^0.24.0",
3838
"vimeo/psalm": "^4.23",
3939
"symfony/filesystem": "^5.4",
40-
"rector/rector": "^0.13.5"
41-
},
40+
"rector/rector": "^0.13.5",
41+
"sebastian/phpcpd": "^6.0.3"
42+
},
4243
"suggest": {
4344
"ext-fileinfo": "Improves mime type detection for files"
4445
},
@@ -62,7 +63,7 @@
6263
}
6364
},
6465
"scripts": {
65-
"analyze": "phpstan analyze",
66+
"analyze": "phpstan --memory-limit=1024M --xdebug analyze",
6667
"ci": [
6768
"Composer\\Config::disableProcessTimeout",
6869
"@deduplicate",
@@ -75,7 +76,7 @@
7576
"deduplicate": "phpcpd src/",
7677
"inspect": "deptrac analyze --cache-file=build/deptrac.cache",
7778
"style": "php-cs-fixer fix --verbose --ansi --using-cache=no",
78-
"test": "phpunit"
79+
"test": "XDEBUG_MODE=coverage phpunit"
7980
},
8081
"support": {
8182
"forum": "http://forum.codeigniter.com/"

src/DataTables.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
class DataTables
1919
{
20-
protected $_table;
21-
protected $_dataTablesScript;
20+
protected object $_table;
21+
protected object $_dataTablesScript;
2222

2323
/**
2424
* Sets up initialize module and return this object.
@@ -28,25 +28,27 @@ public function boot(?Model $model = null): DataTables
2828
$this->_dataTablesScript = $dt = service('dataTablesScript');
2929
$this->_table = service('table');
3030

31-
$this->_table->setModel($model);
31+
if ($model !== null) {
32+
$this->_table->setModel($model);
33+
}
3234

3335
return $this;
3436
}
3537

3638
// Return Table Object
37-
public function table()
39+
public function table(): object
3840
{
3941
return $this->_table;
4042
}
4143

4244
// Return DataTablesScript Object
43-
public function dataTablesScript()
45+
public function dataTablesScript(): object
4446
{
4547
return $this->_dataTablesScript;
4648
}
4749

4850
// Return DataTables Version
49-
public function getVersion()
51+
public function getVersion(): string
5052
{
5153
return DATATABLES_VERSION;
5254
}

src/Html/Table.php

+58-44
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class Table
2424
/**
2525
* The model
2626
*/
27-
protected $model;
27+
protected ?Model $model = null;
2828

2929
/**
3030
* The configuration
3131
*/
32-
protected $configuration;
32+
protected Configuration $configuration;
3333

3434
// endregion
3535

@@ -38,7 +38,7 @@ class Table
3838
/**
3939
* Table constructor.
4040
*
41-
* @return Table
41+
* @return void
4242
*/
4343
public function __construct(?Model $model = null)
4444
{
@@ -75,9 +75,6 @@ public function setConfiguration(Configuration $configuration): Table
7575

7676
// region Getters
7777

78-
/**
79-
* @return Model
80-
*/
8178
public function getModel(): ?Model
8279
{
8380
return $this->model;
@@ -95,8 +92,8 @@ public function getConfiguration(): Configuration
9592
private function getFieldsFromModel(): array
9693
{
9794
$fields = [];
98-
/** @phpstan-ignore-next-line */
99-
foreach ($this->getModel()->allowedFields as $field) {
95+
96+
foreach ($this->getModel()->__get('allowedFields') as $field) {
10097
$fields[] = $field;
10198
}
10299

@@ -113,15 +110,13 @@ private function getTableData(): array
113110
{
114111
return [
115112
[
116-
'fields' => $this->getFieldsFromModel(),
117-
'localize' => 'User',
118-
'class' => 'table table-bordered table-hover table-striped',
119-
'style' => '',
120-
/** @phpstan-ignore-next-line */
121-
'id' => $this->getModel()->table . '_' . time(),
122-
'data-id' => '',
123-
/** @phpstan-ignore-next-line */
124-
'data-table_name' => $this->getModel()->table,
113+
'fields' => $this->getFieldsFromModel(),
114+
'localize' => 'User',
115+
'class' => 'table table-bordered table-hover table-striped',
116+
'style' => '',
117+
'id' => $this->getModel()->__get('table') . '_' . time(),
118+
'data-id' => '',
119+
'data-table_name' => $this->getModel()->__get('table'),
125120
'data-footer' => true,
126121
],
127122
];
@@ -133,7 +128,7 @@ private function getTableData(): array
133128
* @param array $fields The array of fields
134129
* @param bool $localize The name of File for the localization
135130
*/
136-
private function getTableHeader($fields = [], $localize = false): string
131+
private function getTableHeader(array $fields = [], bool $localize = false): string
137132
{
138133
$head = '';
139134

@@ -199,32 +194,51 @@ public function render(?array $data = null): string
199194

200195
// region CRUD
201196

202-
public function fetch_data(): array
203-
{
204-
$fields = '';
205-
/** @phpstan-ignore-next-line */
206-
for ($x = 0; $x < count($this->getModel()->allowedFields); $x++) {
207-
/** @phpstan-ignore-next-line */
208-
$fields .= $this->getModel()->allowedFields[$x] . ', ';
209-
}
210-
211-
return $this->getModel()->select(substr($fields, 0, strlen($fields) - 1))->findAll();
212-
}
213-
214-
public function insert()
215-
{
216-
echo 'insert';
217-
}
218-
219-
public function update()
220-
{
221-
echo 'update';
222-
}
223-
224-
public function delete()
225-
{
226-
echo 'delete';
227-
}
197+
/**
198+
* Fetches all results
199+
*/
200+
/* public function fetch_data(): array
201+
{
202+
$query = null;
203+
$limit = 0;
204+
$offset = 0;
205+
206+
if ($_POST && $_POST !== []) {
207+
if (isset($_POST['query'])) {
208+
$query = $_POST['query'];
209+
// Join,Where,Subquery,ecc...
210+
}
211+
if (isset($_POST['limit'])) {
212+
$limit = $_POST['limit'];
213+
}
214+
if (isset($_POST['offset'])) {
215+
$offset = $_POST['offset'];
216+
}
217+
}
218+
219+
$fields = '';
220+
221+
for ($x = 0; $x < count($this->getModel()->__get('allowedFields')); $x++) {
222+
$fields .= $this->getModel()->__get('allowedFields')[$x] . ', ';
223+
}
224+
225+
return $this->getModel()->select(substr($fields, 0, strlen($fields) - 1))->findAll($limit, $offset);
226+
}
227+
228+
public function insert(): string
229+
{
230+
return 'insert';
231+
}
232+
233+
public function update(): string
234+
{
235+
return 'update';
236+
}
237+
238+
public function delete(): string
239+
{
240+
return 'delete';
241+
}*/
228242

229243
// endregion
230244
}

src/Javascript/DataTablesScript.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DataTablesScript
2323
*
2424
* @var string[]
2525
*/
26-
protected $css = [
26+
protected array $css = [
2727
'//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.css',
2828
'//cdn.datatables.net/1.12.1/css/dataTables.bootstrap4.min.css',
2929
'//cdn.datatables.net/responsive/2.3.0/css/responsive.bootstrap4.min.css',
@@ -34,7 +34,7 @@ class DataTablesScript
3434
*
3535
* @var string[]
3636
*/
37-
protected $javascript = [
37+
protected array $javascript = [
3838
'//cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js',
3939
'//cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js',
4040
'//cdn.datatables.net/1.12.1/js/dataTables.bootstrap4.min.js',
@@ -51,17 +51,17 @@ class DataTablesScript
5151
*/
5252
public function getExternalLibraries(array $css = [], array $javascript = [], array $replace = ['css' => false, 'js' => false])
5353
{
54-
if (isset($replace['css']) && $replace['css']) {
54+
if (isset($replace['css']) && $replace['css'] === true) {
5555
$this->css = $css;
56-
} elseif (isset($replace['css']) && ! $replace['css']) {
56+
} elseif (isset($replace['css']) && $replace['css'] === false) {
5757
$this->css = array_merge($this->css, $css);
5858
} else {
5959
$this->css = [];
6060
}
6161

62-
if (isset($replace['js']) && $replace['js']) {
62+
if (isset($replace['js']) && $replace['js'] === true) {
6363
$this->javascript = $javascript;
64-
} elseif (isset($replace['js']) && ! $replace['js']) {
64+
} elseif (isset($replace['js']) && $replace['js'] === false) {
6565
$this->javascript = array_merge($this->javascript, $javascript);
6666
} else {
6767
$this->javascript = [];
@@ -99,6 +99,7 @@ public function minimizeJavascript(string $javascript)
9999
// condense spaces
100100
$javascript = preg_replace("/\\s*\n\\s*/", "\n", $javascript); // spaces around newlines
101101
$javascript = preg_replace('/\\h+/', ' ', $javascript); // \h+ horizontal white space
102+
102103
// remove unnecessary horizontal spaces around non variables (alphanumerics, underscore, dollar sign)
103104
// $javascript = preg_replace("/\h([^A-Za-z0-9\_\$])/", '$1', $javascript); //Causa problemi con le icone nella tabella dei menu
104105
return preg_replace('/([^A-Za-z0-9\\_$])\\h/', '$1', $javascript);
@@ -189,10 +190,7 @@ public function getDocumentReady(string $id, Configuration $configuration): stri
189190
';
190191
}
191192

192-
/**
193-
* @param string $script
194-
*/
195-
public function getJavascript($script = ''): string
193+
public function getJavascript(string $script = ''): string
196194
{
197195
// $javascript = $this->minimizeJavascript( $this->getDocumentReady($script) );
198196
$javascript_custom = $this->minimizeJavascript($script);

tests/Html/TableTest.php

+43-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313
final class TableTest extends TestCase
1414
{
15-
protected $table;
16-
protected $tableConfiguration;
15+
protected Table $table;
16+
protected Configuration $tableConfiguration;
1717

1818
protected function setUp(): void
1919
{
@@ -23,7 +23,7 @@ protected function setUp(): void
2323
$this->tableConfiguration = $this->table->getConfiguration();
2424
}
2525

26-
public function testGetModelisNull(): void
26+
public function testGetModelIsNull(): void
2727
{
2828
$tableModel = $this->table->getModel() ?? null;
2929

@@ -48,12 +48,48 @@ public function testGetConfigurationInstanceOf(): void
4848
$this->assertInstanceOf(Configuration::class, $this->table->getConfiguration());
4949
}
5050

51-
/*public function testFetch_data(): void
51+
public function testRenderString(): void
5252
{
53+
$model = new UserModel();
54+
$this->table->setModel($model);
55+
56+
$data = [
57+
'fields' => $this->table->getModel()->__get('allowedFields'),
58+
'localize' => 'User',
59+
'class' => 'table table-bordered table-hover table-striped',
60+
'style' => '',
61+
'id' => $this->table->getModel()->__get('table') . '_' . time(),
62+
'data-id' => '',
63+
'data-table_name' => $this->table->getModel()->__get('table'),
64+
'footer' => true,
65+
];
66+
67+
$this->assertIsString(
68+
$this->table->render($data),
69+
'return is string'
70+
);
71+
}
72+
73+
/* public function testFetch_data(): void
74+
{
75+
$model = new UserModel();
76+
$this->table->setModel($model);
77+
78+
$this->assertIsArray($this->table->fetch_data());
79+
}*/
5380

54-
$this->table->setModel(new UserModel());
81+
/* public function testInsert(): void
82+
{
83+
$this->assertStringContainsString('insert', $this->table->insert());
84+
}
5585
56-
$this->assertIsArray($this->table->fetch_data());
86+
public function testUpdate(): void
87+
{
88+
$this->assertStringContainsString('update', $this->table->update());
89+
}
5790
58-
}*/
91+
public function testDelete(): void
92+
{
93+
$this->assertStringContainsString('delete', $this->table->delete());
94+
}*/
5995
}

0 commit comments

Comments
 (0)