Skip to content

Commit

Permalink
better ex
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
mvorisek committed Jan 12, 2023
1 parent 9361910 commit 2b6a0f7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
30 changes: 19 additions & 11 deletions src/AppScopeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,7 +16,7 @@
*/
trait AppScopeTrait
{
/** @var \Atk4\Ui\App Always points to current application. */
/** @var App */
private $_app;

/**
Expand Down Expand Up @@ -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\\')) {
Expand All @@ -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');
}
}

Expand All @@ -67,27 +69,33 @@ 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()) {
if ($this->getApp() === $app) {
return $this;
}

throw new Exception('App cannot be replaced');
}

$this->_app = $app;
Expand Down
2 changes: 1 addition & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/TraitUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('TraitUtil::hasTrait is not indended for use with other than Atk4\Core\* traits');
}

if (!isset(self::$_hasTraitCache[$class][$traitName])) {
Expand Down
24 changes: 24 additions & 0 deletions tests/AppScopeTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +40,29 @@ 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 testAppReplaceException(): void
{
$m = new AppScopeMock();
$fakeApp = new \stdClass();
$fakeApp2 = new \stdClass();
$m->setApp($fakeApp);

$m->setApp($fakeApp);

$this->expectException(Exception::class);
$this->expectErrorMessage('App cannot be replaced');
$m->setApp($fakeApp2);
}
}

class AppScopeMock
Expand Down

0 comments on commit 2b6a0f7

Please sign in to comment.