added tags for timesheets (#604)
This commit is contained in:
68
tests/DataFixtures/TagFixtures.php
Normal file
68
tests/DataFixtures/TagFixtures.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\DataFixtures;
|
||||
|
||||
use App\Entity\Tag;
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
/**
|
||||
* Defines the sample data to load in during controller tests.
|
||||
*/
|
||||
class TagFixtures extends Fixture
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $tagArray = [];
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getTagArray()
|
||||
{
|
||||
return $this->tagArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $tagArray
|
||||
* @return TagFixtures
|
||||
*/
|
||||
public function setTagArray(array $tagArray)
|
||||
{
|
||||
$this->tagArray = $tagArray;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(ObjectManager $manager)
|
||||
{
|
||||
foreach ($this->getTagArray() as $tagName) {
|
||||
$entry = $this->createTagEntry($tagName);
|
||||
$manager->persist($entry);
|
||||
}
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $tagName
|
||||
* @return Tag
|
||||
*/
|
||||
protected function createTagEntry($tagName)
|
||||
{
|
||||
$tagObject = new Tag();
|
||||
$tagObject->setName($tagName);
|
||||
|
||||
return $tagObject;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ namespace App\Tests\DataFixtures;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\Tag;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Entity\User;
|
||||
use App\Entity\UserPreference;
|
||||
@@ -60,6 +61,14 @@ class TimesheetFixtures extends Fixture
|
||||
* @var int
|
||||
*/
|
||||
protected $exported = false;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $useTags = false;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $tags = [];
|
||||
|
||||
/**
|
||||
* @param bool $allowEmptyDescriptions
|
||||
@@ -163,6 +172,28 @@ class TimesheetFixtures extends Fixture
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $useTags
|
||||
* @return TimesheetFixtures
|
||||
*/
|
||||
public function setUseTags(bool $useTags)
|
||||
{
|
||||
$this->useTags = $useTags;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $tags
|
||||
* @return TimesheetFixtures
|
||||
*/
|
||||
public function setTags(array $tags)
|
||||
{
|
||||
$this->tags = $tags;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -195,12 +226,15 @@ class TimesheetFixtures extends Fixture
|
||||
$project = $projects[array_rand($projects)];
|
||||
}
|
||||
|
||||
$tags = $this->getTagObjectList($i);
|
||||
|
||||
$entry = $this->createTimesheetEntry(
|
||||
$user,
|
||||
$activity,
|
||||
$project,
|
||||
$description,
|
||||
$this->getDateTime($i)
|
||||
$this->getDateTime($i),
|
||||
$tags
|
||||
);
|
||||
|
||||
$manager->persist($entry);
|
||||
@@ -214,12 +248,15 @@ class TimesheetFixtures extends Fixture
|
||||
$project = $projects[array_rand($projects)];
|
||||
}
|
||||
|
||||
$tags = $this->getTagObjectList($i);
|
||||
|
||||
$entry = $this->createTimesheetEntry(
|
||||
$user,
|
||||
$activity,
|
||||
$project,
|
||||
$faker->text,
|
||||
$this->getDateTime($i),
|
||||
$tags,
|
||||
false
|
||||
);
|
||||
$manager->persist($entry);
|
||||
@@ -228,6 +265,22 @@ class TimesheetFixtures extends Fixture
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cnt
|
||||
* @return array
|
||||
*/
|
||||
protected function getTagObjectList($cnt)
|
||||
{
|
||||
if (true === $this->useTags) {
|
||||
$tagObject = new Tag();
|
||||
$tagObject->setName($this->tags[($cnt % count($this->tags))]);
|
||||
|
||||
return [$tagObject];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $i
|
||||
* @return bool|\DateTime
|
||||
@@ -278,10 +331,11 @@ class TimesheetFixtures extends Fixture
|
||||
* @param Project $project
|
||||
* @param string $description
|
||||
* @param \DateTime $start
|
||||
* @param null|array $tagArray
|
||||
* @param bool $setEndDate
|
||||
* @return Timesheet
|
||||
*/
|
||||
private function createTimesheetEntry(User $user, Activity $activity, Project $project, $description, \DateTime $start, $setEndDate = true)
|
||||
private function createTimesheetEntry(User $user, Activity $activity, Project $project, $description, \DateTime $start, $tagArray = [], $setEndDate = true)
|
||||
{
|
||||
$end = clone $start;
|
||||
$end = $end->modify('+ ' . (rand(1, 86400)) . ' seconds');
|
||||
@@ -299,6 +353,12 @@ class TimesheetFixtures extends Fixture
|
||||
->setRate($rate)
|
||||
->setBegin($start);
|
||||
|
||||
if (count($tagArray) > 0) {
|
||||
foreach ($tagArray as $item) {
|
||||
$entry->addTag($item);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->fixedRate) {
|
||||
$entry->setFixedRate(rand(10, 100));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user