forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertTest.php
More file actions
106 lines (74 loc) · 3.26 KB
/
Copy pathInsertTest.php
File metadata and controls
106 lines (74 loc) · 3.26 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
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
<?php namespace Builder;
use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\Query;
use CodeIgniter\Database\MockConnection;
class InsertTest extends \CIUnitTestCase
{
protected $db;
//--------------------------------------------------------------------
public function setUp()
{
$this->db = new MockConnection([]);
}
//--------------------------------------------------------------------
public function testSimpleInsert()
{
$builder = $this->db->table('jobs');
$insertData = [
'id' => 1,
'name' => 'Grocery Sales'
];
$builder->insert($insertData, true, true);
$expectedSQL = "INSERT INTO \"jobs\" (\"id\", \"name\") VALUES (:id, :name)";
$expectedBinds = $insertData;
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
$this->assertEquals($expectedBinds, $builder->getBinds());
}
//--------------------------------------------------------------------
public function testThrowsExceptionOnNoValuesSet()
{
$builder = $this->db->table('jobs');
$this->setExpectedException('CodeIgniter\DatabaseException', 'You must use the "set" method to update an entry.');
$builder->insert(null, true, true);
}
//--------------------------------------------------------------------
public function testInsertBatch()
{
$builder = $this->db->table('jobs');
$insertData = array(
['id' => 2, 'name' => 'Commedian', 'description' => 'Theres something in your teeth'],
['id' => 3, 'name' => 'Cab Driver', 'description' => 'Iam yellow'],
);
$this->db->shouldReturn('execute', 1)
->shouldReturn('affectedRows', 1);
$builder->insertBatch($insertData, true, true);
$queries = $this->db->getQueries();
$q1 = $queries[0];
$q2 = $queries[1];
$this->assertTrue($q1 instanceof Query);
$this->assertTrue($q2 instanceof Query);
$raw1 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES (:description,:id,:name)";
$raw2 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES (:description0,:id0,:name0)";
$this->assertEquals($raw1, str_replace("\n", ' ', $q1->getOriginalQuery() ));
$this->assertEquals($raw2, str_replace("\n", ' ', $q2->getOriginalQuery() ));
$expected1 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES ('Theres something in your teeth',2,'Commedian')";
$expected2 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES ('Iam yellow',3,'Cab Driver')";
$this->assertEquals($expected1, str_replace("\n", ' ', $q1->getQuery() ));
$this->assertEquals($expected2, str_replace("\n", ' ', $q2->getQuery() ));
}
//--------------------------------------------------------------------
public function testInsertBatchThrowsExceptionOnNoData()
{
$builder = $this->db->table('jobs');
$this->setExpectedException('CodeIgniter\DatabaseException', 'You must use the "set" method to update an entry.');
$builder->insertBatch();
}
//--------------------------------------------------------------------
public function testInsertBatchThrowsExceptionOnEmptData()
{
$builder = $this->db->table('jobs');
$this->setExpectedException('CodeIgniter\DatabaseException', 'insertBatch() called with no data');
$builder->insertBatch([]);
}
//--------------------------------------------------------------------
}