diff --git a/lib/SP/Domain/Security/Ports/EventlogService.php b/lib/SP/Domain/Security/Ports/EventlogService.php index 9eecf78a..f6ae8e3a 100644 --- a/lib/SP/Domain/Security/Ports/EventlogService.php +++ b/lib/SP/Domain/Security/Ports/EventlogService.php @@ -55,5 +55,5 @@ interface EventlogService * @throws ConstraintException * @throws QueryException */ - public function create(Eventlog $eventlogData): int; + public function create(Eventlog $eventlog): int; } diff --git a/lib/SP/Domain/Security/Services/Eventlog.php b/lib/SP/Domain/Security/Services/Eventlog.php index 30c16774..481e16f4 100644 --- a/lib/SP/Domain/Security/Services/Eventlog.php +++ b/lib/SP/Domain/Security/Services/Eventlog.php @@ -37,7 +37,7 @@ use SP\Domain\Security\Ports\EventlogService; use SP\Infrastructure\Database\QueryResult; /** - * Class EventlogService + * Class Eventlog */ final class Eventlog extends Service implements EventlogService { diff --git a/tests/SPT/Domain/Security/Services/EventlogTest.php b/tests/SPT/Domain/Security/Services/EventlogTest.php new file mode 100644 index 00000000..caf563b3 --- /dev/null +++ b/tests/SPT/Domain/Security/Services/EventlogTest.php @@ -0,0 +1,134 @@ +. + */ + +namespace SPT\Domain\Security\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\MockObject; +use SP\DataModel\ItemSearchData; +use SP\Domain\Core\Exceptions\ConstraintException; +use SP\Domain\Core\Exceptions\QueryException; +use SP\Domain\Core\Exceptions\SPException; +use SP\Domain\Http\RequestInterface; +use SP\Domain\Security\Models\Eventlog as EventlogModel; +use SP\Domain\Security\Ports\EventlogRepository; +use SP\Domain\Security\Services\Eventlog; +use SP\Infrastructure\Database\QueryResult; +use SPT\UnitaryTestCase; + +/** + * Class EventlogTest + */ +#[Group('unitary')] +class EventlogTest extends UnitaryTestCase +{ + + private EventlogRepository|MockObject $eventlogRepository; + private RequestInterface|MockObject $request; + private Eventlog $eventlog; + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testSearch() + { + $itemSearchData = new ItemSearchData('test'); + + $this->eventlogRepository + ->expects($this->once()) + ->method('search') + ->with($itemSearchData) + ->willReturn(new QueryResult()); + + $this->eventlog->search($itemSearchData); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testCreate() + { + $eventlog = new EventlogModel( + [ + 'date' => time(), + 'login' => self::$faker->userName, + 'userId' => self::$faker->randomNumber(3), + 'ipAddress' => self::$faker->ipv4(), + 'action' => self::$faker->colorName(), + 'description' => self::$faker->text(), + 'level' => self::$faker->colorName(), + ] + ); + + $queryResult = new QueryResult(); + + $this->request + ->expects($this->once()) + ->method('getClientAddress') + ->willReturn('192.168.0.1'); + + $this->eventlogRepository + ->expects($this->once()) + ->method('create') + ->with( + self::callback(function (EventlogModel $eventlog) { + $userData = $this->context->getUserData(); + + return $eventlog->getUserId() == $userData->getId() + && $eventlog->getLogin() == $userData->getLogin() + && $eventlog->getIpAddress() == '192.168.0.1'; + }) + ) + ->willReturn($queryResult->setLastId(100)); + + $this->assertEquals(100, $this->eventlog->create($eventlog)); + } + + /** + * @throws ConstraintException + * @throws SPException + * @throws QueryException + */ + public function testClear() + { + $this->eventlogRepository + ->expects($this->once()) + ->method('clear'); + + $this->eventlog->clear(); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->eventlogRepository = $this->createMock(EventlogRepository::class); + $this->request = $this->createMock(RequestInterface::class); + + $this->eventlog = new Eventlog($this->application, $this->eventlogRepository, $this->request); + } + +}