Skip to content

Commit

Permalink
Added some friendly messages for the builder factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kschroeder committed Dec 27, 2017
1 parent 1273bef commit 5151a84
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/Config/BuilderFactory.php
Expand Up @@ -47,6 +47,9 @@ public function getDatabaseConfiguration()

public function getRelationalAdapter()
{
if (!class_exists(Adapter::class)) {
throw new \Exception('Please make sure you have zendframework/zend-db installed for data storage');
}
if (!$this->adapter instanceof Adapter) {
$config = $this->getDatabaseConfiguration();
$this->adapter = new Adapter($config);
Expand All @@ -59,35 +62,46 @@ public function getMongoDsnString()
$config = $this->getDatabaseConfiguration();
$dsn = 'mongodb://'; //[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]';
$at = false;
if (isset($config['username'])) {
if (!empty($config['username'])) {
$dsn .= $config['username'];
$at = true;
}
if (isset($config['password'])) {
if (!empty($config['password'])) {
$dsn .= ':'.$config['password'];
$at = true;
}
if ($at) {
$dsn .= '@';
}
$dsn .= $config['hostname'];
if (isset($config['port'])) {
if (!empty($config['port'])) {
$dsn .= ':' . $config['port'];
}
return $dsn;
}

public function getMongoAdapter()
{
if (!class_exists(Client::class)) {
throw new \Exception('Please make sure you have mongodb/mongodb installed for data storage');
}
$config = $this->getDatabaseConfiguration();
$dsn = $this->getMongoDsnString();
$client = new Client($dsn);
$collection = isset($config['table'])?$config['table']:Mongo::TABLE;
$collection = empty($config['table'])?Mongo::TABLE:$config['table'];
return new Mongo($client->selectCollection($config['database'], $collection));
}

public function getPersistence()
{
if (empty($this->configuration->persistenceConfiguration->driver)) {
throw new \Exception('Please set your driver type either corresponding to its Zend DB adapter '
. 'name or the specific document database, such as mongo. Ensure that you have either installed '
. 'zendframework/zend-db or mongodb/mongodb depending on where you want to store your configuration.');
}
if (empty($this->configuration->persistenceConfiguration->database)) {
throw new \Exception('You must specify a database in your persistenceConfiguration');
}
if (stripos($this->configuration->persistenceConfiguration->driver, 'mongo') === 0) {
return $this->getMongoAdapter();
}
Expand Down Expand Up @@ -125,7 +139,7 @@ public function getConfigurationFiles(array $secureBaseDirectories = [])
$config = json_encode($this->configuration->configurationFiles);
$config = json_decode($config, true);
$files = [];
if (isset($config['file'])) {
if (!empty($config['file'])) {
if (!is_array($config['file'])) {
$config['file'] = [$config['file']];
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Config/BuilderFactoryTest.php
Expand Up @@ -121,6 +121,44 @@ public function testMongoDsn()
self::assertEquals('mongodb://username:password@hostname:27017', $dsn);
}

public function testExceptionThrownWithoutADriver()
{
$this->expectException(\Exception::class);
$config = new \SimpleXMLElement(<<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<magiumBase xmlns="http://www.magiumlib.com/BaseConfiguration">
<persistenceConfiguration />
</magiumBase>
XML
);
$builderFactory = new BuilderFactory(
new \SplFileInfo(__DIR__),
$config,
$this->getMockBuilder(AbstractContextConfigurationFile::class)->disableOriginalConstructor()->getMock()
);
$builderFactory->getPersistence();
}

public function testExceptionThrownWithoutADatabase()
{
$this->expectException(\Exception::class);
$config = new \SimpleXMLElement(<<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<magiumBase xmlns="http://www.magiumlib.com/BaseConfiguration">
<persistenceConfiguration>
<driver>mongo</driver>
</persistenceConfiguration>
</magiumBase>
XML
);
$builderFactory = new BuilderFactory(
new \SplFileInfo(__DIR__),
$config,
$this->getMockBuilder(AbstractContextConfigurationFile::class)->disableOriginalConstructor()->getMock()
);
$builderFactory->getPersistence();
}

public function testMongoDsnWithRequiredOnly()
{
$config = new \SimpleXMLElement(<<<XML
Expand Down

0 comments on commit 5151a84

Please sign in to comment.