From 5151a843cf5029366a39b7d725d30f4a0f5aa9e0 Mon Sep 17 00:00:00 2001 From: Kevin Schroeder Date: Wed, 27 Dec 2017 09:57:42 -0600 Subject: [PATCH] Added some friendly messages for the builder factory --- lib/Config/BuilderFactory.php | 24 ++++++++++++++---- tests/Config/BuilderFactoryTest.php | 38 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/lib/Config/BuilderFactory.php b/lib/Config/BuilderFactory.php index b3b36cf..e3d2573 100644 --- a/lib/Config/BuilderFactory.php +++ b/lib/Config/BuilderFactory.php @@ -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); @@ -59,11 +62,11 @@ 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; } @@ -71,7 +74,7 @@ public function getMongoDsnString() $dsn .= '@'; } $dsn .= $config['hostname']; - if (isset($config['port'])) { + if (!empty($config['port'])) { $dsn .= ':' . $config['port']; } return $dsn; @@ -79,15 +82,26 @@ public function getMongoDsnString() 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(); } @@ -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']]; } diff --git a/tests/Config/BuilderFactoryTest.php b/tests/Config/BuilderFactoryTest.php index a6b9cab..2e3e90f 100644 --- a/tests/Config/BuilderFactoryTest.php +++ b/tests/Config/BuilderFactoryTest.php @@ -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 + ); + $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(<< + + + mongo + + +XML + ); + $builderFactory = new BuilderFactory( + new \SplFileInfo(__DIR__), + $config, + $this->getMockBuilder(AbstractContextConfigurationFile::class)->disableOriginalConstructor()->getMock() + ); + $builderFactory->getPersistence(); + } + public function testMongoDsnWithRequiredOnly() { $config = new \SimpleXMLElement(<<