* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Tests\Icinga\Application; require_once('Zend/Db.php'); require_once('Zend/Db/Adapter/Pdo/Mysql.php'); require_once('Zend/Config.php'); require_once('Zend/Log.php'); require_once('Zend/Config.php'); require_once('../../library/Icinga/Application/Logger.php'); require_once('library/Icinga/Application/ZendDbMock.php'); require_once('../../library/Icinga/Exception/ConfigurationError.php'); require_once('../../library/Icinga/Exception/ProgrammingError.php'); require_once('../../library/Icinga/Application/ConfigAwareFactory.php'); require_once('../../library/Icinga/Application/DbAdapterFactory.php'); use Tests\Icinga\Application\ZendDbMock; use Icinga\Application\DbAdapterFactory; use Icinga\Exception\ConfigurationError; /* * Unit test for the class DbAdapterFactory */ class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase { /** * The resources used for this test */ private $resources; /** * Set up the test fixture */ public function setUp() { $this->resources = array( /* * PostgreSQL databse */ 'resource1' => array( 'type' => 'db', 'db' => 'pgsql', 'dbname' => 'resource1', 'host' => 'host1', 'username' => 'username1', 'password' => 'password1' ), /* * MySQL database */ 'resource2' => array( 'type' => 'db', 'db' => 'mysql', 'dbname' => 'resource2', 'host' => 'host2', 'username' => 'username2', 'password' => 'password2' ), /* * Unsupported database type */ 'resource3' => array( 'type' => 'db', 'db' => 'mssql', 'dbname' => 'resource3', 'host' => 'host3', 'username' => 'username3', 'password' => 'password3' ), /* * Unsupported resource type */ 'resource4' => array( 'type' => 'ldap', ), ); DbAdapterFactory::setConfig( $this->resources, array( 'factory' => 'Tests\Icinga\Application\ZendDbMock' ) ); } public function testGetValidResource() { DbAdapterFactory::getDbAdapter('resource2'); $this->assertEquals( 'Pdo_Mysql', ZendDbMock::getAdapter(), 'The db adapter name must be Pdo_Mysql.'); $this->assertEquals( $this->getOptions($this->resources['resource2']), ZendDbMock::getConfig(), 'The options must match the original config file content' ); } public function testResourceExists() { $this->assertTrue(DbAdapterFactory::resourceExists('resource2'), 'resourceExists() called with an existing resource should return true'); $this->assertFalse(DbAdapterFactory::resourceExists('not existing'), 'resourceExists() called with an existing resource should return false'); $this->assertFalse(DbAdapterFactory::resourceExists('resource4'), 'resourceExists() called with an incompatible resource should return false'); } public function testGetResources() { $withoutIncompatible = array_merge(array(),$this->resources); unset($withoutIncompatible['resource4']); $this->assertEquals( $withoutIncompatible, DbAdapterFactory::getResources(), 'getResources should return an array of all existing resources that are compatible'); } /** * Test if an exception is thrown, when an invalid database is used. * * @expectedException Icinga\Exception\ConfigurationError */ public function testGetInvalidDatabase() { DbAdapterFactory::getDbAdapter('resource3'); } /** * Test if an exception is thrown, when an invalid type is used. * * @expectedException Icinga\Exception\ConfigurationError */ public function testGetInvalidType() { DbAdapterFactory::getDbAdapter('resource4'); } /** * Prepare the options object for assertions * * @param Zend_Config $config The configuration to prepare * * @return array The prepared options object */ private function getOptions($config) { $options = array_merge(array(),$config); unset($options['type']); unset($options['db']); return $options; } }