Drop SQLite support (#2405)

This commit is contained in:
Kevin Papst
2021-03-08 16:06:22 +01:00
committed by GitHub
parent 70f7f35009
commit 30ac5e4c24
78 changed files with 1165 additions and 1596 deletions

View File

@@ -15,12 +15,12 @@ use App\Entity\ActivityMeta;
use App\Entity\ActivityRate;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\RateInterface;
use App\Entity\User;
use App\Repository\ActivityRateRepository;
use App\Repository\ActivityRepository;
use App\Tests\Mocks\ActivityTestMetaFieldSubscriberMock;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
* @group integration
@@ -29,6 +29,20 @@ class ActivityControllerTest extends APIControllerBaseTest
{
use RateControllerTestTrait;
/**
* @param ActivityRate $rate
* @param bool $isCollection
* @return string
*/
protected function getRateUrlByRate(RateInterface $rate, bool $isCollection): string
{
if ($isCollection) {
return $this->getRateUrl($rate->getActivity()->getId());
}
return $this->getRateUrl($rate->getActivity()->getId(), $rate->getId());
}
protected function getRateUrl($id = '1', $rateId = null): string
{
if (null !== $rateId) {
@@ -77,7 +91,7 @@ class ActivityControllerTest extends APIControllerBaseTest
$this->assertUrlIsSecured('/api/activities');
}
protected function loadActivityTestData(HttpKernelBrowser $client)
protected function loadActivityTestData(): array
{
$em = $this->getEntityManager();
@@ -116,15 +130,33 @@ class ActivityControllerTest extends APIControllerBaseTest
$em->persist($activity);
$em->flush();
return [$project, $project2];
}
/**
* @dataProvider getCollectionTestData
*/
public function testGetCollection($url, $parameters, $expected)
public function testGetCollection($url, $project, $parameters, $expected)
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->loadActivityTestData($client);
$imports = $this->loadActivityTestData();
$projectId = $project !== null ? $imports[$project]->getId() : null;
if ($projectId !== null) {
if (\array_key_exists('project', $parameters)) {
$parameters['project'] = $projectId;
}
if (\array_key_exists('projects', $parameters)) {
if (stripos($parameters['projects'], ',') !== false) {
$parameters['projects'] = $projectId . ',' . $projectId;
} else {
$parameters['projects'] = (string) $projectId;
}
}
}
$this->assertAccessIsGranted($client, $url, 'GET', $parameters);
$result = json_decode($client->getResponse()->getContent(), true);
@@ -135,31 +167,31 @@ class ActivityControllerTest extends APIControllerBaseTest
$activity = $result[$i];
$hasProject = $expected[$i][0];
self::assertApiResponseTypeStructure('ActivityCollection', $activity);
if ($hasProject) {
$this->assertEquals($expected[$i][1], $activity['project']);
if ($hasProject && $projectId !== null) {
$this->assertEquals($projectId, $activity['project']);
}
}
}
public function getCollectionTestData()
{
yield ['/api/activities', [], [[false], [true, 2], [true, 2], [null], [true, 1]]];
yield ['/api/activities', null, [], [[false], [true, 2], [true, 2], [null], [true, 1]]];
//yield ['/api/activities', [], [[false], [false], [true, 2], [true, 1], [true, 2]]];
yield ['/api/activities', ['globals' => 'true'], [[false], [false]]];
yield ['/api/activities', ['globals' => 'true', 'visible' => 3], [[false], [false], [false]]];
yield ['/api/activities', ['globals' => 'true', 'visible' => '2'], [[false]]];
yield ['/api/activities', ['globals' => 'true', 'visible' => 1], [[false], [false]]];
yield ['/api/activities', ['project' => '1'], [[false], [false], [true, 1]]];
yield ['/api/activities', ['project' => '2', 'projects' => '2', 'visible' => 1], [[false], [true, 2], [true, 2], [false]]];
yield ['/api/activities', ['project' => '2', 'projects' => '2,2', 'visible' => '3'], [[false], [true, 2], [true, 2], [true, 2], [false], [false]]];
yield ['/api/activities', ['projects' => '2,2', 'visible' => 2], [[true, 2], [false]]];
yield ['/api/activities', ['projects' => '2', 'visible' => 2], [[true, 2], [false]]];
yield ['/api/activities', null, ['globals' => 'true'], [[false], [false]]];
yield ['/api/activities', null, ['globals' => 'true', 'visible' => 3], [[false], [false], [false]]];
yield ['/api/activities', null, ['globals' => 'true', 'visible' => '2'], [[false]]];
yield ['/api/activities', null, ['globals' => 'true', 'visible' => 1], [[false], [false]]];
yield ['/api/activities', 0, ['project' => '1'], [[false], [false], [true, 1]]];
yield ['/api/activities', 1, ['project' => '2', 'projects' => '2', 'visible' => 1], [[true, 2], [true, 2], [false], [false]]];
yield ['/api/activities', 1, ['project' => '2', 'projects' => '2,2', 'visible' => '3'], [[true, 2], [true, 2], [true, 2], [false], [false], [false]]];
yield ['/api/activities', 1, ['projects' => '2,2', 'visible' => 2], [[true, 2], [false]]];
yield ['/api/activities', 1, ['projects' => '2', 'visible' => 2], [[true, 2], [false]]];
}
public function testGetCollectionWithQuery()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->loadActivityTestData($client);
$imports = $this->loadActivityTestData();
$query = ['order' => 'ASC', 'orderBy' => 'project'];
$this->assertAccessIsGranted($client, '/api/activities', 'GET', $query);
@@ -169,9 +201,9 @@ class ActivityControllerTest extends APIControllerBaseTest
$this->assertNotEmpty($result);
$this->assertEquals(5, \count($result));
self::assertApiResponseTypeStructure('ActivityCollection', $result[0]);
$this->assertEquals(1, $result[4]['project']);
$this->assertEquals(2, $result[3]['project']);
$this->assertEquals(2, $result[2]['project']);
$this->assertEquals($imports[0]->getId(), $result[4]['project']);
$this->assertEquals($imports[1]->getId(), $result[3]['project']);
$this->assertEquals($imports[1]->getId(), $result[2]['project']);
}
public function testGetEntity()

View File

@@ -15,6 +15,7 @@ use App\Entity\Customer;
use App\Entity\CustomerMeta;
use App\Entity\CustomerRate;
use App\Entity\Project;
use App\Entity\RateInterface;
use App\Entity\Team;
use App\Entity\User;
use App\Repository\CustomerRateRepository;
@@ -29,6 +30,20 @@ class CustomerControllerTest extends APIControllerBaseTest
{
use RateControllerTestTrait;
/**
* @param CustomerRate $rate
* @param bool $isCollection
* @return string
*/
protected function getRateUrlByRate(RateInterface $rate, bool $isCollection): string
{
if ($isCollection) {
return $this->getRateUrl($rate->getCustomer()->getId());
}
return $this->getRateUrl($rate->getCustomer()->getId(), $rate->getId());
}
protected function getRateUrl($id = '1', $rateId = null): string
{
if (null !== $rateId) {

View File

@@ -14,6 +14,7 @@ use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\ProjectMeta;
use App\Entity\ProjectRate;
use App\Entity\RateInterface;
use App\Entity\Team;
use App\Entity\User;
use App\Repository\ProjectRateRepository;
@@ -30,6 +31,20 @@ class ProjectControllerTest extends APIControllerBaseTest
{
use RateControllerTestTrait;
/**
* @param ProjectRate $rate
* @param bool $isCollection
* @return string
*/
protected function getRateUrlByRate(RateInterface $rate, bool $isCollection): string
{
if ($isCollection) {
return $this->getRateUrl($rate->getProject()->getId());
}
return $this->getRateUrl($rate->getProject()->getId(), $rate->getId());
}
protected function getRateUrl($id = '1', $rateId = null): string
{
if (null !== $rateId) {
@@ -139,15 +154,32 @@ class ProjectControllerTest extends APIControllerBaseTest
$em->persist($project);
$em->flush();
return [$customer, $customer2, $customer3];
}
/**
* @dataProvider getCollectionTestData
*/
public function testGetCollectionWithParams($url, $parameters, $expected)
public function testGetCollectionWithParams($url, $customer, $parameters, $expected)
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->loadProjectTestData($client);
$imports = $this->loadProjectTestData($client);
$customerId = $customer !== null ? $imports[$customer]->getId() : null;
if ($customerId !== null) {
if (\array_key_exists('customer', $parameters)) {
$parameters['customer'] = $customerId;
} elseif (\array_key_exists('customers', $parameters)) {
if (stripos($parameters['customers'], ',') !== false) {
$parameters['customers'] = $customerId . ',' . $customerId;
} else {
$parameters['customers'] = (string) $customerId;
}
}
}
$this->assertAccessIsGranted($client, $url, 'GET', $parameters);
$result = json_decode($client->getResponse()->getContent(), true);
@@ -156,29 +188,30 @@ class ProjectControllerTest extends APIControllerBaseTest
for ($i = 0; $i < \count($expected); $i++) {
$project = $result[$i];
$compare = $expected[$i];
self::assertApiResponseTypeStructure('ProjectCollection', $project);
$this->assertEquals($compare[1], $project['customer']);
if ($customerId !== null) {
$this->assertEquals($customerId, $project['customer']);
}
}
}
public function getCollectionTestData()
{
// if you wonder why: SQLite does case-sensitive ordering, so "Title" > "fifth”
yield ['/api/projects', [], [[true, 1], [false, 1], [false, 3]]];
yield ['/api/projects', ['customer' => '1'], [[true, 1], [false, 1]]];
yield ['/api/projects', ['customer' => '1', 'visible' => VisibilityInterface::SHOW_VISIBLE], [[true, 1], [false, 1]]];
yield ['/api/projects', ['customer' => '1', 'visible' => VisibilityInterface::SHOW_BOTH], [[true, 1], [false, 1], [false, 1]]];
yield ['/api/projects', ['customer' => '1', 'visible' => VisibilityInterface::SHOW_HIDDEN], [[false, 1]]];
// if you wonder why: case-sensitive ordering feels strange ... "Title" > "fifth”
yield ['/api/projects', null, [], [[true, 1], [false, 1], [false, 3]]];
yield ['/api/projects', 0, ['customer' => '1'], [[true, 1], [false, 1]]];
yield ['/api/projects', 0, ['customer' => '1', 'visible' => VisibilityInterface::SHOW_VISIBLE], [[true, 1], [false, 1]]];
yield ['/api/projects', 0, ['customer' => '1', 'visible' => VisibilityInterface::SHOW_BOTH], [[true, 1], [false, 1], [false, 1]]];
yield ['/api/projects', 0, ['customer' => '1', 'visible' => VisibilityInterface::SHOW_HIDDEN], [[false, 1]]];
// customer is invisible, so nothing should be returned
yield ['/api/projects', ['customer' => '2', 'visible' => VisibilityInterface::SHOW_VISIBLE], []];
yield ['/api/projects', ['customer' => '2', 'visible' => VisibilityInterface::SHOW_BOTH], [[false, 2], [false, 2]]];
yield ['/api/projects', ['customer' => '2', 'customers' => '2', 'visible' => VisibilityInterface::SHOW_BOTH], [[false, 2], [false, 2]]];
yield ['/api/projects', ['customer' => '2', 'customers' => '2,2', 'visible' => VisibilityInterface::SHOW_BOTH], [[false, 2], [false, 2]]];
yield ['/api/projects', 1, ['customer' => '2', 'visible' => VisibilityInterface::SHOW_VISIBLE], []];
yield ['/api/projects', 1, ['customer' => '2', 'visible' => VisibilityInterface::SHOW_BOTH], [[false, 2], [false, 2]]];
yield ['/api/projects', 1, ['customer' => '2', 'customers' => '2', 'visible' => VisibilityInterface::SHOW_BOTH], [[false, 2], [false, 2]]];
yield ['/api/projects', 1, ['customer' => '2', 'customers' => '2,2', 'visible' => VisibilityInterface::SHOW_BOTH], [[false, 2], [false, 2]]];
// customer is invisible, so nothing should be returned
yield ['/api/projects', ['customer' => '2', 'visible' => VisibilityInterface::SHOW_HIDDEN, 'start' => '2010-12-11T23:59:59', 'end' => '2030-12-11T23:59:59'], []];
yield ['/api/projects', ['customers' => '2', 'visible' => VisibilityInterface::SHOW_HIDDEN, 'start' => '2010-12-11T23:59:59', 'end' => '2030-12-11T23:59:59'], []];
yield ['/api/projects', ['customers' => '2,2', 'visible' => VisibilityInterface::SHOW_HIDDEN, 'start' => '2010-12-11T23:59:59', 'end' => '2030-12-11T23:59:59'], []];
yield ['/api/projects', 1, ['customer' => '2', 'visible' => VisibilityInterface::SHOW_HIDDEN, 'start' => '2010-12-11T23:59:59', 'end' => '2030-12-11T23:59:59'], []];
yield ['/api/projects', 1, ['customers' => '2', 'visible' => VisibilityInterface::SHOW_HIDDEN, 'start' => '2010-12-11T23:59:59', 'end' => '2030-12-11T23:59:59'], []];
yield ['/api/projects', 1, ['customers' => '2,2', 'visible' => VisibilityInterface::SHOW_HIDDEN, 'start' => '2010-12-11T23:59:59', 'end' => '2030-12-11T23:59:59'], []];
}
public function testGetEntity()
@@ -207,8 +240,9 @@ class ProjectControllerTest extends APIControllerBaseTest
->setEnd($endDate)
;
$em->persist($project);
$em->flush();
$this->assertAccessIsGranted($client, '/api/projects/2');
$this->assertAccessIsGranted($client, '/api/projects/' . $project->getId());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
@@ -216,8 +250,8 @@ class ProjectControllerTest extends APIControllerBaseTest
$expected = [
'parentTitle' => 'first one',
'customer' => 2,
'id' => 2,
'customer' => $customer->getId(),
'id' => $project->getId(),
'name' => 'first',
'orderNumber' => null,
// make sure the timezone is properly applied in serializer (see #1858)

View File

@@ -9,6 +9,7 @@
namespace App\Tests\API;
use App\Entity\RateInterface;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;
@@ -24,9 +25,11 @@ trait RateControllerTestTrait
*/
abstract protected function getRateUrl($id = '1', $rateId = null): string;
abstract protected function getRateUrlByRate(RateInterface $rate, bool $isCollection): string;
/**
* @param string|int $id
* @return array
* @return RateInterface[]
*/
abstract protected function importTestRates($id): array;
@@ -160,12 +163,12 @@ trait RateControllerTestTrait
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$expectedRates = $this->importTestRates(1);
$this->request($client, $this->getRateUrl(1, 1), 'DELETE');
$this->request($client, $this->getRateUrlByRate($expectedRates[0], false), 'DELETE');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertEmpty($client->getResponse()->getContent());
// fetch rates to validate that one was removed
$this->request($client, $this->getRateUrl(1));
$this->request($client, $this->getRateUrlByRate($expectedRates[0], true));
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);

View File

@@ -9,24 +9,28 @@
namespace App\Tests\API;
use App\Entity\Tag;
use App\Entity\User;
use App\Tests\DataFixtures\TagFixtures;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
* @group integration
*/
class TagControllerTest extends APIControllerBaseTest
{
protected function importTagFixtures(HttpKernelBrowser $client): void
/**
* @return Tag[]
*/
protected function importTagFixtures(): array
{
$tagList = ['Test', 'Administration', 'Support', '#2018-001', '#2018-002', '#2018-003', 'Development',
'Marketing', 'First Level Support', 'Bug Fixing'];
$fixture = new TagFixtures();
$fixture->setTagArray($tagList);
$this->importFixture($fixture);
return $this->importFixture($fixture);
}
public function testIsSecure()
@@ -37,7 +41,7 @@ class TagControllerTest extends APIControllerBaseTest
public function testGetCollection()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importTagFixtures($client);
$this->importTagFixtures();
$this->assertAccessIsGranted($client, '/api/tags');
$result = json_decode($client->getResponse()->getContent(), true);
@@ -50,7 +54,7 @@ class TagControllerTest extends APIControllerBaseTest
public function testEmptyCollection()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importTagFixtures($client);
$this->importTagFixtures();
$query = ['name' => 'nothing'];
$this->assertAccessIsGranted($client, '/api/tags', 'GET', $query);
$result = json_decode($client->getResponse()->getContent(), true);
@@ -63,7 +67,7 @@ class TagControllerTest extends APIControllerBaseTest
public function testPostAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTagFixtures($client);
$this->importTagFixtures();
$data = [
'name' => 'foo',
'color' => '#000FFF'
@@ -81,7 +85,7 @@ class TagControllerTest extends APIControllerBaseTest
public function testPostActionWithValidationErrors()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTagFixtures($client);
$this->importTagFixtures();
$data = [
'name' => '1',
'color' => '11231231231',
@@ -95,7 +99,7 @@ class TagControllerTest extends APIControllerBaseTest
public function testPostActionWithInvalidUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importTagFixtures($client);
$this->importTagFixtures();
$data = [
'name' => 'foo',
];
@@ -110,7 +114,7 @@ class TagControllerTest extends APIControllerBaseTest
public function testPartOfEntries()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importTagFixtures($client);
$this->importTagFixtures();
$query = ['name' => 'in'];
$this->assertAccessIsGranted($client, '/api/tags', 'GET', $query);
$result = json_decode($client->getResponse()->getContent(), true);
@@ -127,9 +131,10 @@ class TagControllerTest extends APIControllerBaseTest
public function testDeleteAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTagFixtures($client);
$tags = $this->importTagFixtures();
$id = $tags[0]->getId();
$this->request($client, '/api/tags/1', 'DELETE');
$this->request($client, '/api/tags/' . $id, 'DELETE');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
$this->assertEmpty($client->getResponse()->getContent());
@@ -142,6 +147,6 @@ class TagControllerTest extends APIControllerBaseTest
public function testDeleteActionWithUnknownTimesheet()
{
$this->assertEntityNotFoundForDelete(User::ROLE_ADMIN, '/api/tags/255');
$this->assertEntityNotFoundForDelete(User::ROLE_ADMIN, '/api/tags/' . PHP_INT_MAX);
}
}

View File

@@ -9,21 +9,25 @@
namespace App\Tests\API;
use App\Entity\Team;
use App\Entity\User;
use App\Tests\DataFixtures\TeamFixtures;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
* @group integration
*/
class TeamControllerTest extends APIControllerBaseTest
{
protected function importTeamFixtures(HttpKernelBrowser $client): void
/**
* @return Team[]
*/
protected function importTeamFixtures(): array
{
$fixture = new TeamFixtures();
$fixture->setAmount(1);
$this->importFixture($fixture);
return $this->importFixture($fixture);
}
public function testIsSecure()
@@ -50,7 +54,7 @@ class TeamControllerTest extends APIControllerBaseTest
public function testGetCollection()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTeamFixtures($client);
$this->importTeamFixtures();
$this->assertAccessIsGranted($client, '/api/teams');
$result = json_decode($client->getResponse()->getContent(), true);
@@ -63,8 +67,10 @@ class TeamControllerTest extends APIControllerBaseTest
public function testGetEntity()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTeamFixtures($client);
$this->assertAccessIsGranted($client, '/api/teams/2');
$teams = $this->importTeamFixtures();
$id = $teams[0]->getId();
$this->assertAccessIsGranted($client, '/api/teams/' . $id);
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
@@ -178,8 +184,9 @@ class TeamControllerTest extends APIControllerBaseTest
public function testDeleteAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTeamFixtures($client);
$this->assertAccessIsGranted($client, '/api/teams/2');
$teams = $this->importTeamFixtures();
$id = $teams[0]->getId();
$this->assertAccessIsGranted($client, '/api/teams/' . $id);
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);

View File

@@ -31,7 +31,11 @@ class TimesheetControllerTest extends APIControllerBaseTest
public const DATE_FORMAT_HTML5 = 'Y-m-d\TH:i:s';
public const TEST_TIMEZONE = 'Europe/London';
protected function importFixtureForUser(string $role)
/**
* @param string $role
* @return Timesheet[]
*/
protected function importFixtureForUser(string $role): array
{
$fixture = new TimesheetFixtures();
$fixture
@@ -43,7 +47,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
->setStartDate((new \DateTime('first day of this month'))->setTime(0, 0, 1))
;
$this->importFixture($fixture);
return $this->importFixture($fixture);
}
public function testIsSecure()
@@ -300,8 +304,9 @@ class TimesheetControllerTest extends APIControllerBaseTest
->addTag($tag)
;
$em->persist($timesheet);
$em->flush();
$this->assertAccessIsGranted($client, '/api/timesheets/1');
$this->assertAccessIsGranted($client, '/api/timesheets/' . $timesheet->getId());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
@@ -314,7 +319,6 @@ class TimesheetControllerTest extends APIControllerBaseTest
'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',
@@ -337,16 +341,17 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_ADMIN);
$timesheets = $this->importFixtureForUser(User::ROLE_ADMIN);
$this->assertCount(10, $timesheets);
$this->assertApiAccessDenied($client, '/api/timesheets/15', 'You are not allowed to view this timesheet');
$this->assertApiAccessDenied($client, '/api/timesheets/' . $timesheets[0]->getId(), 'You are not allowed to view this timesheet');
}
public function testGetEntityAccessAllowedForAdmin()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importFixtureForUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/timesheets/1');
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/timesheets/' . $timesheets[0]->getId());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
@@ -355,7 +360,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testGetEntityNotFound()
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/20');
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/' . PHP_INT_MAX);
}
public function testPostAction()
@@ -496,7 +501,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$dateTime = new DateTimeFactory(new \DateTimeZone(self::TEST_TIMEZONE));
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$data = [
'activity' => 1,
'project' => 1,
@@ -505,7 +510,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
'description' => 'foo',
'exported' => true,
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/' . $timesheets[0]->getId(), 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
@@ -530,7 +535,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
->setStartDate(new \DateTime('-10 days'))
->setAllowEmptyDescriptions(false)
;
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$data = [
'activity' => 1,
@@ -540,7 +545,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
'description' => 'foo',
'exported' => true,
];
$this->request($client, '/api/timesheets/15', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/' . $timesheets[0]->getId(), 'PATCH', [], json_encode($data));
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$this->assertEquals(Response::HTTP_FORBIDDEN, $response->getStatusCode());
@@ -556,7 +561,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testInvalidPatchAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$data = [
'activity' => 10,
@@ -565,7 +570,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
'end' => (new \DateTime('- 7 hours'))->format('Y-m-d H:m'),
'description' => 'foo',
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/' . $timesheets[0]->getId(), 'PATCH', [], json_encode($data));
$response = $client->getResponse();
$this->assertEquals(400, $response->getStatusCode());
@@ -577,8 +582,8 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testDeleteAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/timesheets/1');
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/timesheets/' . $timesheets[0]->getId());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
@@ -600,11 +605,9 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testDeleteActionForDifferentUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = 1;
$this->request($client, '/api/timesheets/' . $id, 'DELETE');
$this->request($client, '/api/timesheets/' . $timesheets[0]->getId(), 'DELETE');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
$this->assertEmpty($client->getResponse()->getContent());
@@ -614,9 +617,9 @@ class TimesheetControllerTest extends APIControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_ADMIN);
$timesheets = $this->importFixtureForUser(User::ROLE_ADMIN);
$this->request($client, '/api/timesheets/15', 'DELETE');
$this->request($client, '/api/timesheets/' . $timesheets[0]->getId(), 'DELETE');
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
@@ -632,12 +635,13 @@ class TimesheetControllerTest extends APIControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$id = $timesheet->getId();
$timesheet->setExported(true);
$em->persist($timesheet);
$em->flush();
$this->request($client, '/api/timesheets/1', 'DELETE');
$this->request($client, '/api/timesheets/' . $id, 'DELETE');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to delete this timesheet');
}
@@ -648,12 +652,13 @@ class TimesheetControllerTest extends APIControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$id = $timesheet->getId();
$timesheet->setExported(true);
$em->persist($timesheet);
$em->flush();
$this->request($client, '/api/timesheets/1', 'DELETE');
$this->request($client, '/api/timesheets/' . $id, 'DELETE');
$this->assertTrue($client->getResponse()->isSuccessful());
}
@@ -731,9 +736,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
->setStartDate($start)
->setAmountRunning(1)
;
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$id = $timesheets[0]->getId();
$this->request($client, '/api/timesheets/11/stop', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/stop', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
@@ -743,15 +749,16 @@ class TimesheetControllerTest extends APIControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
}
public function testStopActionFailsOnStoppedEntry()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$this->request($client, '/api/timesheets/1/stop', 'PATCH');
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
$this->request($client, '/api/timesheets/' . $id . '/stop', 'PATCH');
$this->assertApiException($client->getResponse(), 'Timesheet entry already stopped');
}
@@ -777,9 +784,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
->setStartDate($start)
->setAmountRunning(3)
;
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$id = $timesheets[3]->getId();
$this->request($client, '/api/timesheets/12/stop', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/stop', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to stop this timesheet');
}
@@ -806,7 +814,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(5, \count($result));
$this->assertEquals(10, \count($result));
self::assertApiResponseTypeStructure('TimesheetCollection', $result[0]);
$query = ['tags' => 'Test,Admin'];
@@ -831,15 +839,16 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testRestartAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
$data = [
'description' => 'foo',
'tags' => 'another,testing,bar'
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/' . $id, 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/1/restart', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/restart', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
@@ -861,16 +870,17 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testRestartActionWithBegin()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
$data = [
'description' => 'foo',
'tags' => 'another,testing,bar'
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/' . $id, 'PATCH', [], json_encode($data));
$begin = new \DateTime('2019-11-27 13:55:00');
$this->request($client, '/api/timesheets/1/restart', 'PATCH', ['begin' => $begin->format(BaseApiController::DATE_FORMAT_PHP)]);
$this->request($client, '/api/timesheets/' . $id . '/restart', 'PATCH', ['begin' => $begin->format(BaseApiController::DATE_FORMAT_PHP)]);
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
@@ -893,11 +903,12 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testRestartActionWithCopyData()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$timesheet->setDescription('foo');
$timesheet->addTag((new Tag())->setName('another'));
$timesheet->addTag((new Tag())->setName('testing'));
@@ -908,10 +919,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
$em->persist($timesheet);
$em->flush();
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertEquals('foo', $timesheet->getDescription());
$this->request($client, '/api/timesheets/1/restart', 'PATCH', ['copy' => 'all']);
$this->request($client, '/api/timesheets/' . $id . '/restart', 'PATCH', ['copy' => 'all']);
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
@@ -946,9 +957,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
->setStartDate($start)
->setAmountRunning(3)
;
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$id = $timesheets[0]->getId();
$this->request($client, '/api/timesheets/2/restart', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/restart', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to re-start this timesheet');
}
@@ -999,56 +1011,61 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testExportAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertFalse($timesheet->isExported());
$this->request($client, '/api/timesheets/1/export', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/export', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
self::assertApiResponseTypeStructure('TimesheetEntity', $result);
$em->clear();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertTrue($timesheet->isExported());
$this->request($client, '/api/timesheets/1/export', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/export', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$em->clear();
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertFalse($timesheet->isExported());
}
public function testExportNotAllowedForUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
$this->request($client, '/api/timesheets/1/export', 'PATCH');
$this->request($client, '/api/timesheets/' . $id . '/export', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to lock this timesheet');
}
public function testExportThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/42/export', []);
$id = PHP_INT_MAX;
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/' . $id . '/export', []);
}
public function testMetaActionThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/42/meta', []);
$id = PHP_INT_MAX;
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/' . $id . '/meta', []);
}
public function testMetaActionThrowsExceptionOnMissingName()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
return $this->assertExceptionForMethod($client, '/api/timesheets/1/meta', 'PATCH', ['value' => 'X'], [
return $this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['value' => 'X'], [
'code' => 400,
'message' => 'Parameter "name" of value "NULL" violated a constraint "This value should not be null."'
]);
@@ -1057,9 +1074,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testMetaActionThrowsExceptionOnMissingValue()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
return $this->assertExceptionForMethod($client, '/api/timesheets/1/meta', 'PATCH', ['name' => 'X'], [
return $this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['name' => 'X'], [
'code' => 400,
'message' => 'Parameter "value" of value "NULL" violated a constraint "This value should not be null."'
]);
@@ -1068,9 +1086,10 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testMetaActionThrowsExceptionOnMissingMetafield()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
return $this->assertExceptionForMethod($client, '/api/timesheets/1/meta', 'PATCH', ['name' => 'X', 'value' => 'Y'], [
return $this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['name' => 'X', 'value' => 'Y'], [
'code' => 500,
'message' => 'Unknown meta-field requested'
]);
@@ -1079,14 +1098,15 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testMetaAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importFixtureForUser(User::ROLE_USER);
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
static::$container->get('event_dispatcher')->addSubscriber(new TimesheetTestMetaFieldSubscriberMock());
$data = [
'name' => 'metatestmock',
'value' => 'another,testing,bar'
];
$this->request($client, '/api/timesheets/1/meta', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/' . $id . '/meta', 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -1096,7 +1116,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertEquals('another,testing,bar', $timesheet->getMetaField('metatestmock')->getValue());
}
}