From ae70a506193f94337356ddb979b135ca0567854a Mon Sep 17 00:00:00 2001 From: mscherer Date: Thu, 2 Apr 2020 19:02:50 +0200 Subject: [PATCH 1/2] Allow regex pattern for ignore --- src/Utility/TableScanner.php | 36 ++++- tests/TestCase/Command/AllCommandTest.php | 2 +- .../Command/ControllerAllCommandTest.php | 2 +- .../Command/ControllerCommandTest.php | 2 +- .../Command/FixtureAllCommandTest.php | 2 +- tests/TestCase/Command/FixtureCommandTest.php | 2 +- .../TestCase/Command/ModelAllCommandTest.php | 2 +- .../ModelCommandAssociationDetectionTest.php | 2 +- tests/TestCase/Command/ModelCommandTest.php | 2 +- .../Command/TemplateAllCommandTest.php | 2 +- .../TestCase/Command/TemplateCommandTest.php | 2 +- tests/TestCase/Command/TestCommandTest.php | 2 +- .../Utility/Model/AssociationFilterTest.php | 2 +- tests/TestCase/Utility/TableScannerTest.php | 129 ++++++++++++++++++ .../TestCase/Utility/TemplateRendererTest.php | 2 +- tests/TestCase/View/Helper/BakeHelperTest.php | 2 +- 16 files changed, 175 insertions(+), 18 deletions(-) create mode 100644 tests/TestCase/Utility/TableScannerTest.php diff --git a/src/Utility/TableScanner.php b/src/Utility/TableScanner.php index 39a2e91be..0af9c4459 100644 --- a/src/Utility/TableScanner.php +++ b/src/Utility/TableScanner.php @@ -43,14 +43,14 @@ class TableScanner * Constructor * * @param \Cake\Database\Connection $connection The connection name in ConnectionManager - * @param string[]|null $ignore List of tables to ignore. If null, the default ignore + * @param string[]|null $ignore List of tables or regex pattern to ignore. If null, the default ignore * list will be used. */ public function __construct(Connection $connection, ?array $ignore = null) { $this->connection = $connection; if ($ignore === null) { - $ignore = ['i18n', 'cake_sessions', 'phinxlog', 'users_phinxlog']; + $ignore = ['i18n', 'cake_sessions', 'sessions', '/phinxlog/']; } $this->ignore = $ignore; } @@ -69,7 +69,7 @@ public function listAll(): array } sort($tables); - return $tables; + return array_combine($tables, $tables); } /** @@ -81,6 +81,34 @@ public function listUnskipped(): array { $tables = $this->listAll(); - return array_diff($tables, $this->ignore); + foreach ($tables as $key => $table) { + if ($this->shouldSkip($table)) { + unset($tables[$key]); + } + } + + return $tables; + } + + /** + * @param string $table + * + * @return bool + */ + protected function shouldSkip(string $table): bool + { + foreach ($this->ignore as $ignore) { + if (strpos($ignore, '/') === 0) { + if ((bool)preg_match($ignore, $table)) { + return true; + } + } + + if ($ignore === $table) { + return true; + } + } + + return false; } } diff --git a/tests/TestCase/Command/AllCommandTest.php b/tests/TestCase/Command/AllCommandTest.php index 60ee8896b..80533fb89 100644 --- a/tests/TestCase/Command/AllCommandTest.php +++ b/tests/TestCase/Command/AllCommandTest.php @@ -31,7 +31,7 @@ class AllCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.Products', 'plugin.Bake.ProductVersions', ]; diff --git a/tests/TestCase/Command/ControllerAllCommandTest.php b/tests/TestCase/Command/ControllerAllCommandTest.php index e3bc5ab32..8f58ac3ef 100644 --- a/tests/TestCase/Command/ControllerAllCommandTest.php +++ b/tests/TestCase/Command/ControllerAllCommandTest.php @@ -33,7 +33,7 @@ class ControllerAllCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.BakeArticles', 'plugin.Bake.BakeComments', ]; diff --git a/tests/TestCase/Command/ControllerCommandTest.php b/tests/TestCase/Command/ControllerCommandTest.php index 3719e1c81..3731bdc91 100644 --- a/tests/TestCase/Command/ControllerCommandTest.php +++ b/tests/TestCase/Command/ControllerCommandTest.php @@ -34,7 +34,7 @@ class ControllerCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.BakeArticles', 'plugin.Bake.BakeArticlesBakeTags', 'plugin.Bake.BakeComments', diff --git a/tests/TestCase/Command/FixtureAllCommandTest.php b/tests/TestCase/Command/FixtureAllCommandTest.php index 5f5dd27d4..0ce14d191 100644 --- a/tests/TestCase/Command/FixtureAllCommandTest.php +++ b/tests/TestCase/Command/FixtureAllCommandTest.php @@ -32,7 +32,7 @@ class FixtureAllCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'core.Articles', 'core.Comments', ]; diff --git a/tests/TestCase/Command/FixtureCommandTest.php b/tests/TestCase/Command/FixtureCommandTest.php index 2580dc427..95613e893 100644 --- a/tests/TestCase/Command/FixtureCommandTest.php +++ b/tests/TestCase/Command/FixtureCommandTest.php @@ -33,7 +33,7 @@ class FixtureCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'core.Articles', 'core.Comments', 'plugin.Bake.Datatypes', diff --git a/tests/TestCase/Command/ModelAllCommandTest.php b/tests/TestCase/Command/ModelAllCommandTest.php index b1c14e498..95ff87643 100644 --- a/tests/TestCase/Command/ModelAllCommandTest.php +++ b/tests/TestCase/Command/ModelAllCommandTest.php @@ -33,7 +33,7 @@ class ModelAllCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.TodoTasks', 'plugin.Bake.TodoItems', ]; diff --git a/tests/TestCase/Command/ModelCommandAssociationDetectionTest.php b/tests/TestCase/Command/ModelCommandAssociationDetectionTest.php index 71fff6d99..958831aff 100644 --- a/tests/TestCase/Command/ModelCommandAssociationDetectionTest.php +++ b/tests/TestCase/Command/ModelCommandAssociationDetectionTest.php @@ -39,7 +39,7 @@ class ModelCommandAssociationDetectionTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.Categories', 'plugin.Bake.CategoriesProducts', 'plugin.Bake.OldProducts', diff --git a/tests/TestCase/Command/ModelCommandTest.php b/tests/TestCase/Command/ModelCommandTest.php index ea756d9ba..02b5cad56 100644 --- a/tests/TestCase/Command/ModelCommandTest.php +++ b/tests/TestCase/Command/ModelCommandTest.php @@ -43,7 +43,7 @@ class ModelCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'core.Comments', 'core.Tags', 'core.ArticlesTags', diff --git a/tests/TestCase/Command/TemplateAllCommandTest.php b/tests/TestCase/Command/TemplateAllCommandTest.php index 6658ca537..a07115359 100644 --- a/tests/TestCase/Command/TemplateAllCommandTest.php +++ b/tests/TestCase/Command/TemplateAllCommandTest.php @@ -33,7 +33,7 @@ class TemplateAllCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'core.Articles', 'core.Comments', ]; diff --git a/tests/TestCase/Command/TemplateCommandTest.php b/tests/TestCase/Command/TemplateCommandTest.php index 2d6c57308..9b8ae2dd3 100644 --- a/tests/TestCase/Command/TemplateCommandTest.php +++ b/tests/TestCase/Command/TemplateCommandTest.php @@ -37,7 +37,7 @@ class TemplateCommandTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'core.Articles', 'core.Tags', 'core.ArticlesTags', diff --git a/tests/TestCase/Command/TestCommandTest.php b/tests/TestCase/Command/TestCommandTest.php index 784495d43..7f2aa24a6 100644 --- a/tests/TestCase/Command/TestCommandTest.php +++ b/tests/TestCase/Command/TestCommandTest.php @@ -37,7 +37,7 @@ class TestCommandTest extends TestCase * * @var string */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.BakeArticles', 'plugin.Bake.BakeArticlesBakeTags', 'plugin.Bake.BakeComments', diff --git a/tests/TestCase/Utility/Model/AssociationFilterTest.php b/tests/TestCase/Utility/Model/AssociationFilterTest.php index dcc9856b8..2316bb580 100644 --- a/tests/TestCase/Utility/Model/AssociationFilterTest.php +++ b/tests/TestCase/Utility/Model/AssociationFilterTest.php @@ -33,7 +33,7 @@ class AssociationFilterTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'core.Authors', 'core.Tags', 'core.Articles', diff --git a/tests/TestCase/Utility/TableScannerTest.php b/tests/TestCase/Utility/TableScannerTest.php new file mode 100644 index 000000000..e40145285 --- /dev/null +++ b/tests/TestCase/Utility/TableScannerTest.php @@ -0,0 +1,129 @@ +connection = ConnectionManager::get('test'); + } + + /** + * tearDown method + * + * @return void + */ + public function tearDown(): void + { + parent::tearDown(); + unset($this->tableScanner); + } + + /** + * @return void + */ + public function testListAll() + { + $this->tableScanner = new TableScanner($this->connection); + + $result = $this->tableScanner->listAll(); + $list = [ + 'todo_items' => true, + 'todo_tasks' => true, + ]; + foreach ($list as $key => $expected) { + if ($expected) { + $this->assertArrayHasKey($key, $result); + } else { + $this->assertArrayNotHasKey($key, $result); + } + } + } + + /** + * @return void + */ + public function testListUnskipped() + { + $this->tableScanner = new TableScanner($this->connection, ['todo_items']); + + $result = $this->tableScanner->listUnskipped(); + $list = [ + 'todo_items' => false, + 'todo_tasks' => true, + ]; + foreach ($list as $key => $expected) { + if ($expected) { + $this->assertArrayHasKey($key, $result); + } else { + $this->assertArrayNotHasKey($key, $result); + } + } + } + + /** + * @return void + */ + public function testListUnskippedRegex() + { + $this->tableScanner = new TableScanner($this->connection, ['/tasks$/']); + + $result = $this->tableScanner->listUnskipped(); + $list = [ + 'todo_items' => true, + 'todo_tasks' => false, + ]; + foreach ($list as $key => $expected) { + if ($expected) { + $this->assertArrayHasKey($key, $result); + } else { + $this->assertArrayNotHasKey($key, $result); + } + } + } +} diff --git a/tests/TestCase/Utility/TemplateRendererTest.php b/tests/TestCase/Utility/TemplateRendererTest.php index a8fd8bb42..0ad3d8aeb 100644 --- a/tests/TestCase/Utility/TemplateRendererTest.php +++ b/tests/TestCase/Utility/TemplateRendererTest.php @@ -26,7 +26,7 @@ class TemplateRendererTest extends TestCase { /** - * @var \Bake\Utility\TemplateRenderer|\PHPUnit\Framework\MockObject\MockObject + * @var \Bake\Utility\TemplateRenderer */ protected $renderer; diff --git a/tests/TestCase/View/Helper/BakeHelperTest.php b/tests/TestCase/View/Helper/BakeHelperTest.php index acb837014..88ab5e053 100644 --- a/tests/TestCase/View/Helper/BakeHelperTest.php +++ b/tests/TestCase/View/Helper/BakeHelperTest.php @@ -36,7 +36,7 @@ class BakeHelperTest extends TestCase * * @var array */ - public $fixtures = [ + protected $fixtures = [ 'plugin.Bake.BakeArticles', 'plugin.Bake.BakeComments', 'plugin.Bake.BakeArticlesBakeTags', From 28b708a2beee2618c2a8d7d17b16aa0ba852f5a5 Mon Sep 17 00:00:00 2001 From: mscherer Date: Thu, 2 Apr 2020 19:10:04 +0200 Subject: [PATCH 2/2] Fix CS. --- src/Utility/TableScanner.php | 2 +- tests/TestCase/Utility/TableScannerTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Utility/TableScanner.php b/src/Utility/TableScanner.php index 0af9c4459..a49dc31c3 100644 --- a/src/Utility/TableScanner.php +++ b/src/Utility/TableScanner.php @@ -91,7 +91,7 @@ public function listUnskipped(): array } /** - * @param string $table + * @param string $table Table name. * * @return bool */ diff --git a/tests/TestCase/Utility/TableScannerTest.php b/tests/TestCase/Utility/TableScannerTest.php index e40145285..94a3779e2 100644 --- a/tests/TestCase/Utility/TableScannerTest.php +++ b/tests/TestCase/Utility/TableScannerTest.php @@ -18,7 +18,6 @@ use Bake\Test\TestCase\TestCase; use Bake\Utility\TableScanner; -use Cake\Core\Plugin; use Cake\Datasource\ConnectionManager; class TableScannerTest extends TestCase