Repository navigation
Expand file tree
/
Copy pathConnectionTest.php
More file actions
69 lines (55 loc) · 2.06 KB
/
Copy pathConnectionTest.php
File metadata and controls
69 lines (55 loc) · 2.06 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database;
use Hyperf\Database\Connection;
use Hyperf\Database\Query\Expression;
use Hyperf\Database\Query\Grammars\MySqlGrammar;
use Mockery;
use PDO;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use function Hyperf\Support\build_sql;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ConnectionTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}
public function getConnection(): Connection
{
$connection = new Connection(Mockery::mock(PDO::class));
return $connection->setQueryGrammar(new MySqlGrammar());
}
public function testConnectionTable()
{
$connection = $this->getConnection();
$sql = $connection->table('user')->toSql();
$this->assertSame('select * from `user`', $sql);
$sql = $connection->table('user as u')->toSql();
$this->assertSame('select * from `user` as `u`', $sql);
$sql = $connection->table(new Expression('(select 1 as id) a'))->toSql();
$this->assertSame('select * from (select 1 as id) a', $sql);
}
public function testBuildSql()
{
$this->assertSame(build_sql('select * from `user`', []), 'select * from `user`');
$this->assertSame(build_sql('select * from `user` where `id` = ? and `name` = ? and sex = ? and age = ?', [
1, '1', false, true,
]), "select * from `user` where `id` = 1 and `name` = '1' and sex = 0 and age = 1");
$this->assertSame(build_sql('select * from `user` where `name` = ? and `sex` = ?', ['?name', 1]), "select * from `user` where `name` = '?name' and `sex` = 1");
$this->assertSame(build_sql('select * from `user` where `name` = ? and `sex` = ?', ['?name', 1, 2]), "select * from `user` where `name` = '?name' and `sex` = 1");
}
}