added API functions to save meta fields (#1063)

This commit is contained in:
Kevin Papst
2019-08-26 14:30:39 +02:00
committed by GitHub
parent ceeec7b8a6
commit 15673555a2
14 changed files with 624 additions and 68 deletions

View File

@@ -151,6 +151,14 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
}
protected function assertEntityNotFoundForPatch(string $role, string $url, array $data)
{
return $this->assertExceptionForPatchAction($role, $url, $data, [
'code' => 404,
'message' => 'Not found'
]);
}
protected function assertExceptionForPatchAction(string $role, string $url, array $data, array $expectedErrors)
{
$client = $this->getClientForAuthenticatedUser($role);
@@ -158,15 +166,10 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$expected = [
'code' => 404,
'message' => 'Not found'
];
$this->assertEquals(404, $client->getResponse()->getStatusCode());
$this->assertEquals($expectedErrors['code'], $client->getResponse()->getStatusCode());
$this->assertEquals(
$expected,
$expectedErrors,
json_decode($client->getResponse()->getContent(), true)
);
}

View File

@@ -13,6 +13,7 @@ use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\User;
use App\Tests\Mocks\ActivityTestMetaFieldSubscriberMock;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\HttpFoundation\Response;
@@ -219,6 +220,54 @@ class ActivityControllerTest extends APIControllerBaseTest
$this->assertApiCallValidationError($response, ['project']);
}
public function testMetaActionThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/activities/42/meta', []);
}
public function testMetaActionThrowsExceptionOnMissingName()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/activities/1/meta', ['value' => 'X'], [
'code' => 400,
'message' => 'Parameter "name" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingValue()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/activities/1/meta', ['name' => 'X'], [
'code' => 400,
'message' => 'Parameter "value" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingMetafield()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/activities/1/meta', ['name' => 'X', 'value' => 'Y'], [
'code' => 500,
'message' => 'Unknown meta-field requested'
]);
}
public function testMetaAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$client->getContainer()->get('event_dispatcher')->addSubscriber(new ActivityTestMetaFieldSubscriberMock());
$data = [
'name' => 'metatestmock',
'value' => 'another,testing,bar'
];
$this->request($client, '/api/activities/1/meta', 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Activity $activity */
$activity = $em->getRepository(Activity::class)->find(1);
$this->assertEquals('another,testing,bar', $activity->getMetaField('metatestmock')->getValue());
}
protected function assertStructure(array $result, $full = true)
{
$expectedKeys = [

View File

@@ -9,7 +9,9 @@
namespace App\Tests\API;
use App\Entity\Customer;
use App\Entity\User;
use App\Tests\Mocks\CustomerTestMetaFieldSubscriberMock;
use Symfony\Component\HttpFoundation\Response;
/**
@@ -161,6 +163,54 @@ class CustomerControllerTest extends APIControllerBaseTest
$this->assertApiCallValidationError($response, ['currency']);
}
public function testMetaActionThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/customers/42/meta', []);
}
public function testMetaActionThrowsExceptionOnMissingName()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/customers/1/meta', ['value' => 'X'], [
'code' => 400,
'message' => 'Parameter "name" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingValue()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/customers/1/meta', ['name' => 'X'], [
'code' => 400,
'message' => 'Parameter "value" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingMetafield()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/customers/1/meta', ['name' => 'X', 'value' => 'Y'], [
'code' => 500,
'message' => 'Unknown meta-field requested'
]);
}
public function testMetaAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$client->getContainer()->get('event_dispatcher')->addSubscriber(new CustomerTestMetaFieldSubscriberMock());
$data = [
'name' => 'metatestmock',
'value' => 'another,testing,bar'
];
$this->request($client, '/api/customers/1/meta', 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Customer $customer */
$customer = $em->getRepository(Customer::class)->find(1);
$this->assertEquals('another,testing,bar', $customer->getMetaField('metatestmock')->getValue());
}
protected function assertStructure(array $result, $full = true)
{
$expectedKeys = [

View File

@@ -13,6 +13,7 @@ use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\User;
use App\Repository\Query\VisibilityQuery;
use App\Tests\Mocks\ProjectTestMetaFieldSubscriberMock;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\HttpFoundation\Response;
@@ -211,6 +212,54 @@ class ProjectControllerTest extends APIControllerBaseTest
$this->assertApiCallValidationError($response, ['customer']);
}
public function testMetaActionThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/projects/42/meta', []);
}
public function testMetaActionThrowsExceptionOnMissingName()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/projects/1/meta', ['value' => 'X'], [
'code' => 400,
'message' => 'Parameter "name" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingValue()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/projects/1/meta', ['name' => 'X'], [
'code' => 400,
'message' => 'Parameter "value" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingMetafield()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/projects/1/meta', ['name' => 'X', 'value' => 'Y'], [
'code' => 500,
'message' => 'Unknown meta-field requested'
]);
}
public function testMetaAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$client->getContainer()->get('event_dispatcher')->addSubscriber(new ProjectTestMetaFieldSubscriberMock());
$data = [
'name' => 'metatestmock',
'value' => 'another,testing,bar'
];
$this->request($client, '/api/projects/1/meta', 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Project $project */
$project = $em->getRepository(Project::class)->find(1);
$this->assertEquals('another,testing,bar', $project->getMetaField('metatestmock')->getValue());
}
protected function assertStructure(array $result, $full = true)
{
$expectedKeys = [

View File

@@ -18,6 +18,7 @@ use App\Entity\TimesheetMeta;
use App\Entity\User;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\Mocks\Security\UserDateTimeFactoryFactory;
use App\Tests\Mocks\TimesheetTestMetaFieldSubscriberMock;
use App\Timesheet\UserDateTimeFactory;
use Symfony\Component\HttpFoundation\Response;
@@ -619,7 +620,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testStopThrowsNotFound()
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/11/stop', 'PATCH');
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/11/stop', []);
}
public function testStopNotAllowedForUser()
@@ -782,7 +783,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testRestartThrowsNotFound()
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/42/restart', 'PATCH');
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/42/restart', []);
}
public function testExportAction()
@@ -821,7 +822,55 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testExportThrowsNotFound()
{
$this->assertEntityNotFound(User::ROLE_ADMIN, '/api/timesheets/42/export', 'PATCH');
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/42/export', []);
}
public function testMetaActionThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/42/meta', []);
}
public function testMetaActionThrowsExceptionOnMissingName()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/timesheets/1/meta', ['value' => 'X'], [
'code' => 400,
'message' => 'Parameter "name" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingValue()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/timesheets/1/meta', ['name' => 'X'], [
'code' => 400,
'message' => 'Parameter "value" of value "NULL" violated a constraint "This value should not be null."'
]);
}
public function testMetaActionThrowsExceptionOnMissingMetafield()
{
return $this->assertExceptionForPatchAction(User::ROLE_ADMIN, '/api/timesheets/1/meta', ['name' => 'X', 'value' => 'Y'], [
'code' => 500,
'message' => 'Unknown meta-field requested'
]);
}
public function testMetaAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$client->getContainer()->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->assertTrue($client->getResponse()->isSuccessful());
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @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)