-
Notifications
You must be signed in to change notification settings - Fork 10.9k
/
DatabaseConnectionFactoryTest.php
executable file
·109 lines (89 loc) · 5.02 KB
/
DatabaseConnectionFactoryTest.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
<?php
use Mockery as m;
class DatabaseConnectionFactoryPDOStub extends PDO {
public function __construct() {}
}
class DatabaseConnectionFactoryTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testMakeCallsCreateConnection()
{
$factory = $this->getMock('Illuminate\Database\Connectors\ConnectionFactory', array('createConnector', 'createConnection'), array($container = m::mock('Illuminate\Container\Container')));
$container->shouldReceive('bound')->andReturn(false);
$connector = m::mock('stdClass');
$config = array('driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo');
$pdo = new DatabaseConnectionFactoryPDOStub;
$connector->shouldReceive('connect')->once()->with($config)->andReturn($pdo);
$factory->expects($this->once())->method('createConnector')->with($config)->will($this->returnValue($connector));
$mockConnection = m::mock('stdClass');
$passedConfig = array_merge($config, array('name' => 'foo'));
$factory->expects($this->once())->method('createConnection')->with($this->equalTo('mysql'), $this->equalTo($pdo), $this->equalTo('database'), $this->equalTo('prefix'), $this->equalTo($passedConfig))->will($this->returnValue($mockConnection));
$connection = $factory->make($config, 'foo');
$this->assertEquals($mockConnection, $connection);
}
public function testMakeCallsCreateConnectionForReadWrite()
{
$factory = $this->getMock('Illuminate\Database\Connectors\ConnectionFactory', array('createConnector', 'createConnection'), array($container = m::mock('Illuminate\Container\Container')));
$container->shouldReceive('bound')->andReturn(false);
$connector = m::mock('stdClass');
$config = array(
'read' => array('database' => 'database'),
'write' => array('database' => 'database'),
'driver' => 'mysql', 'prefix' => 'prefix', 'name' => 'foo'
);
$expect = $config;
unset($expect['read']);
unset($expect['write']);
$expect['database'] = 'database';
$pdo = new DatabaseConnectionFactoryPDOStub;
$connector->shouldReceive('connect')->twice()->with($expect)->andReturn($pdo);
$factory->expects($this->exactly(2))->method('createConnector')->with($expect)->will($this->returnValue($connector));
$mockConnection = m::mock('stdClass');
$mockConnection->shouldReceive('setReadPdo')->once()->andReturn($mockConnection);
$passedConfig = array_merge($expect, array('name' => 'foo'));
$factory->expects($this->once())->method('createConnection')->with($this->equalTo('mysql'), $this->equalTo($pdo), $this->equalTo('database'), $this->equalTo('prefix'), $this->equalTo($passedConfig))->will($this->returnValue($mockConnection));
$connection = $factory->make($config, 'foo');
$this->assertEquals($mockConnection, $connection);
}
public function testMakeCanCallTheContainer()
{
$factory = $this->getMock('Illuminate\Database\Connectors\ConnectionFactory', array('createConnector'), array($container = m::mock('Illuminate\Container\Container')));
$container->shouldReceive('bound')->andReturn(true);
$connector = m::mock('stdClass');
$config = array('driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo');
$pdo = new DatabaseConnectionFactoryPDOStub;
$connector->shouldReceive('connect')->once()->with($config)->andReturn($pdo);
$passedConfig = array_merge($config, array('name' => 'foo'));
$factory->expects($this->once())->method('createConnector')->with($config)->will($this->returnValue($connector));
$container->shouldReceive('make')->once()->with('db.connection.mysql', array($pdo, 'database', 'prefix', $passedConfig))->andReturn('foo');
$connection = $factory->make($config, 'foo');
$this->assertEquals('foo', $connection);
}
public function testProperInstancesAreReturnedForProperDrivers()
{
$factory = new Illuminate\Database\Connectors\ConnectionFactory($container = m::mock('Illuminate\Container\Container'));
$container->shouldReceive('bound')->andReturn(false);
$this->assertInstanceOf('Illuminate\Database\Connectors\MySqlConnector', $factory->createConnector(array('driver' => 'mysql')));
$this->assertInstanceOf('Illuminate\Database\Connectors\PostgresConnector', $factory->createConnector(array('driver' => 'pgsql')));
$this->assertInstanceOf('Illuminate\Database\Connectors\SQLiteConnector', $factory->createConnector(array('driver' => 'sqlite')));
$this->assertInstanceOf('Illuminate\Database\Connectors\SqlServerConnector', $factory->createConnector(array('driver' => 'sqlsrv')));
}
/**
* @expectedException InvalidArgumentException
*/
public function testIfDriverIsntSetExceptionIsThrown()
{
$factory = new Illuminate\Database\Connectors\ConnectionFactory($container = m::mock('Illuminate\Container\Container'));
$factory->createConnector(array('foo'));
}
/**
* @expectedException InvalidArgumentException
*/
public function testExceptionIsThrownOnUnsupportedDriver()
{
$factory = new Illuminate\Database\Connectors\ConnectionFactory($container = m::mock('Illuminate\Container\Container'));
$factory->createConnector(array('driver' => 'foo'));
}
}