Skip to content

Commit

Permalink
Added support for PHP 5.6 and SwiftMailer 4 and 5
Browse files Browse the repository at this point in the history
Merge branch 'feature/legacy-support'
  • Loading branch information
Finesse committed May 1, 2018
2 parents 414258b + a23aab4 commit 8b0a0e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
22 changes: 17 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly
- 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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 8 additions & 9 deletions src/SwiftMailerDefaultsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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)]);
}
Expand All @@ -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);
}
}
Expand All @@ -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 = [];
}
}
13 changes: 13 additions & 0 deletions tests/SwiftMailerDefaultsPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 8b0a0e2

Please sign in to comment.