/
Category_model_mocking_db_test.php
57 lines (52 loc) · 1.23 KB
/
Category_model_mocking_db_test.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
<?php
class Category_model_mocking_db_test extends UnitTestCase
{
public function setUp()
{
$this->obj = $this->newModel('Category_model');
}
public function test_get_category_list()
{
// Create mock objects for CI_DB_pdo_result and CI_DB_pdo_sqlite_driver
$return = [
0 => (object) ['id' => '1', 'name' => 'Book'],
1 => (object) ['id' => '2', 'name' => 'CD'],
2 => (object) ['id' => '3', 'name' => 'DVD'],
];
$db_result = $this->getMockBuilder('CI_DB_pdo_result')
->disableOriginalConstructor()
->getMock();
$db_result->method('result')->willReturn($return);
$db = $this->getMockBuilder('CI_DB_pdo_sqlite_driver')
->disableOriginalConstructor()
->getMock();
$db->method('get')->willReturn($db_result);
// Verify invocations
$this->verifyInvokedOnce(
$db_result,
'result',
[]
);
$this->verifyInvokedOnce(
$db,
'order_by',
['id']
);
$this->verifyInvokedOnce(
$db,
'get',
['category']
);
// Replace property db with mock object
$this->obj->db = $db;
$expected = [
1 => 'Book',
2 => 'CD',
3 => 'DVD',
];
$list = $this->obj->get_category_list();
foreach ($list as $category) {
$this->assertEquals($expected[$category->id], $category->name);
}
}
}