Files
deck/tests/unit/Service/LabelServiceTest.php
Carl Schwan 5cf486150a refactor: Fix psalm issues
- Add typing for most of the services, controllers and mappers
- Add api doc for mappers
- Use vendor-bin for psalm
- Use attributes for controllers
- Fix upload of attachments

Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
2025-09-28 11:49:06 +02:00

171 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Deck\Service;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\ChangeHelper;
use OCA\Deck\Db\Label;
use OCA\Deck\Db\LabelMapper;
use OCA\Deck\Validators\LabelServiceValidator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class LabelServiceTest extends TestCase {
/** @var LabelMapper|\PHPUnit\Framework\MockObject\MockObject */
private $labelMapper;
/** @var PermissionService|\PHPUnit\Framework\MockObject\MockObject */
private $permissionService;
/** @var LabelService */
private $labelService;
/** @var BoardService|\PHPUnit\Framework\MockObject\MockObject */
private $boardService;
/** @var ChangeHelper|\PHPUnit\Framework\MockObject\MockObject */
private ChangeHelper&MockObject $changeHelper;
private LabelServiceValidator&MockObject $labelServiceValidator;
public function setUp(): void {
parent::setUp();
$this->labelMapper = $this->getMockBuilder(LabelMapper::class)
->disableOriginalConstructor()->getMock();
$this->permissionService = $this->getMockBuilder(PermissionService::class)
->disableOriginalConstructor()->getMock();
$this->boardService = $this->createMock(BoardService::class);
$this->changeHelper = $this->createMock(ChangeHelper::class);
$this->labelServiceValidator = $this->createMock(LabelServiceValidator::class);
$this->labelService = new LabelService(
$this->labelMapper,
$this->permissionService,
$this->boardService,
$this->changeHelper,
$this->labelServiceValidator,
);
}
public function testFind() {
$label = $this->createMock(Label::class);
$this->labelMapper->expects($this->once())->method('find')->willReturn($label);
$this->assertEquals($label, $this->labelService->find(123));
}
public function testCreate() {
$label = new Label();
$label->setTitle('Label title');
$label->setBoardId(123);
$label->setColor('00ff00');
$this->labelMapper->expects($this->once())
->method('insert')
->willReturn($label);
$b = $this->labelService->create('Label title', '00ff00', 123);
$this->assertEquals($b->getTitle(), 'Label title');
$this->assertEquals($b->getBoardId(), 123);
$this->assertEquals($b->getColor(), '00ff00');
}
public function testUpdate() {
$label = new Label();
$label->setTitle('Title');
$label->setBoardId(123);
$label->setColor('00ff00');
$this->labelMapper->expects($this->once())
->method('find')
->with(1)
->willReturn($label);
$this->labelMapper->expects($this->once())
->method('update')
->with($label)
->willReturn($label);
$b = $this->labelService->update(1, 'NewTitle', 'ffffff');
$this->assertEquals($b->getTitle(), 'NewTitle');
$this->assertEquals($b->getBoardId(), 123);
$this->assertEquals($b->getColor(), 'ffffff');
}
public function testCloneLabelIfNotExists() {
$label = new Label();
$label->setId(1);
$label->setTitle('title');
$label->setColor('00ff00');
$this->labelMapper->expects($this->once())
->method('find')
->willReturn($label);
$expectedLabel = new Label();
$expectedLabel->setTitle('title');
$expectedLabel->setColor('00ff00');
$expectedLabel->setBoardId(1);
$this->labelMapper->expects($this->once())
->method('insert')
->with($expectedLabel)
->willReturn($label);
$board = new Board();
$board->setLabels([]);
$this->boardService->expects($this->once())
->method('find')
->willReturn($board);
$this->labelService->cloneLabelIfNotExists(1, 1);
}
public function testCloneLabelIfExists() {
$label = new Label();
$label->setId(1);
$label->setTitle('title');
$label->setColor('00ff00');
$this->labelMapper->expects($this->once())
->method('find')
->willReturn($label);
$this->labelMapper->expects($this->never())
->method('insert')
->with($label);
$board = new Board();
$board->setLabels([$label]);
$this->boardService->expects($this->once())
->method('find')
->willReturn($board);
$this->labelService->cloneLabelIfNotExists(1, 1);
}
public function testDelete() {
$label = new Label();
$label->setId(1);
$this->labelMapper->expects($this->once())
->method('find')
->willReturn($label);
$this->labelMapper->expects($this->once())
->method('delete')
->willReturn($label);
$this->assertEquals($label, $this->labelService->delete(1));
}
}