Skip to content

Commit

Permalink
Refactor callbacks url tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Sep 25, 2021
1 parent 1653623 commit 238d33f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 47 deletions.
4 changes: 2 additions & 2 deletions demos/interactive/modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@

// When $vp2Modal->show() is activate, it will dynamically add this content to it.
$vp2Modal->set(function ($modal) use ($vp3Modal) {
// ViewTester::addTo($modal);
\Atk4\Ui\Message::addTo($modal, ['Message', @$_GET['color']])->text->addParagraph('This text is loaded using a second modal.');
ViewTester::addTo($modal);
\Atk4\Ui\Message::addTo($modal, ['Message', $_GET['color'] ?? 'No color'])->text->addParagraph('This text is loaded using a second modal.');
\Atk4\Ui\Button::addTo($modal)->set('Third modal')->on('click', $vp3Modal->show());
});

Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ parameters:
message: '~^Method Atk4\\Ui\\Callback::setUrlTrigger\(\) has no return typehint specified\.$~'
-
path: 'src/Callback.php'
message: '~^Method Atk4\\Ui\\Callback::isTriggered\(\) has no return typehint specified\.$~'
message: '~^Method Atk4\\Ui\\Callback::add\(\) has parameter \$args with no typehint specified\.$~'
-
path: 'src/Card.php'
message: '~^Method Atk4\\Ui\\Card::setDataId\(\) has no return typehint specified\.$~'
Expand Down
7 changes: 6 additions & 1 deletion src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Callback extends AbstractView
/** @var bool Allow this callback to trigger during a reload. */
public $triggerOnReload = true;

public function add($object, $args = null): AbstractView
{
throw new Exception('Callback can NOT contains children');
}

/**
* Initialization.
*/
Expand Down Expand Up @@ -86,7 +91,7 @@ public function terminateJson(AbstractView $view): void
/**
* Return true if urlTrigger is part of the request.
*/
public function isTriggered()
public function isTriggered(): bool
{
return isset($_GET[self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger]);
}
Expand Down
8 changes: 7 additions & 1 deletion src/VirtualPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VirtualPage extends View
public $cb;

/** @var string specify custom callback trigger for the URL (see Callback::$urlTrigger) */
public $urlTrigger;
protected $urlTrigger;

/** @var string UI container class */
public $ui = 'container';
Expand All @@ -32,6 +32,12 @@ protected function init(): void
parent::init();

$this->cb = $this->add([Callback::class, 'urlTrigger' => $this->urlTrigger ?: $this->name]);
unset($this->{'urlTrigger'});
}

public function getUrlTrigger(): string
{
return $this->cb->getUrlTrigger();
}

/**
Expand Down
91 changes: 49 additions & 42 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,15 +47,21 @@ protected function tearDown(): void
$_POST = [];
}

public function testCallback(): void
/**
* @param Callback|VirtualPage $cb
*/
protected function simulateCallbackTriggering(AbstractView $cb): void
{
$var = null;
$_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) {
$var = $x;
}, [34]);
Expand All @@ -73,14 +81,25 @@ public function testCallbackTrigger(): void

public function testViewUrlCallback(): void
{
$var = null;

$cbApp = \Atk4\Ui\Callback::addTo($this->app, ['urlTrigger' => 'aa']);
$v1 = \Atk4\Ui\View::addTo($this->app);
$cb = \Atk4\Ui\Callback::addTo($v1);
$cb = \Atk4\Ui\Callback::addTo($v1, ['urlTrigger' => 'bb']);

// simulate triggering
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1';
$this->simulateCallbackTriggering($cbApp);
$this->simulateCallbackTriggering($cb);

$expectedUrlCbApp = '?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=callback&' . Callback::URL_QUERY_TARGET . '=aa';
$expectedUrlCb = '?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=1&' . Callback::URL_QUERY_TRIGGER_PREFIX . 'bb=callback&' . Callback::URL_QUERY_TARGET . '=bb';
$this->assertSame($expectedUrlCbApp, $cbApp->getUrl());
$this->assertSame($expectedUrlCb, $cb->getUrl());

// URL must remain the same when urlTrigger is set but name is changed
$cbApp->name = 'aax';
$cb->name = 'bbx';
$this->assertSame($expectedUrlCbApp, $cbApp->getUrl());
$this->assertSame($expectedUrlCb, $cb->getUrl());

$var = null;
$cb->set(function ($x) use (&$var, $v1) {
$v3 = \Atk4\Ui\View::addTo($v1);
$this->assertSame('test.php', $v3->url(['test']));
Expand All @@ -96,11 +115,11 @@ public function testViewUrlCallback(): void

public function testCallbackNotFiring(): void
{
$var = null;

$cb = \Atk4\Ui\Callback::addTo($this->app);

// don't simulate triggering
// do NOT simulate triggering in this test

$var = null;
$cb->set(function ($x) use (&$var) {
$var = $x;
}, [34]);
Expand All @@ -110,13 +129,11 @@ public function testCallbackNotFiring(): void

public function testCallbackLater(): void
{
$var = null;

$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) {
$var = $x;
}, [34]);
Expand All @@ -131,18 +148,16 @@ public function testCallbackLater(): void

public function testCallbackLaterNested(): void
{
$var = null;

$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);

$var = null;
$cb->set(function ($x) use (&$var) {
$cb2 = \Atk4\Ui\CallbackLater::addTo($this->app);

$this->simulateCallbackTriggering($cb2);

$app = $this->app;
$cb->set(function ($x) use (&$var, $app, &$cbname) {
$cb2 = \Atk4\Ui\CallbackLater::addTo($app);
$cbname = $cb2->name;
$cb2->set(function ($y) use (&$var) {
$var = $y;
}, [$x]);
Expand All @@ -158,11 +173,10 @@ public function testCallbackLaterNested(): void

public function testCallbackLaterNotFiring(): void
{
$var = null;

$cb = \Atk4\Ui\CallbackLater::addTo($this->app);

// don't simulate triggering
$var = null;
$cb->set(function ($x) use (&$var) {
$var = $x;
}, [34]);
Expand All @@ -177,13 +191,11 @@ public function testCallbackLaterNotFiring(): void

public function testVirtualPage(): void
{
$var = null;

$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) {
$var = 25;
});
Expand All @@ -195,13 +207,11 @@ public function testVirtualPage(): void

public function testVirtualPageCustomTrigger(): void
{
$var = null;
$vp = VirtualPage::addTo($this->app, ['urlTrigger' => 'bah']);

$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) {
$var = 25;
});
Expand All @@ -220,12 +230,9 @@ public function callPull230()

public function testPull230(): void
{
$var = null;

$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 238d33f

Please sign in to comment.