@@ -290,51 +290,45 @@ And that's all!
290290
291291.. caution ::
292292
293- The ``@group time-sensitive `` annotation is equivalent to ``ClockMock::register(MyTest::class) ``,
294- so if you want to get a time-based function mocked into another class you will need to
295- add it explicitly using ``ClockMock::register(MyClass::class) ``. The ``ClockMock::register `` method
296- creates a mock of the time based functions into the same namespace as your class. So when using
297- ``time() `` you will use the mock instead of the default one::
293+ Time-based function mocking follows the `PHP namespace resolutions rules `_
294+ so "fully qualified function calls" (e.g ``\time() ``) cannot be mocked.
298295
299- namespace App;
296+ The ``@group time-sensitive `` annotation is equivalent to calling
297+ ``ClockMock::register(MyTest::class) ``. If you want to mock a function used in a
298+ different class, do it explicitly using ``ClockMock::register(MyClass::class) ``::
300299
301- class MyClass
300+ // the class that uses the time() function to be mocked
301+ namespace App;
302+
303+ class MyClass
304+ {
305+ public function getTimeInHours()
302306 {
303- public function getTimeInHours()
304- {
305- return time() / 3600;
306- }
307+ return time() / 3600;
307308 }
309+ }
308310
309- .. code-block :: php
310-
311- namespace App\Tests;
311+ // the test that mocks the external time() function explicitly
312+ namespace App\Tests;
312313
313- use App\MyClass;
314- use PHPUnit\Framework\TestCase;
314+ use App\MyClass;
315+ use PHPUnit\Framework\TestCase;
315316
316- /**
317- * @group time-sensitive
318- */
319- class MyTest extends TestCase
317+ /**
318+ * @group time-sensitive
319+ */
320+ class MyTest extends TestCase
321+ {
322+ public function testGetTimeInHours()
320323 {
321- public function testGetTimeInHours()
322- {
323- ClockMock::register(MyClass::class);
324+ ClockMock::register(MyClass::class);
324325
325- $my = new MyClass();
326+ $my = new MyClass();
327+ $result = $my->getTimeInHours();
326328
327- $result = $my->getTimeInHours();
328-
329- $this->assertEquals(time() / 3600, $result);
330- }
329+ $this->assertEquals(time() / 3600, $result);
331330 }
332-
333- .. caution ::
334-
335- Keep in mind that mocking is done by using the namespace resolutions rules
336- (http://php.net/manual/en/language.namespaces.rules.php). So time-based functions need to be used as
337- "Unqualified name", i.e. ``\time() `` cannot be mocked.
331+ }
338332
339333.. tip ::
340334
@@ -385,3 +379,4 @@ the mocked namespaces in the ``phpunit.xml`` file, as done for example in the
385379.. _`@-silencing operator` : https://php.net/manual/en/language.operators.errorcontrol.php
386380.. _`@-silenced` : https://php.net/manual/en/language.operators.errorcontrol.php
387381.. _`Travis CI` : https://travis-ci.org/
382+ .. _`PHP namespace resolutions rules` : https://php.net/manual/en/language.namespaces.rules.php
0 commit comments