Skip to content

Commit

Permalink
Merge pull request #43 from WebFiori/dev
Browse files Browse the repository at this point in the history
refactor: Added More Tests and Fixed Bugs
  • Loading branch information
usernane committed Jul 8, 2024
2 parents 052ffa7 + 7063250 commit c6d0b13
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/php70.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 3 additions & 1 deletion .github/workflows/php71.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 3 additions & 1 deletion .github/workflows/php72.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}


4 changes: 3 additions & 1 deletion .github/workflows/php73.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}



4 changes: 3 additions & 1 deletion .github/workflows/php74.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}



4 changes: 3 additions & 1 deletion .github/workflows/php80.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 3 additions & 1 deletion .github/workflows/php81.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: SonarCloud
uses: SonarSource/sonarcloud-github-action@master
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/php82.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ jobs:
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
185 changes: 183 additions & 2 deletions tests/webfiori/test/ui/HTMLTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace webfiori\test\ui;

use PHPUnit\Framework\TestCase;
use InvalidArgumentException;
use webfiori\ui\Anchor;
use webfiori\ui\HTMLNode;
use webfiori\ui\HTMLTable;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public function test01() {
. '</tr>'
. '</table>', $table->toHTML());
$this->assertEquals('Hello', $table->getValue(0, 0));
$table->setValue(0, 1, new \webfiori\ui\HTMLNode());
$table->setValue(0, 1, new HTMLNode());
$this->assertEquals('<div></div>', $table->getValue(0, 1).'');
$this->assertNull($table->getValue(88, 99));
$table->setIsQuotedAttribute(false);
Expand All @@ -83,4 +84,184 @@ public function test03() {
$this->expectExceptionMessage("Column index must be less than 5 and greater than -1.");
$table->setValue(2, 5, 'Hello');
}
/**
* @test
*/
public function testAddColumn00() {
$table = new HTMLTable(1, 1);
$table->setValue(0, 0, 'Hello');
$table->addColumn(['world']);
$this->assertEquals('world', $table->getValue(0, 1));
}
/**
* @test
*/
public function testAddColumn01() {
$table = new HTMLTable(1, 1);
$table->setValue(0, 0, 'Hello');
$table->addColumn([new Anchor('http://localhost', 'world')]);
$this->assertTrue($table->getValue(0, 1) instanceof Anchor );
}
/**
* @test
*/
public function testAddColumn02() {
$table = new HTMLTable(4, 1);

$table->addColumn([new Anchor('http://localhost', 'world'), 'test']);
$this->assertTrue($table->getValue(0, 1) instanceof Anchor );
$this->assertEquals($table->getValue(1, 1), 'test');
$this->assertEquals($table->getValue(2, 1), '');
$this->assertEquals($table->getValue(3, 1), '');
}
/**
* @test
*/
public function testAddRow00() {
$table = new HTMLTable(1, 1);
$table->setValue(0, 0, 'Hello');
$table->addRow(['world']);
$this->assertEquals('world', $table->getValue(1, 0));
}
/**
* @test
*/
public function testAddRow01() {
$table = new HTMLTable(1, 2);
$table->setValue(0, 0, 'Hello');
$table->addRow([new Anchor('http://localhost', 'world'), 'world']);
$this->assertTrue($table->getValue(1, 0) instanceof Anchor );
$this->assertEquals('world', $table->getValue(1, 1));
}
/**
* @test
*/
public function testAddRow02() {
$table = new HTMLTable(4, 5);

$table->addRow([new Anchor('http://localhost', 'world'), 'test']);
$this->assertTrue($table->getValue(4, 0) instanceof Anchor );
$this->assertEquals($table->getValue(4, 1), 'test');
$this->assertEquals($table->getValue(4, 2), '');
$this->assertEquals($table->getValue(4, 3), '');
$this->assertEquals($table->getValue(4, 4), '');
}
/**
* @test
*/
public function testRemoveCol00() {
$table = new HTMLTable(4, 5);
for ($x = 0 ; $x < $table->rows() ; $x++) {
for ($y = 0 ; $y < $table->cols() ; $y++) {
$table->setValue($x, $y, 'Row '.$x.' Col '.$y);
}
}
$this->assertEquals(5, $table->cols());
$this->assertEquals('Row 0 Col 0', $table->getValue(0, 0));

$table->removeCol(0);

$this->assertEquals(4, $table->cols());
$this->assertEquals('Row 0 Col 1', $table->getValue(0, 0));

$this->assertEquals('Row 0 Col 4', $table->getValue(0, 3));
$table->removeCol(3);

$this->assertEquals(3, $table->cols());
$this->assertEquals('Row 0 Col 3', $table->getValue(0, 2));

$table->removeCol(0);

$this->assertEquals(2, $table->cols());
$this->assertEquals('Row 0 Col 2', $table->getValue(0, 0));

$table->removeCol(1);

$this->assertEquals(1, $table->cols());
$this->assertEquals('Row 0 Col 2', $table->getValue(0, 0));

$table->removeCol(0);

$this->assertEquals(1, $table->cols());
$this->assertEquals('Row 0 Col 2', $table->getValue(0, 0));
}
/**
* @test
*/
public function testRemoveRow00() {
$table = new HTMLTable(4, 5);
for ($x = 0 ; $x < $table->rows() ; $x++) {
for ($y = 0 ; $y < $table->cols() ; $y++) {
$table->setValue($x, $y, 'Row '.$x.' Col '.$y);
}
}
$this->assertEquals(4, $table->rows());
$this->assertEquals('Row 0 Col 0', $table->getValue(0, 0));

$table->removeRow(0);

$this->assertEquals(3, $table->rows());
$this->assertEquals('Row 1 Col 0', $table->getValue(0, 0));

$this->assertEquals('Row 3 Col 3', $table->getValue(2, 3));
$table->removeRow(2);

$this->assertEquals(2, $table->rows());
$this->assertEquals('Row 2 Col 3', $table->getValue(1, 3));

$table->removeRow(0);

$this->assertEquals(1, $table->rows());
$this->assertEquals('Row 2 Col 0', $table->getValue(0, 0));

$table->removeRow(0);

$this->assertEquals(1, $table->rows());
$this->assertEquals('Row 2 Col 0', $table->getValue(0, 0));

}
/**
* @test
*/
public function testSetColAttributes00() {
$table = new HTMLTable(5, 5);
$table->setColAttributes(0, [
'class' => 'first-col'
]);
$table->setColAttributes(1, [
'class' => 'second-col'
]);
$table->setColAttributes(4, [
'class' => 'last-col'
]);
for ($x = 0 ; $x < $table->rows() ; $x++) {
$this->assertEquals('first-col', $table->getCell($x, 0)->getAttribute('class'));
$this->assertEquals('second-col', $table->getCell($x, 1)->getAttribute('class'));
$this->assertEquals('last-col', $table->getCell($x, 4)->getAttribute('class'));
}
}
/**
* @test
*/
public function testSetRowAttributes00() {
$table = new HTMLTable(5, 5);
$table->setRowAttributes(0, [
'class' => 'first-row'
]);
$table->setRowAttributes(1, [
'class' => 'second-row'
]);
$table->setRowAttributes(4, [
'class' => 'last-row'
]);
for ($x = 0 ; $x < $table->cols() ; $x++) {
$this->assertEquals('first-row', $table->getCell(0, $x)->getAttribute('class'));
}
for ($x = 0 ; $x < $table->cols() ; $x++) {
$this->assertEquals('second-row', $table->getCell(1, $x)->getAttribute('class'));
}
for ($x = 0 ; $x < $table->cols() ; $x++) {
$this->assertEquals('last-row', $table->getCell(4, $x)->getAttribute('class'));
}
}
}
7 changes: 7 additions & 0 deletions tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ public function testHeadTemplate01() {
$this->assertEquals('{{title}}', $node->getPageTitle());
$this->assertEquals('UTF-8', $node->getCharSet());
}
/**
* @test
*/
public function test11() {
$this->expectException(\webfiori\ui\exceptions\TemplateNotFoundException::class);
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'not-exist.php');
}
/**
* @test
*/
Expand Down
1 change: 0 additions & 1 deletion webfiori/ui/HTMLDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatt
public function setHeadNode(HeadNode $node) {
$this->getDocumentRoot()->replaceChild($this->headNode, $node);
$this->headNode = $node;

}
/**
* Sets the language of the document.
Expand Down
19 changes: 16 additions & 3 deletions webfiori/ui/HTMLTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function getValue(int $rowIndex, int $colIndex) {
if ($cell != null) {
$ch = $cell->getChild(0);

if ($ch->getName() == '#TEXT') {
if ($ch->getNodeName() == '#TEXT') {
return $ch->getText();
}

Expand All @@ -170,6 +170,8 @@ public function getValue(int $rowIndex, int $colIndex) {
/**
* Removes a column from the table given column index.
*
* Note that if the table has one column, the method will not remove it.
*
* @param int $colIndex The index of the column.
*
* @return array The method will return an array that holds objects that
Expand All @@ -179,25 +181,36 @@ public function getValue(int $rowIndex, int $colIndex) {
public function removeCol(int $colIndex) : array {
$colCells = [];

if ($colIndex < $this->cols()) {
if ($colIndex < $this->cols() && $this->cols() > 1) {
foreach ($this as $row) {
$colCells[] = $row->children()->remove($colIndex);
}
$this->cols--;
}

return $colCells;
}
/**
* Removes a row given its index.
*
* Note that if the table has only one row, the method will not remove it.
*
* @param int $rowIndex The index of the row.
*
* @return TableRow|null If the row is removed, the method will return
* an object that represents the removed row. Other than that, the method
* will return null.
*/
public function removeRow(int $rowIndex) {
return $this->removeChild($rowIndex);
if ($this->rows() > 1) {
$row = $this->removeChild($rowIndex);

if ($row !== null) {
$this->rows--;
}

return $row;
}
}
/**
* Returns number of rows in the table.
Expand Down

0 comments on commit c6d0b13

Please sign in to comment.