diff --git a/src/VirtualPage.php b/src/VirtualPage.php index bfb79179f1..d3f08a6b42 100644 --- a/src/VirtualPage.php +++ b/src/VirtualPage.php @@ -32,7 +32,7 @@ protected function init(): void parent::init(); $this->cb = $this->add([Callback::class, 'urlTrigger' => $this->urlTrigger ?: $this->name]); - unset($this->urlTrigger); + unset($this->{'urlTrigger'}); } public function getUrlTrigger(): string diff --git a/tests/CallbackTest.php b/tests/CallbackTest.php index 5595473c92..d8e43b9039 100644 --- a/tests/CallbackTest.php +++ b/tests/CallbackTest.php @@ -5,7 +5,9 @@ namespace Atk4\Ui\Tests; use Atk4\Core\Phpunit\TestCase; +use Atk4\Ui\AbstractView; use Atk4\Ui\Callback; +use Atk4\Ui\VirtualPage; class AppMock extends \Atk4\Ui\App { @@ -45,12 +47,19 @@ protected function tearDown(): void $_POST = []; } + /** + * @param Callback|VirtualPage $cb + */ + protected function simulateCallbackTriggering(AbstractView $cb): void + { + $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->getUrlTrigger()] = '1'; + } + public function testCallback(): void { $cb = \Atk4\Ui\Callback::addTo($this->app); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1'; + $this->simulateCallbackTriggering($cb); $var = null; $cb->set(function ($x) use (&$var) { @@ -77,14 +86,10 @@ public function testViewUrlCallback(): void $cb = \Atk4\Ui\Callback::addTo($v1, ['urlTrigger' => 'bb']); $cb->name = 'bbx'; - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1'; - - // test for non sticky - $cb->setUrlTrigger($cb->name); + $this->simulateCallbackTriggering($cb); $this->assertSame('?__atk_cb_aa=callback&__atk_cbtarget=aa', $cbApp->getUrl()); - $this->assertSame('?__atk_cb_bbx=callback&__atk_cbtarget=bbx', $cb->getUrl()); + $this->assertSame('?__atk_cb_bb=callback&__atk_cbtarget=bb', $cb->getUrl()); $var = null; $cb->set(function ($x) use (&$var, $v1) { @@ -104,7 +109,8 @@ public function testCallbackNotFiring(): void { $cb = \Atk4\Ui\Callback::addTo($this->app); - // don't simulate triggering + // do NOT simulate triggering + $var = null; $cb->set(function ($x) use (&$var) { $var = $x; @@ -117,8 +123,7 @@ public function testCallbackLater(): void { $cb = \Atk4\Ui\CallbackLater::addTo($this->app); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1'; + $this->simulateCallbackTriggering($cb); $var = null; $cb->set(function ($x) use (&$var) { @@ -137,15 +142,14 @@ public function testCallbackLaterNested(): void { $cb = \Atk4\Ui\CallbackLater::addTo($this->app); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1'; - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name . '_2'] = '1'; + $this->simulateCallbackTriggering($cb); - $app = $this->app; $var = null; - $cb->set(function ($x) use (&$var, $app, &$cbname) { - $cb2 = \Atk4\Ui\CallbackLater::addTo($app); - $cbname = $cb2->name; + $cb->set(function ($x) use (&$var) { + $cb2 = \Atk4\Ui\CallbackLater::addTo($this->app); + + $this->simulateCallbackTriggering($cb2); + $cb2->set(function ($y) use (&$var) { $var = $y; }, [$x]); @@ -179,10 +183,9 @@ public function testCallbackLaterNotFiring(): void public function testVirtualPage(): void { - $vp = \Atk4\Ui\VirtualPage::addTo($this->app); + $vp = VirtualPage::addTo($this->app); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $vp->name] = '1'; + $this->simulateCallbackTriggering($vp); $var = null; $vp->set(function ($p) use (&$var) { @@ -196,10 +199,9 @@ public function testVirtualPage(): void public function testVirtualPageCustomTrigger(): void { - $vp = \Atk4\Ui\VirtualPage::addTo($this->app, ['urlTrigger' => 'bah']); + $vp = VirtualPage::addTo($this->app, ['urlTrigger' => 'bah']); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . 'bah'] = '1'; + $this->simulateCallbackTriggering($vp); $var = null; $vp->set(function ($p) use (&$var) { @@ -220,10 +222,9 @@ public function callPull230() public function testPull230(): void { - $vp = \Atk4\Ui\VirtualPage::addTo($this->app); + $vp = VirtualPage::addTo($this->app); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $vp->name] = '1'; + $this->simulateCallbackTriggering($vp); $vp->set(\Closure::fromCallable([$this, 'callPull230']));