diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-09-05 23:30:56 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-09-05 23:30:56 -0400 |
commit | 710f2c7bb046b43ec9878ae795a181101f6d7515 (patch) | |
tree | b62723b6b49c3b6bf2b3ca41a772f552464a9031 /tests/units/Core | |
parent | 94b38dd94bd819168163003beec8ef693f9d9839 (diff) |
Improve unit tests
Diffstat (limited to 'tests/units/Core')
-rw-r--r-- | tests/units/Core/LexerTest.php | 443 | ||||
-rw-r--r-- | tests/units/Core/OAuth2Test.php | 43 | ||||
-rw-r--r-- | tests/units/Core/RouterTest.php | 79 | ||||
-rw-r--r-- | tests/units/Core/ToolTest.php | 15 |
4 files changed, 580 insertions, 0 deletions
diff --git a/tests/units/Core/LexerTest.php b/tests/units/Core/LexerTest.php new file mode 100644 index 00000000..044655fc --- /dev/null +++ b/tests/units/Core/LexerTest.php @@ -0,0 +1,443 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Core\Lexer; + +class LexerTest extends Base +{ + public function testSwimlaneQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'swimlane:', 'token' => 'T_SWIMLANE'), array('match' => 'Version 42', 'token' => 'T_STRING')), + $lexer->tokenize('swimlane:"Version 42"') + ); + + $this->assertEquals( + array(array('match' => 'swimlane:', 'token' => 'T_SWIMLANE'), array('match' => 'v3', 'token' => 'T_STRING')), + $lexer->tokenize('swimlane:v3') + ); + + $this->assertEquals( + array('T_SWIMLANE' => array('v3')), + $lexer->map($lexer->tokenize('swimlane:v3')) + ); + + $this->assertEquals( + array('T_SWIMLANE' => array('Version 42', 'v3')), + $lexer->map($lexer->tokenize('swimlane:"Version 42" swimlane:v3')) + ); + } + + public function testAssigneeQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'assignee:', 'token' => 'T_ASSIGNEE'), array('match' => 'me', 'token' => 'T_STRING')), + $lexer->tokenize('assignee:me') + ); + + $this->assertEquals( + array(array('match' => 'assignee:', 'token' => 'T_ASSIGNEE'), array('match' => 'everybody', 'token' => 'T_STRING')), + $lexer->tokenize('assignee:everybody') + ); + + $this->assertEquals( + array(array('match' => 'assignee:', 'token' => 'T_ASSIGNEE'), array('match' => 'nobody', 'token' => 'T_STRING')), + $lexer->tokenize('assignee:nobody') + ); + + $this->assertEquals( + array('T_ASSIGNEE' => array('nobody')), + $lexer->map($lexer->tokenize('assignee:nobody')) + ); + + $this->assertEquals( + array('T_ASSIGNEE' => array('John Doe', 'me')), + $lexer->map($lexer->tokenize('assignee:"John Doe" assignee:me')) + ); + } + + public function testColorQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'color:', 'token' => 'T_COLOR'), array('match' => 'Blue', 'token' => 'T_STRING')), + $lexer->tokenize('color:Blue') + ); + + $this->assertEquals( + array(array('match' => 'color:', 'token' => 'T_COLOR'), array('match' => 'Dark Grey', 'token' => 'T_STRING')), + $lexer->tokenize('color:"Dark Grey"') + ); + + $this->assertEquals( + array('T_COLOR' => array('Blue')), + $lexer->map($lexer->tokenize('color:Blue')) + ); + + $this->assertEquals( + array('T_COLOR' => array('Dark Grey')), + $lexer->map($lexer->tokenize('color:"Dark Grey"')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('color: ')) + ); + } + + public function testCategoryQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'category:', 'token' => 'T_CATEGORY'), array('match' => 'Feature Request', 'token' => 'T_STRING')), + $lexer->tokenize('category:"Feature Request"') + ); + + $this->assertEquals( + array('T_CATEGORY' => array('Feature Request')), + $lexer->map($lexer->tokenize('category:"Feature Request"')) + ); + + $this->assertEquals( + array('T_CATEGORY' => array('Feature Request', 'Bug')), + $lexer->map($lexer->tokenize('category:"Feature Request" category:Bug')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('category: ')) + ); + } + + public function testColumnQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'column:', 'token' => 'T_COLUMN'), array('match' => 'Feature Request', 'token' => 'T_STRING')), + $lexer->tokenize('column:"Feature Request"') + ); + + $this->assertEquals( + array('T_COLUMN' => array('Feature Request')), + $lexer->map($lexer->tokenize('column:"Feature Request"')) + ); + + $this->assertEquals( + array('T_COLUMN' => array('Feature Request', 'Bug')), + $lexer->map($lexer->tokenize('column:"Feature Request" column:Bug')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('column: ')) + ); + } + + public function testProjectQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'project:', 'token' => 'T_PROJECT'), array('match' => 'My project', 'token' => 'T_STRING')), + $lexer->tokenize('project:"My project"') + ); + + $this->assertEquals( + array('T_PROJECT' => array('My project')), + $lexer->map($lexer->tokenize('project:"My project"')) + ); + + $this->assertEquals( + array('T_PROJECT' => array('My project', 'plop')), + $lexer->map($lexer->tokenize('project:"My project" project:plop')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('project: ')) + ); + } + + public function testStatusQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'status:', 'token' => 'T_STATUS'), array('match' => 'open', 'token' => 'T_STRING')), + $lexer->tokenize('status:open') + ); + + $this->assertEquals( + array(array('match' => 'status:', 'token' => 'T_STATUS'), array('match' => 'closed', 'token' => 'T_STRING')), + $lexer->tokenize('status:closed') + ); + + $this->assertEquals( + array('T_STATUS' => 'open'), + $lexer->map($lexer->tokenize('status:open')) + ); + + $this->assertEquals( + array('T_STATUS' => 'closed'), + $lexer->map($lexer->tokenize('status:closed')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('status: ')) + ); + } + + public function testReferenceQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'ref:', 'token' => 'T_REFERENCE'), array('match' => '123', 'token' => 'T_STRING')), + $lexer->tokenize('ref:123') + ); + + $this->assertEquals( + array(array('match' => 'reference:', 'token' => 'T_REFERENCE'), array('match' => '456', 'token' => 'T_STRING')), + $lexer->tokenize('reference:456') + ); + + $this->assertEquals( + array('T_REFERENCE' => '123'), + $lexer->map($lexer->tokenize('reference:123')) + ); + + $this->assertEquals( + array('T_REFERENCE' => '456'), + $lexer->map($lexer->tokenize('ref:456')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('ref: ')) + ); + } + + public function testDescriptionQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'description:', 'token' => 'T_DESCRIPTION'), array('match' => 'my text search', 'token' => 'T_STRING')), + $lexer->tokenize('description:"my text search"') + ); + + $this->assertEquals( + array('T_DESCRIPTION' => 'my text search'), + $lexer->map($lexer->tokenize('description:"my text search"')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('description: ')) + ); + } + + public function testDueDateQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => '2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('due:2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => '<2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('due:<2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => '>2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('due:>2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => '<=2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('due:<=2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => '>=2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('due:>=2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => 'yesterday', 'token' => 'T_DATE')), + $lexer->tokenize('due:yesterday') + ); + + $this->assertEquals( + array(array('match' => 'due:', 'token' => 'T_DUE'), array('match' => 'tomorrow', 'token' => 'T_DATE')), + $lexer->tokenize('due:tomorrow') + ); + + $this->assertEquals( + array(), + $lexer->tokenize('due:#2015-05-01') + ); + + $this->assertEquals( + array(), + $lexer->tokenize('due:01-05-1024') + ); + + $this->assertEquals( + array('T_DUE' => '2015-05-01'), + $lexer->map($lexer->tokenize('due:2015-05-01')) + ); + + $this->assertEquals( + array('T_DUE' => '<2015-05-01'), + $lexer->map($lexer->tokenize('due:<2015-05-01')) + ); + + $this->assertEquals( + array('T_DUE' => 'today'), + $lexer->map($lexer->tokenize('due:today')) + ); + } + + public function testModifiedQuery() + { + $lexer = new Lexer; + + $this->assertEquals( + array(array('match' => 'modified:', 'token' => 'T_UPDATED'), array('match' => '2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('modified:2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'modified:', 'token' => 'T_UPDATED'), array('match' => '<2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('modified:<2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'modified:', 'token' => 'T_UPDATED'), array('match' => '>2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('modified:>2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'updated:', 'token' => 'T_UPDATED'), array('match' => '<=2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('updated:<=2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'updated:', 'token' => 'T_UPDATED'), array('match' => '>=2015-05-01', 'token' => 'T_DATE')), + $lexer->tokenize('updated:>=2015-05-01') + ); + + $this->assertEquals( + array(array('match' => 'updated:', 'token' => 'T_UPDATED'), array('match' => 'yesterday', 'token' => 'T_DATE')), + $lexer->tokenize('updated:yesterday') + ); + + $this->assertEquals( + array(array('match' => 'updated:', 'token' => 'T_UPDATED'), array('match' => 'tomorrow', 'token' => 'T_DATE')), + $lexer->tokenize('updated:tomorrow') + ); + + $this->assertEquals( + array(), + $lexer->tokenize('updated:#2015-05-01') + ); + + $this->assertEquals( + array(), + $lexer->tokenize('modified:01-05-1024') + ); + + $this->assertEquals( + array('T_UPDATED' => '2015-05-01'), + $lexer->map($lexer->tokenize('modified:2015-05-01')) + ); + + $this->assertEquals( + array('T_UPDATED' => '<2015-05-01'), + $lexer->map($lexer->tokenize('modified:<2015-05-01')) + ); + + $this->assertEquals( + array('T_UPDATED' => 'today'), + $lexer->map($lexer->tokenize('modified:today')) + ); + } + + public function testMultipleCriterias() + { + $lexer = new Lexer; + + $this->assertEquals( + array('T_COLOR' => array('Dark Grey'), 'T_ASSIGNEE' => array('Fred G'), 'T_TITLE' => 'my task title'), + $lexer->map($lexer->tokenize('color:"Dark Grey" assignee:"Fred G" my task title')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title', 'T_COLOR' => array('yellow')), + $lexer->map($lexer->tokenize('my title color:yellow')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title', 'T_DUE' => '2015-04-01'), + $lexer->map($lexer->tokenize('my title due:2015-04-01')) + ); + + $this->assertEquals( + array('T_TITLE' => 'awesome', 'T_DUE' => '<=2015-04-01'), + $lexer->map($lexer->tokenize('due:<=2015-04-01 awesome')) + ); + + $this->assertEquals( + array('T_TITLE' => 'awesome', 'T_DUE' => 'today'), + $lexer->map($lexer->tokenize('due:today awesome')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title', 'T_COLOR' => array('yellow'), 'T_DUE' => '2015-04-01'), + $lexer->map($lexer->tokenize('my title color:yellow due:2015-04-01')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title', 'T_COLOR' => array('yellow'), 'T_DUE' => '2015-04-01', 'T_ASSIGNEE' => array('John Doe')), + $lexer->map($lexer->tokenize('my title color:yellow due:2015-04-01 assignee:"John Doe"')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title'), + $lexer->map($lexer->tokenize('my title color:')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title'), + $lexer->map($lexer->tokenize('my title color:assignee:')) + ); + + $this->assertEquals( + array('T_TITLE' => 'my title'), + $lexer->map($lexer->tokenize('my title ')) + ); + + $this->assertEquals( + array('T_TITLE' => '#123'), + $lexer->map($lexer->tokenize('#123')) + ); + + $this->assertEquals( + array(), + $lexer->map($lexer->tokenize('color:assignee:')) + ); + } +} diff --git a/tests/units/Core/OAuth2Test.php b/tests/units/Core/OAuth2Test.php new file mode 100644 index 00000000..a0e4b43f --- /dev/null +++ b/tests/units/Core/OAuth2Test.php @@ -0,0 +1,43 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Core\OAuth2; + +class OAuth2Test extends Base +{ + public function testAuthUrl() + { + $oauth = new OAuth2($this->container); + $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g')); + $this->assertEquals('D?response_type=code&client_id=A&redirect_uri=C&scope=f+g', $oauth->getAuthorizationUrl()); + } + + public function testAuthHeader() + { + $oauth = new OAuth2($this->container); + $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g')); + + $oauth->setAccessToken('foobar', 'BeaRer'); + $this->assertEquals('Authorization: Bearer foobar', $oauth->getAuthorizationHeader()); + + $oauth->setAccessToken('foobar', 'unknown'); + $this->assertEquals('', $oauth->getAuthorizationHeader()); + } + + public function testAccessToken() + { + $oauth = new OAuth2($this->container); + $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g')); + $oauth->getAccessToken('something'); + + $data = $this->container['httpClient']->getData(); + $this->assertEquals('something', $data['code']); + $this->assertEquals('A', $data['client_id']); + $this->assertEquals('B', $data['client_secret']); + $this->assertEquals('C', $data['redirect_uri']); + $this->assertEquals('authorization_code', $data['grant_type']); + + $this->assertEquals('E', $this->container['httpClient']->getUrl()); + } +} diff --git a/tests/units/Core/RouterTest.php b/tests/units/Core/RouterTest.php new file mode 100644 index 00000000..99c49ba8 --- /dev/null +++ b/tests/units/Core/RouterTest.php @@ -0,0 +1,79 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Core\Router; + +class RouterTest extends Base +{ + public function testSanitize() + { + $r = new Router($this->container); + + $this->assertEquals('plop', $r->sanitize('PloP', 'default')); + $this->assertEquals('default', $r->sanitize('', 'default')); + $this->assertEquals('default', $r->sanitize('123-AB', 'default')); + $this->assertEquals('default', $r->sanitize('R&D', 'default')); + $this->assertEquals('default', $r->sanitize('Test123', 'default')); + } + + public function testPath() + { + $r = new Router($this->container); + + $this->assertEquals('a/b/c', $r->getPath('/a/b/c')); + $this->assertEquals('a/b/something', $r->getPath('/a/b/something?test=a', 'test=a')); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['PHP_SELF'] = '/a/index.php'; + + $this->assertEquals('b/c', $r->getPath('/a/b/c')); + $this->assertEquals('b/c', $r->getPath('/a/b/c?e=f', 'e=f')); + } + + public function testFindRouteWithEmptyTable() + { + $r = new Router($this->container); + $this->assertEquals(array('app', 'index'), $r->findRoute('')); + $this->assertEquals(array('app', 'index'), $r->findRoute('/')); + } + + public function testFindRouteWithoutPlaceholders() + { + $r = new Router($this->container); + $r->addRoute('a/b', 'controller', 'action'); + $this->assertEquals(array('app', 'index'), $r->findRoute('a/b/c')); + $this->assertEquals(array('controller', 'action'), $r->findRoute('a/b')); + } + + public function testFindRouteWithPlaceholders() + { + $r = new Router($this->container); + $r->addRoute('a/:myvar1/b/:myvar2', 'controller', 'action'); + $this->assertEquals(array('app', 'index'), $r->findRoute('a/123/b')); + $this->assertEquals(array('controller', 'action'), $r->findRoute('a/456/b/789')); + $this->assertEquals(array('myvar1' => 456, 'myvar2' => 789), $_GET); + } + + public function testFindMultipleRoutes() + { + $r = new Router($this->container); + $r->addRoute('a/b', 'controller1', 'action1'); + $r->addRoute('a/b', 'duplicate', 'duplicate'); + $r->addRoute('a', 'controller2', 'action2'); + $this->assertEquals(array('controller1', 'action1'), $r->findRoute('a/b')); + $this->assertEquals(array('controller2', 'action2'), $r->findRoute('a')); + } + + public function testFindUrl() + { + $r = new Router($this->container); + $r->addRoute('a/b', 'controller1', 'action1'); + $r->addRoute('a/:myvar1/b/:myvar2', 'controller2', 'action2', array('myvar1', 'myvar2')); + + $this->assertEquals('a/1/b/2', $r->findUrl('controller2', 'action2', array('myvar1' => 1, 'myvar2' => 2))); + $this->assertEquals('', $r->findUrl('controller2', 'action2', array('myvar1' => 1))); + $this->assertEquals('a/b', $r->findUrl('controller1', 'action1')); + $this->assertEquals('', $r->findUrl('controller1', 'action2')); + } +} diff --git a/tests/units/Core/ToolTest.php b/tests/units/Core/ToolTest.php new file mode 100644 index 00000000..e714f506 --- /dev/null +++ b/tests/units/Core/ToolTest.php @@ -0,0 +1,15 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Core\Tool; + +class ToolTest extends Base +{ + public function testMailboxHash() + { + $this->assertEquals('test1', Tool::getMailboxHash('a+test1@localhost')); + $this->assertEquals('', Tool::getMailboxHash('test1@localhost')); + $this->assertEquals('', Tool::getMailboxHash('test1')); + } +} |