diff --git a/.travis.yml b/.travis.yml index 9a0646f..5820f49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: php php: + - 5.6 - 7.0 - 7.1 - 7.2 @@ -7,10 +8,19 @@ php: - hhvm env: - - DEPENDENCIES="high" - - DEPENDENCIES="low" + - SWIFTMAILER=^4.0 DEPENDENCIES=high + - SWIFTMAILER=^4.0 DEPENDENCIES=low + - SWIFTMAILER=^5.0 DEPENDENCIES=high + - SWIFTMAILER=^5.0 DEPENDENCIES=low + - SWIFTMAILER=^6.0 DEPENDENCIES=high + - SWIFTMAILER=^6.0 DEPENDENCIES=low matrix: + exclude: + - php: 5.6 + env: SWIFTMAILER=^6.0 DEPENDENCIES=high + - php: 5.6 + env: SWIFTMAILER=^6.0 DEPENDENCIES=low allow_failures: - php: hhvm - php: nightly @@ -20,9 +30,11 @@ cache: - $HOME/.composer/cache install: - - if [[ "$DEPENDENCIES" = 'high' ]]; then composer update; fi - - if [[ "$DEPENDENCIES" = 'low' ]]; then composer update --prefer-lowest; fi - - composer require php-coveralls/php-coveralls '^2.0' --dev + - if [[ ! -z $SWIFTMAILER ]]; then composer require "swiftmailer/swiftmailer $SWIFTMAILER" --no-update; fi + - if [[ ${TRAVIS_PHP_VERSION:0:3} > 7.1 ]]; then composer require "phpunit/phpunit >=6.0 <8" --dev --no-update; fi # PHPUnit 5 is installed in PHP 7.2 despite it doesn't work in PHP 7.2 + - if [[ $DEPENDENCIES = high ]]; then composer update; fi + - if [[ $DEPENDENCIES = low ]]; then composer update --prefer-lowest; fi + - composer require php-coveralls/php-coveralls ^2.0 --dev script: - php vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml diff --git a/composer.json b/composer.json index e422e6e..13c23ff 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,11 @@ } ], "require": { - "php": ">=7.0", - "swiftmailer/swiftmailer": "^6.0" + "php": ">=5.6", + "swiftmailer/swiftmailer": ">=4.0 <7.0" }, "require-dev": { - "phpunit/phpunit": "^6.4", + "phpunit/phpunit": ">=5.4.3 <8.0", "mockery/mockery": "^1.0" }, "autoload": { diff --git a/src/SwiftMailerDefaultsPlugin.php b/src/SwiftMailerDefaultsPlugin.php index b345bdb..e3ef8ad 100644 --- a/src/SwiftMailerDefaultsPlugin.php +++ b/src/SwiftMailerDefaultsPlugin.php @@ -17,10 +17,10 @@ class SwiftMailerDefaultsPlugin implements \Swift_Events_EventListener, \Swift_E protected $defaults = []; /** - * @var \Swift_Mime_SimpleMessage|null Message that was received before sending and is not modified. It is kept to - * restore the original message after sending. + * @var array The original values of a sent message properties which were replaced by the default properties. They + * are kept to restore the original message properties after the sending. */ - protected $originalMessage; + protected $originalValues = []; /** * @param array $defaults Default Message properties. See the readme for more information. @@ -40,7 +40,7 @@ public function __construct(array $defaults = []) * @param string $property The property name. See the readme for more information. * @param array ...$arguments The list of argument for the Swift_Mime_SimpleMessage property setter */ - public function setDefault(string $property, ...$arguments) + public function setDefault($property, ...$arguments) { $this->defaults[ucfirst($property)] = $arguments; } @@ -50,7 +50,7 @@ public function setDefault(string $property, ...$arguments) * * @param string $property The property name. See the readme for more information. */ - public function unsetDefault(string $property) + public function unsetDefault($property) { unset($this->defaults[ucfirst($property)]); } @@ -61,11 +61,11 @@ public function unsetDefault(string $property) public function beforeSendPerformed(\Swift_Events_SendEvent $event) { $message = $event->getMessage(); - $this->originalMessage = clone $message; foreach ($this->defaults as $property => $arguments) { $originalValue = $message->{'get'.$property}(); if ($originalValue === null || $originalValue === '' || $originalValue === []) { + $this->originalValues[$property] = $originalValue; $message->{'set'.$property}(...$arguments); } } @@ -78,11 +78,10 @@ public function sendPerformed(\Swift_Events_SendEvent $event) { $message = $event->getMessage(); - foreach ($this->defaults as $property => $arguments) { - $originalValue = $this->originalMessage->{'get'.$property}(); + foreach ($this->originalValues as $property => $originalValue) { $message->{'set'.$property}($originalValue); } - $this->originalMessage = null; + $this->originalValues = []; } } diff --git a/tests/SwiftMailerDefaultsPluginTest.php b/tests/SwiftMailerDefaultsPluginTest.php index 9c5add5..1932fb0 100644 --- a/tests/SwiftMailerDefaultsPluginTest.php +++ b/tests/SwiftMailerDefaultsPluginTest.php @@ -12,6 +12,19 @@ */ class SwiftMailerDefaultsPluginTest extends TestCase { + /** + * @inheritDoc + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + // Early SwiftMailer 4 versions don't support Composer autoload + if (!class_exists('\Swift_Message')) { + require __DIR__.'/../vendor/swiftmailer/swiftmailer/lib/swift_required.php'; + } + } + /** * Tests passing default values to Message */