Skip to content

Commit

Permalink
Merge pull request #84 from TwistPHP/more-travis-tests
Browse files Browse the repository at this point in the history
Added more travis test, coverage now up to 18%
  • Loading branch information
ahosgood committed Mar 23, 2016
2 parents d40d4db + 7ec1248 commit 97e4794
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ php:
- 5.6
env COVERALLS=ON
- 7.0
- hhvm
# - hhvm
- nightly

services:
Expand Down
14 changes: 10 additions & 4 deletions dist/twist/Classes/Error.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,21 @@ public static function handle404(){
* Output HTTP error response code, This function has been deprecated in favour of the response() method
* @param int $intErrorCode
* @param null|string $strCustomDescription
* @param boolean $blExitOnComplete Set false will output error and continue (Used for testing)
* @alias response
* @deprecated
*/
public static function errorPage($intErrorCode,$strCustomDescription = null){
self::response($intErrorCode,$strCustomDescription);
public static function errorPage($intErrorCode,$strCustomDescription = null,$blExitOnComplete = true){
self::response($intErrorCode,$strCustomDescription,$blExitOnComplete);
}

/**
* Output HTTP error response code and a custom message if required to the user, this function handles all HTTP response codes.
* @param int $intErrorCode
* @param null|string $strCustomDescription
* @param boolean $blExitOnComplete Set false will output page and continue (Used for testing)
*/
public static function response($intErrorCode,$strCustomDescription = null){
public static function response($intErrorCode,$strCustomDescription = null,$blExitOnComplete = true){

$strReturn = 'Unknown';
$strDescription = '';
Expand Down Expand Up @@ -634,7 +636,11 @@ public static function response($intErrorCode,$strCustomDescription = null){
'domain' => \Twist::framework() -> setting('SITE_HOST')
);

die(\Twist::View('Exception')->build(sprintf("%s/system/error-page.tpl",TWIST_FRAMEWORK_VIEWS),$arrTags));
if($blExitOnComplete){
die(\Twist::View('Exception')->build(sprintf("%s/system/error-page.tpl",TWIST_FRAMEWORK_VIEWS),$arrTags));
}else{
echo \Twist::View('Exception')->build(sprintf("%s/system/error-page.tpl",TWIST_FRAMEWORK_VIEWS),$arrTags);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/twist/Core/Utilities/Device.utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getOSVersion($strUserAgent = null){
public function getDevice($strUserAgent = null){

$arrInfo = $this->get($strUserAgent);
return $arrInfo['device'];
return $arrInfo['type'];
}

/**
Expand Down
16 changes: 8 additions & 8 deletions dist/twist/Core/Utilities/Route.utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ public function serve($blExitOnComplete = true){

//Check to see if the current route is allowed to bypass
if($this->findURI($this->arrBypassMaintenanceMode,$arrRoute['uri']) === false){
\Twist::respond(503,'The site is currently undergoing maintenance, please check back shortly!');
\Twist::respond(503,'The site is currently undergoing maintenance, please check back shortly!',$blExitOnComplete);
}
}

Expand All @@ -1280,7 +1280,7 @@ public function serve($blExitOnComplete = true){
\Twist::User()->setAfterLoginRedirect();
\Twist::redirect(str_replace('//', '/', $arrRestriction['login_uri']));
}elseif($arrRestriction['allow_access'] == false){
\Twist::respond(403);
\Twist::respond(403,null,$blExitOnComplete);
}else{

$this->meta()->title(\Twist::framework()->setting('SITE_NAME'));
Expand Down Expand Up @@ -1312,7 +1312,7 @@ public function serve($blExitOnComplete = true){

//Run through all the serve types, this has been made into a separate function
//So that it can be extended by other systems
$arrTags = $this->serveTypes($arrRoute,$arrTags);
$arrTags = $this->serveTypes($arrRoute,$arrTags,$blExitOnComplete);

\Twist::recordEvent('Route processed');

Expand Down Expand Up @@ -1392,11 +1392,11 @@ public function serve($blExitOnComplete = true){
}

} elseif ($this->bl404) {
\Twist::respond(404);
\Twist::respond(404,null,$blExitOnComplete);
}
}

protected function serveTypes($arrRoute,$arrTags){
protected function serveTypes($arrRoute,$arrTags,$blExitOnComplete = true){

switch($arrRoute['type']){
case'view':
Expand All @@ -1417,10 +1417,10 @@ protected function serveTypes($arrRoute,$arrTags){
$strMimeType = ($arrRoute['item']['force-download']) ? null : \Twist::File()->mimeType($strFilePath);
\Twist::File()->serve($strFilePath, basename($strFilePath), $strMimeType, null, $arrRoute['item']['speed'], false);
}else{
\Twist::respond(403,'Unsupported file extension, PHP files are disallowed through this method');
\Twist::respond(403,'Unsupported file extension, PHP files are disallowed through this method',$blExitOnComplete);
}
}else{
\Twist::respond(404);
\Twist::respond(404,null,$blExitOnComplete);
}

break;
Expand All @@ -1429,7 +1429,7 @@ protected function serveTypes($arrRoute,$arrTags){
break;
case'ajax':
if(!TWIST_AJAX_REQUEST){
\Twist::respond(403,'Unsupported HTTP protocol used to request this URI');
\Twist::respond(403,'Unsupported HTTP protocol used to request this URI',$blExitOnComplete);
}else{
try{
$arrTags['response'] = $this->processController($arrRoute);
Expand Down
8 changes: 5 additions & 3 deletions dist/twist/Twist.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,12 @@ public static function redirect($urlRedirect,$blPermanent = false){

/**
* Respond with a HTTP status page, pass in the status code that you require
* @param $intResponseCode Code of the required response i.e. 404
* @param int $intResponseCode Code of the required response i.e. 404
* @param null|string $strCustomDescription
* @param boolean $blExitOnComplete Set false will output error and continue (Used for testing)
*/
public static function respond($intResponseCode,$strCustomDescription = null){
Error::errorPage($intResponseCode,$strCustomDescription);
public static function respond($intResponseCode,$strCustomDescription = null,$blExitOnComplete = true){
Error::response($intResponseCode,$strCustomDescription,$blExitOnComplete);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Core/Models/Hooks.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class Hooks extends \PHPUnit_Framework_TestCase{

public function testGetAllHooks(){
$this->assertTrue(array_key_exists('TWIST_VIEW_TAG',\Twist::framework()->hooks()->getAll()));
}

public function testRegisterHook(){
\Twist::framework()->hooks()->register('travisCI','travis-test-hook','test-hook');
$this->assertEquals('test-hook',\Twist::framework()->hooks()->get('travisCI','travis-test-hook'));
}

public function testGetRegisteredHook(){
$this->assertEquals('test-hook',\Twist::framework()->hooks()->get('travisCI','travis-test-hook'));
}

public function testCancelRegisteredHook(){
\Twist::framework()->hooks()->cancel('travisCI','travis-test-hook');
$this->assertEquals(null,\Twist::framework()->hooks()->get('travisCI','travis-test-hook'));
}

}
11 changes: 11 additions & 0 deletions tests/Core/Models/Tools.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class Tools extends \PHPUnit_Framework_TestCase{

public function testTraverseURI(){
//Need to look at this
$this->assertEquals('/my/test',\Twist::framework()->tools()->traverseURI('./','/my/test/uri'));
$this->assertEquals('/my/test',\Twist::framework()->tools()->traverseURI('../','/my/test/uri'));
}

}
29 changes: 29 additions & 0 deletions tests/Core/Models/UserAgent.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class UserAgent extends \PHPUnit_Framework_TestCase{

public function testGet(){
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
$this->assertEquals('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',Twist\Core\Models\UserAgent::get());
}

public function testDetect(){
$arrUserAgent = Twist\Core\Models\UserAgent::detect();
$this->assertEquals('Firefox',$arrUserAgent['browser']['title']);
}

public function testDeviceType(){
$this->assertEquals('Desktop',Twist\Core\Models\UserAgent::getDeviceType('win10'));
}

public function testOS(){
$arrUserAgent = Twist\Core\Models\UserAgent::getOS('win10');
$this->assertEquals('Windows 10',$arrUserAgent['version']);
}

public function testBrowser(){
$arrUserAgent = Twist\Core\Models\UserAgent::getBrowser('firefox');
$this->assertEquals('Firefox',$arrUserAgent['title']);
}

}
24 changes: 24 additions & 0 deletions tests/Core/Utilities/CSV.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class CSV extends \PHPUnit_Framework_TestCase{

public function testGenerate(){

$arrData = array(
0 => array('id' => 1,'name' => 'Dan','description' => 'test desc 1'),
1 => array('id' => 2,'name' => 'Andi','description' => 'test desc 2')
);

$strCSV = \Twist::CSV()->export(TWIST_UPLOADS.'test.csv',$arrData);

$this->assertTrue(strstr($strCSV,',') && strstr($strCSV,"\n") && strstr($strCSV,'id'));
}

public function testImport(){

$arrData = \Twist::CSV()->import(TWIST_UPLOADS.'test.csv',"\n",",",'""',"\\",true);

$this->assertTrue(count($arrData) == 2 && $arrData[0]['id'] == 1 && $arrData[1]['name'] == 'Andi');
}

}
27 changes: 27 additions & 0 deletions tests/Core/Utilities/Device.Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

class Device extends \PHPUnit_Framework_TestCase{

public function testGet(){
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
$arrDevice = Twist::Device()->get();
$this->assertEquals('Firefox',$arrDevice['browser']['title']);
}

public function testGetOS(){
$this->assertEquals('Windows',Twist::Device()->getOS());
}

public function testGetOSVersion(){
$this->assertEquals('Windows 7',Twist::Device()->getOSVersion());
}

public function testGetDevice(){
$this->assertEquals('Desktop',Twist::Device()->getDevice());
}

public function testGetBrowser(){
$this->assertEquals('Firefox',Twist::Device()->getBrowser());
}

}
Loading

0 comments on commit 97e4794

Please sign in to comment.