/
ConnectionTest.php
157 lines (139 loc) · 4.89 KB
/
ConnectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
*
* PHP Version 5.x
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Model.Datasource.Database
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database;
use Cake\Model\Datasource\Database\Connection,
Cake\Core\Configure;
/**
* Tests Connection class
*
**/
class ConnectionTest extends \Cake\TestSuite\TestCase {
public function setUp() {
$this->connection = new Connection(Configure::read('Connections.test'));
}
/**
* Tests connecting to database
*
* @return void
**/
public function testConnect() {
$this->assertFalse($this->connection->isConnected());
$this->assertTrue($this->connection->connect());
$this->assertTrue($this->connection->isConnected());
}
/**
* Tests creating a connection using an invalid driver throws an exception
*
* @expectedException Cake\Model\Datasource\Database\Exception\MissingDriverException
* @expectedExceptionMessage Database driver \Foo\InvalidDriver could not be found.
* @return void
**/
public function testMissingDriver() {
$connection = new Connection(array('datasource' => '\Foo\InvalidDriver'));
}
/**
* Tests trying to use a disabled driver throws an exception
* @expectedException Cake\Model\Datasource\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\Model\Datasource\Database\Connection\Driver', array('enabled'), array(), 'DriverMock');
$connection = new Connection(array('datasource' => get_class($mock)));
}
/**
* Tests that connecting with invalid credentials or database name throws an exception
*
* @expectedException \Cake\Model\Datasource\Database\Exception\MissingConnectionException
* @return void
**/
public function testWrongCredentials() {
$connection = new Connection(array('database' => 'foobar') + Configure::read('Connections.test'));
$connection->connect();
}
/**
* Tests disconnecting from database
*
* @return void
**/
public function testDisconnect() {
$this->assertTrue($this->connection->connect());
$this->assertTrue($this->connection->isConnected());
$this->connection->disconnect();
$this->assertFalse($this->connection->isConnected());
}
/**
* Tests creation of prepared statements
*
* @return void
**/
public function testPrepare() {
$sql = 'SELECT 1 + 1';
$result = $this->connection->prepare($sql);
$this->assertInstanceOf('Cake\Model\Datasource\Database\Statement', $result);
$this->assertEquals($sql, $result->queryString);
}
/**
* Tests executing a simple query using bound values
*
* @return void
**/
public function testExecuteWithArguments() {
$sql = 'SELECT 1 + ?';
$statement = $this->connection->execute($sql, array(1));
$this->assertCount(1, $statement);
$result = $statement->fetch();
$this->assertEquals(array(2), $result);
$sql = 'SELECT 1 + ? + ? AS total';
$statement = $this->connection->execute($sql, array(2, 3));
$this->assertCount(1, $statement);
$result = $statement->fetch('assoc');
$this->assertEquals(array('total' => 6), $result);
$sql = 'SELECT 1 + :one + :two AS total';
$statement = $this->connection->execute($sql, array('one' => 2, 'two' => 3));
$this->assertCount(1, $statement);
$result = $statement->fetch('assoc');
$this->assertEquals(array('total' => 6), $result);
}
/**
* Tests executing a query with params and associated types
*
* @return void
**/
public function testExecuteWithArgumentsAndTypes() {
$sql = 'SELECT ?';
$statement = $this->connection->execute($sql, array(new \DateTime('2012-01-01')), array('date'));
$result = $statement->fetch();
$this->assertEquals('2012-01-01', $result[0]);
$sql = 'SELECT ?, ?, ?';
$params = array(new \DateTime('2012-01-01 10:10:10'), '2000-01-01 10:10:10', 1.1);
$statement = $this->connection->execute($sql, $params, array('date', 'string', 'float'));
$result = $statement->fetch();
$this->assertEquals(array('2012-01-01', '2000-01-01 10:10:10', 1.1), $result);
}
/**
* Tests that passing a unknown value to a query throws an exception
*
* @expectedException \InvalidArgumentException
* @return void
**/
public function testExecuteWithMissingType() {
$sql = 'SELECT ?';
$statement = $this->connection->execute($sql, array(new \DateTime('2012-01-01')), array('bar'));
}
}