2019-07-19 23:55:44 +00:00
|
|
|
<?php
|
2019-11-07 08:12:13 +00:00
|
|
|
|
2022-03-17 00:04:40 +00:00
|
|
|
namespace Unit;
|
|
|
|
|
2019-07-19 23:55:44 +00:00
|
|
|
use App\Entity;
|
2022-03-21 00:39:42 +00:00
|
|
|
use App\Radio\AutoDJ\Scheduler;
|
|
|
|
use App\Tests\Module;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use Codeception\Test\Unit;
|
|
|
|
use DateTimeZone;
|
|
|
|
use Mockery;
|
|
|
|
use UnitTester;
|
|
|
|
|
|
|
|
class StationPlaylistTest extends Unit
|
2019-07-19 23:55:44 +00:00
|
|
|
{
|
2022-03-21 00:39:42 +00:00
|
|
|
protected UnitTester $tester;
|
2020-08-16 14:54:01 +00:00
|
|
|
|
2022-03-21 00:39:42 +00:00
|
|
|
protected Scheduler $scheduler;
|
2020-08-16 14:54:01 +00:00
|
|
|
|
2022-03-21 00:39:42 +00:00
|
|
|
protected function _inject(Module $tests_module): void
|
2020-08-16 14:54:01 +00:00
|
|
|
{
|
|
|
|
$di = $tests_module->container;
|
2022-03-21 00:39:42 +00:00
|
|
|
$this->scheduler = $di->get(Scheduler::class);
|
2020-08-16 14:54:01 +00:00
|
|
|
}
|
2019-07-19 23:55:44 +00:00
|
|
|
|
2021-06-14 03:30:17 +00:00
|
|
|
public function testScheduledPlaylist(): void
|
2019-07-19 23:55:44 +00:00
|
|
|
{
|
|
|
|
/** @var Entity\Station $station */
|
2022-03-21 00:39:42 +00:00
|
|
|
$station = Mockery::mock(Entity\Station::class);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
$playlist = new Entity\StationPlaylist($station);
|
2020-08-16 14:54:01 +00:00
|
|
|
$playlist->setName('Test Playlist');
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Sample playlist that plays from 10PM to 4AM the next day.
|
2020-02-18 04:32:14 +00:00
|
|
|
$scheduleEntry = new Entity\StationSchedule($playlist);
|
2019-11-07 08:12:13 +00:00
|
|
|
$scheduleEntry->setStartTime(2200);
|
|
|
|
$scheduleEntry->setEndTime(400);
|
|
|
|
$scheduleEntry->setDays([1, 2, 3]); // Monday, Tuesday, Wednesday
|
|
|
|
|
|
|
|
$playlist->getScheduleItems()->add($scheduleEntry);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
2022-03-21 00:39:42 +00:00
|
|
|
$utc = new DateTimeZone('UTC');
|
|
|
|
$test_monday = CarbonImmutable::create(2018, 1, 15, 0, 0, 0, $utc);
|
|
|
|
$test_thursday = CarbonImmutable::create(2018, 1, 18, 0, 0, 0, $utc);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Sanity check: Jan 15, 2018 is a Monday, and Jan 18, 2018 is a Thursday.
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertTrue($test_monday->isMonday());
|
|
|
|
self::assertTrue($test_thursday->isThursday());
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD play Monday evening at 10:30PM.
|
|
|
|
$test_time = $test_monday->setTime(22, 30);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD play Thursday morning at 3:00AM.
|
|
|
|
$test_time = $test_thursday->setTime(3, 0);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT play Monday morning at 3:00AM.
|
|
|
|
$test_time = $test_monday->setTime(3, 0);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT play Thursday evening at 10:30PM.
|
|
|
|
$test_time = $test_thursday->setTime(22, 30);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-20 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOncePerXMinutesPlaylist()
|
|
|
|
{
|
|
|
|
/** @var Entity\Station $station */
|
2022-03-21 00:39:42 +00:00
|
|
|
$station = Mockery::mock(Entity\Station::class);
|
2019-07-20 21:04:42 +00:00
|
|
|
|
|
|
|
$playlist = new Entity\StationPlaylist($station);
|
2020-08-16 14:54:01 +00:00
|
|
|
$playlist->setName('Test Playlist');
|
2022-01-07 08:26:40 +00:00
|
|
|
$playlist->setType(Entity\Enums\PlaylistTypes::OncePerXMinutes->value);
|
2019-07-20 21:04:42 +00:00
|
|
|
$playlist->setPlayPerMinutes(30);
|
|
|
|
|
2022-03-21 00:39:42 +00:00
|
|
|
$utc = new DateTimeZone('UTC');
|
|
|
|
$test_day = CarbonImmutable::create(2018, 1, 15, 0, 0, 0, $utc);
|
2019-07-20 21:04:42 +00:00
|
|
|
|
|
|
|
// Last played 20 minutes ago, SHOULD NOT play again.
|
2019-11-07 08:12:13 +00:00
|
|
|
$last_played = $test_day->addMinutes(0 - 20);
|
2019-07-20 21:04:42 +00:00
|
|
|
$playlist->setPlayedAt($last_played->getTimestamp());
|
|
|
|
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_day));
|
2019-07-20 21:04:42 +00:00
|
|
|
|
|
|
|
// Last played 40 minutes ago, SHOULD play again.
|
2019-11-07 08:12:13 +00:00
|
|
|
$last_played = $test_day->addMinutes(0 - 40);
|
2019-07-20 21:04:42 +00:00
|
|
|
$playlist->setPlayedAt($last_played->getTimestamp());
|
|
|
|
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_day));
|
2019-07-19 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOncePerHourPlaylist()
|
|
|
|
{
|
|
|
|
/** @var Entity\Station $station */
|
2022-03-21 00:39:42 +00:00
|
|
|
$station = Mockery::mock(Entity\Station::class);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
$playlist = new Entity\StationPlaylist($station);
|
2020-08-16 14:54:01 +00:00
|
|
|
$playlist->setName('Test Playlist');
|
2022-01-07 08:26:40 +00:00
|
|
|
$playlist->setType(Entity\Enums\PlaylistTypes::OncePerHour->value);
|
2019-07-19 23:55:44 +00:00
|
|
|
$playlist->setPlayPerHourMinute(50);
|
|
|
|
|
2022-03-21 00:39:42 +00:00
|
|
|
$utc = new DateTimeZone('UTC');
|
|
|
|
$test_day = CarbonImmutable::create(2018, 1, 15, 0, 0, 0, $utc);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD try to play at 11:59 PM.
|
|
|
|
$test_time = $test_day->setTime(23, 59);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD try to play at 12:04 PM.
|
|
|
|
$test_time = $test_day->setTime(12, 4);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT try to play at 11:49 PM.
|
|
|
|
$test_time = $test_day->setTime(23, 49);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT try to play at 12:06 PM.
|
|
|
|
$test_time = $test_day->setTime(12, 6);
|
2021-06-14 03:30:17 +00:00
|
|
|
self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
}
|
|
|
|
}
|