/
ExampleUnitTest.php
executable file
·175 lines (144 loc) · 4.74 KB
/
ExampleUnitTest.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace WebXID\PHPUnitSandbox\Tests;
use PHPUnit\Framework\TestCase;
use WebXID\PHPUnitSandbox\UnitSandbox;
UnitSandbox::init(__DIR__ . '/classes/TestClass.php')
->registerAutoloader()
->debugMode(true, false);
class ExampleUnitTest extends TestCase
{
/**
* Test Method mocking up
*/
public function testMockClass()
{
//Make mock up of DB::query()->execute()->$method_name();
UnitSandbox::mockClass('\MyNSpace\DB')
->mockStaticMethod('query', function($self_instance, $args_list) {
// print_r($args_list); //there is full list of parameters passed to method
return $self_instance;
})
->mockMethod('execute', UnitSandbox::SELF_INSTANCE) // Mock up instance method
->mockMethod('return_array', [1,2,3])
->mockMethod('return_int', 1)
->mockMethod('return_float', 2.13)
->mockMethod('return_true', true)
->mockMethod('return_false', false)
->mockMethod('return_null', null)
->mockMethod('return_string', 'value');
//Get result of TestClass::init(); - inside this method class DB is calling
$result_array = UnitSandbox::execute(function () {
return \TestClass::init()
->return_array();
});
$this->assertEquals([1,2,3], $result_array, 'Array does not work');
$result_int = UnitSandbox::execute(function () {
return \TestClass::init()
->return_int();
});
$this->assertTrue(is_int($result_int), 'Integer does not work');
$this->assertEquals(1, $result_int, 'Integer does not work');
$result_float = UnitSandbox::execute(function () {
return \TestClass::init()
->return_float();
});
$this->assertTrue(is_float($result_float), 'Float does not work');
$this->assertEquals(2.13, $result_float, 'Float does not work');
$result_true = UnitSandbox::execute(function () {
return \TestClass::init()
->return_true();
});
$this->assertTrue(is_bool($result_true), 'True does not work');
$this->assertTrue($result_true, 'True does not work');
$result_false = UnitSandbox::execute(function () {
return \TestClass::init()
->return_false();
});
$this->assertTrue(is_bool($result_false), 'False does not work');
$this->assertFalse($result_false, 'False does not work');
$result_null = UnitSandbox::execute(function () {
return [\TestClass::init()
->return_null()];
});
$this->assertTrue(is_array($result_null), 'Null does not work');
$this->assertArrayHasKey(0, $result_null, 'Null does not work');
$this->assertNull($result_null[0], 'Null does not work');
$result_string = UnitSandbox::execute(function () {
return \TestClass::init()
->return_string();
});
$this->assertTrue(is_string($result_string), 'String does not work');
$this->assertEquals('value', $result_string, 'String does not work');
UnitSandbox::cleanMockedData();
}
/**
* Test object property mocking up
*/
public function testdefineProperty()
{
//Make mock up of property \DB::name;
UnitSandbox::mockClass('\DB')
->defineProperty('name', 'value');
$result = UnitSandbox::execute(function () {
return (new \DB())->name;
});
UnitSandbox::cleanMockedData();
//Check result
$this->assertEquals('value', $result);
}
/**
* Test object constant mocking up
*/
public function testdefineConst()
{
//Make mock up of DB::MY_CONST;
UnitSandbox::mockClass('\DB')
->defineConst('MY_CONST', 'value');
$result = UnitSandbox::execute(function () {
return \DB::MY_CONST;
});
UnitSandbox::cleanMockedData();
//Check result
$this->assertEquals('value', $result);
}
/**
* Test static property mocking up
*/
public function testdefineStaticProperty()
{
//Make mock up of DB::$name;
UnitSandbox::mockClass('\DB')
->defineStaticProperty('name', 'value');
$result = UnitSandbox::execute(function () {
return \DB::$name;
});
UnitSandbox::cleanMockedData();
//Check result
$this->assertEquals('value', $result, 'UnitSandbox does not work');
}
/**
* Test Spy Class mocking up
*/
public function testSpyClass()
{
//Rewrite private property for class TestClass;
UnitSandbox::spyClass('\TestClass')
->defineStaticProperty('my_property', 'value');
//Get Concat string "Case: " . "value"
$result_private_property = UnitSandbox::execute(function () {
return \Spy\TestClass::getProperty();
});
//Rewrite private static method for class TestClass;
UnitSandbox::spyClass('\TestClass')
->mockStaticMethod('getString', 'Message: ');
//Get Concat string "Case: " . "value"
$result_private_method = UnitSandbox::execute(function () {
return \Spy\TestClass::getProperty();
});
UnitSandbox::cleanMockedData();
//Check result
$this->assertEquals('Case: Hello world!', \TestClass::getProperty());
$this->assertEquals('Case: value', $result_private_property);
$this->assertEquals('Message: value', $result_private_method);
}
}