8000 [#284]: Updated EmailTrait. by skipper-vp · Pull Request #362 · drevops/behat-steps · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[#284]: Updated EmailTrait. #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 29, 2025
Merged

Conversation

skipper-vp
Copy link
Contributor
@skipper-vp skipper-vp commented Apr 24, 2025

[#284]: Updated EmailTrait.

Summary by CodeRabbit

  • New Features

    • Added new test steps for enabling and disabling the test email system.
    • Introduced step definitions for verifying email content presence, absence, and attachments with both exact and partial subject matching.
    • Added the ability to send test emails with attachments in test scenarios.
  • Improvements

    • Standardized and clarified email-related step definitions for greater consistency and readability across documentation, tests, and features.
    • Enhanced test scenarios to provide more comprehensive coverage of email behaviors, including negative assertions and partial matches.
  • Bug Fixes

    • Ensured emails with custom keys receive appropriate subject lines based on provided parameters.

@coderabbitai 8000 coderabbitai
Copy link
coderabbitai bot commented Apr 24, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (3)
  • main
  • develop
  • master

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update focuses on extensive improvements to email-related Behat test steps and supporting code. Step definitions in documentation and implementation are standardized for clarity and consistency, adopting explicit "should" phrasing and more descriptive parameter names. New steps are introduced for enabling/disabling the test email system, asserting email content presence or absence, and handling email attachments and links with both exact and partial subject matching. The corresponding feature tests and documentation are updated to reflect these changes, and the mail handler is enhanced to support dynamic subjects for non-default email keys. A new test utility is added to send emails with attachments.

Changes

File(s) Change Summary
MIGRATION.md, steps.md Updated and expanded documentation for email-related Behat steps: standardized step phrasing, added new steps for enabling/disabling test email system, clarified parameter naming, and added steps for content presence/absence, attachment, and link handling with exact/partial subject matching. Minor formatting fix in JS step documentation.
src/EmailTrait.php Refactored and extended email step definitions: renamed for clarity and consistency, added steps for enabling/disabling test email system, new assertions for email content and attachments, improved handling of subject matching (exact/partial), and negative assertions. Removed unused import. Improved error messages and parameter handling.
tests/behat/bootstrap/FeatureContextTrait.php Added a new method to send a test email with an attachment for use in Behat scenarios.
tests/behat/features/email.feature Updated feature tests to use new/renamed step definitions, improved scenario coverage, added scenarios for enabling/disabling test email system, content/attachment/link assertions, and clarified step phrasing and parameter usage throughout.
tests/behat/fixtures/d10/web/modules/custom/mysite_core/mysite_core.module Enhanced mail handler to set dynamic subject lines for non-default email keys, supporting new test scenarios involving custom subjects.

Sequence Diagram(s)

sequenceDiagram
    participant Tester as Behat Test
    participant EmailTrait as EmailTrait
    participant MailSystem as Mail Handler

    Tester->>EmailTrait: Enable test email system
    EmailTrait->>MailSystem: Set test email system enabled

    Tester->>EmailTrait: Send email to address with subject/body/attachment
    EmailTrait->>MailSystem: Send email with parameters

    MailSystem-->>EmailTrait: Email is stored for test

    Tester->>EmailTrait: Assert email should be sent to address
    EmailTrait->>MailSystem: Retrieve sent emails
    EmailTrait-->>Tester: Assertion result

    Tester->>EmailTrait: Assert email field (body/header) should (not) contain/be value
    EmailTrait->>MailSystem: Retrieve sent email
    EmailTrait-->>Tester: Assertion result

    Tester->>EmailTrait: Follow link number in email with (partial/exact) subject
    EmailTrait->>MailSystem: Retrieve email by subject
    EmailTrait->>Tester: Visit link in email body

    Tester->>EmailTrait: Assert file attachment in email with (partial/exact) subject
    EmailTrait->>MailSystem: Retrieve email by subject
    EmailTrait-->>Tester: Assertion result
Loading

Suggested labels

PR: Requires more work


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AlexSkrypnyk AlexSkrypnyk marked this pull request as draft April 25, 2025 06:37
Copy link
codecov bot commented Apr 28, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 18 lines in your changes missing coverage. Please review.

Project coverage is 80.48%. Comparing base (1d1a484) to head (6171d69).
Report is 1 commits behind head on 3.x.

Files with missing lines Patch % Lines
src/EmailTrait.php 75.00% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              3.x     #362      +/-   ##
==========================================
+ Coverage   79.46%   80.48%   +1.01%     
==========================================
  Files          30       30              
  Lines        1495     1532      +37     
==========================================
+ Hits         1188     1233      +45     
+ Misses        307      299       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AlexSkrypnyk
Copy link
Member

@AlexSkrypnyk AlexSkrypnyk marked this pull request as ready for review April 29, 2025 00:16
@AlexSkrypnyk AlexSkrypnyk self-requested a review April 29, 2025 00:16
@AlexSkrypnyk AlexSkrypnyk added the PR: Needs review Pull request needs a review from assigned developers label Apr 29, 2025
@AlexSkrypnyk
Copy link
Member
AlexSkrypnyk commented Apr 29, 2025

@coderabbitai full review

Copy link
coderabbitai bot commented Apr 29, 2025
✅ Actions performed

Full review triggered.

Copy link
coderabbitai bot commented Apr 29, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get PR details
gh pr view 362 --repo drevops/behat-steps

# Check the diff of the PR
gh pr diff 362 --repo drevops/behat-steps

Length of output: 50994


@AlexSkrypnyk I've completed a full review of PR #362 which updates the EmailTrait component.

Overall Assessment

This is a well-structured PR that significantly improves the EmailTrait by modernizing its API, enhancing functionality, and improving test coverage. The changes align with what appears to be a broader effort to standardize the codebase as part of a v2 to v3 migration.

Strengths

  1. Improved API Design:

    • Consistent naming convention using "should" in assertion methods
    • More intuitive method names that clearly communicate their purpose
    • Better parameter naming (e.g., :file_name instead of :name)
  2. Enhanced Functionality:

    • New methods for partial matching of email subjects (using "containing")
    • Explicit steps for enabling/disabling the test email system
    • More comprehensive assertions for email content
    • Improved attachment handling
  3. Error Messages:

    • More descriptive and helpful error messages
    • Consistent error message formatting
  4. Documentation:

    • Comprehensive updates to MIGRATION.md
    • Updated steps.md with clear examples
    • Improved PHPDoc comments
  5. Test Coverage:

    • Updated email.feature with tests for new functionality
    • Comprehensive test scenarios for all new methods

Areas for Improvement

  1. Test Coverage:

    • According to Codecov, there are still 18 lines missing coverage in EmailTrait.php (71.87% patch coverage)
    • Consider adding tests for these uncovered lines
  2. Method Organization:

    • Some methods have similar functionality but different names
    • Consider further refactoring to reduce code duplication
  3. Parameter Validation:

    • Some methods could benefit from additional input validation
    • Consider adding type checks and validation for critical parameters
  4. PHPUnit Tests:

    • While there are Behat feature tests, PHPUnit tests could provide more targeted testing

Conclusion

This PR represents a significant improvement to the EmailTrait component, making it more robust, user-friendly, and maintainable. The consistent naming conventions and enhanced functionality will make it easier for developers to write and maintain Behat tests related to email functionality.

I recommend approving this PR after addressing the test coverage issues highlighted by Codecov.

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (5)
src/EmailTrait.php (5)

120-131: ⚠️ Potential issue

Avoid calling instance methods statically – switch to $this->

emailAssertEmailShouldBeSentTo() invokes the non-static method emailGetCollectedEmails() via self::, which triggers a “non-static method called statically” deprecation notice in PHP 8 and will become a fatal error in PHP 9.

-    foreach (self::emailGetCollectedEmails() as $record) {
+    foreach ($this->emailGetCollectedEmails() as $record) {

The same static invocation pattern appears in other assertion helpers (emailAssertNoEmailsShouldBeSent, emailFind, etc.). Please revise them all to use $this->… for future-proof compatibility.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 129-129: src/EmailTrait.php#L129
Added line #L129 was not covered by tests


468-472: ⚠️ Potential issue

Static call to non-static method inside emailEnableTestEmailSystem()

self::emailClearTestEmailSystemQueue(TRUE); is another static call to a non-static method.
Replace with an instance call to prevent the same deprecation/fatal-error path:

-    self::emailClearTestEmailSystemQueue(TRUE);
+    $this->emailClearTestEmailSystemQueue(TRUE);

543-550: ⚠️ Potential issue

Potential undefined-variable and invalid array_map syntax

$query = Database::getConnection()->query(...);
if ($query instanceof StatementInterface) {
    $emails = array_map(unserialize(...), $query->fetchAllKeyed());
}
$emails = empty($emails['system.test_mail_collector']) ? [] : $emails['system.test_mail_collector'];
  1. $emails is only initialised inside the if block—if the query fails, the later dereference raises Undefined variable $emails.
  2. array_map(unserialize(...), …) is invalid PHP; you meant to pass the string 'unserialize'.

Suggested fix:

-    if ($query instanceof StatementInterface) {
-      $emails = array_map(unserialize(...), $query->fetchAllKeyed());
-    }
-    $emails = empty($emails['system.test_mail_collector']) ? [] : $emails['system.test_mail_collector'];
+    $emails = [];
+    if ($query instanceof StatementInterface) {
+        $emails = array_map('unserialize', $query->fetchAllKeyed());
+    }
+    $emails = $emails['system.test_mail_collector'] ?? [];

Without this, the trait will throw a fatal error the first time it runs.


587-593: 🛠️ Refactor suggestion

Prefer (string)$string over strval($string) for PyStringNode

strval($string) on a PyStringNode returns the class name, not its textual content.
Using the cast operator forces the node’s __toString() implementation and returns the real string:

-    $string = strval($string);
+    $string = (string) $string;

This affects emailFind() and emailAssertEmailFieldShouldNotContain() and will cause all content-matching assertions to fail silently.


289-303: 🧹 Nitpick (assertive)

Guard against missing headers to avoid PHP notices

Inside emailAssertEmailFieldShouldNotContain() you directly access $record[$field]; when $field is subject or body that’s fine, but for to/from the key may be absent, leading to Undefined index notices.

Add a null-coalescing fallback:

-      $field_string = $exact ? $record[$field] :
-        trim((string) preg_replace('/\s+/', ' ', (string) $record[$field]));
+      $value = $record[$field] ?? '';
+      $field_string = $exact ? $value :
+        trim((string) preg_replace('/\s+/', ' ', (string) $value));
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 300-300: src/EmailTrait.php#L300
Added line #L300 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1d1a484 and 733de36.

📒 Files selected for processing (6)
  • MIGRATION.md (2 hunks)
  • src/EmailTrait.php (6 hunks)
  • steps.md (1 hunks)
  • tests/behat/bootstrap/FeatureContextTrait.php (1 hunks)
  • tests/behat/features/email.feature (6 hunks)
  • tests/behat/fixtures/d10/web/modules/custom/mysite_core/mysite_core.module (1 hunks)
🧰 Additional context used
🪛 LanguageTool
steps.md

[grammar] ~321-~321: Make sure that the adjective ‘exact’ is correct. Possibly, it should be an adverb (typically ~ly) that modifies ‘specified’. Possibly, it should be the first word in a compound adjective (hyphenated adjective). Possibly, it is correct.
Context: ... the email message header should be the exact specified content ```gherkin @then the email hea...

(ADVERB_OR_HYPHENATED_ADJECTIVE)

🪛 GitHub Check: codecov/patch
src/EmailTrait.php

[warning] 129-129: src/EmailTrait.php#L129
Added line #L129 was not covered by tests


[warning] 139-139: src/EmailTrait.php#L139
Added line #L139 was not covered by tests


[warning] 152-152: src/EmailTrait.php#L152
Added line #L152 was not covered by tests


[warning] 158-158: src/EmailTrait.php#L158
Added line #L158 was not covered by tests


[warning] 165-165: src/EmailTrait.php#L165
Added line #L165 was not covered by tests


[warning] 189-189: src/EmailTrait.php#L189
Added line #L189 was not covered by tests


[warning] 270-270: src/EmailTrait.php#L270
Added line #L270 was not covered by tests


[warning] 300-300: src/EmailTrait.php#L300
Added line #L300 was not covered by tests


[warning] 341-341: src/EmailTrait.php#L341
Added line #L341 was not covered by tests


[warning] 345-345: src/EmailTrait.php#L345
Added line #L345 was not covered by tests


[warning] 371-371: src/EmailTrait.php#L371
Added line #L371 was not covered by tests


[warning] 378-379: src/EmailTrait.php#L378-L379
Added lines #L378 - L379 were not covered by tests


[warning] 382-382: src/EmailTrait.php#L382
Added line #L382 was not covered by tests


[warning] 388-388: src/EmailTrait.php#L388
Added line #L388 was not covered by tests


[warning] 392-392: src/EmailTrait.php#L392
Added line #L392 was not covered by tests


[warning] 439-439: src/EmailTrait.php#L439
Added line #L439 was not covered by tests


[warning] 450-450: src/EmailTrait.php#L450
Added line #L450 was not covered by tests

🔇 Additional comments (23)
tests/behat/fixtures/d10/web/modules/custom/mysite_core/mysite_core.module (1)

26-30: Good enhancement for flexible email subject handling!

Adding a default case to handle non-default email keys with dynamic subjects is a clean solution that increases flexibility. This change aligns well with the new email attachment functionality and supports more diverse testing scenarios.

tests/behat/bootstrap/FeatureContextTrait.php (1)

174-194: Well-implemented email attachment test method!

The new testSendEmailWithAttachment method is a valuable addition that complements the existing email testing functionality. The implementation correctly:

  • Uses descriptive parameter names
  • Properly structures email params including attachments
  • Uses a dedicated mail key for attachment testing
  • Creates test attachment content based on the filename

This method aligns with the improvements to the mail handler and the new attachment assertion steps in EmailTrait.

tests/behat/features/email.feature (18)

12-14: Improved step definition with clearer, more consistent phrasing

The updated step phrase uses "should be sent" instead of "is sent" which is more consistent with BDD assertion patterns.


27-28: Improved step definition consistency

The updated phrasing aligns with BDD best practices by using "should" for assertions.


32-43: Excellent clarity in positive and negative field assertions

The revised assertions use clearer, more consistent wording with "should contain" vs "should not contain" and "should be" vs "should not be" for exact matches, making test intentions more explicit.


44-51: Well-structured content assertions

These assertions verify both exact and approximate text matching scenarios, which provides more comprehensive testing coverage.


62-70: Improved email assertion steps

The updated steps use more consistent phrasing and include header assertion steps that match the rest of the updated steps.


74-91: Good addition: Exact header matching scenario

This new scenario verifies that email headers can be matched exactly, not just with partial content matching. This covers an important testing edge case.


92-119: Well-designed positive content assertion scenarios

These new scenarios provide comprehensive test coverage for different kinds of email content assertions:

  • Exact content matching
  • Content containing a substring
  • Content not containing a substring

This allows for more precise testing of email content.


120-160: Important negative assertion scenarios

These new test scenarios cover negative assertions, verifying that emails with specific content are not sent to certain addresses. This completes the test matrix for email assertions.


170-179: Updated assertions with consistent phrasing

The revised assertions follow the new naming pattern consistently.


183-204: Good test for email system enabling and queue clearing

This scenario now explicitly enables the test email system first, which is more explicit and clear than relying on the @email tag alone.


214-217: Clearer link following step

The updated step for following links in emails now explicitly includes the subject parameter in the step definition, making it easier to understand.


231-240: Good addition: Subject substring matching for link following

This new scenario tests following links in emails by matching partial subjects, which is a useful practical case for testing emails with dynamic subjects.


243-254: Improved "no emails" assertion

The updated step uses "should be sent" rather than "were sent" for consistency with other assertions.


255-274: Good explicit test email system enable/verification test

This new scenario explicitly tests enabling the test email system and verifies it works correctly, which is important for validating the test infrastructure itself.


275-286: Important test for disabling the email system

This scenario confirms that the test email system can be disabled explicitly, which complements the enabling test and provides complete coverage of the system's control functionality.


287-299: Good attachment verification test

This scenario tests the new attachment functionality, verifying that attachments can be correctly associated with emails by subject.


300-307: Useful partial subject matching for attachments

This scenario verifies that attachments can be identified in emails with subjects containing a substring, which is useful for dynamic subject scenarios.


315-321: Updated error message text

The error message text has been updated to match the new wording pattern, maintaining consistency throughout.

MIGRATION.md (2)

44-64: Well-documented EmailTrait migration mappings

The new EmailTrait section in the migration document provides clear and comprehensive mappings from v2 to v3 step definitions. The documentation:

  1. Follows the established format of the document
  2. Clearly shows both renamed steps and new steps
  3. Reflects the standardization to "should" language
  4. Includes all the new steps for email verification, such as:
    • Enabling/disabling the test email system
    • Email content assertions with exact/substring matching
    • Email links with partial subject matching
    • Email attachment assertions

This documentation will be valuable for users migrating from v2 to v3.


99-99: Minor formatting fix in the JsTrait section

The formatting fix in the JsTrait section simplifies the step definition format from a regex pattern to a named parameter style, which is more consistent with the other step definitions.

src/EmailTrait.php (1)

456-467: Unit-test gap flagged by Codecov

Several new control-flow branches (e.g., emailEnableTestEmailSystem()) lack coverage, notably the interaction with the mailsystem module. Consider adding a behavioural test that:

  1. Enables the test mail system via the new step,
  2. Sends a dummy email,
  3. Verifies the collector receives exactly one record,
  4. Disables the system and asserts the configuration is restored.

This will lock in the new functionality and raise patch coverage.

steps.md Outdated
Comment on lines 321 to 326
#### Assert that the email message header should be the exact specified content

```gherkin
@Then an email header :header contains exact:
@Then the email header :header should be:
```

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider smoothing the wording for clarity

The phrase “the email message header should be the exact specified content” is a bit awkward.
A more idiomatic alternative would be:

@Then the email header :header should exactly be:

(or “… should be exactly:”)

Minor, but polishing wording here keeps the step catalogue consistently readable.

🧰 Tools
🪛 LanguageTool

[grammar] ~321-~321: Make sure that the adjective ‘exact’ is correct. Possibly, it should be an adverb (typically ~ly) that modifies ‘specified’. Possibly, it should be the first word in a compound adjective (hyphenated adjective). Possibly, it is correct.
Context: ... the email message header should be the exact specified content ```gherkin @then the email hea...

(ADVERB_OR_HYPHENATED_ADJECTIVE)

@AlexSkrypnyk
Copy link
Member

@skipper-vp
Could you please review and fix and then resolve AI's review points - they are valid.

8000

@AlexSkrypnyk AlexSkrypnyk added PR: Requires more work Pull request was reviewed and reviver(s) asked to work further on the pull request and removed PR: Needs review Pull request needs a review from assigned developers labels Apr 29, 2025
* @When I disable the test email system
*/
public function emailDisableTestEmailSystemStep(): void {
$this->emailDisableTestEmailSystem();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put the step on emailDisableTestEmailSystem() directly?

@skipper-vp
Copy link
Contributor Author

@AlexSkrypnyk All items addressed, please have a look.

@AlexSkrypnyk AlexSkrypnyk merged commit 00ef674 into 3.x Apr 29, 2025
3 of 4 checks passed
@AlexSkrypnyk AlexSkrypnyk deleted the feature/284-update-EmailTrait branch April 29, 2025 03:23
@coderabbitai coderabbitai bot mentioned this pull request May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Requires more work Pull request was reviewed and reviver(s) asked to work further on the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0