diff --git a/lib/triagens/ArangoDb/AqlUserFunction.php b/lib/triagens/ArangoDb/AqlUserFunction.php index 67545303..93c8c8d6 100644 --- a/lib/triagens/ArangoDb/AqlUserFunction.php +++ b/lib/triagens/ArangoDb/AqlUserFunction.php @@ -120,11 +120,12 @@ public function register($name = null, $code = null) $attributes = $this->attributes; - if (is_null($name)) { - $attributes['name'] = $this->getName(); + if ($name) { + $attributes['name'] = $name; } - if (is_null($code)) { - $attributes['code'] = $this->getCode(); + + if ($code) { + $attributes['code'] = $code; } $response = $this->_connection->post( diff --git a/tests/AqlUserFunctionTest.php b/tests/AqlUserFunctionTest.php index 392d6acb..7decc17d 100644 --- a/tests/AqlUserFunctionTest.php +++ b/tests/AqlUserFunctionTest.php @@ -1,232 +1,264 @@ -connection = getConnection(); - - // clean up first - try { - $this->collectionHandler->delete('ArangoDB_PHP_TestSuite_TestCollection_01'); - } catch (\Exception $e) { - // don't bother us, if it's already deleted. - } - } - - - /** - * Test if AqlUserFunctions can be registered, listed and unregistered - */ - public function testRegisterListAndUnregisterAqlUserFunctionWithInitialConfig() - { - - $name = 'myFunctions:myFunction'; - $code = 'function (celsius) { return celsius * 1.8 + 32; }'; - - $array = array( - 'name' => $name, - 'code' => $code - ); - - $userFunction = new AqlUserFunction($this->connection, $array); - - $result = $userFunction->register(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - $list = $userFunction->getRegisteredUserFunctions(); - - $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); - $this->assertTrue( - $list[0]['name'] == $name && $list[0]['code'] == $code, - 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] - ); - - $result = $userFunction->unregister(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - } - - - /** - * Test if AqlUserFunctions can be registered, listed and unregistered with getters and setters - */ - public function testRegisterListAndUnregisterAqlUserFunctionWithGettersAndSetters() - { - $name = 'myFunctions:myFunction'; - $code = 'function (celsius) { return celsius * 1.8 + 32; }'; - - $userFunction = new AqlUserFunction($this->connection); - $userFunction->setName($name); - $userFunction->setCode($code); - - // check if getters work fine - - $this->assertTrue( - $userFunction->getName() == $name, - 'Did not return name, instead returned: ' . $userFunction->getName() - ); - $this->assertTrue( - $userFunction->getCode() == $code, - 'Did not return code, instead returned: ' . $userFunction->getCode() - ); - - - $result = $userFunction->register(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - $list = $userFunction->getRegisteredUserFunctions(); - $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); - $this->assertTrue( - $list[0]['name'] == $name && $list[0]['code'] == $code, - 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] - ); - - $result = $userFunction->unregister(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - } - - - /** - * Test if AqlUserFunctions can be registered, listed and unregistered - */ - public function testRegisterListAndUnregisterAqlUserFunctionWithWithMagicSettersAndGetters() - { - - $name = 'myFunctions:myFunction'; - $code = 'function (celsius) { return celsius * 1.8 + 32; }'; - - - $userFunction = new AqlUserFunction($this->connection); - - $userFunction->name = $name; - $userFunction->code = $code; - - // check if getters work fine - $this->assertTrue( - $userFunction->name == $name, - 'Did not return name, instead returned: ' . $userFunction->name - ); - $this->assertTrue( - $userFunction->code == $code, - 'Did not return code, instead returned: ' . $userFunction->code - ); - - $result = $userFunction->register(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - $list = $userFunction->getRegisteredUserFunctions(); - $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); - $this->assertTrue( - $list[0]['name'] == $name && $list[0]['code'] == $code, - 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] - ); - - $result = $userFunction->unregister(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - } - - - /** - * Test if AqlUserFunctions can be registered, listed and unregistered - * - */ - public function testReRegisterListAndUnregisterAqlUserFunctionTwice() - { - - $name = 'myFunctions:myFunction'; - $code = 'function (celsius) { return celsius * 1.8 + 32; }'; - - - $userFunction = new AqlUserFunction($this->connection); - - $userFunction->name = $name; - $userFunction->code = $code; - - $result = $userFunction->register(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - - $result = $userFunction->register(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - - $list = $userFunction->getRegisteredUserFunctions(); - $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); - $this->assertTrue( - $list[0]['name'] == $name && $list[0]['code'] == $code, - 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] - ); - - $result = $userFunction->unregister(); - - $this->assertTrue( - $result['error'] == false, - 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) - ); - - $e = null; - try { - $userFunction->unregister(); - } catch (Exception $e) { - } - - $this->assertTrue( - $e->getCode() == 404, - 'Did not return code 404, instead returned: ' . $e->getCode() - ); - } - - - public function tearDown() - { - - unset($this->connection); - } -} +connection = getConnection(); + + // clean up first + try { + $this->collectionHandler->delete('ArangoDB_PHP_TestSuite_TestCollection_01'); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + } + + + /** + * Test if AqlUserFunctions can be registered, listed and unregistered + */ + public function testRegisterListAndUnregisterAqlUserFunctionWithInitialConfig() + { + + $name = 'myFunctions:myFunction'; + $code = 'function (celsius) { return celsius * 1.8 + 32; }'; + + $array = array( + 'name' => $name, + 'code' => $code + ); + + $userFunction = new AqlUserFunction($this->connection, $array); + + $result = $userFunction->register(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + $list = $userFunction->getRegisteredUserFunctions(); + + $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); + $this->assertTrue( + $list[0]['name'] == $name && $list[0]['code'] == $code, + 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] + ); + + $result = $userFunction->unregister(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + } + + /** + * Test if AqlUserFunctions can be registered, listed and unregistered using the register() shortcut method + */ + public function testRegisterListAndUnregisterAqlUserFunctionUsingShortcut() + { + + $name = 'myFunctions:myFunction'; + $code = 'function (celsius) { return celsius * 1.8 + 32; }'; + + $userFunction = new AqlUserFunction($this->connection); + + $result = $userFunction->register($name, $code); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + $list = $userFunction->getRegisteredUserFunctions(); + + $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); + $this->assertTrue( + $list[0]['name'] == $name && $list[0]['code'] == $code, + 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] + ); + + $result = $userFunction->unregister($name); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + } + + /** + * Test if AqlUserFunctions can be registered, listed and unregistered with getters and setters + */ + public function testRegisterListAndUnregisterAqlUserFunctionWithGettersAndSetters() + { + $name = 'myFunctions:myFunction'; + $code = 'function (celsius) { return celsius * 1.8 + 32; }'; + + $userFunction = new AqlUserFunction($this->connection); + $userFunction->setName($name); + $userFunction->setCode($code); + + // check if getters work fine + + $this->assertTrue( + $userFunction->getName() == $name, + 'Did not return name, instead returned: ' . $userFunction->getName() + ); + $this->assertTrue( + $userFunction->getCode() == $code, + 'Did not return code, instead returned: ' . $userFunction->getCode() + ); + + + $result = $userFunction->register(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + $list = $userFunction->getRegisteredUserFunctions(); + $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); + $this->assertTrue( + $list[0]['name'] == $name && $list[0]['code'] == $code, + 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] + ); + + $result = $userFunction->unregister(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + } + + + /** + * Test if AqlUserFunctions can be registered, listed and unregistered + */ + public function testRegisterListAndUnregisterAqlUserFunctionWithWithMagicSettersAndGetters() + { + + $name = 'myFunctions:myFunction'; + $code = 'function (celsius) { return celsius * 1.8 + 32; }'; + + + $userFunction = new AqlUserFunction($this->connection); + + $userFunction->name = $name; + $userFunction->code = $code; + + // check if getters work fine + $this->assertTrue( + $userFunction->name == $name, + 'Did not return name, instead returned: ' . $userFunction->name + ); + $this->assertTrue( + $userFunction->code == $code, + 'Did not return code, instead returned: ' . $userFunction->code + ); + + $result = $userFunction->register(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + $list = $userFunction->getRegisteredUserFunctions(); + $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); + $this->assertTrue( + $list[0]['name'] == $name && $list[0]['code'] == $code, + 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] + ); + + $result = $userFunction->unregister(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + } + + + /** + * Test if AqlUserFunctions can be registered, listed and unregistered + * + */ + public function testReRegisterListAndUnregisterAqlUserFunctionTwice() + { + + $name = 'myFunctions:myFunction'; + $code = 'function (celsius) { return celsius * 1.8 + 32; }'; + + + $userFunction = new AqlUserFunction($this->connection); + + $userFunction->name = $name; + $userFunction->code = $code; + + $result = $userFunction->register(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + + $result = $userFunction->register(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + + $list = $userFunction->getRegisteredUserFunctions(); + $this->assertCount(1, $list, 'List returned did not return expected 1 attribute'); + $this->assertTrue( + $list[0]['name'] == $name && $list[0]['code'] == $code, + 'did not return expected Function. Instead returned: ' . $list[0]['name'] . ' and ' . $list[0]['code'] + ); + + $result = $userFunction->unregister(); + + $this->assertTrue( + $result['error'] == false, + 'result[\'error\'] Did not return false, instead returned: ' . print_r($result, 1) + ); + + $e = null; + try { + $userFunction->unregister(); + } catch (Exception $e) { + } + + $this->assertTrue( + $e->getCode() == 404, + 'Did not return code 404, instead returned: ' . $e->getCode() + ); + } + + + public function tearDown() + { + + unset($this->connection); + } +}