TaskwarriorPlugin/tests/iCalEventProcessorTest.php

175 lines
5.8 KiB
PHP
Raw Normal View History

<?php
use Sabre\DAV\Exception\BadRequest;
use Monolog\Logger;
use Sabre\VObject\Component\VCalendar;
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
use Aerex\TaskwarriorPlugin\Config;
use DavidBadura\Taskwarrior\Task;
use DateTime;
class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject
* */
private $mockTaskwarrior;
/***
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mockTaskwarriorConfig;
/***
* @var \PHPUnit_Framework__MockObject
*/
private $mockLogger;
function setup(){
$this->mockTaskwarrior = $this->createMock(TaskwarriorManager::class);
$this->mockTask = $this->createMock(Task::class);
$this->mockTaskwarriorConfig = $this->createMock(Config::class);
$this->mockLogger = $this->createMock(Logger::class);
}
function testConstructorWithConfig() {
$this->mockTaskwarriorConfig->expects($this->once())
->method('getTaskwarriorInstance')
->will($this->returnValue($this->mockTaskwarrior));
$this->taskCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
}
function testBuildToDoComponent(){
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
$startTime = new DateTime('2018-07-04');
$endTime = new DateTime('2018-07-06');
$modifiedTime = new DateTime('2018-08-05');
$categories = array('home', 'test');
$dueDate = new DateTime('2018-08-20');
$description = 'This is a simple todo';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$mockVTodo->add('DSTAMP', $startTime);
$mockVTodo->add('DUE', $dueDate);
$mockVTodo->add('LAST-MODIFIED',$modifiedTime);
$mockVTodo->add('DSTART', $startTime);
$mockVTodo->add('DTEND', $endTime);
$mockVTodo->add('DESCRIPTION', $description);
$mockVTodo->add('CATEGORIES', $categories);
// $this->mockTaskwarrior->expects($this->exactly(4))
// ->method('parseiCalDateTime')
// ->will($this->onConsecutiveCalls($startTime, $startTime, $modifiedTime, $endTime));
$this->mockTaskwarrior->expects($this->once())->method('createTask')->with($this->equalTo($uuid))
->willReturn($this->mockTask);
$this->mockTask->expects($this->once())->method('setDue')->with($this->equalTo($dueDate));
$this->mockTask->expects($this->once())->method('setDescription')->with($this->equalTo($description));
$this->mockTaskwarrior->expects($this->once())->method('convertToStringArray')->with($this->equalTo($mockVTodo->CATEGORIES))
->willReturn($categories);
$this->mockTask->expects($this->once())->method('setTags')
->with($this->equalTo($categories));
$this->mockTaskwarrior->expects($this->exactly(4))
->method('setValue');
$this->mockLogger->expects($this->never())->method('error');
$this->mockTaskwarriorConfig
->expects($this->once())
->method('getTaskwarriorInstance')
->will($this->returnValue($this->mockTaskwarrior));
$this
->mockTaskwarrior
->expects($this->once())
->method('save');
$twCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
$twCalEvent->importTask($mockVTodo);
}
/**
* @expectedException Sabre\DAV\Exception
*
*
*/
function testFailTaskNoComponentDefine(){
$this->mockTaskwarrior->expects($this->never())->method('createTask');
$this->mockTaskwarrior->expects($this->never())->method('setEntryTime');
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
$this->mockTaskwarrior->expects($this->never())->method('setSummary');
$this->mockTaskwarrior->expects($this->never())->method('setCategories');
$this->mockTaskwarrior->expects($this->never())->method('build');
$this->mockTaskwarrior->expects($this->never())->method('save');
$this->mockTaskwarrior->expects($this->never())->method('exists');
$this->mockTaskwarriorConfig->expects($this->once())->method('getTaskwarriorInstance')
->willReturn($this->mockTaskwarrior);
$this->mockLogger->expects($this->once())->method('error');
$this->mockTaskwarriorConfig->expects($this->once())->method('getLogger')
->willReturn($this->mockLogger);
$twCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
$twCalEvent->importTask();
}
/**
* @expectedException Sabre\DAV\Exception\BadRequest
*
*
*/
/*
function testFailTaskExists(){
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$expectedErrorMessage = "already exists";
$this->mockTaskwarrior->expects($this->never())->method('createTask');
$this->mockTaskwarrior->expects($this->never())->method('setEntryTime');
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
$this->mockTaskwarrior->expects($this->never())->method('setSummary');
$this->mockTaskwarrior->expects($this->never())->method('setCategories');
$this->mockTaskwarrior->expects($this->never())->method('build');
$this->mockTaskwarrior->expects($this->never())->method('save');
$this->mockTaskwarrior->expects($this->once())->method('exists')
->willReturn(true);
$this->mockTaskwarriorConfig->expects($this->once())->method('getTaskwarriorInstance')
->willReturn($this->mockTaskwarrior);
$this->mockLogger->expects($this->once())->method('error');
$this->mockTaskwarriorConfig->expects($this->once())->method('getLogger')
->willReturn($this->mockLogger);
$twCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
$twCalEvent->importTask($mockVTodo);
}
*/
}
?>