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
|
<?php
namespace foo;
use phpmock\Mock;
use phpmock\MockBuilder;
use phpmock\MockRegistry;
use phpmock\functions\FixedValueFunction;
use phpmock\environment\SleepEnvironmentBuilder;
use phpmock\TestCaseTrait;
use PHPUnit\Framework\TestCase;
/**
* Tests the example from the documentation.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
*/
class ExampleTest extends TestCase
{
use TestCaseTrait;
protected function tearDownCompat()
{
MockRegistry::getInstance()->unregisterAll();
}
/**
* Tests the example from the documentation.
*/
public function testExample1()
{
$builder = new MockBuilder();
$builder->setNamespace(__NAMESPACE__)
->setName("time")
->setFunction(
function () {
return 1234;
}
);
$mock = $builder->build();
$mock->enable();
assert(time() == 1234);
$this->assertEquals(1234, time());
}
/**
* Tests the example from the documentation.
*/
public function testExample2()
{
$builder = new MockBuilder();
$builder->setNamespace(__NAMESPACE__)
->setName("time")
->setFunctionProvider(new FixedValueFunction(12345));
$mock = $builder->build();
$mock->enable();
assert(time() == 12345);
$this->assertEquals(12345, time());
}
/**
* Tests the example from the documentation.
*/
public function testExample3()
{
$builder = new SleepEnvironmentBuilder();
$builder->addNamespace(__NAMESPACE__)
->setTimestamp(12345);
$environment = $builder->build();
$environment->enable();
sleep(10);
assert(12345 + 10 == time());
$this->assertEquals(12345 + 10, time());
}
/**
* Tests the example from the documentation.
*/
public function testExample4()
{
$function = function () {
throw new \Exception();
};
$mock = new Mock(__NAMESPACE__, "time", $function);
$mock->enable();
try {
$this->expectException(\Exception::class);
time();
} finally {
$mock->disable();
}
}
/**
* Tests the example from the documentation.
*/
public function testExample5()
{
$time = new Mock(
__NAMESPACE__,
"time",
function () {
return 3;
}
);
$time->enable();
$this->assertSame(3, time());
}
}
|