Skip to content

Commit 6c6fb16

Browse files
committed
Update tests
1 parent 11ee12e commit 6c6fb16

3 files changed

Lines changed: 94 additions & 21 deletions

File tree

tests/ConfigurationTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66

77
class ConfigurationTest extends TestCase
88
{
9+
public static function getConfiguration()
10+
{
11+
return new Configuration(
12+
'root',
13+
'secret',
14+
'test',
15+
'localhost',
16+
3306,
17+
'mysql',
18+
'utf8mb4',
19+
'utf8mb4_general_ci'
20+
);
21+
}
22+
923
public function testAttributes()
1024
{
1125
$attributes = array(
@@ -25,16 +39,7 @@ public function testAttributes()
2539

2640
public function testConfig()
2741
{
28-
$configuration = new Configuration(
29-
'root',
30-
'secret',
31-
'test',
32-
'localhost',
33-
3306,
34-
'mysql',
35-
'utf8mb4',
36-
'utf8mb4_general_ci'
37-
);
42+
$configuration = self::getConfiguration();
3843

3944
$this->assertSame('utf8mb4', $configuration->getCharset());
4045
$this->assertSame('utf8mb4_general_ci', $configuration->getCollation());

tests/ConnectionTest.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
namespace PhpOrm\Tests;
33

44
use PHPUnit\Framework\TestCase;
5-
use PhpOrm\Connection;
65
use PhpOrm\Configuration;
6+
use PhpOrm\Connection;
77

88
class ConnectionTest extends TestCase
99
{
10+
public static function getConfiguration()
11+
{
12+
return new Configuration(
13+
'root',
14+
'secret',
15+
'test',
16+
'localhost',
17+
3306,
18+
'mysql',
19+
'utf8mb4',
20+
'utf8mb4_general_ci'
21+
);
22+
}
23+
1024
public function testAttributes()
1125
{
1226
$attributes = array(
@@ -20,18 +34,47 @@ public function testAttributes()
2034

2135
public function testDependencyInjection()
2236
{
23-
$configuration = new Configuration(
24-
'root',
25-
'secret',
26-
'test',
27-
'localhost',
28-
3306,
29-
'mysql',
30-
'utf8mb4',
31-
'utf8mb4_general_ci'
32-
);
37+
$configuration = self::getConfiguration();
3338
$connection = new Connection($configuration);
3439

3540
$this->assertSame('mysql:host=localhost;port=3306;dbname=test;charset=utf8mb4', $connection->getDsn());
3641
}
42+
43+
public function testConnectFailed()
44+
{
45+
$configuration = self::getConfiguration();
46+
$connection = new Connection($configuration);
47+
48+
$this->expectException(\Exception::class);
49+
50+
$connection->connect();
51+
}
52+
53+
public function testDisconnect()
54+
{
55+
$configuration = self::getConfiguration();
56+
$connection = new Connection($configuration);
57+
58+
$connection->disconnect();
59+
60+
$this->assertNull($connection->getDbh());
61+
}
62+
63+
public function testReconnectFailed()
64+
{
65+
$configuration = self::getConfiguration();
66+
$connection = new Connection($configuration);
67+
68+
$this->expectException(\Exception::class);
69+
70+
$connection->reconnect();
71+
}
72+
73+
public function testDbhFailed()
74+
{
75+
$configuration = self::getConfiguration();
76+
$connection = new Connection($configuration);
77+
78+
$this->assertNull($connection->getDbh());
79+
}
3780
}

tests/DBTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66

77
class DBTest extends TestCase
88
{
9+
public static function init()
10+
{
11+
$data = '<?php return array("default" => array(
12+
"username" => "root",
13+
"password" => "secret",
14+
"database" => "test",
15+
"host" => "localhost",
16+
"port" => 3306,
17+
"driver" => "mysql",
18+
"charset" => "utf8mb4",
19+
"collation" => "utf8mb4_general_ci",
20+
));';
21+
$filename = tempnam(sys_get_temp_dir(), 'Tux');
22+
file_put_contents($filename, $data, LOCK_EX);
23+
DB::config($filename);
24+
}
25+
926
public function testAttributes()
1027
{
1128
$attributes = array(
@@ -32,4 +49,12 @@ public function testAttributes()
3249
$this->assertClassHasAttribute($attribute, DB::class);
3350
}
3451
}
52+
53+
public function testConstruct()
54+
{
55+
$this->expectException(\Exception::class);
56+
57+
self::init();
58+
new DB();
59+
}
3560
}

0 commit comments

Comments
 (0)