Skip to content
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

test setup #15

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
.idea
.idea
.phpunit.result.cache
25 changes: 19 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,34 @@
],
"require": {
"illuminate/support": "9.x|10.x|11.x",
"citco/carbon": "^2.0"
"citco/carbon": "^2.0",
"ext-zlib": "*"
},
"require-dev": {
"pestphp/pest": "^2.0",
"orchestra/testbench": "^8.22"
},
"autoload": {
"psr-4": {
"Dcblogdev\\LaravelSentEmails\\": "src"
"Dcblogdev\\LaravelSentEmails\\": "src",
"Dcblogdev\\LaravelSentEmails\\Tests\\": "tests"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"extra": {
"laravel": {
"providers": [
"Dcblogdev\\LaravelSentEmails\\SentEmailsServiceProvider"
],
"aliases": {
"SentEmails": "Dcblogdev\\LaravelSentEmails\\Facades\\SentEmailsFacade"
}
]
}
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
30 changes: 30 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Test">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<!-- <env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>-->
</php>
</phpunit>
12 changes: 7 additions & 5 deletions src/Controllers/SentEmailsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Dcblogdev\LaravelSentEmails\Controllers;

use Citco\Carbon;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Contracts\View\View;
use Dcblogdev\LaravelSentEmails\Models\SentEmail;

class SentEmailsController extends BaseController
class SentEmailsController
{
public function index()
public function index(): View
{
$emails = SentEmail::orderby('id', 'desc');

Expand Down Expand Up @@ -41,15 +41,17 @@ public function index()
return view('sentemails::index', compact('emails'));
}

public function email($id) {
public function email(string $id): View
{
$email = SentEmail::findOrFail($id);

return view('sentemails::email', compact('email'));
}

public function body($id)
public function body(string $id): string
{
$email = SentEmail::findOrFail($id);

return $email->body;
}
}
21 changes: 0 additions & 21 deletions src/Facades/SentEmailsFacade.php

This file was deleted.

14 changes: 6 additions & 8 deletions src/Listeners/EmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class EmailLogger
{
public function handle(MessageSending $event)
public function handle(MessageSending $event): void
{
$message = $event->message;

Expand All @@ -22,23 +22,21 @@ public function handle(MessageSending $event)
]);
}

/**
* Format address strings for from, to, cc, bcc.
*
* @param $field
* @return null|string
*/
function formatAddressField($field): ?string
function formatAddressField(array $field): ?string
{
$strings = [];

foreach($field as $row) {
$email = $row->getAddress();
$name = $row->getName();

if ($name !='') {
$email = $name.' <'.$email.'>';
}

$strings[] = $email;
}

return implode(', ', $strings);
}
}
16 changes: 13 additions & 3 deletions src/Models/SentEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@

class SentEmail extends Model
{
protected $guarded = [];
protected $fillable = [
'date',
'from',
'to',
'cc',
'bcc',
'subject',
'body'
];

public function getBodyAttribute($compressed) {
public function getBodyAttribute($compressed): string
{
return config('sentemails.compressBody')
? gzinflate(base64_decode($compressed))
: $compressed;
}

public function setBodyAttribute($raw) {
public function setBodyAttribute($raw): void
{
$body = config('sentemails.compressBody')
? base64_encode(gzdeflate($raw, 9))
: $raw;
Expand Down
8 changes: 0 additions & 8 deletions src/SentEmails.php

This file was deleted.

20 changes: 6 additions & 14 deletions src/SentEmailsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

namespace Dcblogdev\LaravelSentEmails;

use Illuminate\Support\ServiceProvider;
use Illuminate\Mail\Events\MessageSending;
use Dcblogdev\LaravelSentEmails\Listeners\EmailLogger;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Dcblogdev\LaravelSentEmails\Listeners\EmailLogger;

class SentEmailsServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
Event::listen(
MessageSending::class,
EmailLogger::class
);

$this->loadViewsFrom(__DIR__.'/resources/views', 'sentemails');
$this->loadRoutesFrom(__DIR__.'/routes.php');
$this->loadRoutesFrom(__DIR__ . '/routes/web.php');

if ($this->app->runningInConsole()) {

Expand All @@ -38,17 +39,8 @@ public function boot()
}
}

/**
* Register the application services.
*/
public function register()
public function register(): void
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__. '/../config/sentemails.php', 'sentemails');

// Register the main class to use with the facade
$this->app->singleton('sent-emails', function () {
return new SentEmails;
});
}
}
6 changes: 0 additions & 6 deletions src/routes.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Dcblogdev\LaravelSentEmails\Controllers\SentEmailsController;
use Illuminate\Support\Facades\Route;

Route::middleware(config('sentemails.middleware'))->group(function () {
Route::get(config('sentemails.routepath'), [SentEmailsController::class, 'index'])->name('sentemails.index');
Route::get(config('sentemails.routepath').'/email/{id}', [SentEmailsController::class, 'email'])->name('sentemails.email');
Route::get(config('sentemails.routepath').'/body/{id}', [SentEmailsController::class, 'body'])->name('sentemails.body');
});
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Dcblogdev\LaravelSentEmails\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);
8 changes: 8 additions & 0 deletions tests/SentEmailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Symfony\Component\Routing\Exception\RouteNotFoundException;

//fails due to login route not existing directly in package
test('guests cannot access the sent emails page', function () {
$this->get(route('sentemails.index'))->assertStatus(500);
});
30 changes: 30 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Dcblogdev\LaravelSentEmails\Tests;

use Dcblogdev\LaravelSentEmails\SentEmailsServiceProvider;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
protected function getPackageProviders($app): array
{
return [
SentEmailsServiceProvider::class,
];
}

protected function defineEnvironment($app)
{
$app['config']->set('app.key', 'base64:'.base64_encode(Str::random(32)));

// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'mysql');
$app['config']->set('database.connections.mysql', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
}