From 4df9d4ca15ee2e8fc0edef045253cd03635a78ad Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Mon, 18 Dec 2017 10:09:17 +0800 Subject: [PATCH 1/6] Add details on how the ClockMock::register works Hi, I got some trouble mocking using the ClockMock feature. After dig into the code, I figure myself how it works ! So I decided to do a PR, but I'm not sure of the formatting, --- components/phpunit_bridge.rst | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index 4c19a2580c0..6b6c9152963 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -231,6 +231,59 @@ test:: } And that's all! + +.. caution:: + + The ``@group time-sensitive`` annotation is equivalent to ``ClockMock::register(MyTest::class)``, + so if you want to mock a time based function mocked into one of the source code you will need to + add it explicitly using ``ClockMock::register(MyClass::class)``. The ``ClockMock::register`` method + only create a mock of the time based functions into the same namespace as your class. So when using + ``time()`` you will use the mock instead of the default one. + + +.. code-block:: + + namespace App; + + class MyClass + { + public function getTimeInHours() + { + return time() / 3600; + } + } + +.. code-block:: + + namespace Tests/App; + + use PHPUnit\Framework\TestCase; + use App\MyClass; + + /** + * @group time-sensitive + */ + class MyTest extends TestCase + { + public function testGetTimeInHours() + { + ClockMock::register(MyClass::class); + + $my = new MyClass(); + + $result = $my->getTimeInHours(); + + $this->assertEquals(time() / 3600, $result); + } + } + + class MyClass + { + public function getTimeInHours() + { + return time() / 3600; + } + } .. tip:: From 542b04767e4748eaf3b262664b1b40b5a0f6c6a0 Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Wed, 24 Jan 2018 10:52:41 +0800 Subject: [PATCH 2/6] Remove duplicated code-block --- components/phpunit_bridge.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index 6b6c9152963..7edfb5e9aaf 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -276,14 +276,6 @@ And that's all! $this->assertEquals(time() / 3600, $result); } } - - class MyClass - { - public function getTimeInHours() - { - return time() / 3600; - } - } .. tip:: From c60d63bb21e4e91f473546e59894edc4f61b7194 Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Thu, 28 Jun 2018 17:21:56 +0800 Subject: [PATCH 3/6] Integrate HeahDude feedbacks --- components/phpunit_bridge.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index 7edfb5e9aaf..8a8689d9920 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -231,20 +231,20 @@ test:: } And that's all! - + .. caution:: The ``@group time-sensitive`` annotation is equivalent to ``ClockMock::register(MyTest::class)``, - so if you want to mock a time based function mocked into one of the source code you will need to + so if you want to get a time based function mocked into one of the source code you will need to add it explicitly using ``ClockMock::register(MyClass::class)``. The ``ClockMock::register`` method - only create a mock of the time based functions into the same namespace as your class. So when using - ``time()`` you will use the mock instead of the default one. + only create a mock of the time based functions into the same namespace as your class. So when using + ``time()`` you will use the mock instead of the default one. .. code-block:: namespace App; - + class MyClass { public function getTimeInHours() @@ -255,10 +255,10 @@ And that's all! .. code-block:: - namespace Tests/App; - - use PHPUnit\Framework\TestCase; + namespace App\Tests; + use App\MyClass; + use PHPUnit\Framework\TestCase; /** * @group time-sensitive @@ -268,7 +268,7 @@ And that's all! public function testGetTimeInHours() { ClockMock::register(MyClass::class); - + $my = new MyClass(); $result = $my->getTimeInHours(); From a93b0bec442db8997d4f5e6bfd86a5266c7bee6d Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Thu, 28 Jun 2018 17:37:23 +0800 Subject: [PATCH 4/6] Fix travis build --- components/phpunit_bridge.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index 8a8689d9920..a4dbe32830a 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -241,7 +241,7 @@ And that's all! ``time()`` you will use the mock instead of the default one. -.. code-block:: +.. code-block:: php namespace App; @@ -253,7 +253,7 @@ And that's all! } } -.. code-block:: +.. code-block:: php namespace App\Tests; From 372867b1c166326123923a1a3a0ad31840a2e25b Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Fri, 29 Jun 2018 08:32:18 +0800 Subject: [PATCH 5/6] Fix indentation --- components/phpunit_bridge.rst | 47 ++++++++++++++++------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index a4dbe32830a..cb447e7deed 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -238,44 +238,41 @@ And that's all! so if you want to get a time based function mocked into one of the source code you will need to add it explicitly using ``ClockMock::register(MyClass::class)``. The ``ClockMock::register`` method only create a mock of the time based functions into the same namespace as your class. So when using - ``time()`` you will use the mock instead of the default one. + ``time()`` you will use the mock instead of the default one:: + namespace App; -.. code-block:: php - - namespace App; - - class MyClass - { - public function getTimeInHours() + class MyClass { - return time() / 3600; + public function getTimeInHours() + { + return time() / 3600; + } } - } -.. code-block:: php + .. code-block:: php - namespace App\Tests; + namespace App\Tests; - use App\MyClass; - use PHPUnit\Framework\TestCase; + use App\MyClass; + use PHPUnit\Framework\TestCase; - /** - * @group time-sensitive - */ - class MyTest extends TestCase - { - public function testGetTimeInHours() + /** + * @group time-sensitive + */ + class MyTest extends TestCase { - ClockMock::register(MyClass::class); + public function testGetTimeInHours() + { + ClockMock::register(MyClass::class); - $my = new MyClass(); + $my = new MyClass(); - $result = $my->getTimeInHours(); + $result = $my->getTimeInHours(); - $this->assertEquals(time() / 3600, $result); + $this->assertEquals(time() / 3600, $result); + } } - } .. tip:: From 9ce6bce6cad4ce278b3a876d1f4e0d4af452c9f5 Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Mon, 2 Jul 2018 10:44:16 +0800 Subject: [PATCH 6/6] integrate nicolas grekas feedbacks --- components/phpunit_bridge.rst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index cb447e7deed..af770c53103 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -235,9 +235,9 @@ And that's all! .. caution:: The ``@group time-sensitive`` annotation is equivalent to ``ClockMock::register(MyTest::class)``, - so if you want to get a time based function mocked into one of the source code you will need to + so if you want to get a time-based function mocked into another class you will need to add it explicitly using ``ClockMock::register(MyClass::class)``. The ``ClockMock::register`` method - only create a mock of the time based functions into the same namespace as your class. So when using + creates a mock of the time based functions into the same namespace as your class. So when using ``time()`` you will use the mock instead of the default one:: namespace App; @@ -274,6 +274,12 @@ And that's all! } } +.. caution:: + + Keep in mind that mocking is done by using the namespace resolutions rules + (http://php.net/manual/en/language.namespaces.rules.php). So time-based functions need to be used as + "Unqualified name", i.e. ``\time()`` cannot be mocked. + .. tip:: An added bonus of using the ``ClockMock`` class is that time passes