How to fake DateTime in a phpunit test? - phpunit

ssI write a unittest using Symfony's KernelTestCase and have to test for a functionality, which happens only at a certain tie of the day (extra early or extra late).
So when I let my test run a noon of course nothing happens. How can I fake my system time to pretend that it has a different time and my test case is triggered.
I tried around with Symfony's ClockMock class but it does not work.
https://symfony.com/doc/current/components/phpunit_bridge.html#clock-mocking
This is my test code:
use Symfony\Bridge\PhpUnit\ClockMock;
use \DateTime;
/**
* testUserAchievedEarlyBirdTrophy
* #group time-sensitive
*/
public function testUserAchievedEarlyBirdTrophy()
{
ClockMock::withClockMock(strtotime('2018-11-05 01:00:00'));
echo (new DateTime())->format('Y-m-d H:m:s');
$user8Id = $this->user8->getId();
$progressSaveRequest = new ProgressSaveRequest($user8Id, $this->content_1_1->getId());
$this->progressService->saveProgress($progressSaveRequest);
$this->assertTrue($this->loggerCreator->hasDebugThatContains(
'Early Bird'
));
}
the echo give me today's date: 2019-02-01 16:02:06
I also had the feeling that ClockMock is rather used to skip time for e.g. testing caching instead of sleep().
What am I doing wrong?
The listener configuration is in place in my phpunit.xml
Calling bin/simple-phpunit causes a lot of installation happening.
Can't I use the normal phpunit?
Are there any other options to fake the time of the day?

Following the link you included in your post, the first paragraph says:
The ClockMock class provided by this bridge allows you to mock the
PHP's built-in time functions time(), microtime(), sleep() and
usleep(). Additionally the function date() is mocked so it uses the
mocked time if no timestamp is specified. Other functions with an
optional timestamp parameter that defaults to time() will still use
the system time instead of the mocked time.
(Emphasis added.)
This means that your call of
echo (new DateTime())->format('Y-m-d H:m:s');
is expected to give the system time, not the mocked time.
Change it to
echo date('Y-m-d H:m:s');
in order to match the requirements of ClockMock and get the mocked time.
Note: I've never used ClockMock myself, but just looking at the documentation this should be a first step to see if this resolves your problem.

Related

Doctrine setParameter always produces "?"

I would like to compare a datetime value (from a database) with the current time and check whether the current time is before or after the value in the database. This is done in a Symfony project. I tried to follow the instructions on the Symfony website.
So I wrote the following Doctrine query in a Repository Class which checks for a user lockout time and checks if it still lies in the future:
$user_id = 1; // Just giving $user_id a value for this example
$qb = $this->createQueryBuilder('user')
->andWhere('user.lockout_time > :time')
->setParameter('time', date("Y-m-d H:i:s"))
->andWhere('user.user_id = :user_id')
->setParameter('user_id', $user_id);
$query = $qb->getQuery();
echo $query->getSQL();
die;
When running this, both Where clauses contain "?" in the comparative value (e.g. WHERE user.lockout_time > ?). Obviously I want the actual values to be used in the query.
Initially I thought the date() function might be the issue, but even if I just use the :user_id I get the above error.
If I write ->andWhere('user.user_id = 1') I get the desired result.
If I replace :time with some date in the format 'Y-m-d H:i:s', I get the message "Error: Expected end of string..." (with the ... being the value for the Hour).
So both my setParameter() lines are not passing the values set in them. What am I overlooking?
Edit:
The suggested question here is not a duplicate. That just helps me see the query that is sent off. It was helpful in preparation of this question.
So here my own answer after some (= way too much) time of digging around.
The "?" are escaped values and do not represent what is actually in the query (which is another reason the above link doesn't help). To resolve this I resorted to monitoring the MySQL general log.
Here how to get to it, if someone has the same question. This log shows the actual SQL query.

Mocking datetime.now() midway through a unittest.TestCase in Python 2.7

I'm trying to test a bit of debouncing logic - these are local unittests I run for a Google App Engine webapp, using the 2.7 runtime environment. All my other tests are going well but this one has me stumped!
def testThat_emailDebouncingWorks(self):
# Do something, it triggers an email.
doSomething()
self.assertEqual(emails_sent, 1)
# Do something again, the new email is debounced.
doSomething()
self.assertEqual(emails_sent, 1)
# After an hour, the emails should start working again...
mockWaitingAnHour()
doSomething()
self.assertEqual(emails_sent, 2)
# ... and so should the debouncing.
doSomething()
self.assertEqual(emails_sent, 2)
The file under test logs the time an email was sent using datetime.now(), then reruns datetime.now() on all future attempts and returns early if under an hour has elapsed.
There are two things going wrong:
I think the unittest library only added mock support in 3.X, and I'm not keen on updating my whole app.
Even if I was using 3.X, all the examples I see are about faking a datetime response for your entire test case (using a mock decorator above the test def). Whereas I want to change that behaviour midway through my test, not for the entire case.
Any tips? Thanks in advance!
Okay, I got to the bottom of it and wanted to document the answers for anyone who finds this on Google ;)
1. Enable mocking on AppEngine for Python 2.7
You need to follow the instructions for copying a third party library (in our case, "mock") from the official docs. It's worth noting that on Ubuntu, the suggested command:
pip install -t lib/ mock
Will fail. You'll get an error like this:
DistutilsOptionError: can't combine user with prefix, exec_prefix/home, or install_(plat)base
This is to do with a weird conflict with Ubuntu which seems to have gone unfixed for years, and you'll see a lot of people suggesting a virtualenv workaround. I added the --system flag instead:
pip install --system -t lib/ mock
and it worked fine. Remember to follow the rest of the instructions with appengine_config, and you should be set. "import mock" is a good way to check.
2. Mocking the datetime.now() call
My module under test uses:
from datetime import datetime
In my test module, import some stuff:
from mock import patch, Mock
import my_module #Also known as my_module.py
import datetime
Then the actual test case:
#patch.object(my_module, 'datetime', Mock(wraps=datetime.datetime))
def testThat_myModule_debouncesEmails(self):
fake_time = datetime.datetime.now()
# This is the first time the thing happened. It should send an email.
doSomething()
self.assertEqual(1, emails_sent)
# Five minutes later, the thing happens again. Should be debounced.
fake_time += datetime.timedelta(minutes=5)
my_module.datetime.now.return_value = fake_time
doSomething()
self.assertEqual(1, emails_sent)
# Another 56 minutes pass, the thing happens again. An hour has elapsed, so don't debounce.
fake_time += datetime.timedelta(minutes=56)
my_module.datetime.now.return_value = fake_time
doSomething()
self.assertEqual(2, emails_sent)
# Give it another 15 minutes to check the debouncing kicks back in.
fake_time += datetime.timedelta(minutes=15)
my_module.datetime.now.return_value = fake_time
doSomething()
self.assertEqual(2, emails_sent)
Hope this helps someone!

Extending twilio plugin to work with WordPress REST API

I've worked through the twilio tutorials regarding sending and receiving SMS with WordPress. I integrated them into a test install I have and then merged them into one. (The receive one is pretty short, although it's not a full "receive" more than a blind response).
Then I came across themebound's twilio-core and so I had a look at that and quickly I got a fatal error because they both use the twilio helper library. For testing, I just deactivated the first one and activated the second, which leads me into my first question:
Both of these use the same library, and have both used require_once. Each loaded it into their own plugin folder. The original name of the library is twilio-php-master, one renames it twilio the other twilio-php. Not that it matters at all, since they're in separate locations. The fatal error is as a result of the inner workings having the same function names.
How can I test for the existence of the other plugins helper library and use that in place of the one that I have installed?
What about the order of how WordPress loads the plugins? It's likely if mine loads first... Well, I can't even get that far because it crashes just trying to have both activated.
--- we're now leading into the next question ---
As a result, I'm probably going to go with the twilio-core version because it seems more featured and is available on github, even if the author isn't overly active (there's a pull request that's months old and no discussion about it).
I want to extend the functionality of one of the sending plugins (either one) with respect to the receipt of the message from twilio. At the moment, both examples use classes for the sending and the receive that I'm using is not. As such I have just added it to the end of the main plugin file (the one with the plugin metadata). (a quick note, this is not the cause of the above fatal error, this happened before I started merging the send and receive).
Because the receive is involved with the REST API and not initiated by a user action on the system (ie someone in the admin area accessing the class through the admin panel), I'm not sure if it's appropriate that a) I put it there, and b) use the send function inside the class when further processing the receipt. I have an end goal of analysing the incoming message and forwarding it back out through twilio or other application or even just recording it in wordpress itself.
Should I keep the receive functionality/plugin separate from the sending one?
And this leads on to the hardest question for me:
How would I extend either plugin to make the send function available to my receive plugin? (this is where part of my confusion comes from) -->> Because both plugins only operate in the admin area, and the REST API isn't an actual user operating in the front-end, how can I call those functions in the admin area? Will is "just be available"? Do I have to replicate them on the public side? and then if so, is it necessary to have it in the admin area as well?
edit: With respect to one of the comments below, I have tested and twl_send_sms is available once the helper is loaded. What I will do is determine a way to see if the helper is loaded (a function exists test will probably suffice) and if so, require or not require my version as appropriate.
From the receive message I am now able to craft a separate forward of a new message. But how can I pass the callback function the parameters of the initial inbound message? eg, how do I populate $sms_in with the POST data?
function register_receive_message_route() {
register_rest_route( 'sms/v1', '/receiver_sms', array(
'methods' => 'POST',
'callback' => 'trigger_receive_sms',
) );
}
function trigger_receive_sms($sms_in = '') {
/* we have three things to do:
* 1: send the reply to twilio,
* 2: craft a reply,
* 3: save the data message to the database
*/
echo header('content-type: text/xml');
echo ('<?xml version="1.0" encoding="UTF-8"?>');
echo ('<Response>');
echo (' <Message>Thanks, someone will be in contact shortly.</Message>');
echo ('</Response>');
$args = array(
'number_to' => '+xxxxxxxxxxx',
'message' => "Test Worked!\n$sms_in",
);
twl_send_sms( $args );
Twilio developer evangelist here.
It sounds to me as though your send functionality (whatever you can get out of twilio-core) is separate to your receive functionality. So I would likely split those up as two plugins and follow the instructions from the post you mentioned on how to write something that receives and responds to SMS messages. That way, twilio-core can use the Twilio library it bundles and your receive plugin won't need to use a library (it just needs to respond with TwiML, which is just XML).
I'm not sure I understand the last part of question 4 though, you can't really interact yourself with the receive endpoint of the plugin because all it will do is return XML to you. That's for Twilio to interact with.
Edit
In answer to your updated question, when your callback is triggered by a POST request it is passed a WP_REST_Request object. This object contains the POST body and the parameters can be accessed by array access.
function trigger_receive_sms($request) {
echo $request['Body'];
}
In good news, if you just plan to send two messages then you can do so entirely with TwiML. You just need to use two <Message>s:
echo ('<?xml version="1.0" encoding="UTF-8"?>');
echo ('<Response>');
echo (' <Message>Thanks, someone will be in contact shortly.</Message>');
echo (' <Message to="YOUR_OTHER_NUMBER">It worked!</Message>');
echo ('</Response>');
This way you don't need to worry about that other library.

Unit testing with Symfony's flashBag sessions

I'm creating an application in Silex with unit tests.
Running unit tests works fine against the regular session handler:
$app->register(new Silex\Provider\SessionServiceProvider(), array(
'session.storage.options' => array(
'cookie_lifetime' => 1209600, // 2 weeks
),
));
and setting this flag in my unit tests:
$this->app['session.test'] = true;
If I don't set that session.test flag, my unit tests throw a headers already sent error and all fail. With it on, my tests run well.
The issue is I am attempting to use the flashBag feature (session info that lasts only until first request then get removed):
$foo = $app['session']->getFlashBag()->all();
The flashBag does not seem to respect the session.test flag, and attempts to send headers, which cause all my unit tests to fail:
24)
Yumilicious\UnitTests\Validator\PersonAccountTest::setConstraintsPassesWithMinimumAttributes
RuntimeException: Failed to start the session because headers have
already been sent.
/webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php:142
/webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php:262
/webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Session.php:240
/webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Session.php:250
/webroot/yumilicious/src/app.php:38
/webroot/yumilicious/tests/Yumilicious/UnitTests/Base.php:13
/webroot/yumilicious/vendor/silex/silex/src/Silex/WebTestCase.php:34
/webroot/yumilicious/vendor/EHER/PHPUnit/src/phpunit/phpunit.php:46
/webroot/yumilicious/vendor/EHER/PHPUnit/bin/phpunit:5
I've narrowed it down to this bit of code: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php#L259
Specifically, line 262. Commenting out that single line allows my tests to work properly and all pass green.
I've searched quite a bit to get this to work, but am not having any luck. I think it's because the flashBag stuff is new (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Session.php#L305) and the old methods are being deprecated.
Any suggestions on getting my unit tests to work would be awesome.
For testing you need to replace the session.storage service with an instance of MockArraySessionStorage:
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
$app['session.storage'] = new MockArraySessionStorage();
This is because the native one tries to send a cookie via header which of course fails in a test environment.
EDIT: There is now a session.test parameter that you should set to true. That will automatically make the session use a mock storage.
I had this happen too, if i am not mistaking i fixed by having my unittests run via a different environment, wich has
framework:
test: ~
session:
storage_id: session.storage.mock_file
set in the config_test.yml
I came across similar problem today and temp fix would be to comment out block of code in
\Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage
in start() method
/*
if (ini_get('session.use_cookies') && headers_sent()) {
throw new \RuntimeException('Failed to start the session because headers have already been sent.');
}
*/
This solution keeps tests "green" and from looks of it the application session functionality as is.

Create a timed cache in Drupal

I am looking for more detailed information on how I can get the following caching behavior in Drupal 7.
I want a block that renders information I'm retrieving from an external service. As the block is rendered for many users I do not want to continually request data from that service, but instead cache the result. However, this data is relatively frequent to change, so I'd like to retrieve the latest data every 5 or 10 minutes, then cache it again.
Does anyone know how to achieve such caching behavior without writing too much of the code oneself? I also haven't found much in terms of good documentation on how to use caching in Drupal (7), so any pointers on that are appreciated as well.
Keep in mind that cache_get() does not actually check if an item is expired or not. So you need to use:
if (($cache = cache_get('your_cache_key')) && $cache->expire >= REQUEST_TIME) {
return $cache->data;
}
Also make sure to use the REQUEST_TIME constant rather than time() in D7.
The functions cache_set() and cache_get() are what you are looking for. cache_set() has an expire argument.
You can use them basically like this:
<?php
if ($cached_data = cache_get('your_cache_key')) {
// Return from cache.
return $cached_data->data;
}
// No or outdated cache entry, refresh data.
$data = _your_module_get_data_from_external_service();
// Save data in cache with 5min expiration time.
cache_set('your_cache_key', $data, 'cache', time() + 60 * 5);
return $data;
?>
Note: You can also use a different cache bin (see documentation links) but you need to create a corresponding cache table yourself as part of your schema.
I think this should be $cache->expire, not expires. I didn't have luck with this example if I'm setting REQUEST_TIME + 300 in cache_set() since $cache->expires will always be less than REQUEST_TIME. This works for me:
if (($cache = cache_get('your_cache_key', 'cache')) && (REQUEST_TIME < $cache->expire)) {
return $cache->data;
}

Resources