fix serialized dates from API (#1888)

This commit is contained in:
Kevin Papst
2020-08-12 21:56:47 +02:00
committed by GitHub
parent dc9bc8a8ca
commit 2449a1e8bf
6 changed files with 118 additions and 5 deletions

View File

@@ -184,11 +184,58 @@ class ProjectControllerTest extends APIControllerBaseTest
public function testGetEntity()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/projects/1');
$em = $this->getEntityManager();
$customer = (new Customer())
->setName('first one')
->setVisible(true)
->setCountry('de')
->setTimezone('Europe/Berlin')
;
$em->persist($customer);
$orderDate = new \DateTime('2019-11-29 14:35:17', new \DateTimeZone('Pacific/Tongatapu'));
$startDate = new \DateTime('2020-01-07 18:19:20', new \DateTimeZone('Pacific/Tongatapu'));
$endDate = new \DateTime('2021-03-23 00:00:01', new \DateTimeZone('Pacific/Tongatapu'));
$project = (new Project())
->setName('first')
->setVisible(true)
->setCustomer($customer)
->setOrderDate($orderDate)
->setStart($startDate)
->setEnd($endDate)
;
$em->persist($project);
$this->assertAccessIsGranted($client, '/api/projects/2');
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
self::assertApiResponseTypeStructure('ProjectEntity', $result);
$expected = [
'parentTitle' => 'first one',
'customer' => 2,
'id' => 2,
'name' => 'first',
'orderNumber' => null,
// make sure the timezone is properly applied in serializer (see #1858)
'orderDate' => '2019-11-29T14:35:17+1300',
'start' => '2020-01-07T18:19:20+1300',
'end' => '2021-03-23T00:00:01+1300',
'comment' => null,
'visible' => true,
'budget' => 0.0,
'timeBudget' => 0,
'metaFields' => [],
'teams' => [],
'color' => null,
];
foreach ($expected as $key => $value) {
self::assertEquals($value, $result[$key]);
}
}
public function testNotFound()

View File

@@ -38,9 +38,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
->setHourlyRate(true)
->setAmount(10)
->setUser($this->getUserByRole($role))
->setStartDate((new \DateTime('first day of this month'))->setTime(0, 0, 1))
->setAllowEmptyDescriptions(false)
->setStartDate((new \DateTime('first day of this month'))->setTime(0, 0, 1))
;
$this->importFixture($fixture);
}
@@ -273,12 +274,62 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testGetEntity()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$startDate = new \DateTime('2020-03-27 14:35:59', new \DateTimeZone('Pacific/Tongatapu'));
$endDate = (clone $startDate)->modify('+ 46385 seconds');
$project = $em->getRepository(Project::class)->find(1);
$activity = $em->getRepository(Activity::class)->find(1);
$tag = new Tag();
$tag->setName('test');
$em->persist($tag);
$timesheet = new Timesheet();
$timesheet
->setHourlyRate(137.21)
->setInternalRate(64.96)
->setBegin($startDate)
->setEnd($endDate)
->setExported(true)
->setDescription('**foo**' . PHP_EOL . 'bar')
->setUser($this->getUserByRole(User::ROLE_USER))
->setProject($project)
->setActivity($activity)
->addTag($tag)
;
$em->persist($timesheet);
$this->assertAccessIsGranted($client, '/api/timesheets/1');
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$expected = [
'activity' => 1,
'project' => 1,
'user' => 2,
'tags' => [
0 => 'test'
],
'id' => 1,
// make sure the timezone is properly applied in serializer (see #1858)
// minute and second are different from the above datetime object, because of applied default minute rounding
'begin' => '2020-03-27T14:35:00+1300',
'end' => '2020-03-28T03:30:00+1300',
'description' => "**foo**\nbar",
'duration' => 46500,
'exported' => true,
'metaFields' => [],
'hourlyRate' => 137.21,
'rate' => 1772.3,
'internalRate' => 0.0,
];
foreach ($expected as $key => $value) {
self::assertEquals($value, $result[$key]);
}
}
public function testGetEntityAccessDenied()