<commit_before><?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         3.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\Database;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
 * Tests Connection class
 */
class ConnectionTest extends TestCase {

	public $fixtures = ['core.things'];

	public function setUp() {
		$this->connection = ConnectionManager::get('test');
		parent::setUp();
	}

	public function tearDown() {
		$this->connection->useSavePoints(false);
		unset($this->connection);
		parent::tearDown();
	}

/**
 * Auxiliary method to build a mock for a driver so it can be injected into
 * the connection object
 *
 * @return \Cake\Database\Driver
 */
	public function getMockFormDriver() {
		$driver = $this->getMock('Cake\Database\Driver');
		$driver->expects($this->once())
			->method('enabled')
			->will($this->returnValue(true));
		return $driver;
	}

/**
 * Tests connecting to database
 *
 * @return void
 */
	public function testConnect() {
		$this->assertTrue($this->connection->connect());
		$this->assertTrue($this->connection->isConnected());
	}

/**
 * Tests creating a connection using no driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingDriverException
 * @expectedExceptionMessage Database driver  could not be found.
 * @return void
 */
	public function testNoDriver() {
		$connection = new Connection([]);
	}

/**
 * Tests creating a connection using an invalid driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingDriverException
 * @expectedExceptionMessage Database driver  could not be found.
 * @return void
 */
	public function testEmptyDriver() {
		$connection = new Connection(['driver' => false]);
	}

/**
 * Tests creating a connection using an invalid driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingDriverException
 * @expectedExceptionMessage Database driver \Foo\InvalidDriver could not be found.
 * @return void
 */
	public function testMissingDriver() {
		$connection = new Connection(['driver' => '\Foo\InvalidDriver']);
	}

/**
 * Tests trying to use a disabled driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingExtensionException
 * @expectedExceptionMessage Database driver DriverMock cannot be used due to a missing PHP extension or unmet dependency
 * @return void
 */
	public function testDisabledDriver() {
		$mock = $this->getMock('\Cake\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
		$connection = new Connection(['driver' => $mock]);
	}

/**
 * Tests that connecting with invalid credentials or database name throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingConnectionException
 * @return void
 */
	public function testWrongCredentials() {
		$config = ConnectionManager::config('test');
		$this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
		$connection = new Connection(['database' => '_probably_not_there_'] + ConnectionManager::config('test'));
		$connection->connect();
	}

/**
 * Tests creation of prepared statements
 *
 * @return void
 */
	public function testPrepare() {
		$sql = 'SELECT 1 + 1';
		$result = $this->connection->prepare($sql);
		$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
		$this->assertEquals($sql, $result->queryString);

		$query = $this->connection->newQuery()->select('1 + 1');<commit_msg>Change database for connection to force an exception for SQLite<commit_after><?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         3.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\Database;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
 * Tests Connection class
 */
class ConnectionTest extends TestCase {

	public $fixtures = ['core.things'];

	public function setUp() {
		$this->connection = ConnectionManager::get('test');
		parent::setUp();
	}

	public function tearDown() {
		$this->connection->useSavePoints(false);
		unset($this->connection);
		parent::tearDown();
	}

/**
 * Auxiliary method to build a mock for a driver so it can be injected into
 * the connection object
 *
 * @return \Cake\Database\Driver
 */
	public function getMockFormDriver() {
		$driver = $this->getMock('Cake\Database\Driver');
		$driver->expects($this->once())
			->method('enabled')
			->will($this->returnValue(true));
		return $driver;
	}

/**
 * Tests connecting to database
 *
 * @return void
 */
	public function testConnect() {
		$this->assertTrue($this->connection->connect());
		$this->assertTrue($this->connection->isConnected());
	}

/**
 * Tests creating a connection using no driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingDriverException
 * @expectedExceptionMessage Database driver  could not be found.
 * @return void
 */
	public function testNoDriver() {
		$connection = new Connection([]);
	}

/**
 * Tests creating a connection using an invalid driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingDriverException
 * @expectedExceptionMessage Database driver  could not be found.
 * @return void
 */
	public function testEmptyDriver() {
		$connection = new Connection(['driver' => false]);
	}

/**
 * Tests creating a connection using an invalid driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingDriverException
 * @expectedExceptionMessage Database driver \Foo\InvalidDriver could not be found.
 * @return void
 */
	public function testMissingDriver() {
		$connection = new Connection(['driver' => '\Foo\InvalidDriver']);
	}

/**
 * Tests trying to use a disabled driver throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingExtensionException
 * @expectedExceptionMessage Database driver DriverMock cannot be used due to a missing PHP extension or unmet dependency
 * @return void
 */
	public function testDisabledDriver() {
		$mock = $this->getMock('\Cake\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
		$connection = new Connection(['driver' => $mock]);
	}

/**
 * Tests that connecting with invalid credentials or database name throws an exception
 *
 * @expectedException \Cake\Database\Exception\MissingConnectionException
 * @return void
 */
	public function testWrongCredentials() {
		$config = ConnectionManager::config('test');
		$this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
		$connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::config('test'));
		$connection->connect();
	}

/**
 * Tests creation of prepared statements
 *
 * @return void
 */
	public function testPrepare() {
		$sql = 'SELECT 1 + 1';
		$result = $this->connection->prepare($sql);
		$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
		$this->assertEquals($sql, $result->queryString);

		$query = $this->connection->newQuery()->select('1 + 1');
{'fraction_non_alphanumeric': 0.1355384232777916, 'fraction_numerical': 0.001740860482467048, 'mean_word_length': 4.585416666666666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5314201', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2173, 'n_words': 630}