diff --git a/composer.phar b/composer.phar index 4055d874a1a1f195c0a08966e4e0e9dbaaf0a5e0..545bd4e1e734cf6db165f4789cb8d4d52beb260e 100755 Binary files a/composer.phar and b/composer.phar differ diff --git a/lib/EventManagement.php b/lib/EventManagement.php index 13106e54ed10dfa4b34e432fb7e5a29df07e0bb8..d892f73c306d229132f983c6710e1b3a4a4922a5 100644 --- a/lib/EventManagement.php +++ b/lib/EventManagement.php @@ -30,7 +30,7 @@ class EventManagement{ /** * Add a new handler, which is called on a new incoming event. * - * @param EventHandler $handler + * @param EventListener $handler */ public function registerHandler($handler){ $this->handlers[] = $handler; @@ -132,6 +132,14 @@ class EventManagement{ return new Response("New event sent.", 400); } + if($request->query->get('timeout',null) !== null){ + sleep(10); + } + if($request->query->get('abort',null) !== null){ + //Force server error + return null; + } + $jsonevent = json_decode($eventJSON); if (json_last_error() > 0) { // if response is a string @@ -173,17 +181,19 @@ class EventManagement{ * @param string $certFile path to certificate file to use or null to call URL without certificate * @param string $certKey path to certificate key file, null to avoid usage of key * @param string $certPassphrase path to passphrase file for key file, null for no passphrase + * @param int $timeout time out in seconds until curl call is aborted + * @param \jards\eventsapiclient\Swagger\Client\ApiException $exception output parameter, if exception occurs * * @return boolean true on success, false on error */ - public function sendEvent($name, $description, $baseURL='http://localhost/myapps/EventsAPI/rest/events', $certFile = null, $certKey = null, $certPassphrase = null){ + public function sendEvent($name, $description, $baseURL='http://localhost/myapps/EventsAPI/rest/events', $certFile = null, $certKey = null, $certPassphrase = null, $timeout = null, &$exception=null){ $event = new Event(); $event->setName($name); $event->setDescription($description); $event->setDate(new \DateTime()); - return $this->sendEventObject($event, $baseURL, $certFile, $certKey, $certPassphrase); + return $this->sendEventObject($event, $baseURL, $certFile, $certKey, $certPassphrase, $timeout, $exception); } /** @@ -196,10 +206,11 @@ class EventManagement{ * @param string $certKey path to certificate key file, null to avoid usage of key * @param string $certPassphrase path to passphrase file for key file, null for no passphrase * @param int $timeout time out in seconds until curl call is aborted + * @param \jards\eventsapiclient\Swagger\Client\ApiException $exception output parameter, if exception occurs * * @return boolean true on success, false on error */ - public function sendEventObject($eventObject, $baseURL='http://localhost/myapps/EventsAPI/rest/events', $certFile = null, $certKey = null, $certPassphrase = null, $timeout = null){ + public function sendEventObject($eventObject, $baseURL='http://localhost/myapps/EventsAPI/rest/events', $certFile = null, $certKey = null, $certPassphrase = null, $timeout = null, &$exception=null){ date_default_timezone_set ( 'Europe/Amsterdam' ); $apiClient = new \jards\eventsapiclient\Swagger\Client\ApiClient (); $apiClient->getConfig ()->setHost ( $baseURL ); @@ -223,6 +234,7 @@ class EventManagement{ $eventsApi->eventsPost($eventObject); } catch(\jards\eventsapiclient\Swagger\Client\ApiException $e){ + $exception = $e; return false; } diff --git a/rest/events/index.php b/rest/events/index.php index f7dc9da84f4c63a1fa1983d77c79ac381c2bbbf4..d231f2b74a309c25ef4b362e248101db0d0fbefe 100644 --- a/rest/events/index.php +++ b/rest/events/index.php @@ -2,8 +2,6 @@ require_once __DIR__.'/../../vendor/autoload.php'; use Silex\Application; -use jards\eventsapi\EventListener; -use jards\eventsapi\MailOnEventHandler; use jards\eventsapi\MailOnEventListener; use jards\eventsapi\EventManagement; diff --git a/tests/EventManagementTest.php b/tests/EventManagementTest.php index 23f0ccea707d290ce42be96efe2bacdecebe7c17..e23224f1b98b428aed0368dc8a6d1926a7bcc788 100644 --- a/tests/EventManagementTest.php +++ b/tests/EventManagementTest.php @@ -87,4 +87,88 @@ class EventManagementTest extends TestCase{ $this->assertEquals(join('|', $oldEventIDs), join('|', $newEventIDs), "Could not revert new created events"); } + /** + * Test 404 return code if an invalid URL is tried to be accessed + */ + public function testExceptionForInvalidURL(){ + $url = file_get_contents(__DIR__.'/../configs/myurl.cnf'); + $invalid = $url.'/thisisinvalidforphpunit'; + + $eventManagement = new EventManagement(); + + $event = new Event(); + $event->setName('My event never sent phpunit'); + $event->setDescription('Good event for testing, should never be sent'); + $event->setNewState('none'); + $event->setDate(new \DateTime()); + + /** + * @var \Exception $exception + */ + $exception = null; + $success = $eventManagement->sendEventObject($event, $invalid, null, null, null, null, $exception); + $this->assertFalse($success); + $this->assertNotNull($exception); + $this->assertTrue(strpos($exception->getMessage(), "[404] Error connecting to the API") !== false); + +// echo $exception->getMessage(); +// echo $exception->getTraceAsString(); + } + + /** + * Force sleep on server side. Then test exception thrown by client due to timeout. + */ + public function testExceptionForTimeout(){ + $url = file_get_contents(__DIR__.'/../configs/myurl.cnf'); + + $apiClient = new \jards\eventsapiclient\Swagger\Client\ApiClient (); + $apiClient->getConfig ()->setHost ( $url ); + $apiClient->getConfig()->setCurlTimeout(2); + + $event = new Event(); + $event->setName('My timeout event'); + $event->setDescription('Bad event for phpunit testing'); + $event->setNewState('none'); + $event->setDate(new \DateTime()); + + try{ + $apiClient->callApi('/events', 'POST', ['timeout' => 'true'], $event, ['Accept' => 'application/json', 'Content-Type' => 'application/json'], 'string', '/events'); + } + catch(\jards\eventsapiclient\Swagger\Client\ApiException $apiException){ + $this->assertTrue(strpos($apiException->getMessage(), "Operation timed out after") !== false); + +// echo $apiException->getMessage(); +// echo $apiException->getTraceAsString(); + } + } + + /** + * Force error of server, then check the exception caught + */ + public function testExceptionForServerError(){ + echo "testExceptionForServerError\n"; + $url = file_get_contents(__DIR__.'/../configs/myurl.cnf'); + + $apiClient = new \jards\eventsapiclient\Swagger\Client\ApiClient (); + $apiClient->getConfig ()->setHost ( $url ); + $apiClient->getConfig()->setCurlTimeout(2); + + $event = new Event(); + $event->setName('My abort event'); + $event->setDescription('Bad event for phpunit testing'); + $event->setNewState('none'); + $event->setDate(new \DateTime()); + + try{ + $apiClient->callApi('/events', 'POST', ['abort' => 'true'], $event, ['Accept' => 'application/json', 'Content-Type' => 'application/json'], 'string', '/events'); + } + catch(\jards\eventsapiclient\Swagger\Client\ApiException $apiException){ + $this->assertEquals(500, $apiException->getCode()); + $this->assertTrue(strpos($apiException->getMessage(), "[500] Error connecting to the API") !== false); + +// echo $apiException->getMessage(); +// echo $apiException->getTraceAsString(); +// echo $apiException->getResponseBody(); + } + } } \ No newline at end of file