Problem/Motivation

While working on #2864816: HAL LinkManager doesn't add 'url.site' cache context when needed, I had to add test coverage. In doing so, I had to modernize & harden the existing test coverage. Rather than doing it there, doing it in a separate issue should make it easier to make progress.

Proposed resolution

Do it.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

Wim Leers created an issue. See original summary.

wim leers’s picture

wim leers’s picture

Status: Needs review » Needs work

The last submitted patch, 2: 2915490-2.patch, failed testing. View results

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new1.09 KB
new6.26 KB
+++ b/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php
@@ -46,36 +46,133 @@ protected function setUp() {
+      'configured URL, with optional context to collect cacheability metadata' => [
+        'link_domain' => 'http://llamas-rock.com/for-real/',
+        'entity_type' => 'node',
+        'bundle' => 'page',
+        'context' => $serialization_context_collecting_cacheability,
+        'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
+        'expected context' => [
+          'cacheability' => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
+        ],
+      ],

Oops, this hunk is only for #2864816: HAL LinkManager doesn't add 'url.site' cache context when needed. Removing that.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

This looks great for me! We could try to share code between the two providers, but I actually believe that DRY for tests is totally overstated.

xjm’s picture

+++ b/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php
@@ -46,36 +46,123 @@ protected function setUp() {
+        'link_domain' => 'http://llamas-rock.com/for-real/',

I was disappointed to find this site does not actually exist. Quick, grab the domain! :P

wim leers’s picture

🤣

catch’s picture

+++ b/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php
@@ -46,36 +46,123 @@ protected function setUp() {
+  public function providerTestGetTypeUri() {

Don't these need phpdoc? If they don't is that in coding standards somewhere?

dawehner’s picture

There are glorious examples like:

 /**
   * {@inheritdoc}
   */
  public function providerSource() {

over

  /**
   * Data provider for testPurge().
   *
   * @see testPurge()
   */

to

  /**
   * Provides test data for testApplies().
   *
   * @return array
   *   Array of datasets for testApplies(). Structured as such:
   *   - ForumListBreadcrumbBuilder::applies() expected result.
   *   - ForumListBreadcrumbBuilder::applies() $attributes input array.
   */

I always assumed the only standard was just some kind of naming scheme.

wim leers’s picture

I always assumed the only standard was just some kind of naming scheme.

This.

Honestly, PHPUnit's data provider mechanism already imposes/requires sufficient structure, that adding additional comments for them just is a waste of time.

alexpott’s picture

I agree with @Wim Leers and @dawehner - the most important thing in the data provider is the text in the array keys - makes it so much easier to work out what's going on.

I do think that

+++ b/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php
@@ -46,36 +46,123 @@ protected function setUp() {
   /**
    * @covers ::getRelationUri
+   * @dataProvider providerTestGetRelationUri
    */
-  public function testGetRelationUri() {
+  public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) {

We're going to have a problem when we turn on the rule for @param documentation. I think it might be worth considering coding standards for tests. In some ways it is tempting to say it should be just the same as for regular code. So we don't have to maintain multiple rulesets. I dunno.

I've pinged @catch for more opinion.

wim leers’s picture

It'd be great if we could exempt PHPUnit data providers from that rule. I'm not sure if that's possible though. The exemption would look like this:

Any class that extends \PHPUnit\Framework\TestCase should no docblock for any method whose name has the prefix provider.

That'd remove a nice chunk of annoying boilerplate!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: 2915490-5.patch, failed testing. View results

alexpott’s picture

@Wim Leers - good idea I'll propose a coding standards change in their issue queue.

alexpott’s picture

Status: Needs work » Reviewed & tested by the community

Created #2918440: Define a standard for documenting data providers in PHPUnit-based tests.

Discussed with @catch and he said:

the data provider I just didn’t want us getting tripped up by coding standards checks, that was it.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed ff6381f7cf to 8.5.x and 94f54936f6 to 8.4.x. Thanks!

Backported to 8.4,x because it is a test only change.

  • alexpott committed ff6381f on 8.5.x
    Issue #2915490 by Wim Leers: Modernize & harden HalLinkManagerTest
    

  • alexpott committed 94f5493 on 8.4.x
    Issue #2915490 by Wim Leers: Modernize...
wim leers’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.