fix: notifications

This commit is contained in:
James Cole 2023-07-17 20:33:26 +02:00
parent 2bb4cc7954
commit 788dae1477
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
34 changed files with 192 additions and 99 deletions

View File

@ -379,16 +379,16 @@
},
{
"name": "friendsofphp/php-cs-fixer",
"version": "v3.21.1",
"version": "v3.22.0",
"source": {
"type": "git",
"url": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer.git",
"reference": "229b55b3eae4729a8e2a321441ba40fcb3720b86"
"reference": "92b019f6c8d79aa26349d0db7671d37440dc0ff3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/229b55b3eae4729a8e2a321441ba40fcb3720b86",
"reference": "229b55b3eae4729a8e2a321441ba40fcb3720b86",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/92b019f6c8d79aa26349d0db7671d37440dc0ff3",
"reference": "92b019f6c8d79aa26349d0db7671d37440dc0ff3",
"shasum": ""
},
"require": {
@ -398,7 +398,7 @@
"doctrine/lexer": "^2 || ^3",
"ext-json": "*",
"ext-tokenizer": "*",
"php": "^8.0.1",
"php": "^7.4 || ^8.0",
"sebastian/diff": "^4.0 || ^5.0",
"symfony/console": "^5.4 || ^6.0",
"symfony/event-dispatcher": "^5.4 || ^6.0",
@ -412,6 +412,7 @@
"symfony/stopwatch": "^5.4 || ^6.0"
},
"require-dev": {
"facile-it/paraunit": "^1.3 || ^2.0",
"justinrainbow/json-schema": "^5.2",
"keradus/cli-executor": "^2.0",
"mikey179/vfsstream": "^1.6.11",
@ -463,7 +464,7 @@
],
"support": {
"issues": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues",
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.21.1"
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.22.0"
},
"funding": [
{
@ -471,7 +472,7 @@
"type": "github"
}
],
"time": "2023-07-05T21:50:25+00:00"
"time": "2023-07-16T23:08:06+00:00"
},
{
"name": "psr/cache",

View File

@ -44,7 +44,8 @@ class ShowController extends Controller
/**
*
*/
public function __construct() {
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
@ -60,7 +61,8 @@ class ShowController extends Controller
* TODO add URL
*
*/
public function budgeted(DateRequest $request, Budget $budget): JsonResponse {
public function budgeted(DateRequest $request, Budget $budget): JsonResponse
{
$data = $request->getAll();
$result = $this->repository->budgetedInPeriodForBudget($budget, $data['start'], $data['end']);
$converted = $this->cerSum(array_values($result));
@ -73,7 +75,8 @@ class ShowController extends Controller
* TODO add URL
*
*/
public function spent(DateRequest $request, Budget $budget): JsonResponse {
public function spent(DateRequest $request, Budget $budget): JsonResponse
{
$data = $request->getAll();
$result = $this->repository->spentInPeriodForBudget($budget, $data['start'], $data['end']);
$converted = $this->cerSum(array_values($result));

View File

@ -35,7 +35,8 @@ trait ShowsFriendlyMessages
*
* @return void
*/
public function friendlyError(string $message): void {
public function friendlyError(string $message): void
{
$this->error(sprintf(' [x] %s', trim($message)));
}
@ -44,7 +45,8 @@ trait ShowsFriendlyMessages
*
* @return void
*/
public function friendlyInfo(string $message): void {
public function friendlyInfo(string $message): void
{
$this->friendlyNeutral($message);
}
@ -53,7 +55,8 @@ trait ShowsFriendlyMessages
*
* @return void
*/
public function friendlyNeutral(string $message): void {
public function friendlyNeutral(string $message): void
{
$this->line(sprintf(' [i] %s', trim($message)));
}
@ -62,7 +65,8 @@ trait ShowsFriendlyMessages
*
* @return void
*/
public function friendlyLine(string $message): void {
public function friendlyLine(string $message): void
{
$this->line(sprintf(' %s', trim($message)));
}
@ -71,7 +75,8 @@ trait ShowsFriendlyMessages
*
* @return void
*/
public function friendlyPositive(string $message): void {
public function friendlyPositive(string $message): void
{
$this->info(sprintf(' [✓] %s', trim($message)));
}
@ -80,7 +85,8 @@ trait ShowsFriendlyMessages
*
* @return void
*/
public function friendlyWarning(string $message): void {
public function friendlyWarning(string $message): void
{
$this->warn(sprintf(' [!] %s', trim($message)));
}

View File

@ -41,7 +41,8 @@ class Created extends Event
/**
* @param BudgetLimit $budgetLimit
*/
public function __construct(BudgetLimit $budgetLimit) {
public function __construct(BudgetLimit $budgetLimit)
{
$this->budgetLimit = $budgetLimit;
}
}

View File

@ -41,7 +41,8 @@ class Deleted extends Event
/**
* @param BudgetLimit $budgetLimit
*/
public function __construct(BudgetLimit $budgetLimit) {
public function __construct(BudgetLimit $budgetLimit)
{
$this->budgetLimit = $budgetLimit;
}
}

View File

@ -41,7 +41,8 @@ class Updated extends Event
/**
* @param BudgetLimit $budgetLimit
*/
public function __construct(BudgetLimit $budgetLimit) {
public function __construct(BudgetLimit $budgetLimit)
{
$this->budgetLimit = $budgetLimit;
}
}

View File

@ -49,7 +49,8 @@ class BudgetLimitHandler
*
* @return void
*/
public function created(Created $event): void {
public function created(Created $event): void
{
Log::debug(sprintf('BudgetLimitHandler::created(#%s)', $event->budgetLimit->id));
$this->updateAvailableBudget($event->budgetLimit);
}
@ -59,7 +60,8 @@ class BudgetLimitHandler
*
* @return void
*/
private function updateAvailableBudget(BudgetLimit $budgetLimit): void {
private function updateAvailableBudget(BudgetLimit $budgetLimit): void
{
Log::debug(sprintf('Now in updateAvailableBudget(#%d)', $budgetLimit->id));
// based on the view range of the user (month week quarter etc) the budget limit could
@ -145,7 +147,8 @@ class BudgetLimitHandler
*
* @return void
*/
private function calculateAmount(AvailableBudget $availableBudget): void {
private function calculateAmount(AvailableBudget $availableBudget): void
{
$repository = app(BudgetLimitRepositoryInterface::class);
$repository->setUser($availableBudget->user);
$newAmount = '0';
@ -173,8 +176,8 @@ class BudgetLimitHandler
);
// overlap in days:
$limitPeriod = Period::make(
$budgetLimit->start_date,
$budgetLimit->end_date,
$budgetLimit->start_date,
$budgetLimit->end_date,
precision : Precision::DAY(),
boundaries: Boundaries::EXCLUDE_NONE()
);
@ -210,13 +213,14 @@ class BudgetLimitHandler
*
* @return string
*/
private function getDailyAmount(BudgetLimit $budgetLimit): string {
private function getDailyAmount(BudgetLimit $budgetLimit): string
{
if (0 === (int)$budgetLimit->id) {
return '0';
}
$limitPeriod = Period::make(
$budgetLimit->start_date,
$budgetLimit->end_date,
$budgetLimit->start_date,
$budgetLimit->end_date,
precision : Precision::DAY(),
boundaries: Boundaries::EXCLUDE_NONE()
);
@ -233,7 +237,8 @@ class BudgetLimitHandler
*
* @return void
*/
public function deleted(Deleted $event): void {
public function deleted(Deleted $event): void
{
Log::debug(sprintf('BudgetLimitHandler::deleted(#%s)', $event->budgetLimit->id));
$budgetLimit = $event->budgetLimit;
$budgetLimit->id = null;
@ -245,7 +250,8 @@ class BudgetLimitHandler
*
* @return void
*/
public function updated(Updated $event): void {
public function updated(Updated $event): void
{
Log::debug(sprintf('BudgetLimitHandler::updated(#%s)', $event->budgetLimit->id));
$this->updateAvailableBudget($event->budgetLimit);
}

View File

@ -42,7 +42,8 @@ class TriggerRecurrenceRequest extends FormRequest
*
* @return array
*/
public function getAll(): array {
public function getAll(): array
{
return [
'date' => $this->getCarbonDate('date'),
];
@ -53,7 +54,8 @@ class TriggerRecurrenceRequest extends FormRequest
*
* @return array
*/
public function rules(): array {
public function rules(): array
{
return [
'date' => 'required|date',
];

View File

@ -97,6 +97,10 @@ class TestNotification extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -100,6 +100,10 @@ class UserInvitation extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -98,6 +98,10 @@ class UserRegistration extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -87,14 +87,14 @@ class VersionCheckResult extends Notification
public function toSlack($notifiable)
{
// return (new SlackMessage())->text($this->message)
// ->sectionBlock(function (SectionBlock $block) {
// $button = new ButtonElement('Button');
// $button->url('https://github.com/firefly-iii/firefly-iii/releases');
// $block->accessory($button);
// });
//// ->attachment(function ($attachment) {
//// $attachment->title('Firefly III @ GitHub', 'https://github.com/firefly-iii/firefly-iii/releases');
//// });
// ->sectionBlock(function (SectionBlock $block) {
// $button = new ButtonElement('Button');
// $button->url('https://github.com/firefly-iii/firefly-iii/releases');
// $block->accessory($button);
// });
//// ->attachment(function ($attachment) {
//// $attachment->title('Firefly III @ GitHub', 'https://github.com/firefly-iii/firefly-iii/releases');
//// });
return (new SlackMessage())->content($this->message)
@ -112,6 +112,10 @@ class VersionCheckResult extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -118,6 +118,10 @@ class BillReminder extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -94,6 +94,10 @@ class NewAccessToken extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -122,6 +122,10 @@ class UserLogin extends Notification
*/
public function via($notifiable)
{
return ['mail', 'slack'];
$slackUrl = (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) {
return ['mail', 'slack'];
}
return ['mail'];
}
}

View File

@ -38,7 +38,8 @@ class AccountRepository implements AccountRepositoryInterface
/**
* @inheritDoc
*/
public function searchAccount(string $query, array $types, int $limit): Collection {
public function searchAccount(string $query, array $types, int $limit): Collection
{
// search by group, not by user
$dbQuery = $this->userGroup->accounts()
->where('active', true)

View File

@ -46,7 +46,8 @@ class Calculator
* @return Carbon
* @throws IntervalException
*/
public function nextDateByInterval(Carbon $epoch, Periodicity $periodicity, int $skipInterval = 0): Carbon {
public function nextDateByInterval(Carbon $epoch, Periodicity $periodicity, int $skipInterval = 0): Carbon
{
if (!self::isAvailablePeriodicity($periodicity)) {
throw IntervalException::unavailable($periodicity, self::$intervals);
}
@ -62,7 +63,8 @@ class Calculator
*
* @return bool
*/
public function isAvailablePeriodicity(Periodicity $periodicity): bool {
public function isAvailablePeriodicity(Periodicity $periodicity): bool
{
return self::containsInterval($periodicity);
}
@ -71,14 +73,16 @@ class Calculator
*
* @return bool
*/
private static function containsInterval(Periodicity $periodicity): bool {
private static function containsInterval(Periodicity $periodicity): bool
{
return self::loadIntervalMap()->contains($periodicity);
}
/**
* @return SplObjectStorage
*/
private static function loadIntervalMap(): SplObjectStorage {
private static function loadIntervalMap(): SplObjectStorage
{
if (self::$intervalMap != null) {
return self::$intervalMap;
}
@ -96,7 +100,8 @@ class Calculator
*
* @return int
*/
private function skipInterval(int $skip): int {
private function skipInterval(int $skip): int
{
return self::DEFAULT_INTERVAL + $skip;
}

View File

@ -36,7 +36,7 @@ final class IntervalException extends Exception
{
public readonly array $availableIntervals;
public readonly Periodicity $periodicity;
protected $message = 'The periodicity %s is unknown. Choose one of available periodicity: %s';
protected $message = 'The periodicity %s is unknown. Choose one of available periodicity: %s';
/**
* @param Periodicity $periodicity

View File

@ -38,7 +38,8 @@ final class Daily extends Interval
*
* @return Carbon
*/
public function nextDate(Carbon $date, int $interval = 1): Carbon {
public function nextDate(Carbon $date, int $interval = 1): Carbon
{
return ($date->clone())->addDays($this->skip($interval));
}
}

View File

@ -37,7 +37,8 @@ abstract class Interval implements Interspacable
*
* @return int
*/
public function skip(int $skip): int {
public function skip(int $skip): int
{
return static::INTERVAL * $skip;
}
}

View File

@ -38,7 +38,8 @@ class Monthly extends Interval
*
* @return Carbon
*/
public function nextDate(Carbon $date, int $interval = 1): Carbon {
public function nextDate(Carbon $date, int $interval = 1): Carbon
{
return ($date->clone())->addMonthsNoOverflow($this->skip($interval));
}
}

View File

@ -38,7 +38,8 @@ class Weekly extends Interval
*
* @return Carbon
*/
public function nextDate(Carbon $date, int $interval = 1): Carbon {
public function nextDate(Carbon $date, int $interval = 1): Carbon
{
return ($date->clone())->addWeeks($this->skip($interval));
}
}

View File

@ -38,7 +38,8 @@ final class Yearly extends Interval
*
* @return Carbon
*/
public function nextDate(Carbon $date, int $interval = 1): Carbon {
public function nextDate(Carbon $date, int $interval = 1): Carbon
{
return ($date->clone())->addYearsNoOverflow($this->skip($interval));
}
}

View File

@ -43,7 +43,8 @@ trait AdministrationTrait
/**
* @return int
*/
public function getAdministrationId(): int {
public function getAdministrationId(): int
{
return $this->administrationId;
}
@ -52,7 +53,8 @@ trait AdministrationTrait
*
* @throws FireflyException
*/
public function setAdministrationId(int $administrationId): void {
public function setAdministrationId(int $administrationId): void
{
$this->administrationId = $administrationId;
$this->refreshAdministration();
}
@ -61,7 +63,8 @@ trait AdministrationTrait
* @return void
* @throws FireflyException
*/
private function refreshAdministration(): void {
private function refreshAdministration(): void
{
if (null !== $this->administrationId) {
$memberships = GroupMembership::where('user_id', $this->user->id)
->where('user_group_id', $this->administrationId)
@ -80,7 +83,8 @@ trait AdministrationTrait
*
* @return void
*/
public function setUser(Authenticatable | User | null $user): void {
public function setUser(Authenticatable | User | null $user): void
{
if (null !== $user) {
$this->user = $user;
}

View File

@ -53,7 +53,6 @@ use FireflyIII\Notifications\Admin\TestNotification;
use FireflyIII\Notifications\Admin\UserInvitation;
use FireflyIII\Notifications\Admin\UserRegistration;
use FireflyIII\Notifications\Admin\VersionCheckResult;
use FireflyIII\Notifications\User\NewAccessToken;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@ -487,9 +486,6 @@ class User extends Authenticatable
if ($notification instanceof VersionCheckResult) {
return app('fireflyconfig')->get('slack_webhook_url', '')->data;
}
if ($notification instanceof NewAccessToken) {
return app('fireflyconfig')->get('slack_webhook_url', '')->data;
}
if ($notification instanceof UserInvitation) {
return app('fireflyconfig')->get('slack_webhook_url', '')->data;
}

View File

@ -46,7 +46,8 @@ trait ValidatesAdministrationAccess
* @throws AuthenticationException
* @throws FireflyException
*/
protected function validateAdministration(Validator $validator, array $allowedRoles): void {
protected function validateAdministration(Validator $validator, array $allowedRoles): void
{
Log::debug('Now in validateAdministration()');
if (!auth()->check()) {
Log::error('User is not authenticated.');

View File

@ -37,14 +37,16 @@ readonly class CalculatorProvider
public Periodicity $periodicity;
public int $skip;
private function __construct(IntervalProvider $intervalProvider, Periodicity $periodicity, int $skip = 0) {
private function __construct(IntervalProvider $intervalProvider, Periodicity $periodicity, int $skip = 0)
{
$this->skip = $skip;
$this->intervalProvider = $intervalProvider;
$this->periodicity = $periodicity;
$this->label = "{$periodicity->name} {$intervalProvider->label}";
}
public static function providePeriodicityWithSkippedIntervals(): Generator {
public static function providePeriodicityWithSkippedIntervals(): Generator
{
$intervals = [
CalculatorProvider::from(Periodicity::Daily, new IntervalProvider(Carbon::now(), Carbon::now()->addDays(2)), 1),
CalculatorProvider::from(Periodicity::Daily, new IntervalProvider(Carbon::now(), Carbon::now()->addDays(3)), 2),
@ -117,15 +119,18 @@ readonly class CalculatorProvider
}
}
public static function from(Periodicity $periodicity, IntervalProvider $interval, int $skip = 0): CalculatorProvider {
public static function from(Periodicity $periodicity, IntervalProvider $interval, int $skip = 0): CalculatorProvider
{
return new self($interval, $periodicity, $skip);
}
public function epoch(): Carbon {
public function epoch(): Carbon
{
return $this->intervalProvider->epoch;
}
public function expected(): Carbon {
public function expected(): Carbon
{
return $this->intervalProvider->expected;
}
}

View File

@ -48,7 +48,8 @@ use Tests\unit\Support\Calendar\Periodicity\YearlyTest;
*/
class CalculatorTest extends TestCase
{
public static function provideAllPeriodicity(): Generator {
public static function provideAllPeriodicity(): Generator
{
$intervals = [];
$intervals = array_merge($intervals, self::convert(Periodicity::Daily, DailyTest::provideIntervals()));
$intervals = array_merge($intervals, self::convert(Periodicity::Weekly, WeeklyTest::provideIntervals()));
@ -65,7 +66,8 @@ class CalculatorTest extends TestCase
}
}
private static function convert(Periodicity $periodicity, array $intervals): array {
private static function convert(Periodicity $periodicity, array $intervals): array
{
$periodicityIntervals = [];
/** @var IntervalProvider $interval */
foreach ($intervals as $index => $interval) {
@ -76,7 +78,8 @@ class CalculatorTest extends TestCase
return $periodicityIntervals;
}
public static function provideSkippedIntervals(): Generator {
public static function provideSkippedIntervals(): Generator
{
return CalculatorProvider::providePeriodicityWithSkippedIntervals();
}
@ -84,7 +87,8 @@ class CalculatorTest extends TestCase
* @dataProvider provideAllPeriodicity
* @throws IntervalException
*/
public function testGivenADailyPeriodicityWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider) {
public function testGivenADailyPeriodicityWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider)
{
$calculator = new Calculator();
$period = $calculator->nextDateByInterval($provider->epoch(), $provider->periodicity);
$this->assertEquals($provider->expected()->toDateString(), $period->toDateString());
@ -94,7 +98,8 @@ class CalculatorTest extends TestCase
* @dataProvider provideSkippedIntervals
* @throws IntervalException
*/
public function testGivenAnEpochWithSkipIntervalNumberWhenCallTheNextDateBySkippedIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider) {
public function testGivenAnEpochWithSkipIntervalNumberWhenCallTheNextDateBySkippedIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider)
{
$calculator = new Calculator();
$period = $calculator->nextDateByInterval($provider->epoch(), $provider->periodicity, $provider->skip);
$this->assertEquals($provider->expected()->toDateString(), $period->toDateString());

View File

@ -37,11 +37,13 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class MonthlyTest extends IntervalTestCase
{
public static function factory(): Interval {
public static function factory(): Interval
{
return new Periodicity\Monthly();
}
public static function provideIntervals(): array {
public static function provideIntervals(): array
{
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addMonth(1)),
new IntervalProvider(Carbon::parse('2019-01-01'), Carbon::parse('2019-02-01')),

View File

@ -37,11 +37,13 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class QuarterlyTest extends IntervalTestCase
{
public static function factory(): Interval {
public static function factory(): Interval
{
return new Periodicity\Quarterly();
}
public static function provideIntervals(): array {
public static function provideIntervals(): array
{
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addMonths(3)),
new IntervalProvider(Carbon::parse('2019-01-29'), Carbon::parse('2019-04-29')),

View File

@ -37,11 +37,13 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class WeeklyTest extends IntervalTestCase
{
public static function factory(): Interval {
public static function factory(): Interval
{
return new Periodicity\Weekly();
}
public static function provideIntervals(): array {
public static function provideIntervals(): array
{
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addWeek()),
new IntervalProvider(Carbon::parse('2023-01-31'), Carbon::parse('2023-02-07')),

View File

@ -37,11 +37,13 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class YearlyTest extends IntervalTestCase
{
public static function factory(): Interval {
public static function factory(): Interval
{
return new Periodicity\Yearly();
}
public static function provideIntervals(): array {
public static function provideIntervals(): array
{
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addYears(1)),
new IntervalProvider(Carbon::parse('2019-01-29'), Carbon::parse('2020-01-29')),

View File

@ -39,12 +39,14 @@ class NavigationAddPeriodTest extends TestCase
{
private Navigation $navigation;
public function __construct(string $name) {
public function __construct(string $name)
{
parent::__construct($name);
$this->navigation = new Navigation();
}
public static function provideFrequencies(): array {
public static function provideFrequencies(): array
{
return [
Periodicity::Daily->name => ['periodicity' => Periodicity::Daily, 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
Periodicity::Weekly->name => ['periodicity' => Periodicity::Weekly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeeks(1)],
@ -71,7 +73,8 @@ class NavigationAddPeriodTest extends TestCase
];
}
public static function provideMonthPeriods(): array {
public static function provideMonthPeriods(): array
{
return [
'1M' => ['frequency' => '1M', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'month' => ['frequency' => 'month', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
@ -86,7 +89,8 @@ class NavigationAddPeriodTest extends TestCase
];
}
public static function providePeriods(): array {
public static function providePeriods(): array
{
return [
'1D' => ['frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
'daily' => ['frequency' => 'daily', 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
@ -111,7 +115,8 @@ class NavigationAddPeriodTest extends TestCase
];
}
public static function providePeriodsWithSkippingParam(): Generator {
public static function providePeriodsWithSkippingParam(): Generator
{
$intervals = [
'2019-01-31 to 2019-02-11' => ['skip' => 10, 'frequency' => 'daily', 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-02-11')],
'1D' => ['skip' => 1, 'frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::now()->addDays(2)],
@ -155,7 +160,8 @@ class NavigationAddPeriodTest extends TestCase
/**
* @dataProvider providePeriodsWithSkippingParam
*/
public function testGivenAFrequencyAndSkipIntervalWhenCalculateTheDateThenReturnsTheSkippedDateSuccessful(int $skip, string $frequency, Carbon $from, Carbon $expected) {
public function testGivenAFrequencyAndSkipIntervalWhenCalculateTheDateThenReturnsTheSkippedDateSuccessful(int $skip, string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->addPeriod($from, $frequency, $skip);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
@ -163,7 +169,8 @@ class NavigationAddPeriodTest extends TestCase
/**
* @dataProvider providePeriods
*/
public function testGivenAFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected) {
public function testGivenAFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->addPeriod($from, $frequency, 0);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
@ -171,7 +178,8 @@ class NavigationAddPeriodTest extends TestCase
/**
* @dataProvider provideFrequencies
*/
public function testGivenAIntervalWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(Periodicity $periodicity, Carbon $from, Carbon $expected) {
public function testGivenAIntervalWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(Periodicity $periodicity, Carbon $from, Carbon $expected)
{
$period = $this->navigation->nextDateByInterval($from, $periodicity);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
@ -179,7 +187,8 @@ class NavigationAddPeriodTest extends TestCase
/**
* @dataProvider provideMonthPeriods
*/
public function testGivenAMonthFrequencyWhenCalculateTheDateThenReturnsTheLastDayOfMonthSuccessful(string $frequency, Carbon $from, Carbon $expected) {
public function testGivenAMonthFrequencyWhenCalculateTheDateThenReturnsTheLastDayOfMonthSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->addPeriod($from, $frequency, 0);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}

View File

@ -38,12 +38,14 @@ class NavigationStartOfPeriodTest extends TestCase
{
private Navigation $navigation;
public function __construct(string $name) {
public function __construct(string $name)
{
parent::__construct($name);
$this->navigation = new Navigation();
}
public static function provideDates(): array {
public static function provideDates(): array
{
return [
'custom' => ['frequency' => 'custom', 'from' => Carbon::now(), 'expected' => Carbon::now()],
'1D' => ['frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::now()->startOfDay()],
@ -72,7 +74,8 @@ class NavigationStartOfPeriodTest extends TestCase
];
}
public static function provideUnknownFrequencies(): array {
public static function provideUnknownFrequencies(): array
{
return [
'1day' => ['frequency' => '1day', 'from' => Carbon::now(), 'expected' => Carbon::now()],
'unknown' => ['frequency' => 'unknown', 'from' => Carbon::now(), 'expected' => Carbon::now()->startOfDay()],
@ -83,7 +86,8 @@ class NavigationStartOfPeriodTest extends TestCase
/**
* @dataProvider provideDates
*/
public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected) {
public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->startOfPeriod($from, $frequency);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
@ -91,7 +95,8 @@ class NavigationStartOfPeriodTest extends TestCase
/**
* @dataProvider provideUnknownFrequencies
*/
public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected) {
public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
Log::shouldReceive('error')
->with(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $frequency))
->andReturnNull();