Skip to content

Fixed memory attachment bug and moved tests to Github Actions #27

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

Merged
merged 3 commits into from
Jan 11, 2020
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
74 changes: 74 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Run tests

on:
push:
schedule:
- cron: '0 0 * * *'

jobs:
php-tests:
runs-on: ${{ matrix.os }}

strategy:
matrix:
php: [7.4, 7.3, 7.2]
laravel: [6.*, 5.8.*, 5.7.*, 5.6.*]
os: [ubuntu-latest]
include:
- laravel: 6.*
testbench: 4.*
- laravel: 5.8.*
testbench: 3.8.*
- laravel: 5.7.*
testbench: 3.7.*
- laravel: 5.6.*
testbench: 3.6.*
exclude:
- laravel: 5.7.*
php: 7.4
- laravel: 5.6.*
php: 7.4
- laravel: 5.5.*
php: 7.4

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

services:
mysql:
image: mysql:5.7.27
env:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_PASSWORD:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: test
ports:
- 3307:3306
volumes:
- $HOME/mysql:/var/lib/mysql
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout code
uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v1
with:
php-version: ${{ matrix.php }}
extensions: mbstring, dom, fileinfo, mysql
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
- name: Execute tests
env:
CI_DB_DRIVER: mysql
CI_DB_HOST: 127.0.0.1
CI_DB_PORT: 3307
CI_DB_DATABASE: test
CI_DB_USERNAME: root
CI_DB_PASSWORD: root
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor/
.DS_Store
composer.lock
.phpunit.result.cache
7 changes: 0 additions & 7 deletions .styleci.yml

This file was deleted.

35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 4.1.1 - 2020-01-11

**Fixed**

- Fixed inline attachments could not be stored
- Fixed PHP 7.4 issue when reading empty Mailable from address

## 4.1.0 - 2019-07-13

**Added**
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img src="/logo.png">
</p>
<p align="center">
<a href="https://travis-ci.org/stackkit/laravel-database-emails"><img src="https://travis-ci.org/stackkit/laravel-database-emails.svg?branch=master" alt="Build Status"></a>
<img src="https://github.com/marickvantuil/laravel-database-emails/workflows/Run tests/badge.svg" alt="Build Status">
<a href="https://packagist.org/packages/stackkit/laravel-database-emails"><img src="https://poser.pugx.org/stackkit/laravel-database-emails/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/stackkit/laravel-database-emails"><img src="https://poser.pugx.org/stackkit/laravel-database-emails/license.svg" alt="License"></a>
</p>
Expand Down
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
]
}
},
"require": {
"ext-json": "*"
},
"require-dev": {
"illuminate/database": "5.5.*",
"illuminate/console": "5.5.*",
"illuminate/validation": "5.5.*",
"orchestra/testbench": "^3.5",
"orchestra/database": "^3.5",
"phpunit/phpunit": "^6.0",
"mockery/mockery": "^1.0",
"dompdf/dompdf": "^0.8.2"
"mockery/mockery": "^1.2",
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0",
"symfony/console": "^4.4",
"tecnickcom/tcpdf": "^6.3"
}
}
20 changes: 19 additions & 1 deletion src/HasEncryptedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ trait HasEncryptedAttributes
'cc',
'bcc',
'variables',
'attachments',
];

/**
Expand Down Expand Up @@ -61,6 +60,25 @@ public function getAttribute($key)
}
}

// BC fix for attachments in 4.1.0 and lower.
// Attachments were stored json encoded.
// Because this doesn't work for raw attachments, value is now serialized.
// Check if value is json encoded or serialized, and decode or unserialize accordingly.
if ($key == 'attachments') {
if (substr($value, 0, 2) === 'a:') {
$unserialized = @unserialize($value);
if ($value !== false) {
$value = $unserialized;
}
} else {
$decoded = json_decode($value, true);

if (! is_null($decoded)) {
$value = $decoded;
}
}
}

return $value;
}
}
4 changes: 4 additions & 0 deletions src/MailableReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ private function readFrom(EmailComposer $composer)
{
$from = reset($composer->getData('mailable')->from);

if (!$from) {
return;
}

$composer->from(
$from['address'],
$from['name']
Expand Down
2 changes: 1 addition & 1 deletion src/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private function prepareAttachments(EmailComposer $composer)
}

$composer->getEmail()->fill([
'attachments' => json_encode($attachments),
'attachments' => serialize($attachments),
]);
}

Expand Down
21 changes: 13 additions & 8 deletions tests/DatabaseInteractionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Tests;

use Dompdf\Dompdf;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use TCPDF;

class DatabaseInteractionTest extends TestCase
{
Expand Down Expand Up @@ -85,9 +86,10 @@ public function scheduled_date_should_be_saved_correctly()
$this->assertNull(DB::table('emails')->find(1)->scheduled_at);
$this->assertNull($email->getScheduledDate());

Carbon::setTestNow(Carbon::create(2019, 1, 1, 1, 2, 3));
$email = $this->scheduleEmail('+2 weeks');
$this->assertNotNull(DB::table('emails')->find(2)->scheduled_at);
$this->assertEquals(date('Y-m-d H:i:s', strtotime('+2 weeks')), $email->getScheduledDate());
$this->assertEquals('2019-01-15 01:02:03', $email->getScheduledDate());
}

/** @test */
Expand Down Expand Up @@ -156,7 +158,9 @@ public function attempts_should_be_zero()
/** @test */
public function the_scheduled_date_should_be_saved_correctly()
{
$scheduledFor = date('Y-m-d H:i:s', strtotime('+2 weeks'));
Carbon::setTestNow(Carbon::now());

$scheduledFor = date('Y-m-d H:i:s', Carbon::now()->addWeek(2)->timestamp);

$email = $this->scheduleEmail('+2 weeks');

Expand Down Expand Up @@ -205,19 +209,20 @@ public function attachments_should_be_saved_correctly()
/** @test */
public function in_memory_attachments_should_be_saved_correctly()
{
$pdf = new Dompdf;
$pdf->loadHtml('Hello CI!');
$pdf->setPaper('A4', 'landscape');
$pdf = new TCPDF;
$pdf->Write(0, 'Hello CI!');

$rawData = $pdf->Output('generated.pdf', 'S');

$email = $this->composeEmail()
->attachData($pdf->outputHtml(), 'generated.pdf', [
->attachData($rawData, 'generated.pdf', [
'mime' => 'application/pdf',
])
->send();

$this->assertCount(1, $email->getAttachments());

$this->assertEquals('rawAttachment', $email->getAttachments()[0]['type']);
$this->assertEquals($pdf->outputHtml(), $email->getAttachments()[0]['attachment']['data']);
$this->assertEquals(md5($rawData), md5($email->getAttachments()[0]['attachment']['data']));
}
}
2 changes: 1 addition & 1 deletion tests/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class EncryptionTest extends TestCase
{
public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down
6 changes: 4 additions & 2 deletions tests/SendEmailsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent()

$this->artisan('email:send');

$this->assertEquals($firstSend = date('Y-m-d H:i:s'), $email->fresh()->getSendDate());
$this->assertNotNull($firstSend = $email->fresh()->getSendDate());

sleep(1);

$this->artisan('email:send');

Expand All @@ -55,7 +57,7 @@ public function if_an_email_fails_to_be_sent_it_should_be_logged_in_the_database
$this->artisan('email:send');

$this->assertTrue($email->fresh()->hasFailed());
$this->assertContains('Driver [does-not-exist] not supported.', $email->fresh()->getError());
$this->assertStringContains('Driver [does-not-exist] not supported.', $email->fresh()->getError());
}

/** @test */
Expand Down
31 changes: 23 additions & 8 deletions tests/SenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

namespace Tests;

use Dompdf\Dompdf;
use Swift_Events_SendEvent;
use Illuminate\Support\Facades\Mail;
use Stackkit\LaravelDatabaseEmails\Email;
use Stackkit\LaravelDatabaseEmails\Config;
use TCPDF;

class SenderTest extends TestCase
{
/** @var Swift_Events_SendEvent[] */
public $sent = [];

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -194,12 +193,13 @@ public function attachments_are_not_added_if_the_data_is_not_valid()
/** @test */
public function raw_attachments_are_added_to_the_email()
{
$pdf = new Dompdf;
$pdf->loadHtml('Hello CI!');
$pdf->setPaper('A4');
$pdf = new TCPDF;
$pdf->Write(0, 'Hello CI!');

$rawData = $pdf->Output('generated.pdf', 'S');

$this->composeEmail()
->attachData($pdf->outputHtml(), 'hello-ci.pdf', [
->attachData($rawData, 'hello-ci.pdf', [
'mime' => 'application/pdf',
])
->send();
Expand All @@ -211,7 +211,22 @@ public function raw_attachments_are_added_to_the_email()
$this->assertCount(1, $attachments);
$this->assertEquals('attachment; filename=hello-ci.pdf', $attachment->getHeaders()->get('content-disposition')->getFieldBody());
$this->assertEquals('application/pdf', $attachment->getContentType());
$this->assertContains('Hello CI!', $attachment->getBody());
$this->assertTrue(md5($attachment->getBody()) == md5($rawData));
}

/** @test */
public function old_json_encoded_attachments_can_still_be_read()
{
$email = $this->sendEmail();
$email->attachments = json_encode([1, 2, 3]);
$email->save();

$this->assertEquals([1, 2, 3], $email->fresh()->getAttachments());

$email->attachments = serialize([4, 5, 6]);
$email->save();

$this->assertEquals([4, 5, 6], $email->fresh()->getAttachments());
}

/** @test */
Expand Down
Loading