refactor: remove builder class and use concrete class
tests: added more unit tests for event processor
This commit is contained in:
96
tests/PluginTest.php
Normal file
96
tests/PluginTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
use Sabre\VObject\Component\VCalendar;
|
||||
use Sabre\DAV\Exception\BadRequest;
|
||||
use Aerex\TaskwarriorPlugin\Plugin;
|
||||
use Aerex\TaskwarriorPlugin\Config;
|
||||
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
|
||||
use Sabre\DAV\Server;
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
|
||||
class PluginTest extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
/**
|
||||
* @var Plugin
|
||||
*
|
||||
*/
|
||||
private $plugin;
|
||||
/**
|
||||
* @var VCalendar
|
||||
*/
|
||||
private $cal;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var Server
|
||||
*/
|
||||
private $server;
|
||||
|
||||
/**
|
||||
* @var TaskwarriorCalendarEvent
|
||||
*/
|
||||
private $mockTaskCalEvent;
|
||||
|
||||
|
||||
/**
|
||||
* @var TaskwarriorConfig
|
||||
*/
|
||||
private $mockTaskwarriorConfig;
|
||||
|
||||
function setup(){
|
||||
|
||||
$this->cal = new VCalendar();
|
||||
$this->mockTaskCalEvent = $this->createMock(iCalEventProcessor::class);
|
||||
$this->cal->add('VTODO', [
|
||||
"UID" => "1ff0313e-1ffa-4a18-b8c1-449bddc9109c",
|
||||
], false);
|
||||
|
||||
}
|
||||
|
||||
function testCreateSimpleTask() {
|
||||
|
||||
$this->server = new Server();
|
||||
|
||||
$this->mockTaskCalEvent->expects($this->once())
|
||||
->method('importTask')
|
||||
->with($this->cal->VTODO);
|
||||
|
||||
$this->plugin = new Plugin($this->mockTaskCalEvent);
|
||||
$this->plugin->initialize($this->server);
|
||||
$this->plugin->processCalendarEventForTaskwarrior($this->cal);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO Need to figure out why it is not throwing the correct Exception
|
||||
function testBadRequest(){
|
||||
$expectedBadRequestMessage = 'This was a bad request';
|
||||
|
||||
$this->server = new Server();
|
||||
$this->cal = new VCalendar();
|
||||
|
||||
$this->mockTaskCalEvent->expects($this->once())
|
||||
->method('buildVEVENTForTW')
|
||||
->with($this->cal->VEVENT);
|
||||
$this->mockTaskCalEvent
|
||||
->method('buildVTODOForTW')
|
||||
->with($this->throwException(new BadRequest));
|
||||
|
||||
|
||||
$this->plugin = new Plugin($this->mockTaskCalEvent);
|
||||
$this->plugin->initialize($this->server);
|
||||
try {
|
||||
$this->plugin->processCalendarEventForTaskwarrior($this->cal);
|
||||
} catch(BadRequest $e){
|
||||
$this->assertInstanceOf(BadRequest::class, $e);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
?>
|
13
tests/bootstrap.php
Normal file
13
tests/bootstrap.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
$loader = require __DIR__ . '/../vendor/autoload.php';
|
||||
if (!$loader) {
|
||||
die(<<<'EOT'
|
||||
You must set up the project dependencies, run the following command:
|
||||
composer install
|
||||
EOT
|
||||
);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
171
tests/iCalEventProcessorTest.php
Normal file
171
tests/iCalEventProcessorTest.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
use Sabre\DAV\Exception\BadRequest;
|
||||
use Monolog\Logger;
|
||||
use Sabre\VObject\Component\VCalendar;
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior;
|
||||
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
|
||||
use Aerex\TaskwarriorPlugin\Config;
|
||||
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_MockObject
|
||||
*/
|
||||
private $mockLogger;
|
||||
|
||||
function setup(){
|
||||
|
||||
$this->mockTaskwarrior = $this->createMock(Taskwarrior::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->once())->method('setUUID')
|
||||
->with($this->equalTo($mockVTodo->UID));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setEntryTime')
|
||||
->with($this->equalTo($mockVTodo->DSTAMP));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setEndTime')
|
||||
->with($this->equalTo($mockVTodo->DTEND));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setDueDate')
|
||||
->with($this->equalTo($mockVTodo->DUE));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setSummary')
|
||||
->with($this->equalTo($mockVTodo->DESCRIPTION));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setCategories')
|
||||
->with($this->equalTo($mockVTodo->CATEGORIES));
|
||||
|
||||
$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('build');
|
||||
|
||||
$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('setUUID');
|
||||
$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('setUUID');
|
||||
$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