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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
<?php // phpcs:ignore PSR1.Files.SideEffects.FoundWithSymbols
namespace phpmock;
use PHPUnit\Framework\TestCase;
// When class is used in related repositories we need to add autoloader for PHPUnit 8 compatibility
if (!trait_exists(TestCaseTrait::class)) {
require __DIR__ . '/../tests/autoload.php';
}
/**
* Common tests for mocks.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @see Mock
*/
abstract class AbstractMockTestCase extends TestCase
{
use TestCaseTrait;
/**
* Disable all mocks.
*/
abstract protected function disableMocks();
/**
* Builds an enabled function mock.
*
* @param string $namespace The namespace.
* @param string $functionName The function name.
* @param callable $function The function mock.
*/
abstract protected function mockFunction($namespace, $functionName, callable $function);
/**
* Defines the function mock.
*
* @param string $namespace The namespace.
* @param string $functionName The function name.
*/
abstract protected function defineFunction($namespace, $functionName);
protected function tearDownCompat()
{
parent::tearDown();
$this->disableMocks();
}
/**
* Tests mocking a function without parameters.
*/
public function testMockFunctionWithoutParameters()
{
$this->mockFunction(__NAMESPACE__, "getmyuid", function () {
return 1234;
});
$this->assertEquals(1234, getmyuid());
}
/**
* Tests mocking a previously mocked function again.
* @depends testMockFunctionWithoutParameters
*/
#[\PHPUnit\Framework\Attributes\Depends('testMockFunctionWithoutParameters')]
public function testRedefine()
{
$this->mockFunction(__NAMESPACE__, "getmyuid", function () {
return 5;
});
$this->assertEquals(5, getmyuid());
}
/**
* Tests mocking a function without parameters.
*/
public function testMockFunctionWithParameters()
{
$this->mockFunction(__NAMESPACE__, "rand", function ($min, $max) {
return $max;
});
$this->assertEquals(1234, rand(1, 1234));
}
/**
* Tests mocking of an undefined function.
*/
public function testUndefinedFunction()
{
$this->assertFalse(function_exists("testUndefinedFunction"));
$this->mockFunction(__NAMESPACE__, "testUndefinedFunction", function ($arg) {
return $arg + 1;
});
$result = testUndefinedFunction(1);
$this->assertEquals(2, $result);
}
/**
* Tests failing enabling an already enabled mock.
*/
public function testFailEnable()
{
$name = "testFailEnable";
$this->mockFunction(__NAMESPACE__, $name, "sqrt");
$this->expectException(MockEnabledException::class);
$this->mockFunction(__NAMESPACE__, $name, "sqrt");
}
/**
* Tests passing by value.
*/
public function testPassingByValue()
{
$this->mockFunction(__NAMESPACE__, "testPassingByValue", function ($a) {
return $a + 1;
});
// Tests passing directly the value.
$this->assertEquals(3, testPassingByValue(2));
}
/**
* Test passing by reference.
*/
public function testPassingByReference()
{
$this->mockFunction(__NAMESPACE__, "exec", function ($a, &$b, &$c) {
$a = "notExpected";
$b[] = "test1";
$b[] = "test2";
$c = "test";
});
$noReference = "expected";
$b = [];
$c = "";
exec($noReference, $b, $c);
$this->assertEquals(["test1", "test2"], $b);
$this->assertEquals("test", $c);
$this->assertEquals("test", $c);
$this->assertEquals("expected", $noReference);
}
/**
* Tests that the mock preserves the default argument
*/
public function testPreserveArgumentDefaultValue()
{
$functionName = $this->buildPrivateFunctionName("testPreserveArgumentDefaultValue");
eval("
function $functionName(\$b = \"default\") {
return \$b;
}
");
$this->mockFunction(
__NAMESPACE__,
$functionName,
function ($arg = "expected") {
return $arg;
}
);
$fqfn = __NAMESPACE__ . "\\$functionName";
$result = $fqfn();
$this->assertEquals("expected", $result);
}
/**
* Tests that the disabled mock uses the default argument of the original function.
* @depends testPreserveArgumentDefaultValue
*/
#[\PHPUnit\Framework\Attributes\Depends('testPreserveArgumentDefaultValue')]
public function testResetToDefaultArgumentOfOriginalFunction()
{
$functionName = $this->buildPrivateFunctionName("testPreserveArgumentDefaultValue");
$result = $functionName();
$this->assertEquals("default", $result);
}
/**
* Tests some methods which use the varname "...".
*/
public function testCVariadic()
{
$this->mockFunction(__NAMESPACE__, "min", "max");
$this->assertEquals(2, min(2, 1));
$this->assertEquals(2, min([2, 1]));
}
/**
* Tests some methods which use the varname "..." after a mock was defined.
* @depends testCVariadic
*/
#[\PHPUnit\Framework\Attributes\Depends('testCVariadic')]
public function testCVariadicReset()
{
$this->assertEquals(1, min(2, 1));
$this->assertEquals(1, min([2, 1]));
}
/**
* Setup a mock for testDisable().
*/
public function testDisableSetup()
{
$this->mockFunction(__NAMESPACE__, "rand", function () {
return 1234;
});
$this->mockFunction(__NAMESPACE__, "mt_rand", function () {
return 1234;
});
$this->assertEquals(1234, rand());
$this->assertEquals(1234, mt_rand());
}
/**
* Tests disable().
* @depends testDisableSetup
*/
#[\PHPUnit\Framework\Attributes\Depends('testDisableSetup')]
public function testDisable()
{
$this->assertNotEquals(1234, rand());
$this->assertNotEquals(1234, mt_rand());
}
/**
* Tests mocking the function implicitely defines the function.
*/
public function testImplicitDefine()
{
$functionName = $this->buildPrivateFunctionName("testDefine");
$fqfn = __NAMESPACE__ . "\\$functionName";
$this->assertFalse(function_exists($fqfn));
$this->mockFunction(__NAMESPACE__, $functionName, "sqrt");
$this->assertTrue(function_exists($fqfn));
}
/**
* Tests explicit function defining.
*/
public function testExplicitDefine()
{
$this->defineFunction(__NAMESPACE__, "escapeshellcmd");
$this->escapeshellcmd("foo");
$this->mockFunction(__NAMESPACE__, "escapeshellcmd", function () {
return "bar";
});
$this->assertEquals("bar", self::escapeshellcmd("foo"));
}
/**
* Returns the built-in call to escapeshellcmd().
*
* @param string $command Shell command.
*
* @return string The built-in call.
*/
private function escapeshellcmd($command)
{
return escapeshellcmd($command);
}
/**
* Builds a function name which is has a postfix for the current class.
*
* @param string $name The function name.
*
* @return string The function name.
*/
private function buildPrivateFunctionName($name)
{
return $name . str_replace("\\", "_", get_class($this));
}
/**
* Tests declaring repeatedly a mock with enabled backupStaticAttributes.
* @backupStaticAttributes
* @dataProvider provideTestBackupStaticAttributes
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideTestBackupStaticAttributes')]
#[\PHPUnit\Framework\Attributes\BackupStaticProperties(true)]
public function testBackupStaticAttributes()
{
$this->mockFunction(__NAMESPACE__, "testBackupStaticAttributes", "sqrt");
$this->assertEquals(2, testBackupStaticAttributes(4));
}
/**
* Just repeat testBackupStaticAttributes a few times.
*
* @return array Test cases.
*/
public static function provideTestBackupStaticAttributes()
{
return [
[], [], [], [], [], [], [], [], [], [], [], []
];
}
/**
* Tests the mock in a separate process.
* @runInSeparateProcess
*/
#[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
public function testRunInSeparateProcess()
{
$this->mockFunction(__NAMESPACE__, "time", function () {
return 123;
});
$this->assertEquals(123, time());
}
}
|