From 154a141d8bcffd1e2dc88c3261771244cd702fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 12 Jan 2023 10:56:26 +0100 Subject: [PATCH] improve no app ex --- src/AppScopeTrait.php | 28 ++++++++++++++++------------ src/Factory.php | 2 +- src/TrackableTrait.php | 2 +- src/TraitUtil.php | 2 +- tests/AppScopeTraitTest.php | 21 +++++++++++++++++++++ 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/AppScopeTrait.php b/src/AppScopeTrait.php index 2cf7556e0..85b24c36c 100644 --- a/src/AppScopeTrait.php +++ b/src/AppScopeTrait.php @@ -4,6 +4,8 @@ namespace Atk4\Core; +use Atk4\Ui\App; + /** * Typical software design will create the application scope. Most frameworks * relies on "static" properties, methods and classes. This does puts some @@ -14,7 +16,7 @@ */ trait AppScopeTrait { - /** @var \Atk4\Ui\App Always points to current application. */ + /** @var App */ private $_app; /** @@ -47,8 +49,8 @@ trait AppScopeTrait protected function assertInstanceOfApp(object $app): void { - if (!$app instanceof \Atk4\Ui\App) { - // called from phpunit, allow to use/test this trait without \Atk4\Ui\App class + if (!$app instanceof App) { + // called from phpunit, allow to test this trait without Atk4\Ui\App class if (class_exists(\PHPUnit\Framework\TestCase::class, false)) { foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) { if (str_starts_with($frame['class'] ?? '', 'Atk4\Core\Tests\\')) { @@ -57,7 +59,7 @@ protected function assertInstanceOfApp(object $app): void } } - throw new Exception('App must be instance of \Atk4\Ui\App'); + throw new Exception('App must be instance of Atk4\Ui\App'); } } @@ -67,27 +69,29 @@ public function issetApp(): bool } /** - * @return \Atk4\Ui\App + * @return App */ public function getApp() { - $this->assertInstanceOfApp($this->_app); + $app = $this->_app; + if ($app === null) { + throw new Exception('App is not set'); + } - return $this->_app; + return $app; } /** - * @param \Atk4\Ui\App $app + * @param App $app * * @return static */ public function setApp(object $app) { $this->assertInstanceOfApp($app); - if ($this->issetApp() && $this->getApp() !== $app) { - if ($this->getApp()->catchExceptions || $this->getApp()->alwaysRun) { // allow to replace App created by AbstractView::initDefaultApp() - TODO fix - throw new Exception('App cannot be replaced'); - } + + if ($this->issetApp()) { + throw new Exception('App is already set'); } $this->_app = $app; diff --git a/src/Factory.php b/src/Factory.php index f68695ab3..4983dca2e 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -93,7 +93,7 @@ protected function _mergeSeeds(...$seeds) if (count($injection) > 0) { if (!TraitUtil::hasDiContainerTrait($obj)) { - throw (new Exception('Property injection is possible only to objects that use \Atk4\Core\DiContainerTrait trait')) + throw (new Exception('Property injection is possible only to objects that use Atk4\Core\DiContainerTrait trait')) ->addMoreInfo('object', $obj) ->addMoreInfo('injection', $injection); } diff --git a/src/TrackableTrait.php b/src/TrackableTrait.php index e49710fc4..02e14ebdc 100644 --- a/src/TrackableTrait.php +++ b/src/TrackableTrait.php @@ -33,7 +33,7 @@ public function getOwner(): object public function setOwner(object $owner) { if ($this->issetOwner()) { - throw new Exception('Owner already set'); + throw new Exception('Owner is already set'); } $this->_owner = $owner; diff --git a/src/TraitUtil.php b/src/TraitUtil.php index 23485dbaa..1e6d6702a 100644 --- a/src/TraitUtil.php +++ b/src/TraitUtil.php @@ -28,7 +28,7 @@ public static function hasTrait($class, string $traitName): bool // prevent mass use for other than internal use then we can decide // if we want to keep support this or replace with pure interfaces if (!str_starts_with($traitName, 'Atk4\Core\\')) { - throw new Exception('Core::hasTrait is not indended for use with other than \Atk4\Core\* traits'); + throw new Exception(self::class . '::hasTrait is not indended for use with other than Atk4\Core\* traits'); } if (!isset(self::$_hasTraitCache[$class][$traitName])) { diff --git a/tests/AppScopeTraitTest.php b/tests/AppScopeTraitTest.php index 68db6e863..f9ffbf53e 100644 --- a/tests/AppScopeTraitTest.php +++ b/tests/AppScopeTraitTest.php @@ -6,6 +6,7 @@ use Atk4\Core\AppScopeTrait; use Atk4\Core\ContainerTrait; +use Atk4\Core\Exception; use Atk4\Core\NameTrait; use Atk4\Core\Phpunit\TestCase; use Atk4\Core\TrackableTrait; @@ -39,6 +40,26 @@ public function testConstruct(): void static::assertNull($this->getProtected($child, '_app')); static::assertFalse($child->issetOwner()); } + + public function testAppNotSetException(): void + { + $m = new AppScopeMock(); + + $this->expectException(Exception::class); + $this->expectErrorMessage('App is not set'); + $m->getApp(); + } + + public function testAppSetTwiceException(): void + { + $m = new AppScopeMock(); + $fakeApp = new \stdClass(); + $m->setApp($fakeApp); + + $this->expectException(Exception::class); + $this->expectErrorMessage('App is already set'); + $m->setApp($fakeApp); + } } class AppScopeMock