Comments

hussainweb created an issue. See original summary.

hussainweb’s picture

Parent issue: » #3078585: Writing tests
yonas.legesse’s picture

I've included assertions for selected field. Haven't made a local test run yet.

hussainweb’s picture

Status: Active » Needs work
  1. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,67 @@
    +    $parent->value->willReturn($username);
    

    Prophesize doesn't mock properties. For this to work, we should change the code to use a method and mock that method here.

  2. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,67 @@
    +        case 'contributions':
    

    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?

hussainweb’s picture

You need to add an @group in the class' docblock. Look at other tests for example.

yonas.legesse’s picture

Testing the code here, I'll make a status change depending on the test queue.

hussainweb’s picture

@yonas.legesse, you need to enable the module before being able to use the service. Look at the other kernel test for an example.

yonas.legesse’s picture

StatusFileSize
new3.11 KB
new784 bytes

Added module installation

yonas.legesse’s picture

StatusFileSize
new3.13 KB
new2.48 KB

Adjusted assertion calls

hussainweb’s picture

@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:

  1. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,80 @@
    +    $field_types = ['contributions', 'country', 'first_name',
    +      'irc_nick', 'languages', 'last_name', 'websites',
    +    ];
    

    We don't need this at all. The assertions below can directly use the string.

  2. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,80 @@
    +    $value->value = "hussainweb";
    

    Mocks can only mock methods, not properties.

  3. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,80 @@
    +    $this->typed_data = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataInterface')
    

    Let's stick to using prophecy everywhere.

  4. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,80 @@
    +      ->expects($this->once())
    

    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.

yonas.legesse’s picture

StatusFileSize
new3.09 KB

Applied suggestions and modified assertions.

yonas.legesse’s picture

Status: Needs work » Needs review
StatusFileSize
new3.44 KB

There'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.

hussainweb’s picture

Status: Needs review » Needs work

Good find with the test failure!

  1. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,72 @@
    +    $container->removeDefinition('test.http_client.middleware');
    

    Please add a comment with the d.o issue URL mentioning that we can remove this once that gets merged.

  2. +++ b/tests/src/Kernel/DOComputedFieldsTest.php
    @@ -0,0 +1,72 @@
    +    $this->assertSame($computedField->getValue(), $userData->field_country);
    

    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'

hussainweb’s picture

Ah, 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.