added activity events for plugins (#2516)

This commit is contained in:
Kevin Papst
2021-04-21 15:56:17 +02:00
committed by GitHub
parent 08a144ac31
commit 09c9a95fcf
18 changed files with 591 additions and 8 deletions

View File

@@ -0,0 +1,157 @@
<?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\Activity;
use App\Activity\ActivityService;
use App\Entity\Activity;
use App\Entity\Project;
use App\Event\ActivityCreateEvent;
use App\Event\ActivityCreatePostEvent;
use App\Event\ActivityCreatePreEvent;
use App\Event\ActivityMetaDefinitionEvent;
use App\Event\ActivityUpdatePostEvent;
use App\Event\ActivityUpdatePreEvent;
use App\Repository\ActivityRepository;
use App\Validator\ValidationFailedException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @covers \App\Activity\ActivityService
*/
class ActivityServiceTest extends TestCase
{
private function getSut(
?EventDispatcherInterface $dispatcher = null,
?ValidatorInterface $validator = null,
?ActivityRepository $repository = null
): ActivityService {
if ($repository === null) {
$repository = $this->createMock(ActivityRepository::class);
}
if ($dispatcher === null) {
$dispatcher = $this->createMock(EventDispatcherInterface::class);
}
if ($validator === null) {
$validator = $this->createMock(ValidatorInterface::class);
$validator->method('validate')->willReturn(new ConstraintViolationList());
}
$service = new ActivityService($repository, $dispatcher, $validator);
return $service;
}
public function testCannotSavePersistedProjectAsNew()
{
$project = $this->createMock(Activity::class);
$project->expects($this->once())->method('getId')->willReturn(1);
$sut = $this->getSut();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot create activity, already persisted');
$sut->saveNewActivity($project);
}
public function testsaveNewActivityHasValidationError()
{
$constraints = new ConstraintViolationList();
$constraints->add(new ConstraintViolation('toooo many tests', 'abc.def', [], '$root', 'begin', 4, null, null, null, '$cause'));
$validator = $this->createMock(ValidatorInterface::class);
$validator->method('validate')->willReturn($constraints);
$sut = $this->getSut(null, $validator);
$this->expectException(ValidationFailedException::class);
$this->expectExceptionMessage('Validation Failed');
$sut->saveNewActivity(new Activity());
}
public function testUpdateDispatchesEvents()
{
$project = $this->createMock(Activity::class);
$project->method('getId')->willReturn(1);
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->exactly(2))->method('dispatch')->willReturnCallback(function ($event) use ($project) {
if ($event instanceof ActivityUpdatePostEvent) {
self::assertSame($project, $event->getActivity());
} elseif ($event instanceof ActivityUpdatePreEvent) {
self::assertSame($project, $event->getActivity());
} else {
$this->fail('Invalid event received');
}
});
$sut = $this->getSut($dispatcher);
$sut->updateActivity($project);
}
public function testcreateNewActivityDispatchesEvents()
{
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->exactly(2))->method('dispatch')->willReturnCallback(function ($event) {
if ($event instanceof ActivityMetaDefinitionEvent) {
self::assertInstanceOf(Activity::class, $event->getEntity());
} elseif ($event instanceof ActivityCreateEvent) {
self::assertInstanceOf(Activity::class, $event->getActivity());
} else {
$this->fail('Invalid event received');
}
});
$sut = $this->getSut($dispatcher);
$project = new Project();
$activity = $sut->createNewActivity($project);
self::assertSame($project, $activity->getProject());
}
public function testsaveNewActivityDispatchesEvents()
{
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->exactly(2))->method('dispatch')->willReturnCallback(function ($event) {
if ($event instanceof ActivityCreatePreEvent) {
self::assertInstanceOf(Activity::class, $event->getActivity());
} elseif ($event instanceof ActivityCreatePostEvent) {
self::assertInstanceOf(Activity::class, $event->getActivity());
} else {
$this->fail('Invalid event received');
}
});
$sut = $this->getSut($dispatcher);
$activity = new Activity();
$sut->saveNewActivity($activity);
}
public function testcreateNewActivityWithoutCustomer()
{
$sut = $this->getSut();
$project = $sut->createNewActivity();
self::assertNull($project->getProject());
$project = $sut->createNewActivity();
self::assertNull($project->getProject());
}
}

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\Event;
use App\Entity\Activity;
use App\Event\AbstractActivityEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\EventDispatcher\Event;
abstract class AbstractActivityEventTest extends TestCase
{
abstract protected function createActivityEvent(Activity $activity): AbstractActivityEvent;
public function testGetterAndSetter()
{
$activity = new Activity();
$sut = $this->createActivityEvent($activity);
self::assertInstanceOf(Event::class, $sut);
self::assertSame($activity, $sut->getActivity());
}
}

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\Event;
use App\Entity\Activity;
use App\Event\AbstractActivityEvent;
use App\Event\ActivityCreateEvent;
/**
* @covers \App\Event\AbstractActivityEvent
* @covers \App\Event\ActivityCreateEvent
*/
class ActivityCreateEventTest extends AbstractActivityEventTest
{
protected function createActivityEvent(Activity $activity): AbstractActivityEvent
{
return new ActivityCreateEvent($activity);
}
}

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\Event;
use App\Entity\Activity;
use App\Event\AbstractActivityEvent;
use App\Event\ActivityCreatePostEvent;
/**
* @covers \App\Event\AbstractActivityEvent
* @covers \App\Event\ActivityCreatePostEvent
*/
class ActivityCreatePostEventTest extends AbstractActivityEventTest
{
protected function createActivityEvent(Activity $activity): AbstractActivityEvent
{
return new ActivityCreatePostEvent($activity);
}
}

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\Event;
use App\Entity\Activity;
use App\Event\AbstractActivityEvent;
use App\Event\ActivityCreatePreEvent;
/**
* @covers \App\Event\AbstractActivityEvent
* @covers \App\Event\ActivityCreatePreEvent
*/
class ActivityCreatePreEventTest extends AbstractActivityEventTest
{
protected function createActivityEvent(Activity $activity): AbstractActivityEvent
{
return new ActivityCreatePreEvent($activity);
}
}

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\Event;
use App\Entity\Activity;
use App\Event\AbstractActivityEvent;
use App\Event\ActivityUpdatePostEvent;
/**
* @covers \App\Event\AbstractActivityEvent
* @covers \App\Event\ActivityUpdatePostEvent
*/
class ActivityUpdatePostEventTest extends AbstractActivityEventTest
{
protected function createActivityEvent(Activity $activity): AbstractActivityEvent
{
return new ActivityUpdatePostEvent($activity);
}
}

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\Event;
use App\Entity\Activity;
use App\Event\AbstractActivityEvent;
use App\Event\ActivityUpdatePreEvent;
/**
* @covers \App\Event\AbstractActivityEvent
* @covers \App\Event\ActivityUpdatePreEvent
*/
class ActivityUpdatePreEventTest extends AbstractActivityEventTest
{
protected function createActivityEvent(Activity $activity): AbstractActivityEvent
{
return new ActivityUpdatePreEvent($activity);
}
}

View File

@@ -0,0 +1,35 @@
<?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\Event;
use App\Entity\User;
use App\Event\ReportingEvent;
use App\Reporting\Report;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\ReportingEvent
*/
class ReportingEventTest extends TestCase
{
public function testGetterAndSetter()
{
$user = new User();
$sut = new ReportingEvent($user);
self::assertSame($user, $sut->getUser());
self::assertEquals([], $sut->getReports());
$report = new Report('id', 'route', 'label', 'icon');
$sut->addReport($report);
self::assertSame([$report], $sut->getReports());
}
}