Skip to content

Commit

Permalink
Dev #2773 bc database service test (#2782)
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiem-mb authored Oct 9, 2023
1 parent a3e37b5 commit dac78cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
36 changes: 18 additions & 18 deletions plugins/baser-core/src/Service/BcDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use BaserCore\Error\BcException;
use BaserCore\Model\Table\AppTable;
use BaserCore\Utility\BcContainerTrait;
use BaserCore\Utility\BcFile;
use BaserCore\Utility\BcFolder;
use BaserCore\Utility\BcUtil;
use Cake\Cache\Cache;
use Cake\Core\Configure;
Expand All @@ -27,8 +29,6 @@
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\Exception\MissingDatasourceConfigException;
use Cake\Event\EventManager;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;
use Cake\Log\LogTrait;
use Cake\ORM\Entity;
use Cake\ORM\Table;
Expand Down Expand Up @@ -369,17 +369,18 @@ protected function _loadDefaultDataPattern($pattern, $theme, $plugin = 'BaserCor
$path = BcUtil::getDefaultDataPath($theme, $pattern);
if (!$path) return true;

$Folder = new Folder($path . DS . $plugin);
$files = $Folder->read(true, true, true);
$targetTables = $files[1];
$Folder = new BcFolder($path . DS . $plugin);
$Folder->create();
$files = $Folder->getFiles(['full'=>true]);
$targetTables = $files;
$tableList = $this->getAppTableList($plugin);
$result = true;
foreach($targetTables as $targetTable) {
$targetTable = basename($targetTable, '.csv');
if (in_array($targetTable, $excludes)) continue;
if (!in_array($targetTable, $tableList)) continue;
// 初期データ投入
foreach($files[1] as $file) {
foreach($files as $file) {
if (!preg_match('/\.csv$/', $file)) continue;
$table = basename($file, '.csv');
if ($table !== $targetTable) continue;
Expand Down Expand Up @@ -929,10 +930,10 @@ public function getAppTableList($plugin = ''): array
if (!$pluginPath) continue;
$path = $pluginPath . 'config' . DS . 'Migrations';
if (!is_dir($path)) continue;
$folder = new Folder($path);
$files = $folder->read(true, true);
if (empty($files[1])) continue;
foreach($files[1] as $file) {
$folder = new BcFolder($path);
$files = $folder->getFiles();
if (empty($files)) continue;
foreach($files as $file) {
if (!preg_match('/Create([a-zA-Z]+)\./', $file, $matches)) continue;
$tableName = Inflector::tableize($matches[1]);
$checkName = $prefix . $tableName;
Expand Down Expand Up @@ -983,8 +984,8 @@ public function writeSchema($table, $options)

$dir = dirname($options['path']);
if (!is_dir($dir)) {
$folder = new Folder();
$folder->create($dir);
$folder = new BcFolder($dir);
$folder->create();
}
$describe = TableRegistry::getTableLocator()
->get('BaserCore.App')
Expand All @@ -1010,13 +1011,12 @@ public function writeSchema($table, $options)
BcUtil::onEvent($eventManager, 'View.afterRender', $afterRenderListeners);

if (!is_dir($options['path'])) {
$folder = new Folder();
$folder->create($options['path']);
$folder = new BcFolder($options['path']);
$folder->create();
}

$file = new File($options['path'] . DS . Inflector::camelize($table) . 'Schema.php');
$file = new BcFile($options['path'] . DS . Inflector::camelize($table) . 'Schema.php');
$file->write($content);
$file->close();
return true;
}

Expand Down Expand Up @@ -1193,8 +1193,8 @@ public function connectDb(array $config, $name = 'default')
]);
if($config['datasource'] === 'sqlite') {
if(!is_dir(ROOT . DS . 'db' . DS . 'sqlite')) {
$folder = new Folder(ROOT . DS . 'db' . DS . 'sqlite');
$folder->create(ROOT . DS . 'db' . DS . 'sqlite', 0777);
$folder = new BcFolder(ROOT . DS . 'db' . DS . 'sqlite');
$folder->create();
}
}
$db = ConnectionManager::get($name);
Expand Down
21 changes: 11 additions & 10 deletions plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
use BaserCore\Test\Factory\UsersUserGroupFactory;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use BaserCore\Utility\BcFile;
use BaserCore\Utility\BcFolder;
use BaserCore\Utility\BcUtil;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Postgres;
use Cake\Database\Driver\Sqlite;
use Cake\Filesystem\Folder;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\Filesystem\File;
use Cake\Utility\Inflector;
use Migrations\Migrations;

Expand Down Expand Up @@ -261,7 +261,8 @@ public function test_loadCsv()
// csvフォルダーを作成する
$csvFolder = TMP . 'csv' . DS;
if (!is_dir($csvFolder)) {
new Folder($csvFolder, true, 0777);
$csv = new BcFolder($csvFolder);
$csv->create();
}
// csvファイルを作成する
$table = 'pages';
Expand Down Expand Up @@ -334,7 +335,7 @@ public function test_loadCsvToArray()
$this->assertEquals('メインサイト', $rs[0]['title']);
$this->assertTrue(mb_check_encoding($rs[0]['title'], 'UTF-8'));

$file = new File($path);
$file = new BcFile($path);
$file->delete();
}

Expand Down Expand Up @@ -422,9 +423,9 @@ public function test_loadDefaultDataPattern()
$this->execPrivateMethod($this->BcDatabaseService, '_loadDefaultDataPattern', [$pattern, $theme]);
$path = BcUtil::getDefaultDataPath($theme, $pattern);
$this->assertNotNull($path);
$Folder = new Folder($path . DS . $plugin);
$files = $Folder->read(true, true, true);
$csvList = $files[1];
$Folder = new BcFolder($path . DS . $plugin);
$files = $Folder->getFiles(['full'=>true]);
$csvList = $files;
foreach ($csvList as $path) {
$table = basename($path, '.csv');
if (!in_array($table, $tableList)) continue;
Expand Down Expand Up @@ -537,7 +538,7 @@ public function test_writeCsv()
$this->assertEquals('', $rs[0]['modified']);
$this->assertEquals('', $rs[0]['created']);

$file = new File($path);
$file = new BcFile($path);
$file->delete();
}
/**
Expand Down Expand Up @@ -599,7 +600,7 @@ public function test_loadSchema()
{
$path = TMP . 'schema' . DS;
$fileName = 'UserActionsSchema.php';
$schemaFile = new File($path . $fileName, true);
$schemaFile = new BcFile($path . $fileName, true);
$table = 'user_actions';
// スキーマファイルを生成
$schemaFile->write("<?php
Expand Down Expand Up @@ -670,7 +671,7 @@ public function test_writeSchema()
]);
$expectedFile = TMP . 'schema/UsersSchema.php';
$this->assertFileExists($expectedFile);
$file = new File($expectedFile);
$file = new BcFile($expectedFile);
$file->delete();
}

Expand Down

0 comments on commit dac78cd

Please sign in to comment.