-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAccessLogAPITest.php
More file actions
101 lines (77 loc) · 2.72 KB
/
AccessLogAPITest.php
File metadata and controls
101 lines (77 loc) · 2.72 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
<?php
require_once 'AccessLogAPI.php';
require_once 'MockPDO.php';
require_once 'MockStatement.php';
class AccessLogAPITest extends PHPUnit_Framework_TestCase {
function setUp() {
$this->mockPDOStatement = $this->getMock('MockStatement');
$this->mockPDO = $this->getMock('MockPDO');
$this->accessLogAPI = new AccessLogAPI();
}
function testInsert() {
$object = new AccessLogAPI();
$dbh = new PDO('mysql:host=localhost;dbname=access_log', 'root', '1234');
$object->setPDO($dbh);
$result = $object->insert(null);
$this->assertTrue($result);
}
function testInsertMockSuccess() {
$this->mockPDOStatement->expects($this->exactly(1))
->method('execute')
->will($this->returnValue(true));
$this->mockPDO->expects($this->exactly(1))
->method('prepare')
->will($this->returnValue($this->mockPDOStatement));
$this->accessLogAPI->setPDO($this->mockPDO);
$result = $this->accessLogAPI->insert(null);
$this->assertTrue($result);
}
function testInsertMockFail() {
$this->mockPDOStatement->expects($this->exactly(1))
->method('execute')
->will($this->returnValue(false));
$this->mockPDO->expects($this->exactly(1))
->method('prepare')
->will($this->returnValue($this->mockPDOStatement));
$this->accessLogAPI->setPDO($this->mockPDO);
$result = $this->accessLogAPI->insert(null);
$this->assertFalse($result);
}
function testDelete(){
$this->mockPDOStatement->expects($this->once())
->method('execute');
$this->mockPDOStatement->expects($this->once())
->method('rowCount')
->will($this->returnValue(1));
$this->mockPDO->expects($this->once())
->method('prepare')
->will($this->returnValue($this->mockPDOStatement));
$this->accessLogAPI->setPDO($this->mockPDO);
$result = $this->accessLogAPI->deleteById(1);
$this->assertEquals($result, 1);
}
function testUpdate1Row() {
$expected = 1;
$stubPDOStmt = $this->mockPDOStatement;
$stubPDOStmt->expects($this->once())
->method('execute')
->will($this->returnValue(true));
$stubPDOStmt->expects($this->once())
->method('rowCount')
->will($this->returnValue(1));
$stubPDO = $this->mockPDO;
$stubPDO->expects($this->once())
->method('setAttribute');
$stubPDO->expects($this->once())
->method('prepare')
->will($this->returnValue($stubPDOStmt));
$accessLogAPI = $this->accessLogAPI;
$accessLogAPI->setPDO($stubPDO);
$id = 1;
$keys = array("service_name");
$values = array("AccessLogAPI");
$result = $accessLogAPI->updateById($id, $keys, $values);
$this->assertEquals($expected, $result);
}
}
?>