Skip to content

Firefly III: a personal finances manager

Can now also test event code.

James Colebbe40518e4e8
diff --git a/.env.testing b/.env.testing
index 5cf8784dad..2cafd4c2f8 100755
--- a/.env.testing
+++ b/.env.testing
@@ -26,7 +26,7 @@ REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
-MAIL_DRIVER=smtp
+MAIL_DRIVER=log
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php
index fd802a0fd2..4b02a2a455 100644
--- a/app/Handlers/Events/UserEventHandler.php
+++ b/app/Handlers/Events/UserEventHandler.php
@@ -16,8 +16,8 @@ namespace FireflyIII\Handlers\Events;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
+use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
use FireflyIII\Repositories\User\UserRepositoryInterface;
-use Illuminate\Mail\Message;
use Log;
use Mail;
use Swift_TransportException;
@@ -69,14 +69,12 @@ class UserEventHandler
// send email.
try {
- Mail::send(
- ['emails.password-html', 'emails.password-text'], ['url' => $url, 'ip' => $ipAddress], function (Message $message) use ($email) {
- $message->to($email, $email)->subject('Your password reset request');
- }
- );
+ Mail::to($email)->send(new RequestedNewPasswordMail($url, $ipAddress));
+ // @codeCoverageIgnoreStart
} catch (Swift_TransportException $e) {
Log::error($e->getMessage());
}
+ // @codeCoverageIgnoreEnd
return true;
}
@@ -94,7 +92,7 @@ class UserEventHandler
$sendMail = env('SEND_REGISTRATION_MAIL', true);
if (!$sendMail) {
- return true;
+ return true; // @codeCoverageIgnore
}
// get the email address
$email = $event->user->email;
@@ -103,12 +101,12 @@ class UserEventHandler
// send email.
try {
-
- Mail::to($email)
- ->send(new RegisteredUserMail($address, $ipAddress));
+ Mail::to($email)->send(new RegisteredUserMail($address, $ipAddress));
+ // @codeCoverageIgnoreStart
} catch (Swift_TransportException $e) {
Log::error($e->getMessage());
}
+ // @codeCoverageIgnoreEnd
return true;
}
diff --git a/app/Mail/RequestedNewPassword.php b/app/Mail/RequestedNewPassword.php
new file mode 100644
index 0000000000..b97a47c50f
--- /dev/null
+++ b/app/Mail/RequestedNewPassword.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace FireflyIII\Mail;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Queue\SerializesModels;
+
+class RequestedNewPassword extends Mailable
+{
+ use Queueable, SerializesModels;
+ /** @var string */
+ public $ip;
+ /** @var string */
+ public $url;
+
+ /**
+ * RequestedNewPassword constructor.
+ *
+ * @param string $url
+ * @param string $ip
+ */
+ public function __construct(string $url, string $ip)
+ {
+ $this->url = $url;
+ $this->ip = $ip;
+ }
+
+ /**
+ * Build the message.
+ *
+ * @return $this
+ */
+ public function build()
+ {
+ return $this->view('emails.password-html')->text('emails.password-text')->subject('Your password reset request');
+ }
+}
diff --git a/test.sh b/test.sh
index 01a8be87ad..177dc25cb9 100755
--- a/test.sh
+++ b/test.sh
@@ -10,13 +10,18 @@ TESTINGENV=./.env.testing
resetTestFlag=''
testflag=''
coverageflag=''
+
featureflag=''
featuretestclass=''
+
+unitflag=''
+unittestclass=''
+
verbalflag=''
testsuite=''
configfile='phpunit.xml';
-while getopts 'vcrtf:s:' flag; do
+while getopts 'vcrtf:u:s:' flag; do
case "${flag}" in
r)
resetTestFlag='true'
@@ -37,6 +42,11 @@ while getopts 'vcrtf:s:' flag; do
featuretestclass=./tests/Feature/$OPTARG
echo "Will only run Feature test $OPTARG"
;;
+ u)
+ unitflag='true'
+ unittestclass=./tests/Unit/$OPTARG
+ echo "Will only run Unit test $OPTARG"
+ ;;
s)
testsuite="--testsuite $OPTARG"
echo "Will only run test suite '$OPTARG'"
@@ -45,7 +55,7 @@ while getopts 'vcrtf:s:' flag; do
esac
done
-if [[ $coverageflag == "true" && $featureflag == "true" ]]
+if [[ $coverageflag == "true" && ($featureflag == "true" || $unitflag == "true") ]]
then
echo "Use config file specific.xml"
configfile='phpunit.coverage.specific.xml'
@@ -109,12 +119,12 @@ else
then
echo "Must run PHPUnit without coverage:"
- echo "phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite"
- phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite
+ echo "phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite"
+ phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite
else
echo "Must run PHPUnit with coverage"
- echo "phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite"
- phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite
+ echo "phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite"
+ phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite
fi
fi
diff --git a/tests/Unit/Handlers/Events/UserEventHandlerTest.php b/tests/Unit/Handlers/Events/UserEventHandlerTest.php
new file mode 100644
index 0000000000..0edb4c3938
--- /dev/null
+++ b/tests/Unit/Handlers/Events/UserEventHandlerTest.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ * UserEventHandlerTest.php
+ * Copyright (c) 2017 [email protected]
+ * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Unit\Handlers\Events;
+
+
+use FireflyIII\Events\RegisteredUser;
+use FireflyIII\Events\RequestedNewPassword;
+use FireflyIII\Handlers\Events\UserEventHandler;
+use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
+use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
+use FireflyIII\Repositories\User\UserRepositoryInterface;
+use Illuminate\Support\Facades\Mail;
+use Tests\TestCase;
+
+class UserEventHandlerTest extends TestCase
+{
+ /**
+ * @covers \FireflyIII\Handlers\Events\UserEventHandler::attachUserRole
+ * @covers \FireflyIII\Events\RegisteredUser
+ */
+ public function testAttachUserRole()
+ {
+ $repository = $this->mock(UserRepositoryInterface::class);
+ $user = $this->emptyUser();
+ $event = new RegisteredUser($user, '127.0.0.1');
+
+ $repository->shouldReceive('count')->andReturn(1)->once();
+ $repository->shouldReceive('attachRole')->withArgs([$user, 'owner'])->andReturn(true)->once();
+ $listener = new UserEventHandler();
+ $listener->attachUserRole($event);
+ $this->assertTrue(true);
+ }
+
+ /**
+ * @covers \FireflyIII\Handlers\Events\UserEventHandler::sendNewPassword
+ * @covers \FireflyIII\Events\RequestedNewPassword
+ */
+ public function testSendNewPassword()
+ {
+ Mail::fake();
+ $user = $this->emptyUser();
+ $event = new RequestedNewPassword($user, 'token', '127.0.0.1');
+ $listener = new UserEventHandler;
+ $listener->sendNewPassword($event);
+
+ // must send user an email:
+
+ Mail::assertSent(
+ RequestedNewPasswordMail::class, function ($mail) use ($user) {
+ return $mail->hasTo($user->email) && $mail->ip === '127.0.0.1';
+ }
+ );
+
+ $this->assertTrue(true);
+ }
+
+ /**
+ * @covers \FireflyIII\Handlers\Events\UserEventHandler::sendRegistrationMail
+ * @covers \FireflyIII\Events\RegisteredUser
+ */
+ public function testSendRegistrationMail() {
+ Mail::fake();
+ $user = $this->emptyUser();
+ $event = new RegisteredUser($user, '127.0.0.1');
+
+ $listener = new UserEventHandler;
+ $listener->sendRegistrationMail($event);
+
+ // must send user an email:
+ Mail::assertSent(
+ RegisteredUserMail::class, function ($mail) use ($user) {
+ return $mail->hasTo($user->email) && $mail->ip === '127.0.0.1';
+ }
+ );
+
+ $this->assertTrue(true);
+ }
+
+}
\ No newline at end of file