API improvements (#1826)

This commit is contained in:
Kevin Papst
2020-07-17 20:30:16 +02:00
committed by GitHub
parent 5864c7e127
commit c843dba29a
77 changed files with 1745 additions and 1189 deletions

View File

@@ -59,7 +59,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(10, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testGetCollectionFull()
@@ -72,8 +72,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(10, \count($result));
$this->assertDefaultStructure($result[0], false);
$this->assertHasSubresources($result[0]);
self::assertApiResponseTypeStructure('TimesheetCollectionFull', $result[0]);
}
public function testGetCollectionForOtherUser()
@@ -99,14 +98,13 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(10, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testGetCollectionForAllUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$fixture = new TimesheetFixtures();
$fixture
@@ -125,7 +123,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(17, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testGetCollectionForEmptyResult()
@@ -170,7 +168,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(5, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testGetCollectionWithSingleParamsQuery()
@@ -202,14 +200,13 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(5, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testExportedFilter()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$fixture = new TimesheetFixtures();
$fixture
@@ -240,7 +237,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(7, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
$query = [
'page' => 1,
@@ -256,7 +253,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(10, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
$query = [
'page' => 1,
@@ -270,7 +267,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(17, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testGetEntity()
@@ -281,7 +278,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
}
public function testGetEntityAccessDenied()
@@ -301,7 +298,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
}
public function testGetEntityNotFound()
@@ -327,7 +324,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertNotEmpty($result['id']);
$this->assertTrue($result['duration'] == 57600 || $result['duration'] == 57660); // 1 minute rounding might be applied
$this->assertEquals(2016, $result['rate']);
@@ -357,7 +354,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertNotEmpty($result['id']);
$this->assertEquals($user->getId(), $result['user']);
$this->assertNotEquals($admin->getId(), $result['user']);
@@ -437,7 +434,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertNotEmpty($result['id']);
$this->assertEquals(25200, $result['duration']);
$this->assertEquals(1, $result['exported']);
@@ -447,7 +444,6 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$fixture = new TimesheetFixtures();
$fixture
@@ -510,7 +506,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertNotEmpty($result['id']);
$id = $result['id'];
@@ -585,10 +581,9 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
}
public function testGetRecentCollectionWithSubresources()
public function testGetRecentAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $this->getEntityManager();
$start = new \DateTime('-10 days');
@@ -614,14 +609,12 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(1, \count($result));
$this->assertDefaultStructure($result[0], false);
$this->assertHasSubresources($result[0]);
self::assertApiResponseTypeStructure('TimesheetCollectionFull', $result[0]);
}
public function testActiveAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $this->getEntityManager();
$start = new \DateTime('-10 days');
@@ -642,7 +635,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$results = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals(3, \count($results));
foreach ($results as $timesheet) {
$this->assertDefaultStructure($timesheet, false);
self::assertApiResponseTypeStructure('TimesheetCollectionFull', $timesheet);
}
}
@@ -650,7 +643,6 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$start = new \DateTime('-10 days');
@@ -668,6 +660,11 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->request($client, '/api/timesheets/11/stop', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertNotEmpty($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
@@ -692,7 +689,6 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$start = new \DateTime('-10 days');
@@ -715,7 +711,6 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$em = $this->getEntityManager();
$fixture = new TimesheetFixtures();
$fixture
@@ -736,7 +731,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(5, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
$query = ['tags' => 'Test,Admin'];
$this->assertAccessIsGranted($client, '/api/timesheets', 'GET', $query);
@@ -745,7 +740,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(10, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
$query = ['tags' => 'Nothing-2-see,here'];
$this->assertAccessIsGranted($client, '/api/timesheets', 'GET', $query);
@@ -754,7 +749,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(20, \count($result));
$this->assertDefaultStructure($result[0], false);
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
}
public function testRestartAction()
@@ -772,7 +767,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertDefaultStructure($result, true);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertEmpty($result['description']);
$this->assertEmpty($result['tags']);
@@ -812,7 +807,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertDefaultStructure($result, true);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertEquals('foo', $result['description']);
$this->assertEquals([['name' => 'sdfsdf', 'value' => 'nnnnn'], ['name' => '1234567890', 'value' => '1234567890']], $result['metaFields']);
$this->assertEquals(['another', 'testing', 'bar'], $result['tags']);
@@ -832,8 +827,6 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $this->getEntityManager();
$start = new \DateTime('-10 days');
$fixture = new TimesheetFixtures();
@@ -874,7 +867,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertNotEmpty($result['id']);
$this->assertTrue($result['duration'] == 57600 || $result['duration'] == 57660); // 1 minute rounding might be applied
$this->assertEquals(2016, $result['rate']);
@@ -884,7 +877,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertNotEmpty($result['id']);
$this->assertTrue($result['duration'] == 57600 || $result['duration'] == 57660); // 1 minute rounding might be applied
$this->assertEquals(2016, $result['rate']);
@@ -907,7 +900,8 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->request($client, '/api/timesheets/1/export', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertDefaultStructure(json_decode($client->getResponse()->getContent(), true), true);
$result = json_decode($client->getResponse()->getContent(), true);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$em->clear();
/** @var Timesheet $timesheet */
@@ -988,46 +982,13 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$this->assertEquals(['name' => 'metatestmock', 'value' => 'another,testing,bar'], $result['metaFields'][0]);
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$this->assertEquals('another,testing,bar', $timesheet->getMetaField('metatestmock')->getValue());
}
protected function assertDefaultStructure(array $result, $full = true)
{
$expectedKeys = [
'id', 'begin', 'end', 'duration', 'description', 'rate', 'activity', 'project', 'tags', 'user', 'metaFields', 'internalRate'
];
if ($full) {
$expectedKeys = array_merge($expectedKeys, [
'exported', 'fixedRate', 'hourlyRate'
]);
}
$actual = array_keys($result);
sort($actual);
sort($expectedKeys);
$this->assertEquals($expectedKeys, $actual, 'Timesheet structure does not match');
}
protected function assertHasSubresources(array $result)
{
$this->assertArrayHasKey('activity', $result);
$this->assertArrayHasKey('id', $result['activity']);
$this->assertArrayHasKey('name', $result['activity']);
$this->assertArrayHasKey('visible', $result['activity']);
$this->assertArrayHasKey('project', $result['activity']);
$this->assertArrayHasKey('project', $result);
$this->assertArrayHasKey('id', $result['project']);
$this->assertArrayHasKey('name', $result['project']);
$this->assertArrayHasKey('visible', $result['project']);
$this->assertArrayHasKey('customer', $result['project']);
$this->assertArrayHasKey('id', $result['project']['customer']);
$this->assertArrayHasKey('name', $result['project']['customer']);
$this->assertArrayHasKey('visible', $result['project']['customer']);
}
}