mirror of
https://github.com/werkraum-media/calendar.git
synced 2024-11-27 03:16:10 +01:00
236 lines
7 KiB
PHP
236 lines
7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace WerkraumMedia\Calendar\Tests\Unit\Domain\Model;
|
||
|
|
||
|
/*
|
||
|
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU General Public License
|
||
|
* as published by the Free Software Foundation; either version 2
|
||
|
* of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||
|
* 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
use PHPUnit\Framework\TestCase;
|
||
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||
|
use WerkraumMedia\Calendar\Domain\Model\Day;
|
||
|
use WerkraumMedia\Calendar\Domain\Model\Week;
|
||
|
use WerkraumMedia\Calendar\Tests\ForcePropertyTrait;
|
||
|
|
||
|
/**
|
||
|
* @covers WerkraumMedia\Calendar\Domain\Model\Week
|
||
|
* @testdox A week
|
||
|
*/
|
||
|
class WeekTest extends TestCase
|
||
|
{
|
||
|
use ProphecyTrait;
|
||
|
use ForcePropertyTrait;
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function canBeCreated(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2020);
|
||
|
|
||
|
self::assertInstanceOf(Week::class, $subject);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsSevenDays(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2020);
|
||
|
|
||
|
self::assertCount(7, $subject->getDays());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsSameDaysOnSecondCall(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2020);
|
||
|
|
||
|
self::assertSame($subject->getDays(), $subject->getDays());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsDaysForWeek1(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2020);
|
||
|
$days = $subject->getDays();
|
||
|
|
||
|
self::assertSame('2019-12-30', $days[0]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2019-12-31', $days[1]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-01-01', $days[2]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-01-02', $days[3]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-01-03', $days[4]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-01-04', $days[5]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-01-05', $days[6]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsDaysForWeek53(): void
|
||
|
{
|
||
|
$subject = new Week(53, 2020);
|
||
|
$days = $subject->getDays();
|
||
|
|
||
|
self::assertSame('2020-12-28', $days[0]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-12-29', $days[1]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-12-30', $days[2]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2020-12-31', $days[3]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2021-01-01', $days[4]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2021-01-02', $days[5]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
self::assertSame('2021-01-03', $days[6]->getDateTimeInstance()->format('Y-m-d'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsPreviousWeekForSameYear(): void
|
||
|
{
|
||
|
$subject = new Week(2, 2020);
|
||
|
|
||
|
self::assertSame('01', $subject->getPreviousWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2020', $subject->getPreviousWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsPreviousWeekForYearSwitchFrom2019To2018(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2019);
|
||
|
|
||
|
self::assertSame('52', $subject->getPreviousWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2018', $subject->getPreviousWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsPreviousWeekForYearSwitchFrom2018To2017(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2018);
|
||
|
|
||
|
self::assertSame('52', $subject->getPreviousWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2017', $subject->getPreviousWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsPreviousWeekForPreviousYear(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2021);
|
||
|
|
||
|
self::assertSame('53', $subject->getPreviousWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2020', $subject->getPreviousWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsNextWeekForSameYear(): void
|
||
|
{
|
||
|
$subject = new Week(1, 2020);
|
||
|
|
||
|
self::assertSame('02', $subject->getNextWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2020', $subject->getNextWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsNextWeekForNextYear(): void
|
||
|
{
|
||
|
$subject = new Week(53, 2020);
|
||
|
|
||
|
self::assertSame('01', $subject->getNextWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2021', $subject->getNextWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsNextWeekForNextYearFrom2018To2019(): void
|
||
|
{
|
||
|
$subject = new Week(52, 2018);
|
||
|
|
||
|
self::assertSame('01', $subject->getNextWeek()->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2019', $subject->getNextWeek()->getDateTimeInstance()->format('Y'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function providesDateTimeInstance(): void
|
||
|
{
|
||
|
$subject = new Week(52, 2018);
|
||
|
|
||
|
self::assertSame('52', $subject->getDateTimeInstance()->format('W'));
|
||
|
self::assertSame('2018-12-27 Thursday', $subject->getDateTimeInstance()->format('Y-m-d l'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function providesItselfAsUrlArgument(): void
|
||
|
{
|
||
|
$subject = new Week(52, 2018);
|
||
|
|
||
|
self::assertSame([
|
||
|
'week' => 52,
|
||
|
'year' => 2018,
|
||
|
], $subject->getAsUrlArgument());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsNotActiveIfAllDaysAreInactive(): void
|
||
|
{
|
||
|
$subject = new Week(02, 2018);
|
||
|
|
||
|
$day = $this->prophesize(Day::class);
|
||
|
$day->isActive()->willReturn(false);
|
||
|
$days = [$day->reveal()];
|
||
|
$this->forceProperty($subject, 'days', $days);
|
||
|
|
||
|
self::assertFalse($subject->isActive());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @test
|
||
|
*/
|
||
|
public function returnsActiveIfASingleDayActive(): void
|
||
|
{
|
||
|
$subject = new Week(02, 2018);
|
||
|
|
||
|
$day = $this->prophesize(Day::class);
|
||
|
$day->isActive()->willReturn(true);
|
||
|
$day2 = $this->prophesize(Day::class);
|
||
|
$day2->isActive()->willReturn(false);
|
||
|
$days = [$day->reveal(), $day2->reveal()];
|
||
|
$this->forceProperty($subject, 'days', $days);
|
||
|
|
||
|
self::assertTrue($subject->isActive());
|
||
|
}
|
||
|
}
|