1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reduce\Test\Db; |
4
|
|
|
|
5
|
|
|
use Reduce\Db\ResultSet; |
6
|
|
|
use Reduce\Db\Connection; |
7
|
|
|
use Reduce\Db\Query\QueryBuilder; |
8
|
|
|
use Doctrine\DBAL\Driver\PDOSqlite\Driver; |
9
|
|
|
|
10
|
|
|
class ResultSetTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
const TABLE_NAME = 'tableName'; |
13
|
|
|
protected $resultSet; |
14
|
|
|
|
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
$connection = $this->getMockBuilder('Reduce\Db\Connection') |
18
|
|
|
->setConstructorArgs(['global' => ['memory' => true], new Driver]) |
19
|
|
|
->getMock(); |
20
|
|
|
|
21
|
|
|
$this->resultSet = new ResultSet(static::TABLE_NAME, new QueryBuilder($connection)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
|
public function getTableName() |
26
|
|
|
{ |
27
|
|
|
$this->assertEquals('tableName', $this->resultSet->getTableName()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @test */ |
31
|
|
|
public function simpleSelect() |
32
|
|
|
{ |
33
|
|
|
$sql = 'SELECT * FROM ' . static::TABLE_NAME; |
34
|
|
|
$this->assertEquals((string) $this->resultSet, $sql); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @test */ |
38
|
|
View Code Duplication |
public function addingOrderBy() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->resultSet->orderBy('id'); |
|
|
|
|
41
|
|
|
$sql = 'SELECT * FROM ' . static::TABLE_NAME . ' ORDER BY id ASC'; |
42
|
|
|
|
43
|
|
|
$this->assertEquals((string) $this->resultSet, $sql); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @test */ |
47
|
|
View Code Duplication |
public function addingWhere() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->resultSet->where('id', 1); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$sql = 'SELECT * FROM ' . static::TABLE_NAME . ' WHERE id = ?'; |
52
|
|
|
|
53
|
|
|
$this->assertEquals((string) $this->resultSet, $sql); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @test */ |
57
|
|
View Code Duplication |
public function addingWhereInArray() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->resultSet->where(['id' => 1]); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$sql = 'SELECT * FROM ' . static::TABLE_NAME . ' WHERE id = ?'; |
62
|
|
|
|
63
|
|
|
$this->assertEquals((string) $this->resultSet, $sql); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @test */ |
67
|
|
View Code Duplication |
public function addingFullSyntax() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->resultSet->where('id = ?', 1); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$sql = 'SELECT * FROM ' . static::TABLE_NAME . ' WHERE id = ?'; |
72
|
|
|
|
73
|
|
|
$this->assertEquals((string) $this->resultSet, $sql); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @test */ |
77
|
|
View Code Duplication |
public function addingWhereWithoutValue() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$this->resultSet->where('id IS NULL'); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$sql = 'SELECT * FROM ' . static::TABLE_NAME . ' WHERE id IS NULL'; |
82
|
|
|
|
83
|
|
|
$this->assertEquals((string) $this->resultSet, $sql); |
84
|
|
|
} |
85
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.