Skip to content

Commit

Permalink
Adding Unit Test - Debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenikkusu committed Feb 6, 2025
1 parent 75a3179 commit 3bd99a6
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions tests/unit/Dispatcher/CallActionMethodCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@

namespace Phalcon\Tests\Unit\Dispatcher;

use Codeception\Example;
use Phalcon\Di\Di;
use Phalcon\Events\Event;
use Phalcon\Events\Manager;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Registry;
use Phalcon\Support\Registry;
use UnitTester;

class CallActionMethodCest
{
private bool $wasCalled = false;
private bool $altCalled = false;

public function _before(UnitTester $I)
{
$this->wasCalled = false;
$this->altCalled = false;
}

/**
* Tests Phalcon\Dispatcher :: callActionMethod()
*
Expand All @@ -33,21 +41,15 @@ public function dispatcherCallActionMethod(UnitTester $I)
{
$I->wantToTest('Dispatcher - callActionMethod()');

$wasCalled = (object) [
'wasCalled' => false,
'method' => function () {
$this->wasCalled = true;
}
];

$dispatcher = new Dispatcher();

$dispatcher->callActionMethod(
$wasCalled,
'method'
$this,
'wasCalled'
);

$I->assertTrue($wasCalled->wasCalled);
$I->assertTrue($this->wasCalled);
$I->assertFalse($this->altCalled);
}

/**
Expand All @@ -58,23 +60,13 @@ public function dispatcherCallActionMethod(UnitTester $I)
*/
public function dispatcherCallActionMethodWithEvents(UnitTester $I)
{
$I->wantToTest('Dispatcher - callActionMethod()');

$wasCalled = (object) [
'wasCalled' => false,
'method' => function () {
$this->wasCalled = 1;
},
'altMethod' => function () {
$this->wasCalled = 2;
}
];
$I->wantToTest('Dispatcher - callActionMethod() - Events');

$eventsManager = new Manager();
$eventsManager->attach(
'dispatch:beforeCallAction',
function (Event $event, Dispatcher $dispatcher, Registry $observer) use ($eventsManager, $wasCalled) {
$observer->action = "altMethod";
function (Event $event, Dispatcher $dispatcher, Registry $observer) {
$observer->action = "altCalled";
}
);

Expand All @@ -84,10 +76,21 @@ function (Event $event, Dispatcher $dispatcher, Registry $observer) use ($events
$dispatcher->setDi(new Di());

$dispatcher->callActionMethod(
$wasCalled,
'method'
$this,
'wasCalled'
);

$I->assertEquals(2, $wasCalled->wasCalled);
$I->assertTrue($this->altCalled);
$I->assertFalse($this->wasCalled);
}

public function wasCalled(): void
{
$this->wasCalled = true;
}

public function altCalled(): void
{
$this->altCalled = true;
}
}

0 comments on commit 3bd99a6

Please sign in to comment.