Nextcloud-App/tests/Unit/Service/TicketServiceTest.php

116 lines
3.4 KiB
PHP
Raw Permalink Normal View History

<?php
2023-10-11 19:55:03 +00:00
declare(strict_types=1);
// SPDX-FileCopyrightText: BVSC e.V. <no@example.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
2021-10-10 17:51:12 +00:00
namespace OCA\UPschooling\Tests\Unit\Service;
use OCA\UPschooling\Db\MatrixTicket;
2023-10-11 19:55:03 +00:00
use OCA\UPschooling\Db\MatrixUser;
use OCA\UPschooling\Db\TicketMapper;
2021-09-19 14:55:30 +00:00
use OCA\UPschooling\Db\UserMapper;
2023-10-11 19:55:03 +00:00
use OCA\UPschooling\Exceptions\TicketNotFoundException;
2021-09-18 19:15:03 +00:00
use OCA\UPschooling\Service\MatrixService;
2021-08-22 15:03:19 +00:00
use OCA\UPschooling\Service\TicketService;
use OCP\AppFramework\Db\DoesNotExistException;
2023-10-11 19:55:03 +00:00
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\TestCase;
2021-09-18 19:15:03 +00:00
use Psr\Log\NullLogger;
2021-10-10 17:51:12 +00:00
class TicketServiceTest extends TestCase {
2021-09-18 19:15:03 +00:00
/** @var TicketService */
2023-10-11 19:55:03 +00:00
private $ticketService;
2021-09-18 19:15:03 +00:00
/** @var MatrixService */
private $matrixService;
/** @var TicketMapper */
2021-09-19 14:55:30 +00:00
private $ticketMapper;
/** @var UserMapper */
private $userMapper;
2021-09-18 19:15:03 +00:00
/** @var string */
private $userId = 'john';
public function setUp(): void {
2023-10-11 19:55:03 +00:00
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
2021-09-19 14:55:30 +00:00
$this->ticketMapper = $this->getMockBuilder(TicketMapper::class)
->disableOriginalConstructor()
->getMock();
$this->userMapper = $this->getMockBuilder(UserMapper::class)
->disableOriginalConstructor()
->getMock();
2023-10-11 19:55:03 +00:00
$this->matrixService = $this->getMockBuilder(MatrixService::class)
->disableOriginalConstructor()
->getMock();
$this->ticketService = new TicketService(
$this->userManager,
$this->matrixService,
$this->ticketMapper,
$this->userMapper,
new NullLogger(),
);
}
2023-10-11 19:55:03 +00:00
public function testAssign() {
$ticketId = 3;
$matrixControlRoom = "!room:server";
$assistedUserMatrixId = "@user:server";
$ticket = MatrixTicket::fromRow([
'id' => $ticketId,
'matrixControlRoom' => $matrixControlRoom,
'matrixAssistedUser' => $assistedUserMatrixId,
'matrixHelperUser' => null,
'status' => "open",
'version' => 1,
]);
2021-09-19 14:55:30 +00:00
$this->ticketMapper->expects($this->once())
2023-10-11 19:55:03 +00:00
->method('findTicket')
->with($this->equalTo($ticketId))
->willReturn($ticket);
$stubTicket = new MatrixTicket();
$stubTicket->setId($ticketId);
$stubTicket->setMatrixControlRoom($matrixControlRoom);
2021-09-19 14:55:30 +00:00
$this->ticketMapper->expects($this->once())
->method('update')
2023-10-11 19:55:03 +00:00
->willReturn($stubTicket);
2023-10-11 19:55:03 +00:00
$helperUserMatrixId = "@helper:server";
$helperUserName = "Expected Helper User Name";
$mockUser = $this->getMockBuilder(IUser::class)->getMock();
$mockUser->expects($this->once())
->method('getDisplayName')
->willReturn($helperUserName);
$this->userManager->expects($this->once())
->method('get')
->with($this->equalTo($this->userId))
->willReturn($mockUser);
$helperMatrixUser = new MatrixUser();
$helperMatrixUser->setUserId($helperUserMatrixId);
$this->userMapper->expects($this->once())
->method('find')
2023-10-11 19:55:03 +00:00
->with($this->equalTo($this->userId))
->willReturn($helperMatrixUser);
$this->matrixService->expects($this->once())
->method('getProperty')
->with($this->equalTo($matrixControlRoom))
->willReturn(array(
'id' => $ticketId,
'title' => $this->any(),
'description' => $this->any(),
'matrixControlRoom' => $matrixControlRoom,
'matrixAssistedUser' => $assistedUserMatrixId,
'matrixHelperUser' => $helperUserMatrixId,
'matrixHelperName' => $helperUserName,
'expirationDate' => $this->any(),
));
2023-10-11 19:55:03 +00:00
$result = $this->ticketService->assign($ticketId, $this->userId);
$this->assertEquals($helperUserName, $result['matrixHelperName']);
}
}