do not allow to stop already stopped timesheets (#289)
This commit is contained in:
58
tests/Repository/AbstractRepositoryTest.php
Normal file
58
tests/Repository/AbstractRepositoryTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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\Repository;
|
||||
|
||||
use App\Tests\KernelTestTrait;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* A base test class for AbstractRepository implementations.
|
||||
*/
|
||||
abstract class AbstractRepositoryTest extends KernelTestCase
|
||||
{
|
||||
use KernelTestTrait;
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $entityManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$kernel = self::bootKernel();
|
||||
|
||||
$this->entityManager = $kernel->getContainer()
|
||||
->get('doctrine')
|
||||
->getManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EntityManager
|
||||
*/
|
||||
protected function getEntityManager()
|
||||
{
|
||||
return $this->entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->entityManager->close();
|
||||
$this->entityManager = null; // avoid memory leaks
|
||||
}
|
||||
}
|
||||
@@ -37,11 +37,18 @@ class BaseQueryTest extends TestCase
|
||||
{
|
||||
$this->assertEquals(BaseQuery::RESULT_TYPE_PAGER, $sut->getResultType());
|
||||
|
||||
$sut->setResultType('foo-bar');
|
||||
$this->assertEquals(BaseQuery::RESULT_TYPE_PAGER, $sut->getResultType());
|
||||
|
||||
$sut->setResultType(BaseQuery::RESULT_TYPE_QUERYBUILDER);
|
||||
$this->assertEquals(BaseQuery::RESULT_TYPE_QUERYBUILDER, $sut->getResultType());
|
||||
|
||||
$sut->setResultType(BaseQuery::RESULT_TYPE_OBJECTS);
|
||||
$this->assertEquals(BaseQuery::RESULT_TYPE_OBJECTS, $sut->getResultType());
|
||||
|
||||
try {
|
||||
$sut->setResultType('foo-bar');
|
||||
} catch (\Exception $exception) {
|
||||
$this->assertInstanceOf(\InvalidArgumentException::class, $exception);
|
||||
$this->assertEquals('Unsupported query result type', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected function assertHiddenEntity(BaseQuery $sut)
|
||||
|
||||
102
tests/Repository/TimesheetRepositoryTest.php
Normal file
102
tests/Repository/TimesheetRepositoryTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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\Repository;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Entity\User;
|
||||
use App\Repository\Query\BaseQuery;
|
||||
use App\Repository\Query\TimesheetQuery;
|
||||
use App\Repository\RepositoryException;
|
||||
use App\Tests\DataFixtures\TimesheetFixtures;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
|
||||
/**
|
||||
* @covers \App\Repository\TimesheetRepository
|
||||
*/
|
||||
class TimesheetRepositoryTest extends AbstractRepositoryTest
|
||||
{
|
||||
public function testResultTypeForQueryState()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$repository = $em->getRepository(Timesheet::class);
|
||||
|
||||
$query = new TimesheetQuery();
|
||||
|
||||
$result = $repository->findByQuery($query);
|
||||
$this->assertInstanceOf(Pagerfanta::class, $result);
|
||||
|
||||
$query->setResultType(BaseQuery::RESULT_TYPE_QUERYBUILDER);
|
||||
$result = $repository->findByQuery($query);
|
||||
$this->assertInstanceOf(QueryBuilder::class, $result);
|
||||
|
||||
$query->setResultType(BaseQuery::RESULT_TYPE_PAGER);
|
||||
$result = $repository->findByQuery($query);
|
||||
$this->assertInstanceOf(Pagerfanta::class, $result);
|
||||
|
||||
$query->setResultType(BaseQuery::RESULT_TYPE_OBJECTS);
|
||||
$result = $repository->findByQuery($query);
|
||||
$this->assertInternalType('array', $result);
|
||||
}
|
||||
|
||||
public function testStoppedEntriesCannotBeStoppedAgain()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$user = $this->getUserByRole($em, User::ROLE_USER);
|
||||
$repository = $em->getRepository(Timesheet::class);
|
||||
|
||||
$fixtures = new TimesheetFixtures();
|
||||
$fixtures->setUser($user);
|
||||
$fixtures->setAmount(1);
|
||||
|
||||
$this->importFixture($em, $fixtures);
|
||||
|
||||
$query = new TimesheetQuery();
|
||||
$query->setResultType(BaseQuery::RESULT_TYPE_OBJECTS);
|
||||
$query->setUser($user);
|
||||
$query->setState(TimesheetQuery::STATE_STOPPED);
|
||||
|
||||
$entities = $repository->findByQuery($query);
|
||||
|
||||
$this->assertCount(1, $entities);
|
||||
$this->assertInstanceOf(Timesheet::class, $entities[0]);
|
||||
|
||||
$this->expectException(RepositoryException::class);
|
||||
$this->expectExceptionMessage('Timesheet entry already stopped');
|
||||
|
||||
$repository->stopRecording($entities[0]);
|
||||
}
|
||||
|
||||
public function testStartAndStop()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$user = $this->getUserByRole($em, User::ROLE_USER);
|
||||
$repository = $em->getRepository(Timesheet::class);
|
||||
|
||||
$fixtures = new TimesheetFixtures();
|
||||
$fixtures->setUser($user);
|
||||
$fixtures->setAmount(1);
|
||||
$this->importFixture($em, $fixtures);
|
||||
|
||||
$query = new TimesheetQuery();
|
||||
$query->setResultType(BaseQuery::RESULT_TYPE_OBJECTS);
|
||||
$entities = $repository->findByQuery($query);
|
||||
$activity = $entities[0]->getActivity();
|
||||
|
||||
$user = $this->getUserByRole($em, User::ROLE_USER);
|
||||
$timesheet = $repository->startRecording($user, $activity);
|
||||
$this->assertInstanceOf(Timesheet::class, $timesheet);
|
||||
$this->assertNull($timesheet->getEnd());
|
||||
|
||||
$result = $repository->stopRecording($timesheet);
|
||||
$this->assertTrue($result);
|
||||
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user