Skip to content

Commit

Permalink
add instance of type check (luyadev#476)
Browse files Browse the repository at this point in the history
* add instance of type check

* add tests, add changelog

* changelog, use createObject
  • Loading branch information
nadar authored and slowfox089 committed Dec 10, 2020
1 parent c1c51a5 commit 2242fea
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE
## 3.2.0

+ [#475](https://github.com/luyadev/luya-module-admin/pull/475) Added new option to return a none empty tag title.
+ [#476](https://github.com/luyadev/luya-module-admin/pull/476) Ensure importers skip objects which are not of the certain type. This is importend when a folder is used for other data.

## 3.1.0 (24. March 2020)

Expand Down
17 changes: 11 additions & 6 deletions src/importers/FilterImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace luya\admin\importers;

use Yii;
use luya\admin\base\Filter;
use luya\admin\models\StorageEffect;
use luya\admin\models\StorageFilter;
use luya\console\Importer;
Expand Down Expand Up @@ -57,12 +59,15 @@ public function run()
foreach ($this->getImporter()->getDirectoryFiles('filters') as $file) {
$filterClassName = $file['ns'];
if (class_exists($filterClassName)) {
$object = new $filterClassName();
$object->save();
$list[] = $object->identifier();
$log = $object->getLog();
if (count($log) > 0) {
$this->addLog([$object->identifier() => $log]);
$object = Yii::createObject(['class' => $filterClassName]);
// process only if object is instance of filter
if ($object instanceof Filter) {
$object->save();
$list[] = $object->identifier();
$log = $object->getLog();
if (count($log) > 0) {
$this->addLog([$object->identifier() => $log]);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/importers/PropertyImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace luya\admin\importers;

use Yii;
use luya\admin\base\Property as BaseProperty;
use luya\admin\models\Property;
use luya\console\Importer;

Expand All @@ -25,7 +26,8 @@ public function run()

if (class_exists($className)) {
$object = Yii::createObject(['class' => $className, 'moduleName' => $file['module']]);
if ($object) {
// if object exists and instance of BaseProperty
if ($object && $object instanceof BaseProperty) {
$ids[] = $this->install($object);
//$ids[] = $object->install();
$this->addLog('Property '.$object->varName().' is installed and up to date.');
Expand Down
59 changes: 59 additions & 0 deletions tests/AdminConsoleSqLiteTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace admintests;

use luya\testsuite\cases\BaseTestSuite;
use luya\base\Boot;

require 'vendor/autoload.php';
require 'data/env.php';

class AdminConsoleSqLiteTestCase extends BaseTestSuite
{
public function getConfigArray()
{
return [
'id' => 'testenv',
'siteTitle' => 'Luya Tests',
'remoteToken' => 'testtoken',
'basePath' => dirname(__DIR__),
'defaultRoute' => 'admin',
'language' => 'en',
'aliases' => [
'@runtime' => dirname(__DIR__) . '/runtime',
'@data' => dirname(__DIR__),
],
'modules' => [
'admin' => [
'class' => 'luya\admin\Module',
],
'crudmodulefolderadmin' => [
'class' => 'admintests\data\modules\crudmodulefolder\admin\Module',
]
],
'components' => [
'session' => ['class' => 'yii\web\CacheSession'],
'cache' => ['class' => 'yii\caching\DummyCache'],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite::memory:',
],
'storage' => [
'class' => 'luya\admin\filesystem\DummyFileSystem'
]
],
];
}

public function bootApplication(Boot $boot)
{
$boot->applicationConsole();
}

protected function removeNewline($text)
{
$text = trim(preg_replace('/\s+/', ' ', $text));

return str_replace(['> ', ' <'], ['>', '<'], $text);
}
}
30 changes: 30 additions & 0 deletions tests/admin/importers/FilterImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace admintests\admin\importers;

use admintests\AdminConsoleSqLiteTestCase;
use luya\admin\importers\FilterImporter;
use luya\admin\models\StorageEffect;
use luya\admin\models\StorageFile;
use luya\admin\models\StorageFilter;
use luya\admin\models\StorageFilterChain;
use luya\admin\models\StorageImage;
use luya\console\commands\ImportController;
use luya\testsuite\fixtures\NgRestModelFixture;

class FilterImporterTest extends AdminConsoleSqLiteTestCase
{
public function testInvalidObject()
{
new NgRestModelFixture(['modelClass' => StorageEffect::class]);
new NgRestModelFixture(['modelClass' => StorageFilter::class]);
new NgRestModelFixture(['modelClass' => StorageFilterChain::class]);
new NgRestModelFixture(['modelClass' => StorageFile::class]);
new NgRestModelFixture(['modelClass' => StorageImage::class]);

$base = new ImportController('importer', $this->app);
$importer = new FilterImporter($base, $this->app->getModule('admin'));
$importer->run();
$this->assertNotEmpty($base->getLog());
}
}
37 changes: 37 additions & 0 deletions tests/admin/importers/PropertyImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace admintests\admin\importers;

use admintests\AdminConsoleSqLiteTestCase;
use luya\admin\importers\PropertyImporter;
use luya\admin\models\Property;
use luya\console\commands\ImportController;
use luya\testsuite\fixtures\NgRestModelFixture;

class PropertyImporterTest extends AdminConsoleSqLiteTestCase
{
public function testInvalidObject()
{
new NgRestModelFixture(['modelClass' => Property::class]);

// data properties folder
$path = dirname(__DIR__) . '/../data/properties';
$folder = 'properties';
$ns = '\luya\admin\tests\data\properties';
$module = 'admin';



$base = new ImportController('importer', $this->app);
$this->invokeMethod($base, 'addToDirectory', [
$path, $folder, $ns, $module
// $path, $folderName, $ns, $module
]);

$importer = new PropertyImporter($base, $this->app->getModule('admin'));
$importer->run();


$this->assertNotEmpty($base->getLog());
}
}

0 comments on commit 2242fea

Please sign in to comment.