You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
8.3 KiB
PHP

<?php
namespace Bsr\Webservice;
use PHPUnit\Framework\TestCase;
use Bsr\Utils\Logger\Logger;
use Bsr\Utils\Configuration\Configuration;
class WebserviceTest extends TestCase
{
private $webservice;
public function setUp()
{
$this->webservice = new MockWebserviceSubclass('1.2.3');
}
private function removeLogs()
{
$logsDir = realpath(dirname(Configuration::get('log.file')));
$files = glob("$logsDir/*");
foreach($files as $file){
if(is_file($file)) unlink($file);
}
}
/**
* @see config/configuration.local.php for the mock renderer used
* @runInSeparateProcess
*/
public function testRunSavesTheRequestIntoALogFile()
{
$this->removeLogs();
$log = Logger::getLastLogs();
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
$this->webservice->run(false);
$response = ob_get_clean();
$log = Logger::getLastLogs();
$this->assertNotEquals(Logger::NO_LOG_YET_MSG, $log);
}
/**
* @see config/configuration.local.php for the mock renderer used
* @runInSeparateProcess
*/
public function testReturnsErrorNoArgumentsWhenNoParamtersInGetPostRequest()
{
$_GET = array(); $_POST = array();
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(100, $responseArray['error']['code']);
$this->assertEquals('NoArguments', $responseArray['error']['name']);
}
public function testDoesNotReturnErrorNoArgumentsWhenParamtersInGetRequest()
{
$_GET = array('a' => 'a1');
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertNotEquals(100, $responseArray['error']['code']);
$this->assertNotEquals('NoArguments', $responseArray['error']['name']);
}
public function testDoesNotReturnErrorNoArgumentsWhenParamtersInPostRequest()
{
$_POST = array('a' => 'a1');
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertNotEquals(100, $responseArray['error']['code']);
$this->assertNotEquals('NoArguments', $responseArray['error']['name']);
}
public function testReturnsErrorMissingMethodWhenNoFuncParamInRequest()
{
$_POST = array('a' => 'a1');
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(101, $responseArray['error']['code']);
$this->assertEquals('MissingMethod', $responseArray['error']['name']);
}
public function testDoesNotReturnErrorMissingMethodWhenFuncParamInRequest()
{
$_GET = array('func' => 'someNonExistentMockFunction'); $_POST = array();
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertNotEquals(101, $responseArray['error']['code']);
$this->assertNotEquals('MissingMethod', $responseArray['error']['name']);
}
public function testReturnsErrorBadMethodWhenMethodNotExistsFuncParamInRequest()
{
$_GET = array('func' => 'someNonExistentMockFunction'); $_POST = array();
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(102, $responseArray['error']['code']);
$this->assertEquals('BadMethod', $responseArray['error']['name']);
}
public function testReturnsErrorTooFewArgsWhenMethodNeedsMoreParamsInRequest()
{
$_GET = array('func' => 'someMockFunction'); $_POST = array();
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(103, $responseArray['error']['code']);
$this->assertEquals('TooFewArgs', $responseArray['error']['name']);
}
public function testReturnsErrorTooFewArgsWhenMethodNeedsEvenMoreParamsInRequest()
{
$_GET = array(
'func' => 'someMockFunction',
'wrongParamName' => 'val',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(103, $responseArray['error']['code']);
$this->assertEquals('TooFewArgs', $responseArray['error']['name']);
}
public function testReturnsErrorTooManyArgsWhenMethodHasLessParamsThanInRequest()
{
$_GET = array(
'param1' => 'val',
'param2' => 'val',
'param3' => 'val',
'func' => 'someMockFunction',
'param4' => 'val',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(104, $responseArray['error']['code']);
$this->assertEquals('TooManyArgs', $responseArray['error']['name']);
}
public function testWebserviceSubclassMethodParamNamesDontMatterAsLongThatThereIsTheRightCount()
{
$_GET = array(
'func' => 'someMockFunction',
'wrongParamName' => 'val',
'wrongParamName2' => 'val',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(true, isset($responseArray['result']));
}
public function testReturnsResultIfProvidedAllParameters()
{
$_GET = array(
'func' => 'someMockFunction',
'anyParamName1' => 'val',
'anyParamName2' => 'val',
'anyParamName3' => 'val',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(true, isset($responseArray['result']));
}
public function testReturnsResultFormattedAsMethodNameAndReturnValue()
{
$_GET = array(
'func' => 'someMockFunction',
'anyParamName1' => 'val',
'anyParamName2' => 'val',
'anyParamName3' => 'val',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(array(
'someMockFunction' => 'Some Mock Function Return'
), $responseArray['result']);
}
public function testOrderOfParamatersMatchesArrayOrder()
{
$_GET = array(
'func' => 'someOtherMockFunction',
'anyParamName1' => 'one',
'anyParamName2' => 'two',
'anyParamName3' => 'threeOpt',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(array(
'someOtherMockFunction' => array(
'one' => 'one',
'two' => 'two',
'threeOpt' => 'threeOpt',
)
), $responseArray['result']);
}
public function testOrderOfParamatersMatchesArrayMixedOrder()
{
$_GET = array(
'func' => 'someOtherMockFunction',
'anyParamName2' => 'two',
'anyParamName3' => 'threeOpt',
'anyParamName1' => 'one',
);
$this->webservice->run(false);
$response = ob_get_clean();
$responseArray = json_decode($response, true);
$this->assertEquals(array(
'someOtherMockFunction' => array(
'one' => 'two',
'two' => 'threeOpt',
'threeOpt' => 'one',
)
), $responseArray['result']);
}
public function testWebserviceAddsAllRequestInfoToTheLog()
{
$_GET = array(
'func' => 'someOtherMockFunction',
'anyParamName2' => 'two',
'anyParamName3' => 'threeOpt',
'anyParamName1' => 'one',
);
$this->webservice->run(false);
$response = ob_get_clean();
$this->assertSame(
array('version', 'ip', 'date', 'func', 'error', 'status', 'time'),
array_keys(Logger::getData())
);
}
}