@@ -290,51 +290,45 @@ And that's all!
290
290
291
291
.. caution ::
292
292
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.
298
295
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) ``::
300
299
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()
302
306
{
303
- public function getTimeInHours()
304
- {
305
- return time() / 3600;
306
- }
307
+ return time() / 3600;
307
308
}
309
+ }
308
310
309
- .. code-block :: php
310
-
311
- namespace App\Tests;
311
+ // the test that mocks the external time() function explicitly
312
+ namespace App\Tests;
312
313
313
- use App\MyClass;
314
- use PHPUnit\Framework\TestCase;
314
+ use App\MyClass;
315
+ use PHPUnit\Framework\TestCase;
315
316
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()
320
323
{
321
- public function testGetTimeInHours()
322
- {
323
- ClockMock::register(MyClass::class);
324
+ ClockMock::register(MyClass::class);
324
325
325
- $my = new MyClass();
326
+ $my = new MyClass();
327
+ $result = $my->getTimeInHours();
326
328
327
- $result = $my->getTimeInHours();
328
-
329
- $this->assertEquals(time() / 3600, $result);
330
- }
329
+ $this->assertEquals(time() / 3600, $result);
331
330
}
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
+ }
338
332
339
333
.. tip ::
340
334
@@ -385,3 +379,4 @@ the mocked namespaces in the ``phpunit.xml`` file, as done for example in the
385
379
.. _`@-silencing operator` : https://php.net/manual/en/language.operators.errorcontrol.php
386
380
.. _`@-silenced` : https://php.net/manual/en/language.operators.errorcontrol.php
387
381
.. _`Travis CI` : https://travis-ci.org/
382
+ .. _`PHP namespace resolutions rules` : https://php.net/manual/en/language.namespaces.rules.php
0 commit comments