Skip to content

Commit d0e36ac

Browse files
Merge pull request #27 from stackkit/development
Fixed memory attachment bug and moved tests to Github Actions
2 parents f9bceb8 + d93cadb commit d0e36ac

16 files changed

+199
-115
lines changed

.github/workflows/run-tests.yml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Run tests
2+
3+
on:
4+
push:
5+
schedule:
6+
- cron: '0 0 * * *'
7+
8+
jobs:
9+
php-tests:
10+
runs-on: ${{ matrix.os }}
11+
12+
strategy:
13+
matrix:
14+
php: [7.4, 7.3, 7.2]
15+
laravel: [6.*, 5.8.*, 5.7.*, 5.6.*]
16+
os: [ubuntu-latest]
17+
include:
18+
- laravel: 6.*
19+
testbench: 4.*
20+
- laravel: 5.8.*
21+
testbench: 3.8.*
22+
- laravel: 5.7.*
23+
testbench: 3.7.*
24+
- laravel: 5.6.*
25+
testbench: 3.6.*
26+
exclude:
27+
- laravel: 5.7.*
28+
php: 7.4
29+
- laravel: 5.6.*
30+
php: 7.4
31+
- laravel: 5.5.*
32+
php: 7.4
33+
34+
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
35+
36+
services:
37+
mysql:
38+
image: mysql:5.7.27
39+
env:
40+
MYSQL_USER: root
41+
MYSQL_ROOT_PASSWORD: root
42+
MYSQL_PASSWORD:
43+
MYSQL_ALLOW_EMPTY_PASSWORD: true
44+
MYSQL_DATABASE: test
45+
ports:
46+
- 3307:3306
47+
volumes:
48+
- $HOME/mysql:/var/lib/mysql
49+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
50+
51+
steps:
52+
- name: Checkout code
53+
uses: actions/checkout@v1
54+
55+
- name: Setup PHP
56+
uses: shivammathur/setup-php@v1
57+
with:
58+
php-version: ${{ matrix.php }}
59+
extensions: mbstring, dom, fileinfo, mysql
60+
coverage: none
61+
62+
- name: Install dependencies
63+
run: |
64+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
65+
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
66+
- name: Execute tests
67+
env:
68+
CI_DB_DRIVER: mysql
69+
CI_DB_HOST: 127.0.0.1
70+
CI_DB_PORT: 3307
71+
CI_DB_DATABASE: test
72+
CI_DB_USERNAME: root
73+
CI_DB_PASSWORD: root
74+
run: vendor/bin/phpunit

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
vendor/
33
.DS_Store
44
composer.lock
5+
.phpunit.result.cache

.styleci.yml

-7
This file was deleted.

.travis.yml

-35
This file was deleted.

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## 4.1.1 - 2020-01-11
8+
9+
**Fixed**
10+
11+
- Fixed inline attachments could not be stored
12+
- Fixed PHP 7.4 issue when reading empty Mailable from address
13+
714
## 4.1.0 - 2019-07-13
815

916
**Added**

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="/logo.png">
33
</p>
44
<p align="center">
5-
<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>
5+
<img src="https://github.com/marickvantuil/laravel-database-emails/workflows/Run tests/badge.svg" alt="Build Status">
66
<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>
77
<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>
88
</p>

composer.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
]
2626
}
2727
},
28+
"require": {
29+
"ext-json": "*"
30+
},
2831
"require-dev": {
29-
"illuminate/database": "5.5.*",
30-
"illuminate/console": "5.5.*",
31-
"illuminate/validation": "5.5.*",
32-
"orchestra/testbench": "^3.5",
33-
"orchestra/database": "^3.5",
34-
"phpunit/phpunit": "^6.0",
35-
"mockery/mockery": "^1.0",
36-
"dompdf/dompdf": "^0.8.2"
32+
"mockery/mockery": "^1.2",
33+
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0",
34+
"symfony/console": "^4.4",
35+
"tecnickcom/tcpdf": "^6.3"
3736
}
3837
}

src/HasEncryptedAttributes.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ trait HasEncryptedAttributes
3232
'cc',
3333
'bcc',
3434
'variables',
35-
'attachments',
3635
];
3736

3837
/**
@@ -61,6 +60,25 @@ public function getAttribute($key)
6160
}
6261
}
6362

63+
// BC fix for attachments in 4.1.0 and lower.
64+
// Attachments were stored json encoded.
65+
// Because this doesn't work for raw attachments, value is now serialized.
66+
// Check if value is json encoded or serialized, and decode or unserialize accordingly.
67+
if ($key == 'attachments') {
68+
if (substr($value, 0, 2) === 'a:') {
69+
$unserialized = @unserialize($value);
70+
if ($value !== false) {
71+
$value = $unserialized;
72+
}
73+
} else {
74+
$decoded = json_decode($value, true);
75+
76+
if (! is_null($decoded)) {
77+
$value = $decoded;
78+
}
79+
}
80+
}
81+
6482
return $value;
6583
}
6684
}

src/MailableReader.php

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ private function readFrom(EmailComposer $composer)
6868
{
6969
$from = reset($composer->getData('mailable')->from);
7070

71+
if (!$from) {
72+
return;
73+
}
74+
7175
$composer->from(
7276
$from['address'],
7377
$from['name']

src/Preparer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private function prepareAttachments(EmailComposer $composer)
198198
}
199199

200200
$composer->getEmail()->fill([
201-
'attachments' => json_encode($attachments),
201+
'attachments' => serialize($attachments),
202202
]);
203203
}
204204

tests/DatabaseInteractionTest.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Tests;
44

5-
use Dompdf\Dompdf;
5+
use Carbon\Carbon;
66
use Illuminate\Support\Facades\DB;
7+
use TCPDF;
78

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

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

9395
/** @test */
@@ -156,7 +158,9 @@ public function attempts_should_be_zero()
156158
/** @test */
157159
public function the_scheduled_date_should_be_saved_correctly()
158160
{
159-
$scheduledFor = date('Y-m-d H:i:s', strtotime('+2 weeks'));
161+
Carbon::setTestNow(Carbon::now());
162+
163+
$scheduledFor = date('Y-m-d H:i:s', Carbon::now()->addWeek(2)->timestamp);
160164

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

@@ -205,19 +209,20 @@ public function attachments_should_be_saved_correctly()
205209
/** @test */
206210
public function in_memory_attachments_should_be_saved_correctly()
207211
{
208-
$pdf = new Dompdf;
209-
$pdf->loadHtml('Hello CI!');
210-
$pdf->setPaper('A4', 'landscape');
212+
$pdf = new TCPDF;
213+
$pdf->Write(0, 'Hello CI!');
214+
215+
$rawData = $pdf->Output('generated.pdf', 'S');
211216

212217
$email = $this->composeEmail()
213-
->attachData($pdf->outputHtml(), 'generated.pdf', [
218+
->attachData($rawData, 'generated.pdf', [
214219
'mime' => 'application/pdf',
215220
])
216221
->send();
217222

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

220225
$this->assertEquals('rawAttachment', $email->getAttachments()[0]['type']);
221-
$this->assertEquals($pdf->outputHtml(), $email->getAttachments()[0]['attachment']['data']);
226+
$this->assertEquals(md5($rawData), md5($email->getAttachments()[0]['attachment']['data']));
222227
}
223228
}

tests/EncryptionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class EncryptionTest extends TestCase
66
{
7-
public function setUp()
7+
public function setUp(): void
88
{
99
parent::setUp();
1010

tests/SendEmailsCommandTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent()
3737

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

40-
$this->assertEquals($firstSend = date('Y-m-d H:i:s'), $email->fresh()->getSendDate());
40+
$this->assertNotNull($firstSend = $email->fresh()->getSendDate());
41+
42+
sleep(1);
4143

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

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

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

6163
/** @test */

tests/SenderTest.php

+23-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Tests;
44

5-
use Dompdf\Dompdf;
65
use Swift_Events_SendEvent;
76
use Illuminate\Support\Facades\Mail;
87
use Stackkit\LaravelDatabaseEmails\Email;
9-
use Stackkit\LaravelDatabaseEmails\Config;
8+
use TCPDF;
109

1110
class SenderTest extends TestCase
1211
{
1312
/** @var Swift_Events_SendEvent[] */
1413
public $sent = [];
1514

16-
public function setUp()
15+
public function setUp(): void
1716
{
1817
parent::setUp();
1918

@@ -194,12 +193,13 @@ public function attachments_are_not_added_if_the_data_is_not_valid()
194193
/** @test */
195194
public function raw_attachments_are_added_to_the_email()
196195
{
197-
$pdf = new Dompdf;
198-
$pdf->loadHtml('Hello CI!');
199-
$pdf->setPaper('A4');
196+
$pdf = new TCPDF;
197+
$pdf->Write(0, 'Hello CI!');
198+
199+
$rawData = $pdf->Output('generated.pdf', 'S');
200200

201201
$this->composeEmail()
202-
->attachData($pdf->outputHtml(), 'hello-ci.pdf', [
202+
->attachData($rawData, 'hello-ci.pdf', [
203203
'mime' => 'application/pdf',
204204
])
205205
->send();
@@ -211,7 +211,22 @@ public function raw_attachments_are_added_to_the_email()
211211
$this->assertCount(1, $attachments);
212212
$this->assertEquals('attachment; filename=hello-ci.pdf', $attachment->getHeaders()->get('content-disposition')->getFieldBody());
213213
$this->assertEquals('application/pdf', $attachment->getContentType());
214-
$this->assertContains('Hello CI!', $attachment->getBody());
214+
$this->assertTrue(md5($attachment->getBody()) == md5($rawData));
215+
}
216+
217+
/** @test */
218+
public function old_json_encoded_attachments_can_still_be_read()
219+
{
220+
$email = $this->sendEmail();
221+
$email->attachments = json_encode([1, 2, 3]);
222+
$email->save();
223+
224+
$this->assertEquals([1, 2, 3], $email->fresh()->getAttachments());
225+
226+
$email->attachments = serialize([4, 5, 6]);
227+
$email->save();
228+
229+
$this->assertEquals([4, 5, 6], $email->fresh()->getAttachments());
215230
}
216231

217232
/** @test */

0 commit comments

Comments
 (0)