diff --git a/tests/CallbackTest.php b/tests/CallbackTest.php index 5595473c92..4835229e50 100644 --- a/tests/CallbackTest.php +++ b/tests/CallbackTest.php @@ -5,6 +5,7 @@ namespace Atk4\Ui\Tests; use Atk4\Core\Phpunit\TestCase; +use Atk4\Ui\AbstractView; use Atk4\Ui\Callback; class AppMock extends \Atk4\Ui\App @@ -45,12 +46,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 +85,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 +108,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 +122,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 +141,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]); @@ -181,8 +184,7 @@ public function testVirtualPage(): void { $vp = \Atk4\Ui\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) { @@ -198,8 +200,7 @@ public function testVirtualPageCustomTrigger(): void { $vp = \Atk4\Ui\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) { @@ -222,8 +223,7 @@ public function testPull230(): void { $vp = \Atk4\Ui\VirtualPage::addTo($this->app); - // simulate triggering - $_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $vp->name] = '1'; + $this->simulateCallbackTriggering($vp); $vp->set(\Closure::fromCallable([$this, 'callPull230']));