<?php

namespace SMW\Tests\Connection;

use SMW\Connection\ConnectionManager;
use SMW\Tests\PHPUnitCompat;

/**
 * @covers \SMW\Connection\ConnectionManager
 * @group semantic-mediawiki
 *
 * @license GPL-2.0-or-later
 * @since 2.1
 *
 * @author mwjames
 */
class ConnectionManagerTest extends \PHPUnit\Framework\TestCase {

	use PHPUnitCompat;

	public function testCanConstruct() {
		$this->assertInstanceOf(
			ConnectionManager::class,
			new ConnectionManager()
		);
	}

	public function testDefaultRegisteredConnectionProvided() {
		$instance = new ConnectionManager();
		$instance->releaseConnections();

		$connection = $instance->getConnection( 'mw.db' );

		$this->assertSame(
			$connection,
			$instance->getConnection( 'mw.db' )
		);

		$this->assertInstanceOf(
			'\SMW\MediaWiki\Connection\Database',
			$connection
		);

		$instance->releaseConnections();

		$this->assertNotSame(
			$connection,
			$instance->getConnection( 'mw.db' )
		);
	}

	public function testUnregisteredConnectionTypeThrowsException() {
		$instance = new ConnectionManager();

		$this->expectException( 'RuntimeException' );
		$instance->getConnection( 'mw.master' );
	}

	public function testRegisterConnectionProvider() {
		$connectionProvider = $this->getMockBuilder( '\SMW\Connection\ConnectionProvider' )
			->disableOriginalConstructor()
			->getMock();

		$connectionProvider->expects( $this->once() )
			->method( 'getConnection' );

		$instance = new ConnectionManager();
		$instance->registerConnectionProvider( 'foo', $connectionProvider );

		$instance->getConnection( 'FOO' );
	}

	public function testRegisterCallbackConnection() {
		$connectionProvider = $this->getMockBuilder( '\SMW\Connection\ConnectionProvider' )
			->disableOriginalConstructor()
			->getMock();

		$connectionProvider->expects( $this->once() )
			->method( 'getConnection' );

		$callback = static function () use( $connectionProvider ) {
			return $connectionProvider->getConnection();
		};

		$instance = new ConnectionManager();
		$instance->registerCallbackConnection( 'foo', $callback );

		$instance->getConnection( 'FOO' );
	}

}
