This is a pretty handy test for catching coding errors before we commit them but it takes 2-3 seconds to run on my quick little SSD. The attached patch achieves the same test in ~209ms. It does loose the line number of the failure but its easy enough to search a file for the Drupal\Core.

I'm generally in the camp of not focusing on optimizing tests but this is a really slow unit test and this really achieves the same thing.

Beta things, this is testing and changes no api's or add features or any of that so its clear.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because things work, this issue only speeds up execution.
Issue priority Normal because nothing is really broken, this is an optimization.
Unfrozen changes Unfrozen because it improves automated tests.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

neclimdul’s picture

Status: Active » Needs review

to the testbot

franksj’s picture

Status: Needs review » Reviewed & tested by the community

Before patch:

..

Time: 17.86 seconds, Memory: 86.00Mb

After patch:

..

Time: 8.2 seconds, Memory: 85.50Mb

Vast improvement in speed.

Added use Drupal\Core\Access; to core/lib/Drupal/Component/Annotation/Plugin.php and the test fails as expected.
Looks good!

Mile23’s picture

Issue summary: View changes

Added beta evaluation.

Mile23’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

@neclimdul thank you - this has been bothering for a while. I think we should add a quick test for assertNoCoreUsage to this class. It should be simple enough by catching the php unit fail exception. See https://stackoverflow.com/questions/3917909/how-to-indicate-that-a-phpun...

Mile23’s picture

Status: Needs work » Needs review
FileSize
3.11 KB
2.24 KB

Added a test using vfsStream.

The test is on the same test class as the method being tested, mostly because assertNoCoreUsage() is protected and I'm trying to catch a plane. :-) Easy enough to move to another class and mock up if it's too weird.

neclimdul’s picture

Status: Needs review » Reviewed & tested by the community

works for me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -66,13 +67,73 @@ protected function findPhpClasses($dir) {
+    if ($expected_pass) {
+      $this->assertNoCoreUsage($file_uri);
+      return;
+    }
+    // We don't expect assertNoCoreUseage() to pass, so we set an expectation.
+    // We can't expect a \PHPUnit_Framework_AssertionFailedError, because
+    // PHPUnit will handle that before we get it.
+    $this->setExpectedException('\DomainException');
+    try {
+      $this->assertNoCoreUsage($file_uri);
+    }
+    catch (\PHPUnit_Framework_AssertionFailedError $e) {
+      throw new \DomainException($e->getMessage());
+    }

I think this can be simplified to something like:

    try {
      $this->assertNoCoreUsage($file_uri);
      $pass = TRUE; 
    }
    catch (\PHPUnit_Framework_AssertionFailedError $e) {
      $pass = FALSE;
    }
    $this->assertEquals($expected_pass, $pass);
neclimdul’s picture

Status: Needs work » Needs review
FileSize
1.12 KB
2.81 KB

As suggested, seems to work. Swapped one of the values in the provider to confirm it failed as expected.

Mile23’s picture

Status: Needs review » Needs work

With patch: Time: 6.86 seconds, Memory: 120.75Mb

Without patch: Time: 12.88 seconds, Memory: 121.75Mb

+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -120,20 +120,14 @@ public function testAssertNoCoreUseage($expected_pass, $file_data) {
+    $this->assertEquals($expected_pass, $pass, "");

I bet you $10 the default message is more informative than an empty string.

neclimdul’s picture

Status: Needs work » Needs review
FileSize
677 bytes
2.9 KB

Dope, I started to write this, got pulled away and sat down and said "what was I doing? egh, it works lets post it."

neclimdul’s picture

Note: with the empty string you still get the default message. The messages are additional in phpunit not replacements like in simpletest.

Mile23’s picture

Status: Needs review » Reviewed & tested by the community

Ossum. :-)

I applied the patch in #11. It still speeds everything up. It caught use statements in Component that used Core namespaces which I added to random component code. It also didn't false-error on @see for the same namespaces.

It has tests with super-duper output messages.

I'm calling it RTBC, even though I wrote some of it. Feel free to chastise me for this.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs tests

Committed a7ab29e and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed a7ab29e on 8.0.x
    Issue #2432939 by neclimdul, Mile23: Optimize DrupalComponentTest
    

Status: Fixed » Closed (fixed)

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