diff --git a/Models/Promotion.php b/Models/Promotion.php index 4879701..1e61c3f 100755 --- a/Models/Promotion.php +++ b/Models/Promotion.php @@ -18,10 +18,11 @@ use Modules\Admin\Models\Account; use Modules\Admin\Models\NullAccount; use Modules\Calendar\Models\Calendar; use Modules\Tasks\Models\Task; +use Modules\Tasks\Models\NullTask; use phpOMS\Localization\Money; /** - * Project class. + * Promotion class. * * @package Modules\Marketing\Models * @license OMS License 1.0 @@ -39,20 +40,20 @@ class Promotion protected int $id = 0; /** - * Start of the promotion. + * Event start. * * @var \DateTime * @since 1.0.0 */ - private \DateTime $start; + public \DateTime $start; /** - * End of the promotion. + * Event end. * * @var \DateTime * @since 1.0.0 */ - private \DateTime $end; + public \DateTime $end; /** * Name. @@ -60,7 +61,7 @@ class Promotion * @var string * @since 1.0.0 */ - private string $name = ''; + public string $name = ''; /** * Description. @@ -76,7 +77,7 @@ class Promotion * @var Calendar * @since 1.0.0 */ - private Calendar $calendar; + public Calendar $calendar; /** * Costs. @@ -84,7 +85,7 @@ class Promotion * @var Money * @since 1.0.0 */ - private Money $costs; + public Money $costs; /** * Budget. @@ -92,7 +93,7 @@ class Promotion * @var Money * @since 1.0.0 */ - private Money $budget; + public Money $budget; /** * Earnings. @@ -100,7 +101,15 @@ class Promotion * @var Money * @since 1.0.0 */ - private Money $earnings; + public Money $earnings; + + /** + * Tasks. + * + * @var Task[] + * @since 1.0.0 + */ + private array $tasks = []; /** * Media. @@ -116,7 +125,7 @@ class Promotion * @var int * @since 1.0.0 */ - private int $progress = 0; + public int $progress = 0; /** * Progress type. @@ -127,7 +136,7 @@ class Promotion private int $progressType = ProgressType::MANUAL; /** - * Created at. + * Created. * * @var \DateTimeImmutable * @since 1.0.0 @@ -135,7 +144,7 @@ class Promotion public \DateTimeImmutable $createdAt; /** - * Created by. + * Creator. * * @var Account * @since 1.0.0 @@ -143,35 +152,24 @@ class Promotion public Account $createdBy; /** - * Tasks. + * Constructor. * - * @var Task[] - * @since 1.0.0 - */ - private array $tasks = []; - - /** - * Cosntructor - * - * @param string $name Promotion name + * @param string $name Event name/title * * @since 1.0.0 */ public function __construct(string $name = '') { - $this->start = new \DateTime('now'); - $this->end = new \DateTime('now'); - $this->end->modify('+1 month'); + $this->start = new \DateTime('now'); + $this->end = (new \DateTime('now'))->modify('+1 month'); + $this->calendar = new Calendar(); + $this->costs = new Money(); + $this->budget = new Money(); + $this->earnings = new Money(); $this->createdAt = new \DateTimeImmutable('now'); $this->createdBy = new NullAccount(); - $this->calendar = new Calendar(); - - $this->costs = new Money(); - $this->budget = new Money(); - $this->earnings = new Money(); - - $this->setName($name); + $this->name = $name; } /** @@ -212,6 +210,32 @@ class Promotion $this->media[] = $media; } + /** + * Get progress type. + * + * @return int + * + * @since 1.0.0 + */ + public function getProgressType() : int + { + return $this->progressType; + } + + /** + * Set progress type. + * + * @param int $type Progress type + * + * @return void + * + * @since 1.0.0 + */ + public function setProgressType(int $type) : void + { + $this->progressType = $type; + } + /** * Add task. * @@ -223,11 +247,7 @@ class Promotion */ public function addTask(Task $task) : void { - if ($task->getId() !== 0) { - $this->tasks[$task->getId()] = $task; - } else { - $this->tasks[] = $task; - } + $this->tasks[] = $task; } /** @@ -261,7 +281,7 @@ class Promotion */ public function getTask(int $id) : Task { - return $this->tasks[$id] ?? new Task(); + return $this->tasks[$id] ?? new NullTask(); } /** @@ -289,223 +309,33 @@ class Promotion } /** - * Get start date. - * - * @return \DateTime - * - * @since 1.0.0 + * {@inheritdoc} */ - public function getStart() : \DateTime + public function toArray() : array { - return $this->start; + return [ + 'id' => $this->id, + 'start' => $this->start, + 'end' => $this->end, + 'name' => $this->name, + 'description' => $this->description, + 'calendar' => $this->calendar, + 'costs' => $this->costs, + 'budget' => $this->budget, + 'earnings' => $this->earnings, + 'tasks' => $this->tasks, + 'media' => $this->media, + 'progress' => $this->progress, + 'progressType' => $this->progressType, + 'createdAt' => $this->createdAt, + ]; } /** - * Set start date. - * - * @param \DateTime $start Start date - * - * @return void - * - * @since 1.0.0 + * {@inheritdoc} */ - public function setStart(\DateTime $start) : void + public function jsonSerialize() { - $this->start = $start; - } - - /** - * Set end. - * - * @param \DateTime $end End date - * - * @return void - * - * @since 1.0.0 - */ - public function setEnd(\DateTime $end) : void - { - $this->end = $end; - } - - /** - * Get end. - * - * @return \DateTime - * - * @since 1.0.0 - */ - public function getEnd() : \DateTime - { - return $this->end; - } - - /** - * Get progress. - * - * @return int - * - * @since 1.0.0 - */ - public function getProgress() : int - { - return $this->progress; - } - - /** - * Set progress. - * - * @param int $progress Progress - * - * @return void - * - * @since 1.0.0 - */ - public function setProgress(int $progress) : void - { - $this->progress = $progress; - } - - /** - * Get progress type. - * - * @return int - * - * @since 1.0.0 - */ - public function getProgressType() : int - { - return $this->progressType; - } - - /** - * Set progress type. - * - * @param int $type Progress type - * - * @return void - * - * @since 1.0.0 - */ - public function setProgressType(int $type) : void - { - $this->progressType = $type; - } - - /** - * Get calendar. - * - * @return Calendar - * - * @since 1.0.0 - */ - public function getCalendar() : Calendar - { - return $this->calendar; - } - - /** - * Get name - * - * @return string - * - * @since 1.0.0 - */ - public function getName() : string - { - return $this->name; - } - - /** - * Set name - * - * @param string $name Name - * - * @return void - * - * @since 1.0.0 - */ - public function setName(string $name) : void - { - $this->name = $name; - $this->calendar->name = $name; - } - - /** - * Get costs. - * - * @return Money - * - * @since 1.0.0 - */ - public function getCosts() : Money - { - return $this->costs; - } - - /** - * Get budget. - * - * @return Money - * - * @since 1.0.0 - */ - public function getBudget() : Money - { - return $this->budget; - } - - /** - * Get earnings. - * - * @return Money - * - * @since 1.0.0 - */ - public function getEarnings() : Money - { - return $this->earnings; - } - - /** - * Set costs. - * - * @param Money $costs Costs - * - * @return void - * - * @since 1.0.0 - */ - public function setCosts(Money $costs) : void - { - $this->costs = $costs; - } - - /** - * Set budget. - * - * @param Money $budget Budget - * - * @return void - * - * @since 1.0.0 - */ - public function setBudget(Money $budget) : void - { - $this->budget = $budget; - } - - /** - * Set earnings. - * - * @param Money $earnings Earnings - * - * @return void - * - * @since 1.0.0 - */ - public function setEarnings(Money $earnings) : void - { - $this->earnings = $earnings; + return $this->toArray(); } } diff --git a/tests/Models/PromotionMapperTest.php b/tests/Models/PromotionMapperTest.php index 32473f5..4f5310d 100755 --- a/tests/Models/PromotionMapperTest.php +++ b/tests/Models/PromotionMapperTest.php @@ -17,6 +17,7 @@ namespace Modules\Marketing\tests\Models; use Modules\Admin\Models\NullAccount; use Modules\Marketing\Models\Promotion; use Modules\Marketing\Models\PromotionMapper; +use Modules\Marketing\Models\ProgressType; use Modules\Media\Models\Media; use Modules\Tasks\Models\Task; use phpOMS\Localization\Money; @@ -35,37 +36,40 @@ final class PromotionMapperTest extends \PHPUnit\Framework\TestCase { $promotion = new Promotion(); - $promotion->setName('Promotionname'); + $promotion->name = 'Promotionname'; $promotion->description = 'Description'; $promotion->createdBy = new NullAccount(1); - $promotion->setStart(new \DateTime('2000-05-05')); - $promotion->setEnd(new \DateTime('2005-05-05')); + $promotion->start = new \DateTime('2000-05-05'); + $promotion->end = new \DateTime('2005-05-05'); $money = new Money(); $money->setString('1.23'); - $promotion->setCosts($money); - $promotion->setBudget($money); - $promotion->setEarnings($money); + $promotion->costs = $money; + $promotion->budget = $money; + $promotion->earnings = $money; $task = new Task(); - $task->title = 'PromotionTask 1'; + $task->title = 'EventTask 1'; $task->setCreatedBy(new NullAccount(1)); $task2 = new Task(); - $task2->title = 'PromotionTask 2'; + $task2->title = 'EventTask 2'; $task2->setCreatedBy(new NullAccount(1)); $promotion->addTask($task); $promotion->addTask($task2); + $promotion->progress = 11; + $promotion->setProgressType(ProgressType::TASKS); + $media = new Media(); $media->createdBy = new NullAccount(1); $media->description = 'desc'; $media->setPath('some/path'); $media->size = 11; $media->extension = 'png'; - $media->name = 'Promotion Media'; + $media->name = 'Event Media'; $promotion->addMedia($media); $id = PromotionMapper::create($promotion); @@ -74,15 +78,16 @@ final class PromotionMapperTest extends \PHPUnit\Framework\TestCase $promotionR = PromotionMapper::get($promotion->getId()); - self::assertEquals($promotion->getName(), $promotionR->getName()); + self::assertEquals($promotion->name, $promotionR->name); self::assertEquals($promotion->description, $promotionR->description); self::assertEquals($promotion->countTasks(), $promotionR->countTasks()); - self::assertEquals($promotion->getCosts()->getAmount(), $promotionR->getCosts()->getAmount()); - self::assertEquals($promotion->getBudget()->getAmount(), $promotionR->getBudget()->getAmount()); - self::assertEquals($promotion->getEarnings()->getAmount(), $promotionR->getEarnings()->getAmount()); - self::assertEquals($promotion->createdAt->format('Y-m-d'), $promotionR->createdAt->format('Y-m-d')); - self::assertEquals($promotion->getStart()->format('Y-m-d'), $promotionR->getStart()->format('Y-m-d')); - self::assertEquals($promotion->getEnd()->format('Y-m-d'), $promotionR->getEnd()->format('Y-m-d')); + self::assertEquals($promotion->start->format('Y-m-d'), $promotionR->start->format('Y-m-d')); + self::assertEquals($promotion->end->format('Y-m-d'), $promotionR->end->format('Y-m-d')); + self::assertEquals($promotion->costs->getAmount(), $promotionR->costs->getAmount()); + self::assertEquals($promotion->budget->getAmount(), $promotionR->budget->getAmount()); + self::assertEquals($promotion->earnings->getAmount(), $promotionR->earnings->getAmount()); + self::assertEquals($promotion->progress, $promotionR->progress); + self::assertEquals($promotion->getProgressType(), $promotionR->getProgressType()); $expected = $promotion->getMedia(); $actual = $promotionR->getMedia(); @@ -100,33 +105,4 @@ final class PromotionMapperTest extends \PHPUnit\Framework\TestCase self::assertCount(1, $newest); } - - /** - * @group volume - * @group module - * @coversNothing - */ - public function testVolume() : void - { - for ($i = 1; $i < 100; ++$i) { - $text = new Text(); - - $promotion = new Promotion(); - - $promotion->setName($text->generateText(\mt_rand(3, 7))); - $promotion->description = $text->generateText(\mt_rand(20, 100)); - $promotion->createdBy = new NullAccount(1); - $promotion->setStart(new \DateTime('2000-05-05')); - $promotion->setEnd(new \DateTime('2005-05-05')); - - $money = new Money(); - $money->setString('1.23'); - - $promotion->setCosts($money); - $promotion->setBudget($money); - $promotion->setEarnings($money); - - $id = PromotionMapper::create($promotion); - } - } } diff --git a/tests/Models/PromotionTest.php b/tests/Models/PromotionTest.php index 91c0a31..4492932 100755 --- a/tests/Models/PromotionTest.php +++ b/tests/Models/PromotionTest.php @@ -14,8 +14,9 @@ declare(strict_types=1); namespace Modules\Marketing\tests\Models; -use Modules\Admin\Models\NullAccount; use Modules\Marketing\Models\Promotion; +use Modules\Marketing\Models\ProgressType; +use Modules\Media\Models\Media; use Modules\Tasks\Models\Task; use phpOMS\Localization\Money; @@ -24,71 +25,158 @@ use phpOMS\Localization\Money; */ final class PromotionTest extends \PHPUnit\Framework\TestCase { + private Promotion $promotion; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->promotion = new Promotion(); + } + /** * @covers Modules\Marketing\Models\Promotion * @group module */ public function testDefault() : void { - $promotion = new Promotion(); - - self::assertEquals(0, $promotion->getId()); - self::assertInstanceOf('\Modules\Calendar\Models\Calendar', $promotion->getCalendar()); - self::assertEquals((new \DateTime('now'))->format('Y-m-d'), $promotion->createdAt->format('Y-m-d')); - self::assertEquals((new \DateTime('now'))->format('Y-m-d'), $promotion->getStart()->format('Y-m-d')); - self::assertEquals((new \DateTime('now'))->modify('+1 month')->format('Y-m-d'), $promotion->getEnd()->format('Y-m-d')); - self::assertEquals(0, $promotion->createdBy->getId()); - self::assertEquals('', $promotion->getName()); - self::assertEquals('', $promotion->description); - self::assertEquals(0, $promotion->getCosts()->getInt()); - self::assertEquals(0, $promotion->getBudget()->getInt()); - self::assertEquals(0, $promotion->getEarnings()->getInt()); - self::assertEmpty($promotion->getTasks()); - self::assertFalse($promotion->removeTask(2)); - self::assertInstanceOf('\Modules\Tasks\Models\Task', $promotion->getTask(0)); + self::assertEquals(0, $this->promotion->getId()); + self::assertInstanceOf('\Modules\Calendar\Models\Calendar', $this->promotion->calendar); + self::assertEquals((new \DateTime('now'))->format('Y-m-d'), $this->promotion->start->format('Y-m-d')); + self::assertEquals((new \DateTime('now'))->modify('+1 month')->format('Y-m-d'), $this->promotion->end->format('Y-m-d')); + self::assertEquals(0, $this->promotion->costs->getInt()); + self::assertEquals(0, $this->promotion->budget->getInt()); + self::assertEquals(0, $this->promotion->earnings->getInt()); + self::assertFalse($this->promotion->removeTask(2)); + self::assertEmpty($this->promotion->getTasks()); + self::assertEmpty($this->promotion->getMedia()); + self::assertInstanceOf('\Modules\Tasks\Models\NullTask', $this->promotion->getTask(1)); + self::assertEquals(0, $this->promotion->progress); + self::assertEquals(ProgressType::MANUAL, $this->promotion->getProgressType()); } /** * @covers Modules\Marketing\Models\Promotion * @group module */ - public function testSetGet() : void + public function testCostsInputOutput() : void { - $promotion = new Promotion(); - - $promotion->setName('Promotion'); - self::assertEquals('Promotion', $promotion->getName()); - - $promotion->description = 'Description'; - self::assertEquals('Description', $promotion->description); - - $promotion->setStart($date = new \DateTime('2000-05-05')); - self::assertEquals($date->format('Y-m-d'), $promotion->getStart()->format('Y-m-d')); - - $promotion->setEnd($date = new \DateTime('2000-05-05')); - self::assertEquals($date->format('Y-m-d'), $promotion->getEnd()->format('Y-m-d')); - $money = new Money(); $money->setString('1.23'); - $promotion->setCosts($money); - self::assertEquals($money->getAmount(), $promotion->getCosts()->getAmount()); + $this->promotion->costs = $money; + self::assertEquals($money->getAmount(), $this->promotion->costs->getAmount()); + } - $promotion->setBudget($money); - self::assertEquals($money->getAmount(), $promotion->getBudget()->getAmount()); + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testBudgetInputOutput() : void + { + $money = new Money(); + $money->setString('1.23'); - $promotion->setEarnings($money); - self::assertEquals($money->getAmount(), $promotion->getEarnings()->getAmount()); + $this->promotion->budget = $money; + self::assertEquals($money->getAmount(), $this->promotion->budget->getAmount()); + } + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testEarningsInputOutput() : void + { + $money = new Money(); + $money->setString('1.23'); + + $this->promotion->earnings = $money; + self::assertEquals($money->getAmount(), $this->promotion->earnings->getAmount()); + } + + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testMediaInputOutput() : void + { + $this->promotion->addMedia(new Media()); + self::assertCount(1, $this->promotion->getMedia()); + } + + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testTaskInputOutput() : void + { $task = new Task(); - $task->title = 'Promo Task A'; - $task->setCreatedBy(new NullAccount(1)); + $task->title = 'A'; - $promotion->addTask($task); + $this->promotion->addTask($task); + self::assertEquals('A', $this->promotion->getTask(0)->title); - self::assertEquals('Promo Task A', $promotion->getTask(0)->title); - self::assertCount(1, $promotion->getTasks()); - self::assertTrue($promotion->removeTask(0)); - self::assertEquals(0, $promotion->countTasks()); + self::assertTrue($this->promotion->removeTask(0)); + self::assertEquals(0, $this->promotion->countTasks()); + + $this->promotion->addTask($task); + self::assertCount(1, $this->promotion->getTasks()); + } + + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testProgressInputOutput() : void + { + $this->promotion->progress = 10; + self::assertEquals(10, $this->promotion->progress); + } + + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testProgressTypeInputOutput() : void + { + $this->promotion->setProgressType(ProgressType::TASKS); + self::assertEquals(ProgressType::TASKS, $this->promotion->getProgressType()); + } + + /** + * @covers Modules\Marketing\Models\Promotion + * @group module + */ + public function testSerialize() : void + { + $this->promotion->name = 'Name'; + $this->promotion->description = 'Description'; + $this->promotion->start = new \DateTime(); + $this->promotion->end = new \DateTime(); + $this->promotion->progress = 10; + $this->promotion->setProgressType(ProgressType::TASKS); + + $serialized = $this->promotion->jsonSerialize(); + unset($serialized['calendar']); + unset($serialized['createdAt']); + + self::assertEquals( + [ + 'id' => 0, + 'start' => $this->promotion->start, + 'end' => $this->promotion->end, + 'name' => 'Name', + 'description' => 'Description', + 'costs' => new Money(), + 'budget' => new Money(), + 'earnings' => new Money(), + 'tasks' => [], + 'media' => [], + 'progress' => 10, + 'progressType' => ProgressType::TASKS, + ], + $serialized + ); } } diff --git a/tests/phpunit_default.xml b/tests/phpunit_default.xml index 722365c..9208c99 100755 --- a/tests/phpunit_default.xml +++ b/tests/phpunit_default.xml @@ -1,5 +1,5 @@ - + *vendor*