Skip to content
Merged

Dev #263

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 85 additions & 2 deletions tests/webfiori/framework/test/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,100 @@ public function testBeforeRender00() {
$c->assertTrue($p->getDocument()->hasChild('super-el'));
},0, [$this]);
$this->assertNotNull($c);
$this->assertEquals(2, $c->getID());
$this->assertEquals(0, $c->getPriority());

$c2 = $page->addBeforeRender(function(WebPage $p, TestCase $c)
{
$ch = $p->insert('div');
$ch->setID('super-el');
}, 3, [$this]);
$this->assertEquals(3, $c2->getID());
$this->assertEquals(3, $c2->getPriority());
}
/**
* @test
*/
public function testBeforeRender01() {
$page = new WebPage();
$c = $page->addBeforeRender(function (WebPage $p, TestCase $c)
{
$c->assertTrue($p->getDocument()->getChildByID('super-el') !== null);
},0, [$this]);

$c2 = $page->addBeforeRender(function(WebPage $p, TestCase $c)
{
$ch = $p->insert('div');
$ch->setID('super-el');
}, 3, [$this]);
$this->assertNull($page->getDocument()->getChildByID('super-el'));
$page->beforeRender();
$this->assertNotNull($page->getDocument()->getChildByID('super-el'));
}
/**
* @test
*/
public function testBeforeRender02() {
$page = new WebPage();
$c = $page->addBeforeRender(function (WebPage $p, TestCase $c)
{
$c->assertTrue($p->getDocument()->getChildByID('super-el') === null);
},0, [$this]);

$c2 = $page->addBeforeRender(function(WebPage $p, TestCase $c)
{
$ch = $p->insert('div');
$ch->setID('super-el');
}, 3, [$this]);
$page->removeBeforeRender($c2->getID());
$page->beforeRender();
$this->assertNull($page->getDocument()->getChildByID('super-el'));
}
/**
* @test
*/
public function testBeforeRender03() {
$page = new WebPage();
$c = $page->addBeforeRender(function (WebPage $p, TestCase $c)
{

$p->addBeforeRender(function (WebPage $p, TestCase $c) {
$c->assertTrue($p->getDocument()->getChildByID('super-el') === null);
}, 4, [$this]);
$p->addBeforeRender(function(WebPage $p, TestCase $c)
{
$ch = $p->insert('div');
$ch->setID('super-el');
}, 3, [$this]);
$p->addBeforeRender(function (WebPage $p, TestCase $c) {
$c->assertTrue($p->getDocument()->getChildByID('super-el') !== null);
}, 2, [$this]);

},0, [$this]);

$page->beforeRender();
$this->assertNotNull($page->getDocument()->getChildByID('super-el'));
}
/**
* @test
*/
public function testBeforeRender04() {
$page = new WebPage();

$c2 = $page->addBeforeRender(function(WebPage $p, TestCase $c)
{
$ch = $p->insert('div');
$ch->setID('super-el');
}, 3, [$this]);

$c2->setCallback(function(WebPage $p, TestCase $c)
{
$ch = $p->insert('div');
$ch->setID('super-cool');
}, [$this]);

$page->beforeRender();
$this->assertNull($page->getDocument()->getChildByID('super-el'));
$this->assertNotNull($page->getDocument()->getChildByID('super-cool'));
}
/**
* @test
*/
Expand Down
30 changes: 16 additions & 14 deletions tests/webfiori/framework/test/cli/AddCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace webfiori\framework\test\cli;

use PHPUnit\Framework\TestCase;
use webfiori\cli\CommandTestCase;
use webfiori\cli\Runner;
use webfiori\file\File;
use webfiori\framework\App;
Expand All @@ -13,30 +13,32 @@
*
* @author Ibrahim
*/
class AddCommandTest extends TestCase {
class AddCommandTest extends CommandTestCase {
/**
* @test
*/
public function test00() {
$output = $this->executeSingleCommand(new AddCommand(), [], [
'3'
]);
$runner = new Runner();
$runner->setInputs([
'3'
]);
$this->assertEquals(0, $runner->runCommand(new AddCommand()));
$this->assertEquals([
"What would you like to add?\n",
"0: New database connection.\n",
"1: New SMTP connection.\n",
"2: New website language.\n",
"3: Quit. <--\n"
], $runner->getOutput());
], $output);
$this->assertEquals(0, $this->getExitCode());
}
/**
* @test
*/
public function testAddDBConnection00() {
$runner = App::getRunner();
$runner->setInputs([
$output = $this->executeSingleCommand(new AddCommand(), [], [
'0',
'0',
'127.0.0.1',
Expand All @@ -46,11 +48,8 @@ public function testAddDBConnection00() {
'testing_db',
''
]);
$runner->setArgsVector([
'webfiori',
'add'
]);
$this->assertEquals(0, $runner->start());


$connName = 'db-connection-'.(count(App::getConfig()->getDBConnections()) - 1);
$this->assertEquals([
"What would you like to add?\n",
Expand All @@ -70,7 +69,8 @@ public function testAddDBConnection00() {
"Trying to connect to the database...\n",
"Success: Connected. Adding the connection...\n",
"Success: Connection information was stored in application configuration.\n"
], $runner->getOutput());
], $output);
$this->assertEquals(0, $this->getExitCode());
}
/**
* @test
Expand Down Expand Up @@ -111,8 +111,9 @@ public function testAddDBConnection01() {
"Database name:\n",
"Give your connection a friendly name: Enter = '$connName'\n",
"Trying to connect to the database...\n",
"Trying with 'localhost'...\n",
"Error: Unable to connect to the database.\n",
"Error: Unable to connect to database: 2002 - No such file or directory\n",
"Error: Unable to connect to database: 1045 - Access denied for user 'root'@'localhost' (using password: YES)\n",
"Would you like to store connection information anyway?(y/N)\n",
"Success: Connection information was stored in application configuration.\n"
], $runner->getOutput());
Expand Down Expand Up @@ -155,8 +156,9 @@ public function testAddDBConnection02() {
"Database name:\n",
"Give your connection a friendly name: Enter = '$connName'\n",
"Trying to connect to the database...\n",
"Trying with 'localhost'...\n",
"Error: Unable to connect to the database.\n",
"Error: Unable to connect to database: 2002 - No such file or directory\n",
"Error: Unable to connect to database: 1045 - Access denied for user 'root'@'localhost' (using password: YES)\n",
"Would you like to store connection information anyway?(y/N)\n",
], $runner->getOutput());
}
Expand Down
4 changes: 2 additions & 2 deletions webfiori/framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ public static function initFrameworkVersionInfo() {
*
* @since 2.1
*/
define('WF_VERSION', '3.0.0-Beta.26');
define('WF_VERSION', '3.0.0-Beta.27');
/**
* A constant that tells the type of framework version.
*
Expand All @@ -653,7 +653,7 @@ public static function initFrameworkVersionInfo() {
*
* @since 2.1
*/
define('WF_RELEASE_DATE', '2025-04-07');
define('WF_RELEASE_DATE', '2025-05-26');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions webfiori/framework/cli/commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ private function addDbConnection(): int {
$this->println('Trying to connect to the database...');

$addConnection = $this->tryConnect($connInfoObj);

$orgHost = $connInfoObj->getHost();
$orgErr = $addConnection !== true ? $addConnection->getMessage() : '';

if ($addConnection !== true) {
if ($connInfoObj->getHost() == '127.0.0.1') {
$this->println("Trying with 'localhost'...");
$connInfoObj->setHost('localhost');
$addConnection = $this->tryConnect($connInfoObj);
} else if ($connInfoObj->getHost() == 'localhost') {
$this->println("Trying with '127.0.0.1'...");
$connInfoObj->setHost('127.0.0.1');
$addConnection = $this->tryConnect($connInfoObj);
}
Expand All @@ -92,8 +96,9 @@ private function addDbConnection(): int {
App::getConfig()->addOrUpdateDBConnection($connInfoObj);
$this->success('Connection information was stored in application configuration.');
} else {
$connInfoObj->setHost($orgHost);
$this->error('Unable to connect to the database.');
$this->error($addConnection->getMessage());
$this->error($orgErr);
$this->confirmAdd($connInfoObj);
}

Expand Down
21 changes: 21 additions & 0 deletions webfiori/framework/cli/commands/RunMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,19 @@ private function getDBConnection(?MigrationsRunner $runner = null) {
return CLIUtils::getConnectionName($this);
}
}
public function getNext(MigrationsRunner $runner) : ?AbstractMigration {
foreach ($runner->getMigrations() as $m) {
if ($runner->isApplied($m->getName())) {
continue;
} else {
return $m;
}
}
return null;
}
private function applyNext(MigrationsRunner $runner, &$listOfApplied) : bool {
$toBeApplied = $this->getNext($runner);

try {
//$this->println("Executing migration...");
$applied = $runner->applyOne();
Expand All @@ -248,6 +260,15 @@ private function applyNext(MigrationsRunner $runner, &$listOfApplied) : bool {
$this->error('Failed to execute migration due to following:');
$this->println($ex->getMessage().' (Line '.$ex->getLine().')');
$this->warning('Execution stopped.');
$this->warning('Rolling back changes...');

try {
$toBeApplied->down($runner);
} catch (Throwable $exc) {
$this->error('Failed to rollback due to following:');
$this->println($ex->getMessage().' (Line '.$ex->getLine().')');
}

return false;
}
}
Expand Down
24 changes: 18 additions & 6 deletions webfiori/framework/ui/BeforeRenderCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,22 @@ class BeforeRenderCallback implements Comparable {
*
*/
public function __construct(callable $func, int $priority = 0, array $params = []) {
$this->setCallback($func, $params);
$this->setPriority($priority >= 0 ? $priority : 0);
$this->isExecuted = false;
$this->setID(hash('sha256', microtime().''.$priority.random_bytes(20)));
}
/**
* Sets the callback.
*
* @param callable $func A function to be executed.
*
* @param array $params An optional array that can hold extra parameters to
* pass to the callback.
*/
public function setCallback(callable $func, array $params = []) {
$this->callback = $func;
$this->params = $params;
$this->priority = $priority >= 0 ? $priority : 0;
$this->isExecuted = false;
}
/**
* Execute the callback.
Expand Down Expand Up @@ -75,9 +87,9 @@ public function compare($other): int {
/**
* Returns the identifier of the callback.
*
* @return int The identifier of the callback.
* @return string The identifier of the callback.
*/
public function getID() : int {
public function getID() : string {
return $this->id;
}
/**
Expand All @@ -104,9 +116,9 @@ public function isExecuted() : bool {
/**
* Sets a unique identifier for the callback.
*
* @param int $id A unique identifier for the callback.
* @param string $id A unique identifier for the callback.
*/
public function setID(int $id) {
public function setID(string $id) {
$this->id = $id;
}
/**
Expand Down
35 changes: 32 additions & 3 deletions webfiori/framework/ui/WebPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ public function __construct() {
*
* @since 1.0
*/
public function addBeforeRender(callable $callable, $priority = 0, array $params = []) : BeforeRenderCallback {
public function &addBeforeRender(callable $callable, $priority = 0, array $params = []) : BeforeRenderCallback {
$beforeRender = new BeforeRenderCallback($callable, $priority, $params);
$beforeRender->setID($this->beforeRenderCallbacks->size() + 1);
$this->beforeRenderCallbacks->add($beforeRender);

return $beforeRender;
Expand Down Expand Up @@ -728,6 +727,29 @@ public function insert($node, string $parentNodeId = self::MAIN_ELEMENTS[2]) {
public function isThemeLoaded() : bool {
return $this->theme instanceof Theme;
}
/**
* Removes a callback given its ID.
*
* @param string $id The unique identifier of the callback.
*
* @return BeforeRenderCallback|null If removed, an object will be returned
* that holds its information. Other than that, null is returned.
*/
public function removeBeforeRender(string $id) : ?BeforeRenderCallback {
$index = -1;
$tempIndex = 0;

foreach ($this->beforeRenderCallbacks as $callback) {

if ($callback->getID() == $id) {
$index = $tempIndex;
break;
}
$tempIndex++;
}

return $this->beforeRenderCallbacks->remove($index);
}
/**
* Removes a child node from the document of the page.
*
Expand Down Expand Up @@ -762,7 +784,7 @@ public function removeChild($node) {
* @since 1.0
*/
public function render(bool $formatted = false, bool $returnResult = false) {
$this->invokeBeforeRender();
$this->beforeRender();

if (!$returnResult) {
$formatted = $formatted === true || (defined('WF_VERBOSE') && WF_VERBOSE);
Expand Down Expand Up @@ -1227,6 +1249,10 @@ private function getHead() {

return $headNode;
}
public function beforeRender() {
$this->beforeRenderCallbacks->insertionSort(false);
$this->invokeBeforeRender();
}
private function invokeBeforeRender(int $current = 0) {
$currentCount = count($this->beforeRenderCallbacks);

Expand All @@ -1237,6 +1263,9 @@ private function invokeBeforeRender(int $current = 0) {
$newCount = count($this->beforeRenderCallbacks);

if ($newCount != $currentCount) {
//This part is used to handel callbacks
//which are added during the process of executing
//callbacks
$this->beforeRenderCallbacks->insertionSort(false);
$this->invokeBeforeRender();
} else {
Expand Down
Loading