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 a221eca
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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]);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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']));

Expand Down

0 comments on commit a221eca

Please sign in to comment.