Skip to content

Commit

Permalink
unify test cb triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 26, 2021
1 parent e880c01 commit 1cf01e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/VirtualPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 28 additions & 27 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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]);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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']));

Expand Down

0 comments on commit 1cf01e8

Please sign in to comment.