Skip to content

Commit

Permalink
Added some old mutable DateTime in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Giarelli committed Aug 3, 2021
1 parent 6855bf5 commit 456a925
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 121 deletions.
98 changes: 26 additions & 72 deletions src/Calendar.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
<?php

/*
* This file is part of CalendR, a Fréquence web project.
*
* (c) 2012 Fréquence web
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CalendR;

use CalendR\Event\Collection\CollectionInterface;
use CalendR\Event\EventInterface;
use CalendR\Event\Exception\NoProviderFound;
use CalendR\Event\Manager;
use CalendR\Period\Day;
use CalendR\Period\Factory;
use CalendR\Period\FactoryInterface;
use CalendR\Period\Hour;
use CalendR\Period\Minute;
use CalendR\Period\Month;
use CalendR\Period\PeriodInterface;
use CalendR\Period\Second;
use CalendR\Period\Week;

/**
Expand All @@ -29,28 +22,16 @@
*/
class Calendar
{
/**
* @var Manager
*/
private $eventManager;
private ?Manager $eventManager = null;

/**
* @var FactoryInterface
*/
protected $factory;
protected ?FactoryInterface $factory = null;

/**
* @param Manager $eventManager
*/
public function setEventManager(Manager $eventManager)
public function setEventManager(Manager $eventManager): void
{
$this->eventManager = $eventManager;
}

/**
* @return Manager
*/
public function getEventManager()
public function getEventManager(): Manager
{
if (null === $this->eventManager) {
$this->eventManager = new Manager();
Expand All @@ -61,17 +42,17 @@ public function getEventManager()

public function getYear($yearOrStart): Period\Year
{
if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(sprintf('%s-01-01', $yearOrStart));
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(sprintf('%s-01-01', $yearOrStart));
}

return $this->getFactory()->createYear($yearOrStart);
}

public function getMonth($yearOrStart, ?int $month = null): Month
{
if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(sprintf('%s-%s-01', $yearOrStart, $month));
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(sprintf('%s-%s-01', $yearOrStart, $month));
}

return $this->getFactory()->createMonth($yearOrStart);
Expand All @@ -81,65 +62,44 @@ public function getWeek($yearOrStart, ?int $week = null): Week
{
$factory = $this->getFactory();

if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(sprintf('%s-W%s', $yearOrStart, str_pad($week, 2, 0, STR_PAD_LEFT)));
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(sprintf('%s-W%s', $yearOrStart, str_pad($week, 2, 0, STR_PAD_LEFT)));
}

return $factory->createWeek($factory->findFirstDayOfWeek($yearOrStart));
}

public function getDay($yearOrStart, ?int $month = null, ?int $day = null): Day
{
if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(sprintf('%s-%s-%s', $yearOrStart, $month, $day));
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(sprintf('%s-%s-%s', $yearOrStart, $month, $day));
}

return $this->getFactory()->createDay($yearOrStart);
}

/**
* @param \DateTime|int $yearOrStart
* @param null|int $month
* @param null|int $day
*
* @return PeriodInterface
*/
public function getHour($yearOrStart, $month = null, $day = null, $hour = null)
public function getHour($yearOrStart, ?int $month = null, ?int $day = null, ?int $hour = null): Hour
{
if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(sprintf('%s-%s-%s %s:00', $yearOrStart, $month, $day, $hour));
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(sprintf('%s-%s-%s %s:00', $yearOrStart, $month, $day, $hour));
}

return $this->getFactory()->createHour($yearOrStart);
}

/**
* @param \DateTime|int $yearOrStart
* @param null|int $month
* @param null|int $day
*
* @return PeriodInterface
*/
public function getMinute($yearOrStart, $month = null, $day = null, $hour = null, $minute = null)
public function getMinute($yearOrStart, $month = null, $day = null, $hour = null, $minute = null): Minute
{
if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(sprintf('%s-%s-%s %s:%s', $yearOrStart, $month, $day, $hour, $minute));
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(sprintf('%s-%s-%s %s:%s', $yearOrStart, $month, $day, $hour, $minute));
}

return $this->getFactory()->createMinute($yearOrStart);
}

/**
* @param \DateTime|int $yearOrStart
* @param null|int $month
* @param null|int $day
*
* @return PeriodInterface
*/
public function getSecond($yearOrStart, $month = null, $day = null, $hour = null, $minute = null, $second = null)
public function getSecond($yearOrStart, ?int $month = null, ?int $day = null, ?int $hour = null, ?int $minute = null, ?int $second = null): Second
{
if (!$yearOrStart instanceof \DateTime) {
$yearOrStart = new \DateTime(
if (!$yearOrStart instanceof \DateTimeInterface) {
$yearOrStart = new \DateTimeImmutable(
sprintf('%s-%s-%s %s:%s:%s', $yearOrStart, $month, $day, $hour, $minute, $second)
);
}
Expand All @@ -155,18 +115,12 @@ public function getEvents(PeriodInterface $period, array $options = []): Collect
return $this->getEventManager()->find($period, $options);
}

/**
* @param FactoryInterface $factory
*/
public function setFactory(FactoryInterface $factory)
public function setFactory(FactoryInterface $factory): void
{
$this->factory = $factory;
}

/**
* @return FactoryInterface
*/
public function getFactory()
public function getFactory(): FactoryInterface
{
if (null === $this->factory) {
$this->factory = new Factory();
Expand Down
16 changes: 8 additions & 8 deletions src/Period/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ interface FactoryInterface
/**
* Create and return a Second.
*/
public function createSecond(\DateTimeInterface $begin): PeriodInterface;
public function createSecond(\DateTimeInterface $begin): Second;

/**
* Create and return a Minute.
*/
public function createMinute(\DateTimeInterface $begin): PeriodInterface;
public function createMinute(\DateTimeInterface $begin): Minute;

/**
* Create and return an Hour.
*/
public function createHour(\DateTimeInterface $begin): PeriodInterface;
public function createHour(\DateTimeInterface $begin): Hour;

/**
* Create and return a Day.
*/
public function createDay(\DateTimeInterface $begin): PeriodInterface;
public function createDay(\DateTimeInterface $begin): Day;

/**
* Create and return a Week.
*/
public function createWeek(\DateTimeInterface $begin): PeriodInterface;
public function createWeek(\DateTimeInterface $begin): Week;

/**
* Create and return a Month.
*/
public function createMonth(\DateTimeInterface $begin): PeriodInterface;
public function createMonth(\DateTimeInterface $begin): Month;

/**
* Create and return a Year.
*/
public function createYear(\DateTimeInterface $begin): PeriodInterface;
public function createYear(\DateTimeInterface $begin): Year;

/**
* Create and return a Range.
*/
public function createRange(\DateTimeInterface $begin, \DateTimeInterface $end): PeriodInterface;
public function createRange(\DateTimeInterface $begin, \DateTimeInterface $end): Range;

/**
* Define the first day of week (e.g. Monday in France and Sunday in U.S.)
Expand Down
72 changes: 37 additions & 35 deletions tests/Period/AlternatePeriodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,86 @@
use CalendR\Period\Month;
use CalendR\Period\Week;
use CalendR\Period\Year;
use CalendR\Test\Fixtures\Period\Month as FixtureMonth;
use CalendR\Test\Fixtures\Period\Week as FixtureWeek;
use CalendR\Test\Fixtures\Period\Year as FixtureYear;
use PHPUnit\Framework\TestCase;
use CalendR\Test\Fixtures\Period\Day;
use CalendR\Test\Fixtures\Period\Range;

class AlternatePeriodsTest extends TestCase
{
protected $options = array(
'day_class' => 'CalendR\Test\Fixtures\Period\Day',
'week_class' => 'CalendR\Test\Fixtures\Period\Week',
'month_class' => 'CalendR\Test\Fixtures\Period\Month',
'year_class' => 'CalendR\Test\Fixtures\Period\Year',
'range_class' => 'CalendR\Test\Fixtures\Period\Range',
);
protected array $options = [
'day_class' => Day::class,
'week_class' => FixtureWeek::class,
'month_class' => FixtureMonth::class,
'year_class' => FixtureYear::class,
'range_class' => Range::class,
];

protected $periodFactory;

/** @var $calendar Calendar */
protected $calendar;
protected Calendar $calendar;

protected function setUp(): void
{
$this->calendar = new Calendar();
$this->calendar->setFactory(new Factory($this->options));
}

public function testCalendar()
public function testCalendar(): void
{
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Year', $this->calendar->getYear(2013));
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Month', $this->calendar->getMonth(2013, 1));
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Week', $this->calendar->getWeek(2013, 1));
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Day', $this->calendar->getDay(2013, 1, 1));
$this->assertInstanceOf(FixtureYear::class, $this->calendar->getYear(2013));
$this->assertInstanceOf(FixtureMonth::class, $this->calendar->getMonth(2013, 1));
$this->assertInstanceOf(FixtureWeek::class, $this->calendar->getWeek(2013, 1));
$this->assertInstanceOf(Day::class, $this->calendar->getDay(2013, 1, 1));
}

public function testCalendarSetOptions()
public function testCalendarSetOptions(): void
{
$options = array('week_class' => 'CalendR\Test\Fixtures\Period\Week');
$options = ['week_class' => FixtureWeek::class];
$calendar = new Calendar;
$calendar->setFactory(new Factory($options));
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Week', $calendar->getWeek(new \DateTime('2012W01')));
$this->assertInstanceOf(FixtureWeek::class, $calendar->getWeek(new \DateTimeImmutable('2012W01')));
}

public function testCalendarSetOption()
public function testCalendarSetOption(): void
{
$calendar = new Calendar();
$calendar->setFactory(new Factory(array('week_class' => 'CalendR\Test\Fixtures\Period\Week')));
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Week', $calendar->getWeek(new \DateTime('2012W01')));
$calendar->setFactory(new Factory(['week_class' => FixtureWeek::class]));
$this->assertInstanceOf(FixtureWeek::class, $calendar->getWeek(new \DateTimeImmutable('2012W01')));
}

public function testCalendarGetOption()
public function testCalendarGetOption(): void
{
$calendar = new Calendar();
$this->assertEquals(1, $calendar->getFactory()->getFirstWeekday());
$calendar->setFactory(new Factory(array('first_weekday' => 0)));
$calendar->setFactory(new Factory(['first_weekday' => 0]));
$this->assertEquals(0, $calendar->getFactory()->getFirstWeekday());
}

public function testYear()
public function testYear(): void
{
$year = new Year(new \DateTime('2013-01-01'), new Factory($this->options));
$year = new Year(new \DateTimeImmutable('2013-01-01'), new Factory($this->options));
foreach ($year as $month) {
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Month', $month);
$this->assertInstanceOf(FixtureMonth::class, $month);
}
}

public function testMonth()
public function testMonth(): void
{
$month = new Month(new \DateTime('2013-01-01'), new Factory($this->options));
$month = new Month(new \DateTimeImmutable('2013-01-01'), new Factory($this->options));
foreach ($month as $week) {
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Week', $week);
$this->assertInstanceOf(FixtureWeek::class, $week);
}
$days = $month->getDays();
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Day', $days[0]);
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Range', $month->getExtendedMonth());
$this->assertInstanceOf(Day::class, $days[0]);
$this->assertInstanceOf(Range::class, $month->getExtendedMonth());
}

public function testWeek()
public function testWeek(): void
{
$week = new Week(new \DateTime('2013W01'), new Factory($this->options));
$week = new Week(new \DateTimeImmutable('2013W01'), new Factory($this->options));
foreach ($week as $day) {
$this->assertInstanceOf('CalendR\Test\Fixtures\Period\Day', $day);
$this->assertInstanceOf(Day::class, $day);
}
}
}
Loading

0 comments on commit 456a925

Please sign in to comment.