Problem/Motivation

Child issue of #1826054: [Meta] Expose Drupal Components outside of Drupal

We want to be able to test Drupal's core Components in isolation as much as possible.

This enables testing the dependency bounds of the components themselves.

Proposed resolution

For each component:

File an issue to move the component tests.

Move tests in core/tests/Drupal/Tests/Components to their respective components under a Tests/ directory.

Add test directories to composer.json exclude-from-classmap.

Add test namespaces to composer.json require-dev.

The Transliteration component tests use data which is currently under src/data/. This should be moved to the tests/ directory, plus amend the tests that use this data.

Leave Drupal\Tests\Component\DrupalComponentTest where it is but modify it as needed.

Remaining tasks

Determine the fate of DrupalComponentTest.

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#11 2943856_11.patch3.17 KBmile23

Comments

Mile23 created an issue. See original summary.

mile23’s picture

Immediately postponed on #2943842: Allow component namespaces to be autoloaded independently while we make sure the component namespaces work.

mile23’s picture

Status: Active » Postponed
alexpott’s picture

One problem is that the regular runtime autoloader should not be able to autoload tests so each composer.json is going to need something like

    "autoload": {
        "exclude-from-classmap": [
            "/Tests/"
        ]
   },

And we need to handle this for core too... which is going to be fun because our test infra is rather bizarre in that it autoloads tests....

alexpott’s picture

Ah autoloading tests is not an issue! We can just add a testsuite like

        <testsuite name="Drupal Component Test Suite">
            <directory>./lib/Drupal/Component/*/Tests/</directory>
        </testsuite>

And use regular PHPUnit discovery!

alexpott’s picture

... because we should not have any other type of test than a unit test here :)

dawehner’s picture

While I understand that it is nice to have tests near the actual code I don't see the point of this being a child issue of #1826054: [Meta] Expose Drupal Components outside of Drupal. You would not use the tests anyway when you require these libraries.

mile23’s picture

@dawehner: Currently, the requirements and version constraints in each of the components' composer.json file is at best a guess, because it's hard to test.

There's this issue which will never be fixed because the solution seems like its too complex to review: #2876669-40: Fix dependency version requirement declarations in components

That solution is complex because it has to create a whole other testing system just for the components. Having the tests local to the components would make it easier to understand what's going on in that sort of issue.

mile23’s picture

alexpott’s picture

@Mile23 I'd argue that do to this right we should go component by component anyway. We're going to have to edit each component's composer.json, the testing docs, create a phpunit.xml.dist and get core to run the tests too.

mile23’s picture

StatusFileSize
new3.17 KB

@alexpott: I'm not sure we need to do some of that. We'll definitely need to change TESTING.txt to whatever solution we arrive at.

We should also change TestDiscovery to use the test suites, and then also discover WTB tests.

Here's a patch that adds a testsuite called 'component'. It's governed by ComponentTestSuite.

The patch moves the Uuid test to the component directory.

You can do this:

$ ./vendor/bin/phpunit -c core --testsuite component --group Uuid

And you can also do this:

$ cd core/lib/Drupal/Component/Uuid/
$ ../../../../../vendor/bin/phpunit .

:-)

This patch also automatically adds the component testsuite to the unit testsuite, so whenever you say --testsuite unit you'll also run component. This is OK since all component tests must be unit tests, and they should fail anyway if they aren't.

$ ./vendor/bin/phpunit -c core/ --testsuite unit --group Uuid
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

Testing 
.......

Time: 14.27 seconds, Memory: 66.00MB

OK (7 tests, 7 assertions)

Where this doesn't help us is run-tests.sh:

$ php ./core/scripts/run-tests.sh --types PHPUnit-Unit Uuid
  ERROR: Test group not found: Uuid

That's because run-tests.sh uses TestDiscovery, which is above my pay grade to maintain at this point in time and space on this postponed issue.

alexpott’s picture

We need to the do the component autoloader change. Tests classes here must not be autoloadable.

Having them run as part of Unit makes sense because it'll limit the need for changes to testbot.

mile23’s picture

Having them run as part of Unit makes sense because it'll limit the need for changes to testbot.

Well, except that run-tests.sh doesn't use test suites. It's a completely different discovery system. So we still have to make TestDiscovery either pay attention to the test suite classes, or have it do its own version of discovering tests in the components.

At this point I give a shoutout to #2863055-36: Move TestDiscovery out of simpletest module, minimize dependencies. :-)

mile23’s picture

Title: Move component tests to component directories » Reorganize Components so they are testable in isolation
Issue tags: +Needs issue summary update

Contemplating #2849669-51: Fix \Drupal\Component\Utility\Unicode() because of the Symfony mbstring polyfill and this issue....

The problem is that if we move the tests to where the components are, we end up with tests in our source.

So the solution is something like this:

core/
  lib/
  tests/
components/
  lib/
  tests/

We could also split them out into individual directories, something like:

components/
  Plugin/
    composer.json
    lib/
    tests/

This would involve restructuring the subtree split, and changing some paths in composer merges. Also probably adding another test suite to phpunit called components.

alexpott’s picture

@Mile23 we can also do exactly what Symfony does and use the exclude-from-classmap key like

    "autoload": {
        "psr-4": { "Symfony\\Component\\BrowserKit\\": "" },
        "exclude-from-classmap": [
            "/Tests/"
        ]
    },

I think this is the way to go.

mile23’s picture

Title: Reorganize Components so they are testable in isolation » [meta] Reorganize Components so they are testable in isolation
Issue summary: View changes
Issue tags: -Needs issue summary update

Ok, checking it out on the Uuid component.

  • Add exclude-from-classmap to the composer.json file.
  • Add autoload-dev to the composer.json file.
  • Add a Tests directory with a test in it.
  • $ cd core/lib/Drupal/Component/Uuid
  • $ composer validate
  • $ composer dumpautoload --no-dev
  • Poke around in the generated vendor directory and see what's autoloadable.

The thing we verify here is that Tests is excluded from the classmap, but we never declared a classmap, so it kind of doesn't matter in that regard. When we say composer dumpautoload -o the test file is excluded from the map, even if it's in the autoload-dev PSR-4 list.

mile23’s picture

Issue tags: +Composer

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mile23’s picture

Maybe add a generic PHPUnit step to the testbot? #2837112-6: Add a test definition for a generic PHPUnit test run

It would give us drupalci.yml that would look like this:

      composer.annotation:
        working-directory: core/lib/Drupal/Component/Annotation
      phpunit.annotation:
        working-directory: core/lib/Drupal/Component/Annotation

...repeat for all the components.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mixologic’s picture

Component: other » composer
Issue tags: -Composer +Composer initiative

Re-filing/re-tagging

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mstrelan’s picture

I'd like to see the same for tests in tests/Drupal/Tests/Composer. They currently depend on the Drupal\Composer\Composer class which doesn't exist unless you checkout the git repo. This means you can't run the full core test suite from a site built from composer or tarballs. Moving the tests to composer/Tests would solve this.

mile23’s picture

We're still in technical debt mode on this because we don't have a way for run-tests.sh to run the tests outside of core/tests.

You can see how cumbersome it is now in #2876669: Fix dependency version requirement declarations in components, to the point that that issue is still outstanding.

Here's the travis CI version of it: https://github.com/paul-m/drupal_component_tester

I'm generally +1 on doing this for Composer stuff, in spirit. But it's also true that most of the Composer based tests are integration ones which build out a codebase from the git repo, to prove that it works. It might be that more strict unit tests could be moved to the composer/ directory, much as we'd do with components.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.