Add a trait for autowiring properties in tests

Created on 29 July 2024, 4 months ago

Problem/Motivation

Currently when adding services as properties in tests you need to declare the properties and initialize them in ::setUp. This is unnecessary boilerplate.

Steps to reproduce

Proposed resolution

Add a trait similar to AutowireTrait to initialize the services in setUp.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Feature request
Status

Active

Version

11.0 🔥

Component
PHPUnit 

Last updated about 11 hours ago

Created by

🇦🇺Australia mstrelan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @mstrelan
  • Merge request !8965Add a trait for autowiring properties in tests → (Open) created by mstrelan
  • Status changed to Needs review 4 months ago
  • Pipeline finished with Failed
    4 months ago
    Total: 117s
    #236856
  • Pipeline finished with Success
    4 months ago
    Total: 589s
    #236867
  • Status changed to Needs work 4 months ago
  • 🇺🇸United States smustgrave

    Left a comment on the MR.

  • 🇮🇹Italy mondrake 🇮🇹

    Like the idea; IMHO we should always explicitly add the #[Autowire()] attribute to the property.

    If we do this, then I think we should consider adding the trait to KernelTestBase and BrowserTestBase.

  • Status changed to Needs review 4 months ago
  • 🇦🇺Australia mstrelan

    Thanks for review, responding to both. Have also split up the setUp method in the trait so classes that have their own setUp method can just call autoSetUp without having parent::setUp called twice.

  • Pipeline finished with Success
    4 months ago
    Total: 654s
    #237799
  • Merge request !8978AutowireProperty attribute and trait → (Open) created by mstrelan
  • 🇦🇺Australia mstrelan

    Added another MR based on suggestions from @mondrake in #5

  • Pipeline finished with Failed
    4 months ago
    Total: 394s
    #237835
  • Pipeline finished with Failed
    4 months ago
    Total: 470s
    #237848
  • Pipeline finished with Success
    4 months ago
    #237856
  • Pipeline finished with Failed
    4 months ago
    Total: 132s
    #237980
  • 🇮🇹Italy mondrake 🇮🇹

    I tried to use Symfony's #[Autowire] attribute to make things more explicit - PoC so did not change everything.

    Ideally we could mark autowireProperties() with #[Before] and let PHPUnit call it prior to setUp, but the container is not yet defined at that point.

  • Pipeline finished with Failed
    4 months ago
    Total: 126s
    #238096
  • Pipeline finished with Failed
    4 months ago
    Total: 418s
    #238098
  • Pipeline finished with Failed
    4 months ago
    Total: 127s
    #238112
  • Pipeline finished with Failed
    4 months ago
    Total: 403s
    #238124
  • Pipeline finished with Failed
    4 months ago
    Total: 432s
    #238212
  • Pipeline finished with Success
    4 months ago
    Total: 623s
    #238242
  • Pipeline finished with Failed
    4 months ago
    Total: 125s
    #238572
  • Pipeline finished with Failed
    4 months ago
    Total: 518s
    #238579
  • Pipeline finished with Failed
    4 months ago
    Total: 115s
    #238702
  • Pipeline finished with Success
    4 months ago
    Total: 596s
    #238706
  • Status changed to Needs work 4 months ago
  • 🇦🇺Australia mstrelan

    @mondrake I accidentally force pushed the wrong branch, can you push your branch back?

  • Pipeline finished with Success
    4 months ago
    Total: 516s
    #238861
  • 🇮🇹Italy mondrake 🇮🇹

    @mstrelan done!

  • Pipeline finished with Success
    4 months ago
    Total: 718s
    #239220
  • Status changed to Needs review about 2 months ago
  • 🇮🇹Italy mondrake 🇮🇹

    mondrake changed the visibility of the branch 3464357-autosetup to hidden.

  • Pipeline finished with Success
    about 2 months ago
    Total: 667s
    #301204
  • Pipeline finished with Failed
    about 2 months ago
    Total: 788s
    #301220
  • Pipeline finished with Success
    about 2 months ago
    #301231
  • Pipeline finished with Failed
    about 2 months ago
    Total: 668s
    #301516
  • Pipeline finished with Failed
    about 2 months ago
    Total: 132s
    #301555
  • 🇮🇹Italy mondrake 🇮🇹

    Drupal\Tests\content_moderation\Kernel\WorkspacesContentModerationStateTest::testNonLangcodeEntityTypeModeration fails because it inherits from Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest, but the inherited properties are not scanned for autowiring - we need to take care of inheritance especially for base test classes.

  • Pipeline finished with Failed
    about 2 months ago
    #301561
  • Pipeline finished with Failed
    about 2 months ago
    Total: 132s
    #301638
  • Pipeline finished with Success
    about 2 months ago
    Total: 983s
    #301648
  • 🇮🇹Italy mondrake 🇮🇹

    #14 is wrong. The inherited properties inherit the attributes too, https://3v4l.org/WYsXd. The problem was in mixing \Drupal::xxx calls with injected services and usage of traits. Maybe to be addressed but not here.

  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇮🇹Italy mondrake 🇮🇹

    Rebased

  • Pipeline finished with Success
    about 1 month ago
    Total: 1631s
    #305222
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇮🇹Italy mondrake 🇮🇹

    rebased

  • Pipeline finished with Success
    about 1 month ago
    Total: 6404s
    #306681
  • Pipeline finished with Failed
    27 days ago
    Total: 709s
    #320863
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    17 days ago
    Total: 153s
    #329365
  • Pipeline finished with Failed
    17 days ago
    Total: 164s
    #329367
  • 🇮🇹Italy mondrake 🇮🇹
  • Pipeline finished with Failed
    17 days ago
    Total: 701s
    #329371
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Canceled
    about 11 hours ago
    #345289
  • 🇮🇹Italy mondrake 🇮🇹

    rebased

  • Pipeline finished with Success
    about 11 hours ago
    Total: 541s
    #345299
Production build 0.71.5 2024