Skip to content

Commit

Permalink
Add support for customizable connection alias for table lookups (#462)
Browse files Browse the repository at this point in the history
* Add $connectionName property to config file and Configuration class

* Model scanner consume and use configured connection name

Co-authored-by: Jamison Bryant <[email protected]>
  • Loading branch information
jamisonbryant and Jamison Bryant authored Sep 3, 2022
1 parent bb72b53 commit 248a425
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
3 changes: 3 additions & 0 deletions assets/swagger_bake.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* ################################
* # OPTIONAL SETTINGS:
* ################################
*
* @var string $connectionName The connection name to use when loading tables for building schemas from models.
* Default: default
*
* @var array $editActionMethods The default HTTP methods to use for CakePHPs edit() action.
* Default: ['PATCH']
Expand Down
37 changes: 37 additions & 0 deletions src/Lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace SwaggerBake\Lib;

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -77,6 +78,11 @@ class Configuration
*/
private array $editActionMethods = ['PATCH'];

/**
* @var string The connection name to use when loading tables for building schemas from models.
*/
private string $connectionName = 'default';

/**
* @var array Array of namespaces. Useful if your controllers or entities exist in non-standard namespace such
* as a plugin. This was mostly added to aid in unit testing, but there are cases where controllers may
Expand Down Expand Up @@ -455,6 +461,37 @@ public function setJsonOptions(int $jsonOptions)
return $this;
}

/**
* @return string
*/
public function getConnectionName(): string
{
return $this->connectionName;
}

/**
* @param string $connectionName Connection name to use when loading tables for building schemas from models.
* @return $this
*/
public function setConnectionName(string $connectionName)
{
$configuredConnections = ConnectionManager::configured();

if (!in_array($connectionName, $configuredConnections)) {
throw new InvalidArgumentException(
sprintf(
'Invalid connectionName supplied: %s. Must be one of %s',
$connectionName,
implode(', ', $configuredConnections)
)
);
}

$this->connectionName = $connectionName;

return $this;
}

/**
* @return string[]
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Model/ModelScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getModelDecorators(): array
{
$return = [];

$connection = ConnectionManager::get('default');
$connection = ConnectionManager::get($this->config->getConnectionName());
$namespaces = $this->config->getNamespaces();

foreach ($namespaces['tables'] as $tableNs) {
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/Lib/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SwaggerBake\Test\TestCase\Lib;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use SwaggerBake\Lib\Configuration;
Expand All @@ -22,7 +23,8 @@ class ConfigurationTest extends TestCase

public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
parent::setUp();

$this->configuration = $this->createConfiguration();
}

Expand Down Expand Up @@ -112,6 +114,7 @@ public function dataProviderInvalidConfig(): array
['webPath', 'nope', \InvalidArgumentException::class, 'Invalid webPath'],
['docType', 'nope', \InvalidArgumentException::class, 'Invalid docType'],
['editActionMethods', ['nope'], \InvalidArgumentException::class, 'Invalid editActionMethod'],
['connectionName', 'nope', \InvalidArgumentException::class, 'Invalid connectionName'],
];
}
}
13 changes: 7 additions & 6 deletions tests/TestCase/Lib/Model/ModelScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace SwaggerBake\Test\TestCase\Lib\Model;

use Cake\Collection\Collection;
use Cake\Datasource\ConnectionManager;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Exception\SwaggerBakeRunTimeException;
use SwaggerBake\Lib\Model\ModelDecorator;
use SwaggerBake\Lib\Model\ModelScanner;
use SwaggerBake\Lib\Route\RouteScanner;
Expand All @@ -29,12 +27,14 @@ class ModelScannerTest extends TestCase

public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
parent::setUp();

$router = new Router();
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees');
});

$this->router = $router;

$this->config = [
Expand All @@ -50,7 +50,8 @@ public function setUp(): void
'controllers' => ['\SwaggerBakeTest\App\\'],
'entities' => ['\SwaggerBakeTest\App\\'],
'tables' => ['\SwaggerBakeTest\App\\'],
]
],
'connectionName' => 'test',
];
}

Expand All @@ -66,7 +67,7 @@ public function test_model_should_not_be_decorated_when_no_route_exists(): void
$this->assertCount(0, $collection);
}

/* public function test_should_use_naming_conventions_when_multiple_models_found(): void
/* public function test_should_use_naming_conventions_when_multiple_models_found(): void
{
$config = new Configuration($this->config, SWAGGER_BAKE_TEST_APP);
$routeScanner = new RouteScanner($this->router, $config);
Expand All @@ -75,4 +76,4 @@ public function test_model_should_not_be_decorated_when_no_route_exists(): void
$modelDecorators = (new ModelScanner($routeScanner, $config))->getModelDecorators();
}*/
}
}

0 comments on commit 248a425

Please sign in to comment.