Skip to content

Commit

Permalink
Merge pull request #667 from cakephp/table-scanner-ignore
Browse files Browse the repository at this point in the history
Allow regex pattern for ignore
  • Loading branch information
markstory authored Apr 5, 2020
2 parents e11edef + 28b708a commit c88883e
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 18 deletions.
36 changes: 32 additions & 4 deletions src/Utility/TableScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -69,7 +69,7 @@ public function listAll(): array
}
sort($tables);

return $tables;
return array_combine($tables, $tables);
}

/**
Expand All @@ -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 Table name.
*
* @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;
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Command/AllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AllCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.Products',
'plugin.Bake.ProductVersions',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/ControllerAllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ControllerAllCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.BakeArticles',
'plugin.Bake.BakeComments',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/ControllerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ControllerCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.BakeArticles',
'plugin.Bake.BakeArticlesBakeTags',
'plugin.Bake.BakeComments',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/FixtureAllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FixtureAllCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'core.Articles',
'core.Comments',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/FixtureCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FixtureCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'core.Articles',
'core.Comments',
'plugin.Bake.Datatypes',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/ModelAllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ModelAllCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.TodoTasks',
'plugin.Bake.TodoItems',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ModelCommandAssociationDetectionTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.Categories',
'plugin.Bake.CategoriesProducts',
'plugin.Bake.OldProducts',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ModelCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'core.Comments',
'core.Tags',
'core.ArticlesTags',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/TemplateAllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TemplateAllCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'core.Articles',
'core.Comments',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/TemplateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TemplateCommandTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'core.Articles',
'core.Tags',
'core.ArticlesTags',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TestCommandTest extends TestCase
*
* @var string
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.BakeArticles',
'plugin.Bake.BakeArticlesBakeTags',
'plugin.Bake.BakeComments',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Utility/Model/AssociationFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AssociationFilterTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'core.Authors',
'core.Tags',
'core.Articles',
Expand Down
128 changes: 128 additions & 0 deletions tests/TestCase/Utility/TableScannerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Test\TestCase\Utility;

use Bake\Test\TestCase\TestCase;
use Bake\Utility\TableScanner;
use Cake\Datasource\ConnectionManager;

class TableScannerTest extends TestCase
{
/**
* @var string[]
*/
protected $fixtures = [
'plugin.Bake.TodoTasks',
'plugin.Bake.TodoItems',
];

/**
* @var \Bake\Utility\TableScanner
*/
protected $tableScanner;

/**
* @var \Cake\Database\Connection
*/
protected $connection;

/**
* setUp method
*
* @return void
*/
public function setUp(): void
{
parent::setUp();

$this->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);
}
}
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Utility/TemplateRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class TemplateRendererTest extends TestCase
{
/**
* @var \Bake\Utility\TemplateRenderer|\PHPUnit\Framework\MockObject\MockObject
* @var \Bake\Utility\TemplateRenderer
*/
protected $renderer;

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Helper/BakeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BakeHelperTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected $fixtures = [
'plugin.Bake.BakeArticles',
'plugin.Bake.BakeComments',
'plugin.Bake.BakeArticlesBakeTags',
Expand Down

0 comments on commit c88883e

Please sign in to comment.