Skip to content
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

Getter for 'from', 'to' event's configuration #68

Merged
merged 15 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
30 changes: 30 additions & 0 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ class Event implements PingableInterface
*/
protected $timezone;

/**
* Datetime or time since the task is evaluated and possibly executed only for display purposes.
*/
protected \DateTime|string|null $from = null;

/**
* Datetime or time until the task is evaluated and possibly executed only for display purposes.
*/
protected \DateTime|string|null $to = null;

/**
* The user the command should run as.
*
Expand Down Expand Up @@ -447,6 +457,8 @@ public function between($from, $to)
*/
public function from($datetime)
{
$this->from = $datetime;

return $this->skip(fn () => $this->notYet($datetime));
}

Expand All @@ -459,6 +471,8 @@ public function from($datetime)
*/
public function to($datetime)
{
$this->to = $datetime;

return $this->skip(fn () => $this->past($datetime));
}

Expand Down Expand Up @@ -921,6 +935,22 @@ public function getExpression()
return $this->expression;
}

/**
* Get the 'from' configuration for the event if present.
*/
public function getFrom(): \DateTime|string|null
{
return $this->from;
}

/**
* Get the 'to' configuration for the event if present.
*/
public function getTo(): \DateTime|string|null
{
return $this->to;
}

/**
* Set the event's command.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,50 @@ public function test_cron_life_time(): void
);
}

public function test_cron_life_time_get_from(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there :)

  • name test cases like test_get_from and test_get_to
  • test between in other test case
  • use data provider (can be one for both cases) to test string and \DateTime in from/to
  • use camelCase for variables' names
  • use assertSame in cases like self::assertTrue($date_from === $event->getFrom());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases renamed.
Test case between introduced.
Variables' name renamed in camelCase.

Do you prefer to have the dataproviders all together at the end of the controls file near the other one or near the functions that use them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer to have the dataproviders all together at the end of the controls file near the other one or near the functions that use them?

End of file, before private methods, please.

{
$timezone = new \DateTimeZone('UTC');

$date_from = '2023-10-01';
$date_to = '2023-10-30';

$event = new Event($this->id, 'php foo');
$event->between($date_from, $date_to);

self::assertTrue(
$date_from === $event->getFrom()
);

$event = new Event($this->id, 'php foo');
$event->from($date_from);

self::assertTrue(
$date_from === $event->getFrom()
);
}

public function test_cron_life_time_get_to(): void
{
$timezone = new \DateTimeZone('UTC');

$date_from = '2023-10-01';
$date_to = '2023-10-30';

$event = new Event($this->id, 'php foo');
$event->between($date_from, $date_to);

self::assertTrue(
$date_to === $event->getTo()
);

$event = new Event($this->id, 'php foo');
$event->to($date_to);

self::assertTrue(
$date_to === $event->getTo()
);
}

public function test_cron_conditions(): void
{
$timezone = new \DateTimeZone('UTC');
Expand Down