|
| 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 Phalcon\Di\Di; |
| 17 | +use Phalcon\Events\Event; |
| 18 | +use Phalcon\Events\Manager; |
| 19 | +use Phalcon\Mvc\Dispatcher; |
| 20 | +use Phalcon\Support\Collection; |
| 21 | +use UnitTester; |
| 22 | + |
| 23 | +class CallActionMethodCest |
| 24 | +{ |
| 25 | + private bool $wasCalled = false; |
| 26 | + private bool $altCalled = false; |
| 27 | + private string $paramCalled = ''; |
| 28 | + |
| 29 | + public function _before(UnitTester $I) |
| 30 | + { |
| 31 | + $this->wasCalled = false; |
| 32 | + $this->altCalled = false; |
| 33 | + $this->paramCalled = ''; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Tests Phalcon\Dispatcher :: callActionMethod() |
| 38 | + * |
| 39 | + * @author Phalcon Team <team@phalcon.io> |
| 40 | + * @since 2025-01-06 |
| 41 | + */ |
| 42 | + public function dispatcherCallActionMethod(UnitTester $I) |
| 43 | + { |
| 44 | + $I->wantToTest('Dispatcher - callActionMethod()'); |
| 45 | + |
| 46 | + $dispatcher = new Dispatcher(); |
| 47 | + |
| 48 | + $dispatcher->callActionMethod( |
| 49 | + $this, |
| 50 | + '_wasCalled' |
| 51 | + ); |
| 52 | + |
| 53 | + $I->assertTrue($this->wasCalled); |
| 54 | + $I->assertFalse($this->altCalled); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Tests Phalcon\Dispatcher :: callActionMethod() |
| 59 | + * |
| 60 | + * @author Phalcon Team <team@phalcon.io> |
| 61 | + * @since 2025-01-06 |
| 62 | + */ |
| 63 | + public function dispatcherCallActionMethodWithParams(UnitTester $I) |
| 64 | + { |
| 65 | + $I->wantToTest('Dispatcher - callActionMethod() - Params'); |
| 66 | + |
| 67 | + $dispatcher = new Dispatcher(); |
| 68 | + |
| 69 | + $dispatcher->callActionMethod( |
| 70 | + $this, |
| 71 | + '_paramCalled', |
| 72 | + [ |
| 73 | + 'something' => 'else' |
| 74 | + ] |
| 75 | + ); |
| 76 | + |
| 77 | + $I->assertFalse($this->wasCalled); |
| 78 | + $I->assertFalse($this->altCalled); |
| 79 | + $I->assertEquals('else', $this->paramCalled); |
| 80 | + |
| 81 | + $dispatcher->callActionMethod( |
| 82 | + $this, |
| 83 | + '_paramCalled', |
| 84 | + [ |
| 85 | + 'something' |
| 86 | + ] |
| 87 | + ); |
| 88 | + |
| 89 | + $I->assertFalse($this->wasCalled); |
| 90 | + $I->assertFalse($this->altCalled); |
| 91 | + $I->assertEquals('something', $this->paramCalled); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Tests Phalcon\Dispatcher :: callActionMethod() Events |
| 96 | + * |
| 97 | + * @author Phalcon Team <team@phalcon.io> |
| 98 | + * @since 2025-01-06 |
| 99 | + */ |
| 100 | + public function dispatcherCallActionMethodWithEvents(UnitTester $I) |
| 101 | + { |
| 102 | + $I->wantToTest('Dispatcher - callActionMethod() - Events'); |
| 103 | + |
| 104 | + $eventsManager = new Manager(); |
| 105 | + $eventsManager->attach( |
| 106 | + 'dispatch:beforeCallAction', |
| 107 | + function (Event $event, Dispatcher $dispatcher, Collection $observer) { |
| 108 | + $observer->action = "_altCalled"; |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + $dispatcher = new Dispatcher(); |
| 113 | + $dispatcher->setEventsManager($eventsManager); |
| 114 | + $dispatcher->setDi(new Di()); |
| 115 | + |
| 116 | + $dispatcher->callActionMethod( |
| 117 | + $this, |
| 118 | + '_wasCalled' |
| 119 | + ); |
| 120 | + |
| 121 | + $I->assertTrue($this->altCalled); |
| 122 | + $I->assertFalse($this->wasCalled); |
| 123 | + } |
| 124 | + |
| 125 | + public function _wasCalled(): void |
| 126 | + { |
| 127 | + $this->wasCalled = true; |
| 128 | + } |
| 129 | + |
| 130 | + public function _altCalled(): void |
| 131 | + { |
| 132 | + $this->altCalled = true; |
| 133 | + } |
| 134 | + |
| 135 | + public function _paramCalled(string $param): void |
| 136 | + { |
| 137 | + $this->paramCalled = $param; |
| 138 | + } |
| 139 | +} |
0 commit comments