Skip to content

Commit

Permalink
Merge pull request #1 from rogervila/dev-initial
Browse files Browse the repository at this point in the history
Merge initial development into master
  • Loading branch information
rogervila authored Jul 8, 2019
2 parents 686589a + 6d6c1f0 commit 051216d
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
build: false
version: appveyor-{branch}-{build}
shallow_clone: false
clone_folder: C:\projects\app

environment:
matrix:
- php_ver: 7.3.1
- php_ver: 7.2.14
- php_ver: 7.1.26

cache:
- '%APPDATA%\Composer'
- '%LOCALAPPDATA%\Composer'
- C:\tools\php -> .appveyor.yml
- C:\tools\composer.phar -> .appveyor.yml

init:
- SET PATH=C:\tools\php;%PATH%

install:
- ps: Set-Service wuauserv -StartupType Manual
- IF NOT EXIST C:\tools\php (choco install --yes --allow-empty-checksums php --version %php_ver% --params '/InstallDir:C:\tools\php')
- cd C:\tools\php
- copy php.ini-production php.ini
- echo date.timezone="UTC" >> php.ini
- echo memory_limit=512M >> php.ini
- echo extension_dir=ext >> php.ini
- echo extension=php_curl.dll >> php.ini
- echo extension=php_openssl.dll >> php.ini
- echo extension=php_mbstring.dll >> php.ini
- IF NOT EXIST C:\tools\composer.phar (cd C:\tools && appveyor DownloadFile https://getcomposer.org/composer.phar)
- php C:\tools\composer.phar --version
- cd C:\projects\app

before_test:
- cd C:\projects\app
- php C:\tools\composer.phar update --optimize-autoloader --no-interaction --no-progress --prefer-stable --no-ansi
- php C:\tools\composer.phar info -D | sort

test_script:
- cd C:\projects\app
- vendor\bin\phpunit
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_style = space
indent_size = 2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
composer.lock
vendor
junit-logfile.xml
clover.xml
.scannerwork
.php_cs.cache
15 changes: 15 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->notPath('vendor')
->in(__DIR__)
->name('*.php');

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'no_unused_imports' => false,
])
->setFinder($finder);
5 changes: 5 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
preset: psr2

finder:
exclude:
- "vendor"
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: php

php:
- 7.1
- 7.2
- 7.3

cache:
directories:
- $HOME/.composer/cache

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev

script:
- vendor/bin/phpunit
- sonar-scanner -Dsonar.projectKey=rogervila_laravel-email-failer -Dsonar.sources=. -Dsonar.exclusions='index.php, vendor/**, tests/**' -Dsonar.php.tests.reportPath=junit-logfile.xml -Dsonar.php.coverage.reportPaths=clover.xml

addons:
sonarcloud:
organization: "rogervila-github"
token:
secure: "Pjbbhk3aoDsggVm2+LDFHLMN00V5B5987uUe+CMWEqlAK+t3xFe4Zoy60nOJxA4L1/gfrsgLfn22mW6chE6Bf/T+DhMZtzAIv3TQfIc+xNcc98aTAkrJiX2addZLAViud7OLJYrelWBGwMhe+X1YiTyvS6OQ8MNBqF8b3g59nzoeugUfcVQSWqzEhoG1Ex6rkrVOyhBj05AHZ4icfmss2SgS80wuEba87YzIYSgy9q03KJtwhIHgMCUVB3mWd68+tl+i3F2cIgtGjiej/YbP8t9aVYpX/zXq7tMvlWe2ZREpk0Q4AcilOX4AZX/aGydAFis+5fc6W+CMBpk/hOHi1+PLErQowIxyCA/j5HOX8Ge9R7RIzVi5hRwX86r6Kiactn/09QSa3UqkPakAniGGCmhv0PZ2910vUolnU2pz8cF4Stt33V1leX6zZjmCcvdlHRg5mtp/fGGZeEE/J9Z7xecCS34fP925p/W0PKT4RxK2ve1MMQ5CU3UPX7wglOIax4plMeCvOV6mGYOMdehV9HwQLtMQqGVBJg/xe63Nr73GFdBfMXxrWG89LWmaU3TywJGaSxg2DeNXDnz+N9M6RFfNWI7wmO391cGNiIsmpGhUJ5KLN27ICjtKnvCxTPVdphC+n205ULN5R+vX2IwNgrGtnXcMxC03GtizLrXBkrY="
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "rogervila/laravel-email-failer",
"description": "Helper class for testing Mail Failures",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Roger Vilà",
"email": "[email protected]"
}
],
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"psr-4": {
"LaravelEmailFailer\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require": {
"laravel/framework": "5.8.x"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^7.5",
"orchestra/testbench": "^3"
}
}
39 changes: 39 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="clover.xml"/>
<log type="junit" target="junit-logfile.xml"/>
</logging>
<php>
<env name="APP_KEY" value="a859c705fa75b94a7df4433c694310bc"/>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
51 changes: 51 additions & 0 deletions src/MailFailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace LaravelEmailFailer;

use Illuminate\Support\Testing\Fakes\MailFake;

class MailFailer extends MailFake
{
/**
* Array of failed recipients.
*
* @var array
*/
protected $failedRecipients = [];

/**
* Send a new message using a view.
*
* @param string|array $view
* @param array $data
* @param \Closure|string $callback
* @return void
* @throws \Swift_TransportException
*/
public function send($view, array $data = [], $callback = null)
{
try {
foreach ($view->to as $to) {
array_push($this->failedRecipients, $to['address']);
}

throw new \Swift_TransportException(
'Expected response code 354 but got code "554", with message "554 5.5.1 Error: no valid recipients'
);
} catch (\Exception $e) {
throw new \Swift_TransportException(
$e->getMessage()
);
}
}

/**
* Get the array of failed recipients.
*
* @return array
*/
public function failures()
{
return $this->failedRecipients;
}
}
57 changes: 57 additions & 0 deletions tests/Feature/MailFailureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tests\Feature;

class FakeMailable extends \Illuminate\Mail\Mailable
{
//
}

class MailFailureTest extends \Orchestra\Testbench\TestCase
{
public function testMailFails()
{
$this->expectException(\Swift_TransportException::class);

$address = '[email protected]';
$mailer = new \LaravelEmailFailer\MailFailer();
$this->app->instance('mailer', $mailer);

$mailable = new FakeMailable;
$mailable->subject('test')->to($address);

$mailer->send($mailable);
}

public function testMailFailuresContainsAddress()
{
try {
$address = '[email protected]';
$mailer = new \LaravelEmailFailer\MailFailer();
$this->app->instance('mailer', $mailer);

$mailable = new FakeMailable;
$mailable->subject('test')->to($address);

$mailer->send($mailable);
} catch (\Exception $e) {
$this->assertTrue(in_array($address, $mailer->failures()));
}
}

public function testMailFailuresContainsMultipleAddresses()
{
try {
$addresses = ['[email protected]', '[email protected]'];
$mailer = new \LaravelEmailFailer\MailFailer();
$this->app->instance('mailer', $mailer);

$mailable = new FakeMailable;
$mailable->subject('test')->to($addresses);

$mailer->send($mailable);
} catch (\Exception $e) {
$this->assertCount(0, array_diff($addresses, $mailer->failures()));
}
}
}

0 comments on commit 051216d

Please sign in to comment.