Make WordPress Core

Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#34161 closed enhancement (fixed)

PhpMailer mock decorator methods

Reported by: welcher's profile welcher Owned by: boonebgorges's profile boonebgorges
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: has-patch dev-feedback has-unit-tests
Focuses: Cc:

Description (last modified by welcher)

When writing unit tests for items that use the Mock PHPMailer working with the object is tedious. I am proposing some decorator methods to allow better clarity and access to the various items in the mock_sent array.

The attached patch includes the new methods and updates a test to demonstrate usage as well as the enhanced readability the new methods provide.

Attachments (3)

34161.diff (6.7 KB) - added by welcher 11 years ago.
34161.2.diff (15.8 KB) - added by welcher 11 years ago.
34161.3.diff (15.9 KB) - added by welcher 10 years ago.
Patch refreshed post 4.4

Download all attachments as: .zip

Change History (15)

#1 @welcher
11 years ago

  • Description modified (diff)
  • Keywords dev-feedback added

@welcher
11 years ago

This ticket was mentioned in Slack in #core by welcher. View the logs.


11 years ago

#3 @DrewAPicture
11 years ago

This approach definitely seems a lot cleaner, though that's a lot of code to make 10 assertions shorter.

Can you also add access modifiers to the new methods? And I'm not terribly concerned about the docs formatting in tests as long as it gets the message across.

#4 @welcher
11 years ago

@DrewAPicture thanks for the notes.

I'll explicitly add the access modifiers in for sure. I can remove the helper methods that call the get_address method if that makes more sense from a code bloat point of view as well.

As for the tests, I only updated one of the Unit Tests in the tests/mail.php file so there are definitely more tests in the that file and core in general that would benefit from these changes. I'm happy to update all the calls ( and probably should anyway ) to get a better sense of the full scope.

Last edited 11 years ago by welcher (previous) (diff)

@welcher
11 years ago

#5 @welcher
11 years ago

Updated patch with the following changes:

  1. Removing the helper methods in favour of the generic get_address to keep the code additions to the minimum.
  2. Updated all tests that where accessing $GLOBALS['phpmailer'] to now use this new approach and all tests are passing.

This ticket was mentioned in Slack in #core by welcher. View the logs.


11 years ago

@welcher
10 years ago

Patch refreshed post 4.4

#7 @welcher
10 years ago

  • Keywords has-unit-tests added

#8 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 4.5

#9 @welcher
10 years ago

  • Owner set to boonebgorges
  • Status changed from new to assigned

#10 @boonebgorges
10 years ago

Thanks for the patch, @welcher. This is definitely more sane that the current method of reaching into the global. I think we can simplify somewhat; given the elegance of syntax like $mailer->get_sent()->to etc, why would we ever want get_sent() or get_recipient() to return an array rather than an object?

#11 @boonebgorges
10 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 36594:

Tests: Add decorators to PHPMailer mock object.

The new get_recipient() and get_sent() methods greatly simplify the
syntax required when writing tests for wp_mail().

Props welcher.
Fixes #34161.

#12 @welcher
10 years ago

@boonebgorges thanks for getting this committed! Originally, my thought process around choosing the data type being returned was for flexibility - in case if was needed moving forward but I see your point. The patch is much more concise now.

Note: See TracTickets for help on using tickets.