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());
}
}

View File

@@ -1,38 +0,0 @@
<?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\Command;
use App\Command\CreateReleaseCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \App\Command\CreateReleaseCommand
* @group integration
*/
class CreateReleaseCommandTest extends KernelTestCase
{
public function testCommandName()
{
$kernel = self::bootKernel(['environment' => 'test']);
$application = new Application($kernel);
$application->add(new CreateReleaseCommand(realpath(__DIR__ . '/../../'), 'test'));
$command = $application->find('kimai:create-release');
self::assertTrue($command->isEnabled());
self::assertInstanceOf(CreateReleaseCommand::class, $command);
}
public function testCommandNameIsNotAvailableInProd()
{
$command = new CreateReleaseCommand(realpath(__DIR__ . '/../../'), 'prod');
self::assertFalse($command->isEnabled());
}
}

View File

@@ -10,11 +10,9 @@
namespace App\Tests\Command;
use App\Command\InstallCommand;
use App\Constants;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @covers \App\Command\InstallCommand
@@ -40,30 +38,4 @@ class InstallCommandTest extends KernelTestCase
return $this->application->find('kimai:install');
}
public function testFullRunWithEverythingPreInstalled()
{
$command = $this->getCommand();
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Kimai installation running', $result);
// create database is skipped
self::assertStringContainsString('[NOTE] Database is existing and connection could be established', $result);
// make sure migrations run always
self::assertStringContainsString('Application Migrations', $result);
self::assertStringContainsString('No migrations to execute.', $result);
self::assertStringContainsString(
sprintf('[OK] Congratulations! Successfully installed Kimai 2 version %s (%s)', Constants::VERSION, Constants::STATUS),
$result
);
self::assertEquals(0, $commandTester->getStatusCode());
}
}

View File

@@ -21,7 +21,7 @@ use App\Repository\ProjectRepository;
use App\Repository\TimesheetRepository;
use App\Repository\UserRepository;
use App\Tests\DataFixtures\CustomerFixtures;
use App\Tests\DataFixtures\InvoiceFixtures;
use App\Tests\DataFixtures\InvoiceTemplateFixtures;
use App\Tests\DataFixtures\ProjectFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\KernelTestTrait;
@@ -176,7 +176,7 @@ class InvoiceCreateCommandTest extends KernelTestCase
public function testCreateInvoice()
{
$fixture = new InvoiceFixtures();
$fixture = new InvoiceTemplateFixtures();
$this->importFixture($fixture);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--set-exported' => null, '--customer' => 1, '--template' => 'Invoice', '--start' => '2020-01-01', '--end' => '2020-03-01']);
@@ -185,7 +185,7 @@ class InvoiceCreateCommandTest extends KernelTestCase
$this->assertStringContainsString('+----+----------+-------+------------- Created 1 invoice(s) --------------------------------------+', $output);
$this->assertStringContainsString('| ID | Customer | Total | Filename |', $output);
$this->assertStringContainsString('+----+----------+-------+-------------------------------------------------------------------------+', $output);
$this->assertStringContainsString('| 1 | Test | 0 EUR | /', $output);
$this->assertStringContainsString('| Test | 0 EUR | /', $output);
$this->assertStringContainsString('/tests/_data/invoices/' . ((new \DateTime())->format('Y')) . '-001-Test.html |', $output);
}
@@ -201,22 +201,24 @@ class InvoiceCreateCommandTest extends KernelTestCase
$meta->setValue('Invoice');
$customer->setMetaField($meta);
});
$this->importFixture($fixture);
$customer = $this->importFixture($fixture)[0];
$fixture = new ProjectFixtures();
$fixture->setCustomers([$em->getRepository(Customer::class)->find(2)]);
$fixture->setCustomers([$customer]);
$fixture->setAmount(1);
$this->importFixture($fixture);
$projects = $this->importFixture($fixture);
$fixture = new TimesheetFixtures();
$fixture->setUser($this->getUserByName(UserFixtures::USERNAME_SUPER_ADMIN));
$fixture->setAmount(20);
$fixture->setStartDate($start);
$fixture->setProjects([$em->getRepository(Project::class)->find(2)]);
$fixture->setProjects($projects);
$this->importFixture($fixture);
$fixture = new InvoiceFixtures();
$fixture = new InvoiceTemplateFixtures();
$this->importFixture($fixture);
return [$customer];
}
public function testCreateInvoiceByCustomer()
@@ -237,9 +239,10 @@ class InvoiceCreateCommandTest extends KernelTestCase
$start = new \DateTime('-2 months');
$end = new \DateTime();
$this->prepareFixtures($start);
$imports = $this->prepareFixtures($start);
$customer = $imports[0]->getId();
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => '2,1', '--template-meta' => 'template', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => $customer . ',1', '--template-meta' => 'template', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Created 1 invoice(s) ', $output);

View File

@@ -9,29 +9,30 @@
namespace App\Tests\Command;
use App\Command\ResetCommand;
use App\Command\ResetDevelopmentCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \App\Command\ResetCommand
* @covers \App\Command\ResetDevelopmentCommand
* @group integration
*/
class ResetCommandTest extends KernelTestCase
class ResetDevelopmentCommandTest extends KernelTestCase
{
public function testCommandName()
{
$kernel = self::bootKernel();
$application = new Application($kernel);
$application->add(new ResetCommand('test'));
$application->add(new ResetDevelopmentCommand('test', $this->createMock(EntityManagerInterface::class)));
$command = $application->find('kimai:reset-dev');
self::assertInstanceOf(ResetCommand::class, $command);
self::assertInstanceOf(ResetDevelopmentCommand::class, $command);
}
public function testCommandNameIsNotEnabledInProd()
{
$command = new ResetCommand('prod');
$command = new ResetDevelopmentCommand('prod', $this->createMock(EntityManagerInterface::class));
self::assertFalse($command->isEnabled());
}
}

View File

@@ -0,0 +1,38 @@
<?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\Command;
use App\Command\ResetTestCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \App\Command\ResetTestCommand
* @group integration
*/
class ResetTestCommandTest extends KernelTestCase
{
public function testCommandName()
{
$kernel = self::bootKernel();
$application = new Application($kernel);
$application->add(new ResetTestCommand('test', $this->createMock(EntityManagerInterface::class)));
$command = $application->find('kimai:reset-test');
self::assertInstanceOf(ResetTestCommand::class, $command);
}
public function testCommandNameIsNotEnabledInProd()
{
$command = new ResetTestCommand('prod', $this->createMock(EntityManagerInterface::class));
self::assertFalse($command->isEnabled());
}
}

View File

@@ -0,0 +1,29 @@
<?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;
use App\ConsoleApplication;
use App\Constants;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* @covers \App\ConsoleApplication
*/
class ConsoleApplicationTest extends TestCase
{
public function testVersion()
{
$kernel = $this->createMock(KernelInterface::class);
$sut = new ConsoleApplication($kernel);
self::assertEquals(Constants::SOFTWARE, $sut->getName());
self::assertEquals(Constants::VERSION, $sut->getVersion());
}
}

View File

@@ -180,7 +180,11 @@ class ActivityControllerTest extends ControllerBaseTest
$client->followRedirect();
$this->assertHasDataTable($client);
$this->request($client, '/admin/activity/2/edit');
$activities = $this->getEntityManager()->getRepository(Activity::class)->findAll();
$activity = array_pop($activities);
$id = $activity->getId();
$this->request($client, '/admin/activity/' . $id . '/edit');
$editForm = $client->getCrawler()->filter('form[name=activity_edit_form]')->form();
$this->assertEquals('An AcTiVitY Name', $editForm->get('activity_edit_form[name]')->getValue());
// make sure customer and project are pre-selected for none global activities
@@ -240,15 +244,16 @@ class ActivityControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Activity $activity */
$activity = $em->getRepository(Activity::class)->find(1);
$activity = $em->getRepository(Activity::class)->findAll()[0];
self::assertEquals(0, $activity->getTeams()->count());
$id = $activity->getId();
$fixture = new TeamFixtures();
$fixture->setAmount(2);
$fixture->setAddCustomer(false);
$this->importFixture($fixture);
$this->assertAccessIsGranted($client, '/admin/activity/1/permissions');
$this->assertAccessIsGranted($client, '/admin/activity/' . $id . '/permissions');
$form = $client->getCrawler()->filter('form[name=activity_team_permission_form]')->form();
/** @var ChoiceFormField $team1 */
$team1 = $form->get('activity_team_permission_form[teams][0]');
@@ -263,7 +268,7 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertHasDataTable($client);
/** @var Activity $activity */
$activity = $em->getRepository(Activity::class)->find(1);
$activity = $em->getRepository(Activity::class)->find($id);
self::assertEquals(2, $activity->getTeams()->count());
}
@@ -342,10 +347,9 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->findAll();
// $this->assertEquals(0, count($timesheets));
$em->clear();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(0, \count($timesheets));
$this->request($client, '/admin/activity/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
@@ -364,7 +368,9 @@ class ActivityControllerTest extends ControllerBaseTest
$this->importFixture($fixture);
$fixture = new ActivityFixtures();
$fixture->setAmount(1)->setIsGlobal(true)->setIsVisible(true);
$this->importFixture($fixture);
$activities = $this->importFixture($fixture);
$activity = $activities[0];
$id = $activity->getId();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, \count($timesheets));
@@ -381,7 +387,7 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertStringEndsWith($this->createUrl('/admin/activity/1/delete'), $form->getUri());
$client->submit($form, [
'form' => [
'activity' => 2
'activity' => $id
]
]);
@@ -395,7 +401,7 @@ class ActivityControllerTest extends ControllerBaseTest
/** @var Timesheet $entry */
foreach ($timesheets as $entry) {
$this->assertEquals(2, $entry->getActivity()->getId());
$this->assertEquals($id, $entry->getActivity()->getId());
}
$this->request($client, '/admin/activity/1/edit');

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Controller;
use App\Entity\Customer;
use App\Entity\CustomerComment;
use App\Entity\CustomerMeta;
use App\Entity\Timesheet;
use App\Entity\User;
@@ -176,9 +177,12 @@ class CustomerControllerTest extends ControllerBaseTest
$node = $client->getCrawler()->filter('div.box#comments_box .direct-chat-msg');
self::assertStringContainsString('Blah foo bar', $node->html());
$node = $client->getCrawler()->filter('div.box#comments_box .box-body a.confirmation-link');
self::assertEquals($this->createUrl('/admin/customer/1/comment_delete'), $node->attr('href'));
self::assertStringEndsWith('/comment_delete', $node->attr('href'));
$this->request($client, '/admin/customer/1/comment_delete');
$comments = $this->getEntityManager()->getRepository(CustomerComment::class)->findAll();
$id = $comments[0]->getId();
$this->request($client, '/admin/customer/' . $id . '/comment_delete');
$this->assertIsRedirect($client, $this->createUrl('/admin/customer/1/details'));
$client->followRedirect();
$node = $client->getCrawler()->filter('div.box#comments_box .box-body');
@@ -202,12 +206,15 @@ class CustomerControllerTest extends ControllerBaseTest
$node = $client->getCrawler()->filter('div.box#comments_box .direct-chat-text a.btn.active');
self::assertEquals(0, $node->count());
$this->request($client, '/admin/customer/1/comment_pin');
$comments = $this->getEntityManager()->getRepository(CustomerComment::class)->findAll();
$id = $comments[0]->getId();
$this->request($client, '/admin/customer/' . $id . '/comment_pin');
$this->assertIsRedirect($client, $this->createUrl('/admin/customer/1/details'));
$client->followRedirect();
$node = $client->getCrawler()->filter('div.box#comments_box .box-body a.btn.active');
self::assertEquals(1, $node->count());
self::assertEquals($this->createUrl('/admin/customer/1/comment_pin'), $node->attr('href'));
self::assertEquals($this->createUrl('/admin/customer/' . $id . '/comment_pin'), $node->attr('href'));
}
public function testCreateDefaultTeamAction()
@@ -278,7 +285,7 @@ class CustomerControllerTest extends ControllerBaseTest
'name' => 'Test Customer',
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/customer/2/details'));
$this->assertIsRedirect($client, '/details');
$client->followRedirect();
$this->assertHasFlashSuccess($client);
}
@@ -352,22 +359,24 @@ class CustomerControllerTest extends ControllerBaseTest
$fixture = new CustomerFixtures();
$fixture->setAmount(1);
$this->importFixture($fixture);
$customers = $this->importFixture($fixture);
$customer = $customers[0];
$id = $customer->getId();
$this->request($client, '/admin/customer/2/edit');
$this->request($client, '/admin/customer/' . $id . '/edit');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->request($client, '/admin/customer/2/delete');
$this->request($client, '/admin/customer/' . $id . '/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=form]')->form();
$this->assertStringEndsWith($this->createUrl('/admin/customer/2/delete'), $form->getUri());
$this->assertStringEndsWith($this->createUrl('/admin/customer/' . $id . '/delete'), $form->getUri());
$client->submit($form);
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->request($client, '/admin/customer/2/edit');
$this->request($client, '/admin/customer/' . $id . '/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}
@@ -401,10 +410,9 @@ class CustomerControllerTest extends ControllerBaseTest
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->findAll();
// $this->assertEquals(0, count($timesheets));
$em->clear();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(0, \count($timesheets));
$this->request($client, '/admin/customer/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
@@ -421,7 +429,9 @@ class CustomerControllerTest extends ControllerBaseTest
$this->importFixture($fixture);
$fixture = new CustomerFixtures();
$fixture->setAmount(1)->setIsVisible(true);
$this->importFixture($fixture);
$customers = $this->importFixture($fixture);
$customer = $customers[0];
$id = $customer->getId();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, \count($timesheets));
@@ -438,7 +448,7 @@ class CustomerControllerTest extends ControllerBaseTest
$this->assertStringEndsWith($this->createUrl('/admin/customer/1/delete'), $form->getUri());
$client->submit($form, [
'form' => [
'customer' => 2
'customer' => $id
]
]);
@@ -452,7 +462,7 @@ class CustomerControllerTest extends ControllerBaseTest
/** @var Timesheet $entry */
foreach ($timesheets as $entry) {
$this->assertEquals(2, $entry->getProject()->getCustomer()->getId());
$this->assertEquals($id, $entry->getProject()->getCustomer()->getId());
}
$this->request($client, '/admin/customer/1/edit');

View File

@@ -9,13 +9,13 @@
namespace App\Tests\Controller;
use App\Entity\Invoice;
use App\Entity\InvoiceTemplate;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Form\Type\DateRangeType;
use App\Tests\DataFixtures\InvoiceFixtures;
use App\Tests\DataFixtures\InvoiceTemplateFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
@@ -69,10 +69,11 @@ class InvoiceControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$templates = $this->importFixture($fixture);
$id = $templates[0]->getId();
$this->request($client, '/invoice/?customer=1&template=1&preview=');
$this->request($client, '/invoice/?customer=1&template=' . $id . '&preview=');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasNoEntriesWithFilter($client);
@@ -82,7 +83,7 @@ class InvoiceControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$fixture = new InvoiceFixtures();
$fixture = new InvoiceTemplateFixtures();
$this->importFixture($fixture);
$this->request($client, '/invoice/template');
@@ -117,15 +118,13 @@ class InvoiceControllerTest extends ControllerBaseTest
public function testCopyTemplateAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$em = $this->getEntityManager();
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$templates = $this->importFixture($fixture);
/** @var InvoiceTemplate $template */
$template = $em->getRepository(InvoiceTemplate::class)->find(1);
$template = $templates[0];
$this->request($client, '/invoice/template/create/1');
$this->request($client, '/invoice/template/create/' . $template->getId());
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=invoice_template_form]')->form();
@@ -144,11 +143,10 @@ class InvoiceControllerTest extends ControllerBaseTest
public function testCreateAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
/** @var EntityManager $em */
$em = $this->getEntityManager();
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$templates = $this->importFixture($fixture);
$template = $templates[0];
$begin = new \DateTime('first day of this month');
$end = new \DateTime('last day of this month');
@@ -158,7 +156,10 @@ class InvoiceControllerTest extends ControllerBaseTest
->setAmount(20)
->setStartDate($begin)
;
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
foreach ($timesheets as $timesheet) {
$this->assertFalse($timesheet->isExported());
}
$this->request($client, '/invoice/');
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -170,7 +171,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$node->setAttribute('action', $this->createUrl('/invoice/?preview='));
$node->setAttribute('method', 'GET');
$client->submit($form, [
'template' => 1,
'template' => $template->getId(),
'daterange' => $dateRange,
'customer' => 1,
]);
@@ -188,7 +189,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$node->setAttribute('action', $this->createUrl('/invoice/?create='));
$node->setAttribute('method', 'GET');
$client->submit($form, [
'template' => 1,
'template' => $template->getId(),
'daterange' => $dateRange,
'customer' => 1,
'projects' => [1],
@@ -200,7 +201,10 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertEquals(1, $node->count());
$this->assertEquals('invoice_print', $node->getIterator()[0]->getAttribute('class'));
$em = $this->getEntityManager();
$em->clear();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertCount(20, $timesheets);
/** @var Timesheet $timesheet */
foreach ($timesheets as $timesheet) {
$this->assertTrue($timesheet->isExported());
@@ -210,11 +214,10 @@ class InvoiceControllerTest extends ControllerBaseTest
public function testPrintAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
/** @var EntityManager $em */
$em = $this->getEntityManager();
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$templates = $this->importFixture($fixture);
$id = $templates[0]->getId();
$begin = new \DateTime('first day of this month');
$end = new \DateTime('last day of this month');
@@ -236,7 +239,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$node->setAttribute('action', $this->createUrl('/invoice/?preview='));
$node->setAttribute('method', 'GET');
$client->submit($form, [
'template' => 1,
'template' => $id,
'daterange' => $dateRange,
'customer' => 1,
]);
@@ -254,7 +257,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$node->setAttribute('action', $this->createUrl('/invoice/?print='));
$node->setAttribute('method', 'GET');
$client->submit($form, [
'template' => 1,
'template' => $id,
'daterange' => $dateRange,
'customer' => 1,
'projects' => [1],
@@ -269,11 +272,10 @@ class InvoiceControllerTest extends ControllerBaseTest
public function testCreateActionAsAdminWithDownloadAndStatusChangeAndDelete()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
/** @var EntityManager $em */
$em = $this->getEntityManager();
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$templates = $this->importFixture($fixture);
$template = $templates[0];
$begin = new \DateTime('first day of this month');
$end = new \DateTime('last day of this month');
@@ -295,7 +297,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$node->setAttribute('action', $this->createUrl('/invoice/?preview='));
$node->setAttribute('method', 'GET');
$client->submit($form, [
'template' => 1,
'template' => $template->getId(),
'daterange' => $dateRange,
'customer' => 1,
]);
@@ -313,14 +315,17 @@ class InvoiceControllerTest extends ControllerBaseTest
$node->setAttribute('action', $this->createUrl('/invoice/?create='));
$node->setAttribute('method', 'GET');
$client->submit($form, [
'template' => 1,
'template' => $template->getId(),
'daterange' => $dateRange,
'customer' => 1,
'projects' => [1],
'markAsExported' => 1,
]);
$this->assertIsRedirect($client, '/invoice/show?id=1');
$invoices = $this->getEntityManager()->getRepository(Invoice::class)->findAll();
$id = $invoices[0]->getId();
$this->assertIsRedirect($client, '/invoice/show?id=' . $id);
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -330,28 +335,28 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertDataTableRowCount($client, 'datatable_invoices', 1);
// make sure the invoice is saved
$this->request($client, '/invoice/download/1');
$this->request($client, '/invoice/download/' . $id);
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
self::assertInstanceOf(BinaryFileResponse::class, $response);
self::assertFileExists($response->getFile());
$this->request($client, '/invoice/change-status/1/pending');
$this->request($client, '/invoice/change-status/' . $id . '/pending');
$this->assertIsRedirect($client, '/invoice/show');
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->request($client, '/invoice/change-status/1/paid');
$this->request($client, '/invoice/change-status/' . $id . '/paid');
$this->assertIsRedirect($client, '/invoice/show');
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->request($client, '/invoice/change-status/1/new');
$this->request($client, '/invoice/change-status/' . $id . '/new');
$this->assertIsRedirect($client, '/invoice/show');
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->request($client, '/invoice/delete/1');
$this->request($client, '/invoice/delete/' . $id);
$this->assertIsRedirect($client, '/invoice/show');
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -361,10 +366,11 @@ class InvoiceControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$template = $this->importFixture($fixture);
$id = $template[0]->getId();
$this->request($client, '/invoice/template/1/edit?page=1');
$this->request($client, '/invoice/template/' . $id . '/edit?page=1');
$form = $client->getCrawler()->filter('form[name=invoice_template_form]')->form();
$client->submit($form, [
'invoice_template_form' => [
@@ -387,26 +393,25 @@ class InvoiceControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$em = $this->getEntityManager();
$fixture = new InvoiceFixtures();
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$template = $this->importFixture($fixture);
$id = $template[0]->getId();
$this->request($client, '/invoice/template/1/delete');
$this->request($client, '/invoice/template/' . $id . '/delete');
$this->assertIsRedirect($client, '/invoice/template');
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$this->assertEquals(0, $em->getRepository(InvoiceTemplate::class)->count([]));
$this->assertEquals(0, $this->getEntityManager()->getRepository(InvoiceTemplate::class)->count([]));
}
public function testUploadDocumentAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$em = $this->getEntityManager();
$fixture = new InvoiceFixtures();
$fixture = new InvoiceTemplateFixtures();
$this->importFixture($fixture);
$this->request($client, '/invoice/document_upload');

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Controller;
use App\DataFixtures\UserFixtures;
use App\Entity\Role;
use App\Entity\RolePermission;
use App\Entity\User;
use Symfony\Component\Security\Csrf\CsrfToken;
@@ -104,6 +105,15 @@ class PermissionControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
$roles = $this->getEntityManager()->getRepository(Role::class)->findAll();
$id = null;
foreach ($roles as $role) {
if ($role->getName() === 'TEST_ROLE') {
$id = $role->getId();
break;
}
}
$content = $client->getResponse()->getContent();
self::assertStringContainsString('<th data-field="TEST_ROLE" class="alwaysVisible text-center">', $content);
@@ -126,7 +136,7 @@ class PermissionControllerTest extends ControllerBaseTest
/** @var CsrfToken $token */
$token = static::$kernel->getContainer()->get('security.csrf.token_manager')->getToken('user_role_permissions');
$this->request($client, '/admin/permissions/roles/1/delete/' . $token->getValue());
$this->request($client, '/admin/permissions/roles/' . $id . '/delete/' . $token->getValue());
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
@@ -166,9 +176,18 @@ class PermissionControllerTest extends ControllerBaseTest
$rolePermissions = $em->getRepository(RolePermission::class)->findAll();
$this->assertEquals(0, \count($rolePermissions));
$roles = $em->getRepository(Role::class)->findAll();
$id = null;
foreach ($roles as $role) {
if ($role->getName() === 'TEST_ROLE') {
$id = $role->getId();
break;
}
}
// create the permission
$token = static::$kernel->getContainer()->get('security.csrf.token_manager')->getToken('user_role_permissions');
$this->request($client, '/admin/permissions/roles/1/view_user/1/' . $token->getValue(), 'POST');
$this->request($client, '/admin/permissions/roles/' . $id . '/view_user/1/' . $token->getValue(), 'POST');
self::assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
@@ -182,14 +201,14 @@ class PermissionControllerTest extends ControllerBaseTest
self::assertEquals('view_user', $permission->getPermission());
self::assertTrue($permission->isAllowed());
self::assertEquals('TEST_ROLE', $permission->getRole()->getName());
self::assertEquals(1, $permission->getRole()->getId());
self::assertEquals($id, $permission->getRole()->getId());
// flush the cache to prevent wrong results
$em->clear();
// update the permission
$token = static::$kernel->getContainer()->get('security.csrf.token_manager')->getToken('user_role_permissions');
$this->request($client, '/admin/permissions/roles/1/view_user/0/' . $token->getValue(), 'POST');
$this->request($client, '/admin/permissions/roles/' . $id . '/view_user/0/' . $token->getValue(), 'POST');
self::assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);

View File

@@ -13,6 +13,7 @@ use App\Entity\Activity;
use App\Entity\ActivityMeta;
use App\Entity\ActivityRate;
use App\Entity\Project;
use App\Entity\ProjectComment;
use App\Entity\ProjectMeta;
use App\Entity\ProjectRate;
use App\Entity\Team;
@@ -213,7 +214,7 @@ class ProjectControllerTest extends ControllerBaseTest
$em->persist($rate);
$this->request($client, '/admin/project/1/duplicate');
$this->assertIsRedirect($client, $this->createUrl('/admin/project/2/details'));
$this->assertIsRedirect($client, '/details');
$client->followRedirect();
$node = $client->getCrawler()->filter('div.box#project_rates_box');
self::assertEquals(1, $node->count());
@@ -253,9 +254,12 @@ class ProjectControllerTest extends ControllerBaseTest
$node = $client->getCrawler()->filter('div.box#comments_box .direct-chat-text');
self::assertStringContainsString('Foo bar blub', $node->html());
$node = $client->getCrawler()->filter('div.box#comments_box .box-body a.confirmation-link');
self::assertEquals($this->createUrl('/admin/project/1/comment_delete'), $node->attr('href'));
$this->request($client, '/admin/project/1/comment_delete');
$comments = $this->getEntityManager()->getRepository(ProjectComment::class)->findAll();
$id = $comments[0]->getId();
self::assertEquals($this->createUrl('/admin/project/' . $id . '/comment_delete'), $node->attr('href'));
$this->request($client, '/admin/project/' . $id . '/comment_delete');
$this->assertIsRedirect($client, $this->createUrl('/admin/project/1/details'));
$client->followRedirect();
$node = $client->getCrawler()->filter('div.box#comments_box .box-body');
@@ -279,12 +283,15 @@ class ProjectControllerTest extends ControllerBaseTest
$node = $client->getCrawler()->filter('div.box#comments_box .box-body a.btn.active');
self::assertEquals(0, $node->count());
$this->request($client, '/admin/project/1/comment_pin');
$comments = $this->getEntityManager()->getRepository(ProjectComment::class)->findAll();
$id = $comments[0]->getId();
$this->request($client, '/admin/project/' . $id . '/comment_pin');
$this->assertIsRedirect($client, $this->createUrl('/admin/project/1/details'));
$client->followRedirect();
$node = $client->getCrawler()->filter('div.box#comments_box .box-body a.btn.active');
self::assertEquals(1, $node->count());
self::assertEquals($this->createUrl('/admin/project/1/comment_pin'), $node->attr('href'));
self::assertEquals($this->createUrl('/admin/project/' . $id . '/comment_pin'), $node->attr('href'));
}
public function testCreateDefaultTeamAction()
@@ -346,7 +353,7 @@ class ProjectControllerTest extends ControllerBaseTest
'customer' => 1,
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/project/2/details'));
$this->assertIsRedirect($client, '/details');
$client->followRedirect();
$this->assertHasFlashSuccess($client);
}
@@ -418,22 +425,24 @@ class ProjectControllerTest extends ControllerBaseTest
$fixture = new ProjectFixtures();
$fixture->setAmount(1);
$this->importFixture($fixture);
/** @var Project[] $projects */
$projects = $this->importFixture($fixture);
$id = $projects[0]->getId();
$this->request($client, '/admin/project/2/edit');
$this->request($client, '/admin/project/' . $id . '/edit');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->request($client, '/admin/project/2/delete');
$this->request($client, '/admin/project/' . $id . '/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=form]')->form();
$this->assertStringEndsWith($this->createUrl('/admin/project/2/delete'), $form->getUri());
$this->assertStringEndsWith($this->createUrl('/admin/project/' . $id . '/delete'), $form->getUri());
$client->submit($form);
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->request($client, '/admin/project/2/edit');
$this->request($client, '/admin/project/' . $id . '/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}
@@ -467,10 +476,9 @@ class ProjectControllerTest extends ControllerBaseTest
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->findAll();
// $this->assertEquals(0, count($timesheets));
$em->clear();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(0, \count($timesheets));
$this->request($client, '/admin/project/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
@@ -487,7 +495,8 @@ class ProjectControllerTest extends ControllerBaseTest
$this->importFixture($fixture);
$fixture = new ProjectFixtures();
$fixture->setAmount(1)->setIsVisible(true);
$this->importFixture($fixture);
$projects = $this->importFixture($fixture);
$id = $projects[0]->getId();
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, \count($timesheets));
@@ -504,7 +513,7 @@ class ProjectControllerTest extends ControllerBaseTest
$this->assertStringEndsWith($this->createUrl('/admin/project/1/delete'), $form->getUri());
$client->submit($form, [
'form' => [
'project' => 2
'project' => $id
]
]);
@@ -518,7 +527,7 @@ class ProjectControllerTest extends ControllerBaseTest
/** @var Timesheet $entry */
foreach ($timesheets as $entry) {
$this->assertEquals(2, $entry->getProject()->getId());
$this->assertEquals($id, $entry->getProject()->getId());
}
$this->request($client, '/admin/project/1/edit');

View File

@@ -12,21 +12,24 @@ namespace App\Tests\Controller;
use App\Entity\Tag;
use App\Entity\User;
use App\Tests\DataFixtures\TagFixtures;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
* @group integration
*/
class TagControllerTest extends ControllerBaseTest
{
protected function importTags(HttpKernelBrowser $client): void
/**
* @return Tag[]
*/
protected function importTags(): 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 +40,7 @@ class TagControllerTest extends ControllerBaseTest
public function testIndexAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importTags($client);
$this->importTags();
$this->assertAccessIsGranted($client, '/admin/tags/');
$this->assertHasDataTable($client);
@@ -47,7 +50,7 @@ class TagControllerTest extends ControllerBaseTest
public function testIndexActionWithSearchTermQuery()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importTags($client);
$this->importTags();
$this->request($client, '/admin/tags/');
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -76,7 +79,9 @@ class TagControllerTest extends ControllerBaseTest
$client->followRedirect();
$this->assertHasDataTable($client);
$this->request($client, '/admin/tags/1/edit');
$id = $this->getEntityManager()->getRepository(Tag::class)->findAll()[0]->getId();
$this->request($client, '/admin/tags/' . $id . '/edit');
$editForm = $client->getCrawler()->filter('form[name=tag_edit_form]')->form();
$this->assertEquals('A tAG Name!', $editForm->get('tag_edit_form[name]')->getValue());
}
@@ -84,9 +89,10 @@ class TagControllerTest extends ControllerBaseTest
public function testEditAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->importTags($client);
$tags = $this->importTags();
$id = $tags[0]->getId();
$this->assertAccessIsGranted($client, '/admin/tags/1/edit');
$this->assertAccessIsGranted($client, '/admin/tags/' . $id . '/edit');
$form = $client->getCrawler()->filter('form[name=tag_edit_form]')->form();
$client->submit($form, [
'tag_edit_form' => ['name' => 'Test 2 updated']
@@ -94,7 +100,7 @@ class TagControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/tags/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->request($client, '/admin/tags/1/edit');
$this->request($client, '/admin/tags/' . $id . '/edit');
$editForm = $client->getCrawler()->filter('form[name=tag_edit_form]')->form();
$this->assertEquals('Test 2 updated', $editForm->get('tag_edit_form[name]')->getValue());
}
@@ -102,7 +108,7 @@ class TagControllerTest extends ControllerBaseTest
public function testMultiDeleteAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importTags($client);
$this->importTags();
$this->assertAccessIsGranted($client, '/admin/tags/');

View File

@@ -78,16 +78,15 @@ class TeamControllerTest extends ControllerBaseTest
$this->assertAccessIsGranted($client, '/admin/teams/create');
$form = $client->getCrawler()->filter('form[name=team_edit_form]')->form();
$editForm = $client->getCrawler()->filter('form[name=team_edit_form]')->form();
$this->assertEquals('', $editForm->get('team_edit_form[name]')->getValue());
$this->assertEquals('5', $editForm->get('team_edit_form[teamlead]')->getValue());
$this->assertEquals('', $form->get('team_edit_form[name]')->getValue());
$this->assertEquals('5', $form->get('team_edit_form[teamlead]')->getValue());
$client->submit($form, [
'team_edit_form' => [
'name' => 'Test Team',
'name' => 'Test Team' . uniqid(),
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/teams/2/edit'));
$this->assertIsRedirect($client, '/edit');
$client->followRedirect();
$this->assertHasFlashSuccess($client);
$this->assertHasCustomerAndProjectPermissionBoxes($client);
@@ -210,7 +209,7 @@ class TeamControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->request($client, '/admin/teams/1/duplicate');
$this->assertIsRedirect($client, $this->createUrl('/admin/teams/2/edit'));
$this->assertIsRedirect($client, '/edit');
$client->followRedirect();
$node = $client->getCrawler()->filter('#team_edit_form_name');
self::assertEquals(1, $node->count());

View File

@@ -181,7 +181,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertNull($timesheet->getEnd());
$this->assertEquals('Testing is fun!', $timesheet->getDescription());
@@ -218,7 +218,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
$this->assertEquals($expectedDuration, $timesheet->getDuration());
@@ -293,7 +293,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
$this->assertEquals(50, $timesheet->getRate());
@@ -327,7 +327,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
$this->assertEquals(50, $timesheet->getRate());
@@ -428,7 +428,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
$this->assertEquals(800, $timesheet->getRate());
@@ -450,9 +450,10 @@ class TimesheetControllerTest extends ControllerBaseTest
$fixture->setAmount(10);
$fixture->setUser($this->getUserByRole(User::ROLE_USER));
$fixture->setStartDate('2017-05-01');
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$id = $timesheets[0]->getId();
$this->request($client, '/timesheet/1/edit');
$this->request($client, '/timesheet/' . $id . '/edit');
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
@@ -478,7 +479,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertEquals('foo-bar', $timesheet->getDescription());
}

View File

@@ -192,7 +192,7 @@ class TimesheetTeamControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->findAll()[0];
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertNull($timesheet->getEnd());
$this->assertEquals('Testing is fun!', $timesheet->getDescription());
@@ -271,9 +271,10 @@ class TimesheetTeamControllerTest extends ControllerBaseTest
$fixture->setAmount(10);
$fixture->setUser($user);
$fixture->setStartDate('2017-05-01');
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$id = $timesheets[0]->getId();
$this->request($client, '/team/timesheet/1/edit');
$this->request($client, '/team/timesheet/' . $id . '/edit');
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
@@ -300,7 +301,7 @@ class TimesheetTeamControllerTest extends ControllerBaseTest
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$timesheet = $em->getRepository(Timesheet::class)->find($id);
$this->assertEquals('foo-bar', $timesheet->getDescription());
$this->assertEquals($teamlead->getId(), $timesheet->getUser()->getId());
}

View File

@@ -98,7 +98,7 @@ class UserControllerTest extends ControllerBaseTest
public function testCreateAction()
{
$username = '亚历山德拉';
$username = '亚历山德拉' . uniqid();
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/user/create');
$form = $client->getCrawler()->filter('form[name=user_create]')->form();
@@ -188,10 +188,9 @@ class UserControllerTest extends ControllerBaseTest
$client->followRedirect();
$this->assertHasFlashDeleteSuccess($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->count([]);
// $this->assertEquals(0, $timesheets);
$em->clear();
$timesheets = $em->getRepository(Timesheet::class)->count([]);
$this->assertEquals(0, $timesheets);
$this->request($client, '/admin/user/' . $user->getId() . '/edit');
$this->assertFalse($client->getResponse()->isSuccessful());

View File

@@ -11,14 +11,13 @@ namespace App\Tests\DataFixtures;
use App\Entity\Activity;
use App\Entity\Project;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
final class ActivityFixtures extends Fixture
final class ActivityFixtures implements TestFixture
{
/**
* @var int
@@ -95,10 +94,13 @@ final class ActivityFixtures extends Fixture
}
/**
* {@inheritdoc}
* @param ObjectManager $manager
* @return Activity[]
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
$projects = $this->projects;
if (empty($projects)) {
$projects = $this->getAllProjects($manager);
@@ -126,16 +128,20 @@ final class ActivityFixtures extends Fixture
\call_user_func($this->callback, $activity);
}
$manager->persist($activity);
$created[] = $activity;
}
$manager->flush();
return $created;
}
/**
* @param ObjectManager $manager
* @return array<int|string, Project>
*/
protected function getAllProjects(ObjectManager $manager): array
private function getAllProjects(ObjectManager $manager): array
{
$all = [];
/** @var Project[] $entries */

View File

@@ -10,14 +10,13 @@
namespace App\Tests\DataFixtures;
use App\Entity\Customer;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
final class CustomerFixtures extends Fixture
final class CustomerFixtures implements TestFixture
{
/**
* @var int
@@ -65,10 +64,13 @@ final class CustomerFixtures extends Fixture
}
/**
* {@inheritdoc}
* @param ObjectManager $manager
* @return Customer[]
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
$faker = Factory::create();
for ($i = 0; $i < $this->amount; $i++) {
@@ -92,8 +94,11 @@ final class CustomerFixtures extends Fixture
\call_user_func($this->callback, $customer);
}
$manager->persist($customer);
$created[] = $customer;
}
$manager->flush();
return $created;
}
}

View File

@@ -10,28 +10,22 @@
namespace App\Tests\DataFixtures;
use App\Entity\InvoiceTemplate;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
class InvoiceFixtures extends Fixture
class InvoiceTemplateFixtures implements TestFixture
{
/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
{
$this->loadInvoiceTemplates($manager);
}
/**
* @param ObjectManager $manager
* @return InvoiceTemplate[]
*/
private function loadInvoiceTemplates(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
$faker = Factory::create();
$template = new InvoiceTemplate();
@@ -58,5 +52,9 @@ class InvoiceFixtures extends Fixture
$manager->persist($template);
$manager->flush();
$created[] = $template;
return $created;
}
}

View File

@@ -11,14 +11,13 @@ namespace App\Tests\DataFixtures;
use App\Entity\Customer;
use App\Entity\Project;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
final class ProjectFixtures extends Fixture
final class ProjectFixtures implements TestFixture
{
/**
* @var int
@@ -81,10 +80,12 @@ final class ProjectFixtures extends Fixture
}
/**
* {@inheritdoc}
* @param ObjectManager $manager
* @return Project[]
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
$customers = $this->customers;
if (empty($customers)) {
$customers = $this->getAllCustomers($manager);
@@ -109,16 +110,19 @@ final class ProjectFixtures extends Fixture
\call_user_func($this->callback, $project);
}
$manager->persist($project);
$created[] = $project;
}
$manager->flush();
return $created;
}
/**
* @param ObjectManager $manager
* @return array<int|string, Customer>
*/
protected function getAllCustomers(ObjectManager $manager): array
private function getAllCustomers(ObjectManager $manager): array
{
$all = [];
/** @var Customer[] $entries */

View File

@@ -10,13 +10,12 @@
namespace App\Tests\DataFixtures;
use App\Entity\Tag;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
/**
* Defines the sample data to load in during controller tests.
*/
final class TagFixtures extends Fixture
final class TagFixtures implements TestFixture
{
/**
* @var string[]
@@ -60,10 +59,13 @@ final class TagFixtures extends Fixture
}
/**
* {@inheritdoc}
* @param ObjectManager $manager
* @return Tag[]
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
foreach ($this->getTagArray() as $tagName) {
$tag = $this->createTagEntry($tagName);
@@ -71,8 +73,11 @@ final class TagFixtures extends Fixture
\call_user_func($this->callback, $tag);
}
$manager->persist($tag);
$created[] = $tag;
}
$manager->flush();
return $created;
}
private function createTagEntry(string $tagName): Tag

View File

@@ -12,14 +12,13 @@ namespace App\Tests\DataFixtures;
use App\Entity\Customer;
use App\Entity\Team;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
final class TeamFixtures extends Fixture
final class TeamFixtures implements TestFixture
{
/**
* @var int
@@ -89,10 +88,13 @@ final class TeamFixtures extends Fixture
}
/**
* {@inheritdoc}
* @param ObjectManager $manager
* @return Team[]
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
$faker = Factory::create();
$user = $this->getAllUsers($manager);
$customer = $this->getAllCustomers($manager);
@@ -131,9 +133,12 @@ final class TeamFixtures extends Fixture
\call_user_func($this->callback, $team);
}
$manager->persist($team);
$created[] = $team;
}
$manager->flush();
return $created;
}
/**

View File

@@ -0,0 +1,23 @@
<?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 Doctrine\Persistence\ObjectManager;
/**
* Defines the sample data to load in during controller tests.
*/
interface TestFixture
{
/**
* Load data fixtures with the passed EntityManager and returns the created objects.
*/
public function load(ObjectManager $manager): array;
}

View File

@@ -16,14 +16,13 @@ use App\Entity\Timesheet;
use App\Entity\User;
use App\Entity\UserPreference;
use App\Timesheet\Util;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
final class TimesheetFixtures extends Fixture
final class TimesheetFixtures implements TestFixture
{
/**
* @var User
@@ -205,10 +204,13 @@ final class TimesheetFixtures extends Fixture
}
/**
* {@inheritdoc}
* @param ObjectManager $manager
* @return Timesheet[]
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): array
{
$created = [];
$activities = $this->activities;
if (empty($activities)) {
$activities = $this->getAllActivities($manager);
@@ -222,6 +224,12 @@ final class TimesheetFixtures extends Fixture
$faker = Factory::create();
$user = $this->user;
$tags = $this->getTagObjectList();
foreach ($tags as $tag) {
$manager->persist($tag);
}
$manager->flush();
for ($i = 0; $i < $this->amount; $i++) {
$description = $faker->text;
if ($this->allowEmptyDescriptions) {
@@ -239,8 +247,6 @@ final class TimesheetFixtures extends Fixture
$project = $projects[array_rand($projects)];
}
$tags = $this->getTagObjectList($i);
$timesheet = $this->createTimesheetEntry(
$user,
$activity,
@@ -254,6 +260,7 @@ final class TimesheetFixtures extends Fixture
\call_user_func($this->callback, $timesheet);
}
$manager->persist($timesheet);
$created[] = $timesheet;
}
for ($i = 0; $i < $this->running; $i++) {
@@ -264,8 +271,6 @@ final class TimesheetFixtures extends Fixture
$project = $projects[array_rand($projects)];
}
$tags = $this->getTagObjectList($i);
$timesheet = $this->createTimesheetEntry(
$user,
$activity,
@@ -280,24 +285,32 @@ final class TimesheetFixtures extends Fixture
\call_user_func($this->callback, $timesheet);
}
$manager->persist($timesheet);
$created[] = $timesheet;
}
$manager->flush();
return $created;
}
protected function getTagObjectList(int $cnt): array
private function getTagObjectList(): array
{
if (true === $this->useTags) {
$tagObject = new Tag();
$tagObject->setName($this->tags[($cnt % \count($this->tags))]);
$all = [];
foreach ($this->tags as $tagName) {
$tagObject = new Tag();
$tagObject->setName($tagName);
return [$tagObject];
$all[] = $tagObject;
}
return $all;
}
return [];
}
protected function getDateTime(int $i): \DateTime
private function getDateTime(int $i): \DateTime
{
$start = \DateTime::createFromFormat('Y-m-d', $this->startDate);
$start->modify("+ $i days");
@@ -310,7 +323,7 @@ final class TimesheetFixtures extends Fixture
* @param ObjectManager $manager
* @return array<int|string, Activity>
*/
protected function getAllActivities(ObjectManager $manager): array
private function getAllActivities(ObjectManager $manager): array
{
$all = [];
/** @var Activity[] $entries */
@@ -326,7 +339,7 @@ final class TimesheetFixtures extends Fixture
* @param ObjectManager $manager
* @return array<int|string, Project>
*/
protected function getAllProjects(ObjectManager $manager): array
private function getAllProjects(ObjectManager $manager): array
{
$all = [];
/** @var Project[] $entries */

View File

@@ -1,71 +0,0 @@
<?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\Doctrine;
use App\Doctrine\SqliteSessionInitSubscriber;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Doctrine\SqliteSessionInitSubscriber
*/
class SqliteSessionInitSubscriberTest extends TestCase
{
public function testGetSubscribedEvents()
{
$sut = new SqliteSessionInitSubscriber();
$events = $sut->getSubscribedEvents();
$this->assertTrue(\in_array(Events::postConnect, $events));
}
public function testPostConnectWithSqlite()
{
$sut = new SqliteSessionInitSubscriber();
$platformMock = $this->getMockBuilder(SqlitePlatform::class)
->onlyMethods(['getName'])
->disableOriginalConstructor()
->getMock();
$platformMock->expects($this->once())->method('getName')->willReturn('sqlite');
$connectionMock = $this->createMock(Connection::class);
$connectionMock->expects($this->once())->method('getDatabasePlatform')->willReturn($platformMock);
$connectionMock->expects($this->once())->method('exec')->with('PRAGMA foreign_keys = ON;');
$args = new ConnectionEventArgs($connectionMock);
$sut->postConnect($args);
}
public function testPostConnectWithMysql()
{
$sut = new SqliteSessionInitSubscriber();
$platformMock = $this->getMockBuilder(MySqlPlatform::class)
->onlyMethods(['getName'])
->disableOriginalConstructor()
->getMock();
$platformMock->expects($this->once())->method('getName')->willReturn('mysql');
$connectionMock = $this->createMock(Connection::class);
$connectionMock->expects($this->once())->method('getDatabasePlatform')->willReturn($platformMock);
$connectionMock->expects($this->never())->method('exec')->with('PRAGMA foreign_keys = ON;');
$args = new ConnectionEventArgs($connectionMock);
$sut->postConnect($args);
}
}

View File

@@ -12,9 +12,9 @@ namespace App\Tests\Doctrine;
use App\Doctrine\UTCDateTimeType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\TestCase;
/**
@@ -24,9 +24,9 @@ class UTCDateTimeTypeTest extends TestCase
{
public function testGetUtc()
{
Type::overrideType(Type::DATETIME, UTCDateTimeType::class);
Type::overrideType(Types::DATETIME_MUTABLE, UTCDateTimeType::class);
/** @var UTCDateTimeType $type */
$type = Type::getType(Type::DATETIME);
$type = Type::getType(Types::DATETIME_MUTABLE);
$this->assertInstanceOf(UTCDateTimeType::class, $type);
$utc = $type::getUtc();
@@ -39,9 +39,9 @@ class UTCDateTimeTypeTest extends TestCase
*/
public function testConvertToDatabaseValue(AbstractPlatform $platform)
{
Type::overrideType(Type::DATETIME, UTCDateTimeType::class);
Type::overrideType(Types::DATETIME_MUTABLE, UTCDateTimeType::class);
/** @var UTCDateTimeType $type */
$type = Type::getType(Type::DATETIME);
$type = Type::getType(Types::DATETIME_MUTABLE);
$result = $type->convertToDatabaseValue(null, $platform);
$this->assertNull($result);
@@ -67,9 +67,9 @@ class UTCDateTimeTypeTest extends TestCase
*/
public function testConvertToPHPValue(AbstractPlatform $platform)
{
Type::overrideType(Type::DATETIME, UTCDateTimeType::class);
Type::overrideType(Types::DATETIME_MUTABLE, UTCDateTimeType::class);
/** @var UTCDateTimeType $type */
$type = Type::getType(Type::DATETIME);
$type = Type::getType(Types::DATETIME_MUTABLE);
$result = $type->convertToPHPValue(null, $platform);
$this->assertNull($result);
@@ -89,9 +89,9 @@ class UTCDateTimeTypeTest extends TestCase
{
$this->expectException(ConversionException::class);
Type::overrideType(Type::DATETIME, UTCDateTimeType::class);
Type::overrideType(Types::DATETIME_MUTABLE, UTCDateTimeType::class);
/** @var UTCDateTimeType $type */
$type = Type::getType(Type::DATETIME);
$type = Type::getType(Types::DATETIME_MUTABLE);
$type->convertToPHPValue('201xx01-17 13:30:00', $platform);
}
@@ -101,9 +101,9 @@ class UTCDateTimeTypeTest extends TestCase
*/
public function testRequiresSQLCommentHint(AbstractPlatform $platform)
{
Type::overrideType(Type::DATETIME, UTCDateTimeType::class);
Type::overrideType(Types::DATETIME_MUTABLE, UTCDateTimeType::class);
/** @var UTCDateTimeType $type */
$type = Type::getType(Type::DATETIME);
$type = Type::getType(Types::DATETIME_MUTABLE);
self::assertTrue($type->requiresSQLCommentHint($platform));
}
@@ -111,7 +111,6 @@ class UTCDateTimeTypeTest extends TestCase
{
return [
[new MySqlPlatform()],
[new SqlitePlatform()],
];
}
}

26
tests/KernelTest.php Normal file
View File

@@ -0,0 +1,26 @@
<?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;
use App\Kernel;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Kernel
*/
class KernelTest extends TestCase
{
public function testBuild()
{
$sut = new Kernel('test', false);
$this->assertEquals($sut->getCacheDir(), realpath(__DIR__ . '/../var/cache/test'));
$this->assertEquals($sut->getLogDir(), realpath(__DIR__ . '/../var/log'));
}
}

View File

@@ -11,9 +11,7 @@ namespace App\Tests;
use App\DataFixtures\UserFixtures;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Loader;
use App\Tests\DataFixtures\TestFixture;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -31,15 +29,9 @@ trait KernelTestTrait
return $this::$container->get('doctrine.orm.entity_manager');
}
protected function importFixture(Fixture $fixture)
protected function importFixture(TestFixture $fixture): array
{
$em = $this::$container->get('doctrine.orm.entity_manager');
$loader = new Loader();
$loader->addFixture($fixture);
$executor = new ORMExecutor($em, null);
$executor->execute($loader->getFixtures(), true);
return $fixture->load($this->getEntityManager());
}
protected function getUserByName(string $username): ?User

View File

@@ -38,13 +38,6 @@ class TagRepositoryTest extends AbstractRepositoryTest
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(7, \count($result));
$this->assertEquals(1, $result[0]);
$this->assertEquals(3, $result[1]);
$this->assertEquals(4, $result[2]);
$this->assertEquals(5, $result[3]);
$this->assertEquals(6, $result[4]);
$this->assertEquals(7, $result[5]);
$this->assertEquals(11, $result[6]);
}
public function testFindNoIds()

View File

@@ -88,9 +88,9 @@ class TimesheetRepositoryTest extends AbstractRepositoryTest
$fixture = new TimesheetFixtures();
$fixture->setUser($user);
$fixture->setAmountRunning(1);
$this->importFixture($fixture);
$timesheets = $this->importFixture($fixture);
$timesheet = $repository->find(1);
$timesheet = $timesheets[0];
$this->assertInstanceOf(Timesheet::class, $timesheet);
$this->assertNull($timesheet->getEnd());
@@ -123,7 +123,7 @@ class TimesheetRepositoryTest extends AbstractRepositoryTest
$this->assertNull($timesheet->getId());
$repository->save($timesheet);
$this->assertEquals(1, $timesheet->getId());
$this->assertNotNull($timesheet->getId());
}
public function testSaveWithTags()
@@ -156,11 +156,11 @@ class TimesheetRepositoryTest extends AbstractRepositoryTest
$this->assertNull($timesheet->getId());
$repository->save($timesheet);
$this->assertEquals(1, $timesheet->getId());
$this->assertNotNull($timesheet->getId());
$this->assertEquals(2, $timesheet->getTags()->count());
$this->assertEquals('Travel', $timesheet->getTags()->get(0)->getName());
$this->assertEquals(1, $timesheet->getTags()->get(0)->getId());
$this->assertNotNull($timesheet->getTags()->get(0)->getId());
$this->assertEquals('Picture', $timesheet->getTags()->get(1)->getName());
$this->assertEquals(2, $timesheet->getTags()->get(1)->getId());
$this->assertNotNull($timesheet->getTags()->get(1)->getId());
}
}

24
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,24 @@
<?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.
*/
require __DIR__ . '/../vendor/autoload.php';
if (isset($_ENV['BOOTSTRAP_RESET_DATABASE']) && $_ENV['BOOTSTRAP_RESET_DATABASE'] == true) {
echo 'Re-Installing test database ...' . PHP_EOL;
exec(sprintf(
'APP_ENV=test php "%s/../bin/console" kimai:reset-test --env=test --no-interaction -vvv',
__DIR__
), $output, $exitCode);
if ($exitCode !== 0) {
dump($output);
throw new \Exception('Failed to setup test database.');
}
}