Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
That test uses its own mock date formatter (with wrong arguments), it could just as well use the actual date string that it expects.
Also strange that time doesn't seem to be defined at all, would make more sense to explicityl define that and also explicitly assert the received arguments in the mocked method.
Proposed resolution
Hardcode the expected result, pass ['time' => $timestamp] as second argument to render() so it more closely reflects a real call.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#12 | 3019115.8_12.interdiff.txt | 954 bytes | Waldoswndrwrld |
#12 | 3019115-12.patch | 1.16 KB | Waldoswndrwrld |
#8 | issue_3019115_TwigExtensionTest__testFormatDate.patch | 1.1 KB | tamer.kamel |
#2 | 3019115-TwigExtensionTest.patch | 729 bytes | anantjain60 |
Comments
Comment #2
anantjain60 CreditAttribution: anantjain60 as a volunteer and at Google Summer of Code commentedplz review
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #4
BerdirThe mocking is still wrong, that's now actually how the function works. We need to make sure that the $timestamp is passed to that method.
Comment #5
gnugetHi anantjain60.
Berdir points two problems, we can review them in order:
If you check:
Drupal\Core\Datetime::format()
method signature you will see:That means that the first argument is a
timestamp
and the second thetype
if you check how the mock is being used you will see:The format method is receiving just the type and it should be receiving the timestamp first, so for this example, you need to define that variable (in your patch you don't even define the
$timestamp
variable you just use it.)And the other problem is:
If you see what is being rendered:
The
time
variable is not defined, so you need to defined it as well,If you check the
render
signature:You can pass a
$context
to the render, so you can define the "time" variable that is going to be rendered.What I suggest is:
Define the
$timestamp
variable with a fixed value.Pass that variable to the
render
Pass that variable as well to the
dateFormatter->format()
CallAND finally, edit the mock
To instead to return a fixed string, use the $timestamp and transform it back to the expected format
return date('Y-m-d', $timestamp);
In order to do that you will need to change the
willReturn
to a callback, check how that is done here: https://stackoverflow.com/a/4711782And as a final suggestion, before to upload a patch make sure that at least the tests that you are working on is still passing after your changes.
You can read here how to run the tests locally. and if you use PHPStorm it has a great integration
Remember, understanding the problem is half of the solution, always take your time and make sure to you understand what needs to be done.
Thank you for your work.
Comment #6
tamer.kamel CreditAttribution: tamer.kamel as a volunteer commentedPlease check this patch
Comment #8
tamer.kamel CreditAttribution: tamer.kamel as a volunteer commentedComment #9
gnugetThis looks great!
Thank you.
Comment #10
alexpottLet's not call the mock on the expected side of this assertion. Let's hard code the expectation and it to
$this->assertEquals('1978-11-19', $result);
. Which means we need tochange to
$this->exactly(2)
to$this->exactly(1)
- which also points to this big the correct change as we're currently asserting that the test method is calling its own mock.Comment #11
tamer.kamel CreditAttribution: tamer.kamel as a volunteer commentedComment #12
Waldoswndrwrld CreditAttribution: Waldoswndrwrld at iO commentedRe: #10
Hard coded the expectation & invoke method once.
Comment #14
gnugetLooks good.
Thanks!
Comment #15
alexpottCommitted and pushed a0f43e0abd to 8.8.x and 8c4461de2e to 8.7.x. Thanks!
Fixed coding standards on commit.