We added computed fields in #3077599: Create computed field properties for user information. Let's add some tests for this. I am thinking kernel tests would be a good addition.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | add_tests_for_computed_fields-3143091-12.patch | 3.44 KB | yonas.legesse |
Issue fork do_username-3143091
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
hussainwebComment #3
yonas.legesse commentedI've included assertions for selected field. Haven't made a local test run yet.
Comment #4
hussainwebProphesize doesn't mock properties. For this to work, we should change the code to use a method and mock that method here.
I think all of the cases are dropping through here. In fact, why do we need a loop and switch/case here. Why not just assert one thing after other?
Comment #5
hussainwebYou need to add an
@groupin the class' docblock. Look at other tests for example.Comment #6
yonas.legesse commentedTesting the code here, I'll make a status change depending on the test queue.
Comment #7
hussainweb@yonas.legesse, you need to enable the module before being able to use the service. Look at the other kernel test for an example.
Comment #8
yonas.legesse commentedAdded module installation
Comment #9
yonas.legesse commentedAdjusted assertion calls
Comment #10
hussainweb@yonas.legesse, please see #4 again. You haven't changed the code in DOComputedFields.php to call `getValue` rather than reading the `value` property. It's on line 30 and the reason for this failure. More comments below:
We don't need this at all. The assertions below can directly use the string.
Mocks can only mock methods, not properties.
Let's stick to using prophecy everywhere.
If we remove this, can we reuse the mock for all the assertions? I am not sure I like a helper method just to create this mock.
Comment #11
yonas.legesse commentedApplied suggestions and modified assertions.
Comment #12
yonas.legesse commentedThere's an issue in regards to making outbound http request during Kernel tests for which there's a patch in #2571475(which is the reason for the error during test in #11). I have included a simple function as a solution in the current patch.
Comment #13
hussainwebGood find with the test failure!
Please add a comment with the d.o issue URL mentioning that we can remove this once that gets merged.
Somethings not right here. All your $computedField are based on a single $typed_data which returns exactly the same value, yet it works when you compare it to different values? In this line, shouldn't
$computedField->getValue()return'hussainweb'Comment #14
hussainwebAh, ignore my last comment's 2nd point. I didn't go through the code properly. I see how this works now.
Still, I am thinking if we should mock the drupal user information service. That is not what we are testing and relying on an API makes our test fragile. That could be more relevant for a functional test later. Let's keep this kernel test tiny, fast, and reliable.
Once we remove the HTTP request, you can then ignore my previous comment's 1st point as well as you would be able to service alter for the alter method. You might still need that method to mock the drupal user information service.