refactor: clean up code and remove some dependencies
This commit is contained in:
59
tests/Processors/TodoTest.php
Normal file
59
tests/Processors/TodoTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use Sabre\DAV\Exception\BadRequest;
|
||||
use Monolog\Logger;
|
||||
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
|
||||
use Aerex\TaskwarriorPlugin\Processors\ToDo;
|
||||
use Sabre\VObject\Component\VCalendar;
|
||||
use DavidBadura\Taskwarrior\Task;
|
||||
use DateTime;
|
||||
|
||||
|
||||
|
||||
class TodoTest extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
* */
|
||||
private $mockTaskwarriorManager;
|
||||
|
||||
|
||||
function setup(){
|
||||
|
||||
$this->mockTaskwarriorManager = $this->createMock(TaskwarriorManager::class);
|
||||
|
||||
}
|
||||
|
||||
function testImportAndAddTask(){
|
||||
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
|
||||
$this->mockTaskwarriorManager->expects($this->once())->method('exists')->with($this->equalTo($uuid))
|
||||
->willReturn(false);
|
||||
|
||||
$this->mockTaskwarriorManager->expects($this->once())->method('addTask')->with($this->equalTo($mockVTodo));
|
||||
$todo = new ToDo($this->mockTaskwarriorManager);
|
||||
$todo->import($mockVTodo);
|
||||
|
||||
}
|
||||
|
||||
function testImportAndUpdateTask(){
|
||||
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
|
||||
$this->mockTaskwarriorManager->expects($this->once())->method('exists')
|
||||
->willReturn(true);
|
||||
|
||||
|
||||
$this->mockTaskwarriorManager->expects($this->once())->method('updateTask');
|
||||
|
||||
$todo = new ToDo($this->mockTaskwarriorManager);
|
||||
$todo->import($mockVTodo);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
173
tests/TaskTest.php
Normal file
173
tests/TaskTest.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
use Sabre\VObject\Component\VCalendar;
|
||||
use Sabre\DAV\Exception\BadRequest;
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
|
||||
use Aerex\TaskwarriorPlugin\Plugin;
|
||||
use Carbon\Carbon;
|
||||
use Aerex\TaskwarriorPlugin\Config;
|
||||
use Sabre\DAV\Server;
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
|
||||
class TaskTest extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
/**
|
||||
* @var Plugin
|
||||
*
|
||||
*/
|
||||
private $plugin;
|
||||
/**
|
||||
* @var VCalendar
|
||||
*/
|
||||
private $cal;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var Server
|
||||
*/
|
||||
private $server;
|
||||
|
||||
/**
|
||||
* @var TaskwarriorCalendarEvent
|
||||
*/
|
||||
private $mockTaskCalEvent;
|
||||
|
||||
|
||||
|
||||
public function testSetDescriptionUsingDescription(){
|
||||
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
|
||||
$expectedDescription = 'This is a description';
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
|
||||
$mockVTodo->DESCRIPTION = $expectedDescription;
|
||||
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setDescription($mockVTodo);
|
||||
$actualDescription = $todo->getDescription();
|
||||
|
||||
$this->assertEquals($expectedDescription, $actualDescription);
|
||||
}
|
||||
public function testSetDescriptionUsingSummary(){
|
||||
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
|
||||
$expectedDescription = 'This is a description';
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
|
||||
$mockVTodo->SUMMARY = $expectedDescription;
|
||||
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setDescription($mockVTodo);
|
||||
$actualDescription = $todo->getDescription();
|
||||
|
||||
$this->assertEquals($expectedDescription, $actualDescription);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Error
|
||||
*/
|
||||
public function testFailureSetDescription(){
|
||||
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
|
||||
$expectedDescription = 'This is a description';
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setDescription($mockVTodo);
|
||||
$actualDescription = $todo->getDescription();
|
||||
|
||||
}
|
||||
public function testSetEntryTime(){
|
||||
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
|
||||
$expectedEntryTime = new DateTime('2018-11-11');
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
$expectedCarbonDate = new Carbon($expectedEntryTime->format('Y-m-d'));
|
||||
$mockVTodo->DTSTAMP = $expectedEntryTime;
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setEntryTime($mockVTodo);
|
||||
$actualEntryTime = $todo->getEntryTime();
|
||||
|
||||
$this->assertEquals($expectedCarbonDate, $actualEntryTime);
|
||||
}
|
||||
public function testSetStartTime(){
|
||||
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
|
||||
$expectedStartTime = new DateTime('2018-11-11');
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
$expectedCarbonDate = new Carbon($expectedStartTime->format('Y-m-d'));
|
||||
$mockVTodo->DTSTART = $expectedStartTime;
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setStartTime($mockVTodo);
|
||||
$actualStartTime = $todo->getStartTime();
|
||||
|
||||
$this->assertEquals($expectedCarbonDate, $actualStartTime);
|
||||
}
|
||||
public function testSetModifiedTime(){
|
||||
$uuid = '182a6301-98e6-44df-97eb-8c7620f25b43';
|
||||
$expectedModifiedTime = new DateTime('2018-11-11');
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
$expectedCarbonDate = new Carbon($expectedModifiedTime->format('Y-m-d'));
|
||||
$mockVTodo->{'LAST-MODIFIED'} = $expectedModifiedTime;
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setModifiedTime($mockVTodo);
|
||||
$actualModifiedTime = $todo->getModifiedTime();
|
||||
|
||||
$this->assertEquals($expectedCarbonDate, $actualModifiedTime);
|
||||
}
|
||||
public function testSetDue(){
|
||||
$uuid = 'dde78b02-97d9-4e11-8603-e0fc14474c7c';
|
||||
$expectedDueTime = new DateTime('2018-11-11');
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
$expectedCarbonDate = new Carbon($expectedDueTime->format('Y-m-d'));
|
||||
$mockVTodo->DUE = $expectedDueTime;
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setDue($mockVTodo);
|
||||
$actualDue = $todo->getDue();
|
||||
|
||||
$this->assertEquals($expectedCarbonDate, $actualDue);
|
||||
}
|
||||
|
||||
public function testSetCategories(){
|
||||
|
||||
$uuid = '182a6301-98e6-44df-97eb-8c7620f25b43';
|
||||
$expectedCategories = array("home", "work");
|
||||
|
||||
$mockVCalendar = new VCalendar();
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
$mockVTodo->CATEGORIES = $expectedCategories;
|
||||
|
||||
$todo = new Task();
|
||||
|
||||
$todo->setCategories($mockVTodo);
|
||||
$actualCategories = $todo->getCategories();
|
||||
|
||||
$this->assertEquals($expectedCategories, $actualCategories);
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,174 +0,0 @@
|
||||
<?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);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
Reference in New Issue
Block a user