|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Phalcon Framework. |
| 5 | + * |
| 6 | + * (c) Phalcon Team <team@phalcon.io> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE.txt |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Phalcon\Tests\Unit\Dispatcher; |
| 15 | + |
| 16 | +use Codeception\Example; |
| 17 | +use Phalcon\Di\Di; |
| 18 | +use Phalcon\Events\Event; |
| 19 | +use Phalcon\Events\Manager; |
| 20 | +use Phalcon\Mvc\Dispatcher; |
| 21 | +use Phalcon\Registry; |
| 22 | +use UnitTester; |
| 23 | + |
| 24 | +class CallActionMethodCest |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Tests Phalcon\Dispatcher :: callActionMethod() |
| 28 | + * |
| 29 | + * @author Phalcon Team <team@phalcon.io> |
| 30 | + * @since 2025-01-06 |
| 31 | + */ |
| 32 | + public function dispatcherCallActionMethod(UnitTester $I) |
| 33 | + { |
| 34 | + $I->wantToTest('Dispatcher - callActionMethod()'); |
| 35 | + |
| 36 | + $wasCalled = (object) [ |
| 37 | + 'wasCalled' => false, |
| 38 | + 'method' => function () { |
| 39 | + $this->wasCalled = true; |
| 40 | + } |
| 41 | + ]; |
| 42 | + |
| 43 | + $dispatcher = new Dispatcher(); |
| 44 | + |
| 45 | + $dispatcher->callActionMethod( |
| 46 | + $wasCalled, |
| 47 | + 'method' |
| 48 | + ); |
| 49 | + |
| 50 | + $I->assertTrue($wasCalled->wasCalled); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tests Phalcon\Dispatcher :: callActionMethod() Events |
| 55 | + * |
| 56 | + * @author Phalcon Team <team@phalcon.io> |
| 57 | + * @since 2025-01-06 |
| 58 | + */ |
| 59 | + public function dispatcherCallActionMethodWithEvents(UnitTester $I) |
| 60 | + { |
| 61 | + $I->wantToTest('Dispatcher - callActionMethod()'); |
| 62 | + |
| 63 | + $wasCalled = (object) [ |
| 64 | + 'wasCalled' => false, |
| 65 | + 'method' => function () { |
| 66 | + $this->wasCalled = 1; |
| 67 | + }, |
| 68 | + 'altMethod' => function () { |
| 69 | + $this->wasCalled = 2; |
| 70 | + } |
| 71 | + ]; |
| 72 | + |
| 73 | + $eventsManager = new Manager(); |
| 74 | + $eventsManager->attach( |
| 75 | + 'dispatch:beforeCallAction', |
| 76 | + function (Event $event, Dispatcher $dispatcher, Registry $observer) use ($eventsManager, $wasCalled) { |
| 77 | + $observer->action = "altMethod"; |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + |
| 82 | + $dispatcher = new Dispatcher(); |
| 83 | + $dispatcher->setEventsManager($eventsManager); |
| 84 | + $dispatcher->setDi(new Di()); |
| 85 | + |
| 86 | + $dispatcher->callActionMethod( |
| 87 | + $wasCalled, |
| 88 | + 'method' |
| 89 | + ); |
| 90 | + |
| 91 | + $I->assertEquals(2, $wasCalled->wasCalled); |
| 92 | + } |
| 93 | +} |
0 commit comments