-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMockFunctionTest.php
More file actions
135 lines (111 loc) · 4.28 KB
/
MockFunctionTest.php
File metadata and controls
135 lines (111 loc) · 4.28 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
require_once './MockFunction.php';
class PHPUnit_Extensions_MockFunctionTest extends PHPUnit_Framework_TestCase
{
public function testFunctionReturnsExpectedValue()
{
$value = 'non date value';
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->expects($this->any())
->will($this->returnValue($value));
$this->assertEquals($value, date());
}
public function testParameterIsPassedToFunction()
{
$format = 'Y-m-d H:i:s';
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->expects($this->once())
->with($this->equalTo($format));
date($format);
}
public function testCallingFunctionTwice()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->expects($this->exactly(2));
date();
date();
}
public function testWhenDisabledReturnsOriginalFunctionality()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->disable();
$this->assertEquals('2013-04-22', date('Y-m-d', 1366620395));
}
public function testWhenSwitchOffAndOnReturnsCorrectValue()
{
$value = 'non date value';
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->expects($this->any())
->will($this->returnValue($value));
$mockFunction->disable();
$mockFunction->enable();
$this->assertEquals($value, date());
}
public function testNoSetupReturnsNullValue()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$this->assertNull(date());
}
public function testOriginalFunctionCall()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$this->assertEquals('2013-04-22', $mockFunction->callOriginal('Y-m-d', 1366620395));
}
public function testGetOriginalCallbackWhenInactive()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->disable();
$this->assertEquals('date', $mockFunction->getOriginalCallback());
}
public function testExpectsReturnsCorrectObject()
{
$expected = 'PHPUnit_Framework_MockObject_Builder_InvocationMocker';
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$this->assertInstanceOf($expected, $mockFunction->expects($this->any()));
}
public function testNormalFunctionalityIsReturnedOnDestruct()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
unset($mockFunction);
$this->assertEquals('2013-04-22', date('Y-m-d', 1366620395));
}
public function testCreatingTwoInstanceForDifferentFunctions()
{
$value1 = 'testval1';
$mockDate = new PHPUnit_Extensions_MockFunction('date', $this);
$mockDate->expects($this->once())
->will($this->returnValue($value1));
$value2 = 'testval2';
$mockTime = new PHPUnit_Extensions_MockFunction('time', $this);
$mockTime->expects($this->once())
->will($this->returnValue($value2));
$this->assertEquals($value1, date());
$this->assertEquals($value2, time());
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Can not create second function mock
*/
public function testCreatingTwoInstancesForTheSameFunction()
{
$mockDate1 = new PHPUnit_Extensions_MockFunction('date', $this);
$mockDate2 = new PHPUnit_Extensions_MockFunction('date', $this);
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Failed to get mock object
*/
public function testGetMockWithUnknownFunction()
{
$mockDate = new PHPUnit_Extensions_MockFunction('date', $this);
PHPUnit_Extensions_MockFunction::getMock('time');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid function name
*/
public function testGettingMockForInvalidFunctioName()
{
$mockFunction = new PHPUnit_Extensions_MockFunction('some1_bad2_function3_name4_that5_doesnt6_exist7_hopefully8_ever9', $this);
}
}