Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #2773 bc database service test #2782

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions plugins/baser-core/src/Utility/BcFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,15 @@ public function write($data)
return (bool) file_put_contents($this->path, $data);
}

/**
* ファイルを削除
* @return bool
*/
public function delete()
{
if(!is_file($this->path)) {
return false;
}
return unlink($this->path);
}
}
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
14 changes: 14 additions & 0 deletions plugins/baser-core/tests/TestCase/Utility/BcFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ public function testWriteAndRead()
$this->assertEquals('test', $file->read());
(new BcFolder(dirname($path)))->delete();
}

/**
* test Delete
* @return void
*/
public function testDelete()
{
$path = TMP_TESTS . 'test' . DS . 'test.txt';
$file = new BcFile($path);
$file->create();
$this->assertTrue($file->delete());
(new BcFolder(dirname($path)))->delete();
$this->assertFalse($file->delete());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace BaserCore\Test\TestCase\View\Helper;

use BaserCore\Utility\BcFile;
use ReflectionClass;
use Cake\Event\Event;
use Cake\Core\Configure;
use Cake\Routing\Router;
use Cake\Filesystem\File;
use Cake\Event\EventManager;
use Cake\View\Helper\UrlHelper;
use Cake\View\Helper\HtmlHelper;
Expand Down Expand Up @@ -222,13 +222,13 @@ public function testGetElement()

// 管理画面用のテンプレートがなくフロントのテンプレートがある場合
$templateDir = ROOT . DS . 'plugins' . DS . 'bc-admin-third' . DS . 'templates'. DS;
$fileFront = new File($templateDir . 'element' . DS . 'test.php');
$fileFront = new BcFile($templateDir . 'element' . DS . 'test.php');
$fileFront->create();
$fileFront->write('front');
$this->assertTextContains('front', $this->BcBaser->getElement('test'));

// 管理画面用のテンプレートとフロントのテンプレートの両方がある場合
$fileAdmin = new File($templateDir . 'Admin' . DS . 'element' . DS . 'test.php');
$fileAdmin = new BcFile($templateDir . 'Admin' . DS . 'element' . DS . 'test.php');
$fileAdmin->create();
$fileAdmin->write('admin');
$this->assertTextContains('admin', $this->BcBaser->getElement('test'));
Expand Down Expand Up @@ -1813,9 +1813,8 @@ public function testIncludeCore($selectPlugin, $name, $data, $options, $expected
$path2 = ROOT . '/lib/Baser/Plugin/Test/View';
mkdir($path2);
$path3 = ROOT . '/lib/Baser/Plugin/Test/View/test.php';
$plugin = new File($path3);
$plugin = new BcFile($path3);
$plugin->write('test');
$plugin->close();
}

$this->expectOutputRegex('/' . $expected . '/', $message);
Expand Down