Skip to content

Commit

Permalink
Add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
atsanna committed Oct 17, 2023
1 parent ffc0f98 commit 1dfd4fb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;
use Nexus\CsConfig\Fixer\Comment\NoCodeSeparatorCommentFixer;
use Nexus\CsConfig\Fixer\Comment\SpaceAfterCommentStartFixer;
// use Nexus\CsConfig\Fixer\Comment\SpaceAfterCommentStartFixer;
use Nexus\CsConfig\FixerGenerator;
use PhpCsFixer\Finder;

Expand Down Expand Up @@ -48,7 +48,7 @@
'customFixers' => FixerGenerator::create('vendor/nexusphp/cs-config/src/Fixer', 'Nexus\\CsConfig\\Fixer'),
'customRules' => [
NoCodeSeparatorCommentFixer::name() => true,
SpaceAfterCommentStartFixer::name() => true,
// SpaceAfterCommentStartFixer::name() => true,
],
];

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4 || ^8.0 || ^8.1",
"php": "^7.4 || ^8.0 || ^8.1 || ^8.2",
"ext-json": "*",
"codeigniter4/framework": "^4.2",
"codeigniter4/shield": "dev-develop",
Expand All @@ -37,8 +37,9 @@
"qossmic/deptrac-shim": "^0.24.0",
"vimeo/psalm": "^4.23",
"symfony/filesystem": "^5.4",
"rector/rector": "^0.13.5"
},
"rector/rector": "^0.13.5",
"sebastian/phpcpd": "^6.0.3"
},
"suggest": {
"ext-fileinfo": "Improves mime type detection for files"
},
Expand All @@ -62,7 +63,7 @@
}
},
"scripts": {
"analyze": "phpstan analyze",
"analyze": "phpstan --memory-limit=1024M --xdebug analyze",
"ci": [
"Composer\\Config::disableProcessTimeout",
"@deduplicate",
Expand All @@ -75,7 +76,7 @@
"deduplicate": "phpcpd src/",
"inspect": "deptrac analyze --cache-file=build/deptrac.cache",
"style": "php-cs-fixer fix --verbose --ansi --using-cache=no",
"test": "phpunit"
"test": "XDEBUG_MODE=coverage phpunit"
},
"support": {
"forum": "http://forum.codeigniter.com/"
Expand Down
14 changes: 8 additions & 6 deletions src/DataTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

class DataTables
{
protected $_table;
protected $_dataTablesScript;
protected object $_table;
protected object $_dataTablesScript;

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

$this->_table->setModel($model);
if ($model !== null) {
$this->_table->setModel($model);
}

return $this;
}

// Return Table Object
public function table()
public function table(): object
{
return $this->_table;
}

// Return DataTablesScript Object
public function dataTablesScript()
public function dataTablesScript(): object
{
return $this->_dataTablesScript;
}

// Return DataTables Version
public function getVersion()
public function getVersion(): string
{
return DATATABLES_VERSION;
}
Expand Down
102 changes: 58 additions & 44 deletions src/Html/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class Table
/**
* The model
*/
protected $model;
protected ?Model $model = null;

/**
* The configuration
*/
protected $configuration;
protected Configuration $configuration;

// endregion

Expand All @@ -38,7 +38,7 @@ class Table
/**
* Table constructor.
*
* @return Table
* @return void
*/
public function __construct(?Model $model = null)
{
Expand Down Expand Up @@ -75,9 +75,6 @@ public function setConfiguration(Configuration $configuration): Table

// region Getters

/**
* @return Model
*/
public function getModel(): ?Model
{
return $this->model;
Expand All @@ -95,8 +92,8 @@ public function getConfiguration(): Configuration
private function getFieldsFromModel(): array
{
$fields = [];
/** @phpstan-ignore-next-line */
foreach ($this->getModel()->allowedFields as $field) {

foreach ($this->getModel()->__get('allowedFields') as $field) {
$fields[] = $field;
}

Expand All @@ -113,15 +110,13 @@ private function getTableData(): array
{
return [
[
'fields' => $this->getFieldsFromModel(),
'localize' => 'User',
'class' => 'table table-bordered table-hover table-striped',
'style' => '',
/** @phpstan-ignore-next-line */
'id' => $this->getModel()->table . '_' . time(),
'data-id' => '',
/** @phpstan-ignore-next-line */
'data-table_name' => $this->getModel()->table,
'fields' => $this->getFieldsFromModel(),
'localize' => 'User',
'class' => 'table table-bordered table-hover table-striped',
'style' => '',
'id' => $this->getModel()->__get('table') . '_' . time(),
'data-id' => '',
'data-table_name' => $this->getModel()->__get('table'),
'data-footer' => true,
],
];
Expand All @@ -133,7 +128,7 @@ private function getTableData(): array
* @param array $fields The array of fields
* @param bool $localize The name of File for the localization
*/
private function getTableHeader($fields = [], $localize = false): string
private function getTableHeader(array $fields = [], bool $localize = false): string
{
$head = '';

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

// region CRUD

public function fetch_data(): array
{
$fields = '';
/** @phpstan-ignore-next-line */
for ($x = 0; $x < count($this->getModel()->allowedFields); $x++) {
/** @phpstan-ignore-next-line */
$fields .= $this->getModel()->allowedFields[$x] . ', ';
}

return $this->getModel()->select(substr($fields, 0, strlen($fields) - 1))->findAll();
}

public function insert()
{
echo 'insert';
}

public function update()
{
echo 'update';
}

public function delete()
{
echo 'delete';
}
/**
* Fetches all results
*/
/* public function fetch_data(): array
{
$query = null;
$limit = 0;
$offset = 0;
if ($_POST && $_POST !== []) {
if (isset($_POST['query'])) {
$query = $_POST['query'];
// Join,Where,Subquery,ecc...
}
if (isset($_POST['limit'])) {
$limit = $_POST['limit'];
}
if (isset($_POST['offset'])) {
$offset = $_POST['offset'];
}
}
$fields = '';
for ($x = 0; $x < count($this->getModel()->__get('allowedFields')); $x++) {
$fields .= $this->getModel()->__get('allowedFields')[$x] . ', ';
}
return $this->getModel()->select(substr($fields, 0, strlen($fields) - 1))->findAll($limit, $offset);
}
public function insert(): string
{
return 'insert';
}
public function update(): string
{
return 'update';
}
public function delete(): string
{
return 'delete';
}*/

// endregion
}
18 changes: 8 additions & 10 deletions src/Javascript/DataTablesScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DataTablesScript
*
* @var string[]
*/
protected $css = [
protected array $css = [
'//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.css',
'//cdn.datatables.net/1.12.1/css/dataTables.bootstrap4.min.css',
'//cdn.datatables.net/responsive/2.3.0/css/responsive.bootstrap4.min.css',
Expand All @@ -34,7 +34,7 @@ class DataTablesScript
*
* @var string[]
*/
protected $javascript = [
protected array $javascript = [
'//cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js',
'//cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js',
'//cdn.datatables.net/1.12.1/js/dataTables.bootstrap4.min.js',
Expand All @@ -51,17 +51,17 @@ class DataTablesScript
*/
public function getExternalLibraries(array $css = [], array $javascript = [], array $replace = ['css' => false, 'js' => false])
{
if (isset($replace['css']) && $replace['css']) {
if (isset($replace['css']) && $replace['css'] === true) {
$this->css = $css;
} elseif (isset($replace['css']) && ! $replace['css']) {
} elseif (isset($replace['css']) && $replace['css'] === false) {
$this->css = array_merge($this->css, $css);
} else {
$this->css = [];
}

if (isset($replace['js']) && $replace['js']) {
if (isset($replace['js']) && $replace['js'] === true) {
$this->javascript = $javascript;
} elseif (isset($replace['js']) && ! $replace['js']) {
} elseif (isset($replace['js']) && $replace['js'] === false) {
$this->javascript = array_merge($this->javascript, $javascript);
} else {
$this->javascript = [];
Expand Down Expand Up @@ -99,6 +99,7 @@ public function minimizeJavascript(string $javascript)
// condense spaces
$javascript = preg_replace("/\\s*\n\\s*/", "\n", $javascript); // spaces around newlines
$javascript = preg_replace('/\\h+/', ' ', $javascript); // \h+ horizontal white space

// remove unnecessary horizontal spaces around non variables (alphanumerics, underscore, dollar sign)
// $javascript = preg_replace("/\h([^A-Za-z0-9\_\$])/", '$1', $javascript); //Causa problemi con le icone nella tabella dei menu
return preg_replace('/([^A-Za-z0-9\\_$])\\h/', '$1', $javascript);
Expand Down Expand Up @@ -189,10 +190,7 @@ public function getDocumentReady(string $id, Configuration $configuration): stri
';
}

/**
* @param string $script
*/
public function getJavascript($script = ''): string
public function getJavascript(string $script = ''): string
{
// $javascript = $this->minimizeJavascript( $this->getDocumentReady($script) );
$javascript_custom = $this->minimizeJavascript($script);
Expand Down
50 changes: 43 additions & 7 deletions tests/Html/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
final class TableTest extends TestCase
{
protected $table;
protected $tableConfiguration;
protected Table $table;
protected Configuration $tableConfiguration;

protected function setUp(): void
{
Expand All @@ -23,7 +23,7 @@ protected function setUp(): void
$this->tableConfiguration = $this->table->getConfiguration();
}

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

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

/*public function testFetch_data(): void
public function testRenderString(): void
{
$model = new UserModel();
$this->table->setModel($model);

$data = [
'fields' => $this->table->getModel()->__get('allowedFields'),
'localize' => 'User',
'class' => 'table table-bordered table-hover table-striped',
'style' => '',
'id' => $this->table->getModel()->__get('table') . '_' . time(),
'data-id' => '',
'data-table_name' => $this->table->getModel()->__get('table'),
'footer' => true,
];

$this->assertIsString(
$this->table->render($data),
'return is string'
);
}

/* public function testFetch_data(): void
{
$model = new UserModel();
$this->table->setModel($model);
$this->assertIsArray($this->table->fetch_data());
}*/

$this->table->setModel(new UserModel());
/* public function testInsert(): void
{
$this->assertStringContainsString('insert', $this->table->insert());
}
$this->assertIsArray($this->table->fetch_data());
public function testUpdate(): void
{
$this->assertStringContainsString('update', $this->table->update());
}
}*/
public function testDelete(): void
{
$this->assertStringContainsString('delete', $this->table->delete());
}*/
}

0 comments on commit 1dfd4fb

Please sign in to comment.