mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 16:57:09 +00:00
Can now also test event code.
This commit is contained in:
@@ -26,7 +26,7 @@ REDIS_HOST=127.0.0.1
|
|||||||
REDIS_PASSWORD=null
|
REDIS_PASSWORD=null
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
|
|
||||||
MAIL_DRIVER=smtp
|
MAIL_DRIVER=log
|
||||||
MAIL_HOST=mailtrap.io
|
MAIL_HOST=mailtrap.io
|
||||||
MAIL_PORT=2525
|
MAIL_PORT=2525
|
||||||
MAIL_FROM=changeme@example.com
|
MAIL_FROM=changeme@example.com
|
||||||
|
@@ -16,8 +16,8 @@ namespace FireflyIII\Handlers\Events;
|
|||||||
use FireflyIII\Events\RegisteredUser;
|
use FireflyIII\Events\RegisteredUser;
|
||||||
use FireflyIII\Events\RequestedNewPassword;
|
use FireflyIII\Events\RequestedNewPassword;
|
||||||
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
|
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
|
||||||
|
use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
|
||||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||||
use Illuminate\Mail\Message;
|
|
||||||
use Log;
|
use Log;
|
||||||
use Mail;
|
use Mail;
|
||||||
use Swift_TransportException;
|
use Swift_TransportException;
|
||||||
@@ -69,14 +69,12 @@ class UserEventHandler
|
|||||||
|
|
||||||
// send email.
|
// send email.
|
||||||
try {
|
try {
|
||||||
Mail::send(
|
Mail::to($email)->send(new RequestedNewPasswordMail($url, $ipAddress));
|
||||||
['emails.password-html', 'emails.password-text'], ['url' => $url, 'ip' => $ipAddress], function (Message $message) use ($email) {
|
// @codeCoverageIgnoreStart
|
||||||
$message->to($email, $email)->subject('Your password reset request');
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (Swift_TransportException $e) {
|
} catch (Swift_TransportException $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -94,7 +92,7 @@ class UserEventHandler
|
|||||||
|
|
||||||
$sendMail = env('SEND_REGISTRATION_MAIL', true);
|
$sendMail = env('SEND_REGISTRATION_MAIL', true);
|
||||||
if (!$sendMail) {
|
if (!$sendMail) {
|
||||||
return true;
|
return true; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
// get the email address
|
// get the email address
|
||||||
$email = $event->user->email;
|
$email = $event->user->email;
|
||||||
@@ -103,12 +101,12 @@ class UserEventHandler
|
|||||||
|
|
||||||
// send email.
|
// send email.
|
||||||
try {
|
try {
|
||||||
|
Mail::to($email)->send(new RegisteredUserMail($address, $ipAddress));
|
||||||
Mail::to($email)
|
// @codeCoverageIgnoreStart
|
||||||
->send(new RegisteredUserMail($address, $ipAddress));
|
|
||||||
} catch (Swift_TransportException $e) {
|
} catch (Swift_TransportException $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
38
app/Mail/RequestedNewPassword.php
Normal file
38
app/Mail/RequestedNewPassword.php
Normal file
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
22
test.sh
22
test.sh
@@ -10,13 +10,18 @@ TESTINGENV=./.env.testing
|
|||||||
resetTestFlag=''
|
resetTestFlag=''
|
||||||
testflag=''
|
testflag=''
|
||||||
coverageflag=''
|
coverageflag=''
|
||||||
|
|
||||||
featureflag=''
|
featureflag=''
|
||||||
featuretestclass=''
|
featuretestclass=''
|
||||||
|
|
||||||
|
unitflag=''
|
||||||
|
unittestclass=''
|
||||||
|
|
||||||
verbalflag=''
|
verbalflag=''
|
||||||
testsuite=''
|
testsuite=''
|
||||||
configfile='phpunit.xml';
|
configfile='phpunit.xml';
|
||||||
|
|
||||||
while getopts 'vcrtf:s:' flag; do
|
while getopts 'vcrtf:u:s:' flag; do
|
||||||
case "${flag}" in
|
case "${flag}" in
|
||||||
r)
|
r)
|
||||||
resetTestFlag='true'
|
resetTestFlag='true'
|
||||||
@@ -37,6 +42,11 @@ while getopts 'vcrtf:s:' flag; do
|
|||||||
featuretestclass=./tests/Feature/$OPTARG
|
featuretestclass=./tests/Feature/$OPTARG
|
||||||
echo "Will only run Feature test $OPTARG"
|
echo "Will only run Feature test $OPTARG"
|
||||||
;;
|
;;
|
||||||
|
u)
|
||||||
|
unitflag='true'
|
||||||
|
unittestclass=./tests/Unit/$OPTARG
|
||||||
|
echo "Will only run Unit test $OPTARG"
|
||||||
|
;;
|
||||||
s)
|
s)
|
||||||
testsuite="--testsuite $OPTARG"
|
testsuite="--testsuite $OPTARG"
|
||||||
echo "Will only run test suite '$OPTARG'"
|
echo "Will only run test suite '$OPTARG'"
|
||||||
@@ -45,7 +55,7 @@ while getopts 'vcrtf:s:' flag; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ $coverageflag == "true" && $featureflag == "true" ]]
|
if [[ $coverageflag == "true" && ($featureflag == "true" || $unitflag == "true") ]]
|
||||||
then
|
then
|
||||||
echo "Use config file specific.xml"
|
echo "Use config file specific.xml"
|
||||||
configfile='phpunit.coverage.specific.xml'
|
configfile='phpunit.coverage.specific.xml'
|
||||||
@@ -109,12 +119,12 @@ else
|
|||||||
then
|
then
|
||||||
echo "Must run PHPUnit without coverage:"
|
echo "Must run PHPUnit without coverage:"
|
||||||
|
|
||||||
echo "phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite"
|
echo "phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite"
|
||||||
phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite
|
phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite
|
||||||
else
|
else
|
||||||
echo "Must run PHPUnit with coverage"
|
echo "Must run PHPUnit with coverage"
|
||||||
echo "phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite"
|
echo "phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite"
|
||||||
phpunit $verbalflag --configuration $configfile $featuretestclass $testsuite
|
phpunit $verbalflag --configuration $configfile $featuretestclass $unittestclass $testsuite
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
88
tests/Unit/Handlers/Events/UserEventHandlerTest.php
Normal file
88
tests/Unit/Handlers/Events/UserEventHandlerTest.php
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* UserEventHandlerTest.php
|
||||||
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user